Skip to content

Commit ac5a62f

Browse files
authored
Merge pull request #9933 from gitbutlerapp/e2e-playwright-env-refactor
e2e: Extract env vars and update setup to use them
2 parents 28c9532 + a8996f5 commit ac5a62f

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

e2e/playwright/playwright.config.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BUT_SERVER_PORT, DESKTOP_PORT } from './src/env.ts';
12
import { defineConfig, devices } from '@playwright/test';
23
import path from 'node:path';
34

@@ -9,9 +10,6 @@ import path from 'node:path';
910
// import path from 'path';
1011
// dotenv.config({ path: path.resolve(__dirname, '.env') });
1112

12-
const BUT_SERVER_PORT = process.env.BUTLER_PORT || '6978';
13-
const DESKTOP_PORT = process.env.DESKTOP_PORT || '3000';
14-
1513
const AMOUNT_OF_WORKERS = 2;
1614

1715
/**

e2e/playwright/src/env.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import path from 'node:path';
2+
3+
const ROOT = path.resolve(import.meta.dirname, '../../..');
4+
5+
export const BUT_SERVER_PORT = process.env.BUTLER_PORT || '6978';
6+
export const DESKTOP_PORT = process.env.DESKTOP_PORT || '3000';
7+
export const BUT_TESTING =
8+
process.env.BUT_TESTING || path.join(ROOT, 'target', 'debug', 'but-testing');

e2e/playwright/src/setup.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1+
import { BUT_SERVER_PORT, BUT_TESTING, DESKTOP_PORT } from './env.ts';
12
import { type BrowserContext } from '@playwright/test';
23
import { ChildProcess, spawn } from 'node:child_process';
34
import { existsSync, mkdirSync } from 'node:fs';
45
import { Socket } from 'node:net';
56
import path from 'node:path';
67

7-
const DESKTOP_PORT = process.env.DESKTOP_PORT || '3000';
8-
const BUTLER_PORT = process.env.BUTLER_PORT || '6978';
9-
108
export function getBaseURL() {
119
const parallelId = process.env.TEST_PARALLEL_INDEX ?? '0';
1210
const id = parseInt(parallelId, 10);
@@ -18,7 +16,7 @@ export function getBaseURL() {
1816
export function getButlerPort(): string {
1917
const parallelId = process.env.TEST_PARALLEL_INDEX ?? '0';
2018
const id = parseInt(parallelId, 10);
21-
return `${parseInt(BUTLER_PORT, 10) + id}`;
19+
return `${parseInt(BUT_SERVER_PORT, 10) + id}`;
2220
}
2321

2422
export interface GitButler {
@@ -49,7 +47,7 @@ class GitButlerManager implements GitButler {
4947
}
5048

5149
this.butServerProcess = spawnProcess('cargo', ['run', '-p', 'but-server'], this.rootDir, {
52-
E2E_TEST_APP_DATA_DIR: this.configDir || path.join(this.workdir, 'config'),
50+
E2E_TEST_APP_DATA_DIR: this.configDir,
5351
BUTLER_PORT: getButlerPort()
5452
});
5553

@@ -69,9 +67,10 @@ class GitButlerManager implements GitButler {
6967
}
7068

7169
async init() {
72-
const butReady = await waitForServer(process.env.BUTLER_PORT || '6978', 'localhost');
70+
const port = getButlerPort();
71+
const butReady = await waitForServer(port, 'localhost');
7372
if (!butReady) {
74-
throw new Error(`Butler server failed to start on localhost:${process.env.BUTLER_PORT}`);
73+
throw new Error(`Butler server failed to start on localhost:${port}`);
7574
}
7675
}
7776

@@ -88,10 +87,16 @@ class GitButlerManager implements GitButler {
8887
const scriptPath = path.resolve(this.scriptsDir, scriptName);
8988
if (!existsSync(scriptPath)) log(`Script not found: ${scriptPath}`, colors.red);
9089
const scriptArgs = args ?? [];
91-
await runCommand('bash', [scriptPath, ...scriptArgs], this.workdir);
90+
await runCommand('bash', [scriptPath, ...scriptArgs], this.workdir, {
91+
GITBUTLER_CLI_DATA_DIR: getButlerDataDir(this.configDir)
92+
});
9293
}
9394
}
9495

96+
function getButlerDataDir(configDir: string): string {
97+
return path.join(configDir, 'com.gitbutler.app');
98+
}
99+
95100
async function waitForServer(port: string, host = 'localhost', maxAttempts = 500) {
96101
const parsed = parseInt(port, 10);
97102

@@ -163,6 +168,7 @@ function spawnProcess(
163168
...process.env,
164169
ELECTRON_ENV: 'development',
165170
VITE_BUILD_TARGET: 'web',
171+
BUT_TESTING: BUT_TESTING,
166172
VITE_HOST,
167173
...env
168174
}
@@ -192,11 +198,16 @@ async function setProjectPathCookie(context: BrowserContext, workdir: string): P
192198
]);
193199
}
194200

195-
async function runCommand(command: string, args: string[], cwd = process.cwd()) {
201+
async function runCommand(
202+
command: string,
203+
args: string[],
204+
cwd = process.cwd(),
205+
env: Record<string, string> = {}
206+
): Promise<void> {
196207
return await new Promise<void>((resolve, reject) => {
197208
log(`Running: ${command} ${args.join(' ')}`, colors.cyan);
198209

199-
const child = spawnProcess(command, args, cwd);
210+
const child = spawnProcess(command, args, cwd, env);
200211

201212
child.on('message', (message) => {
202213
log(`Child process message: ${message}`, colors.blue);

0 commit comments

Comments
 (0)