Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions tests/electron/connections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { createBackend, type Backend } from "../../electron/create-backend.js";
import { Tailscale } from "../../electron/local-server/tailscale.js";
import { FakeTailscale } from "../helpers/fake-tailscale.js";
import { InProcessLocalServer } from "../helpers/in-process-local-server.js";
import { environmentRestorer } from "../helpers/restore-environment.js";
import { adminRequest } from "../../server/admin.js";
import { MasterKeyEncryption } from "../../server/master-key.js";
import { createWispServer, type WispServer } from "../../server/wisp-server.js";
Expand All @@ -40,11 +41,11 @@ const silent = new StructuredLogger({ info: () => undefined, warn: () => undefin
const fakeSsh = path.join(__dirname, "../fixtures/fake-ssh.cjs");
const TEST_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHRlc3R0ZXN0dGVzdHRlc3R0ZXN0dGVzdHRlc3Q= wisp@laptop";
const cleanups: Array<() => Promise<void> | void> = [];
const environment = { ...process.env };
const restoreEnvironment = environmentRestorer();

afterEach(async () => {
for (const cleanup of cleanups.splice(0).reverse()) await cleanup();
process.env = { ...environment };
restoreEnvironment();
});

/** Sets up a new store with one Wisp, as the app does on a first run. */
Expand Down
5 changes: 3 additions & 2 deletions tests/electron/ssh-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { sshTestFailure, testSshConnection } from "../../electron/connections/ssh-test.js";
import type { SshConnectionProfile } from "../../shared/connections.js";
import { environmentRestorer } from "../helpers/restore-environment.js";

const profile: SshConnectionProfile = {
id: "test",
Expand All @@ -13,9 +14,9 @@ const profile: SshConnectionProfile = {
serverPort: 8787,
};
const sshPath = path.join(__dirname, "../fixtures/fake-ssh.cjs");
const environment = { ...process.env };
const restoreEnvironment = environmentRestorer();
afterEach(() => {
process.env = { ...environment };
restoreEnvironment();
});

describe("SSH connection test", () => {
Expand Down
5 changes: 3 additions & 2 deletions tests/electron/ssh-tunnel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import { MasterKeyEncryption } from "../../server/master-key.js";
import { createWispServer } from "../../server/wisp-server.js";
import type { SshConnectionProfile, SshPromptView } from "../../shared/connections.js";
import { environmentRestorer } from "../helpers/restore-environment.js";

const TEST_KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHRlc3R0ZXN0dGVzdHRlc3R0ZXN0dGVzdHRlc3Q= wisp@laptop";
const fakeSsh = path.join(__dirname, "../fixtures/fake-ssh.cjs");
Expand All @@ -43,11 +44,11 @@ async function anotherService(): Promise<number> {
cleanups.push(() => new Promise<void>((resolve) => service.close(() => resolve())));
return (service.address() as AddressInfo).port;
}
const environment = { ...process.env };
const restoreEnvironment = environmentRestorer();

afterEach(async () => {
for (const cleanup of cleanups.splice(0).reverse()) await cleanup();
process.env = { ...environment };
restoreEnvironment();
});

const profile = (serverPort: number): SshConnectionProfile => ({
Expand Down
12 changes: 12 additions & 0 deletions tests/helpers/restore-environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Remembers `process.env` and returns a function that puts it back. It changes
* the object in place because, from Node 26, child processes do not see
* variables set on an object assigned to `process.env`.
*/
export function environmentRestorer(): () => void {
const environment = { ...process.env };
return () => {
for (const name of Object.keys(process.env)) if (!(name in environment)) delete process.env[name];
Object.assign(process.env, environment);
};
}
Loading