|
| 1 | +import { setWorldConstructor, World, IWorldOptions } from '@cucumber/cucumber' |
| 2 | +import { randomUUID, randomBytes } from 'crypto' |
| 3 | +import * as child_process from 'child_process' |
| 4 | +import * as fs from 'fs' |
| 5 | +import * as path from 'path' |
| 6 | + |
| 7 | +interface BundleServerParameters { |
| 8 | + bundleServerCommand: string |
| 9 | + bundleWebServerCommand: string |
| 10 | + trashDirectoryBase: string |
| 11 | +} |
| 12 | + |
| 13 | +export class BundleServerWorld extends World<BundleServerParameters> { |
| 14 | + // Internal variables |
| 15 | + private trashDirectory: string |
| 16 | + private webServer: child_process.ChildProcess | undefined |
| 17 | + private bundleUriBase: string | undefined |
| 18 | + private route: string | undefined |
| 19 | + |
| 20 | + cloneResult: child_process.SpawnSyncReturns<Buffer> | undefined |
| 21 | + |
| 22 | + |
| 23 | + constructor(options: IWorldOptions<BundleServerParameters>) { |
| 24 | + super(options) |
| 25 | + |
| 26 | + // Set up the trash directory |
| 27 | + this.trashDirectory = this.pathParamToAbsolute( |
| 28 | + path.join(this.parameters.trashDirectoryBase, randomUUID()) |
| 29 | + ) |
| 30 | + fs.mkdirSync(this.trashDirectory, { recursive: true }); |
| 31 | + } |
| 32 | + |
| 33 | + pathParamToAbsolute(relPath: string): string { |
| 34 | + let absPath = this.parameters.bundleWebServerCommand |
| 35 | + if (!path.isAbsolute(absPath)) { |
| 36 | + // Assume relative path is relative to 'test/e2e' |
| 37 | + absPath = path.resolve(__dirname, "../..", relPath) |
| 38 | + } |
| 39 | + return absPath |
| 40 | + } |
| 41 | + |
| 42 | + startWebServer(port: number): void { |
| 43 | + if (this.webServer !== undefined) { |
| 44 | + throw new Error("Tried to start web server, but web server is already running") |
| 45 | + } |
| 46 | + |
| 47 | + const webServerCmd = this.pathParamToAbsolute(this.parameters.bundleWebServerCommand) |
| 48 | + this.webServer = child_process.spawn(webServerCmd, ["--port", String(port)]) |
| 49 | + this.bundleUriBase = `http://localhost:${port}/` |
| 50 | + } |
| 51 | + |
| 52 | + bundleServerInit(url: string): child_process.SpawnSyncReturns<Buffer> { |
| 53 | + const serverCliCmd = this.pathParamToAbsolute(this.parameters.bundleServerCommand) |
| 54 | + this.route = `e2e/${randomBytes(8).toString('hex')}` |
| 55 | + return child_process.spawnSync(serverCliCmd, ["init", url, this.route]) |
| 56 | + } |
| 57 | + |
| 58 | + cloneWithBundleUri(url: string): void { |
| 59 | + if (this.route === undefined || this.bundleUriBase === undefined) { |
| 60 | + throw new Error("Bundle URI is not initialized") |
| 61 | + } |
| 62 | + |
| 63 | + this.cloneResult = child_process.spawnSync("git", |
| 64 | + ["clone", `--bundle-uri=${this.bundleUriBase}/${this.route}/`, url, this.trashDirectory] |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + runGitInLocalRepo(...args: string[]): child_process.SpawnSyncReturns<Buffer> { |
| 69 | + return child_process.spawnSync("git", |
| 70 | + ["-C", this.trashDirectory].concat(args) |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + cleanup(): void { |
| 75 | + // Stop the web server |
| 76 | + if (this.webServer !== undefined) { |
| 77 | + const killed = this.webServer.kill('SIGINT') |
| 78 | + if (!killed) { |
| 79 | + console.warn("Web server process was not successfully stopped") |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Delete the added route |
| 84 | + if (this.route !== undefined) { |
| 85 | + const serverCliCmd = this.pathParamToAbsolute(this.parameters.bundleServerCommand) |
| 86 | + child_process.spawnSync(serverCliCmd, ["delete", this.route]) |
| 87 | + } |
| 88 | + |
| 89 | + // Delete the trash directory |
| 90 | + fs.rmSync(this.trashDirectory, { recursive: true }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +setWorldConstructor(BundleServerWorld) |
0 commit comments