|
| 1 | +import { StdioOptions, execSync, spawn } from "child_process" |
| 2 | +import { EnvKey } from "../env" |
| 3 | +import { GitCredentialHelperOperation } from "../git-credential-types" |
| 4 | + |
| 5 | +let serverProcess: ReturnType<typeof spawn> |
| 6 | + |
| 7 | +const setupStdio: StdioOptions = "ignore" |
| 8 | +const PORT_FOR_TEST_SERVER = 47472 |
| 9 | +const TEST_PASSWORD = "myMockSecretPassword" |
| 10 | + |
| 11 | +beforeAll(() => { |
| 12 | + execSync(`pnpm build`, { stdio: setupStdio }) |
| 13 | + execSync(`pnpm build-mock-git`, { stdio: setupStdio }) |
| 14 | + |
| 15 | + serverProcess = spawn("node", ["./dist/gcf-server.js"], { |
| 16 | + detached: true, |
| 17 | + env: { |
| 18 | + // use the mock git app on the server side |
| 19 | + [EnvKey.GIT_PATH]: "node ./src/__tests__/dist/mock-git.js", |
| 20 | + [EnvKey.PORT]: PORT_FOR_TEST_SERVER.toString(), |
| 21 | + // will be read by mock git and used in its mock return |
| 22 | + TEST_PASSWORD: TEST_PASSWORD |
| 23 | + }, |
| 24 | + stdio: setupStdio |
| 25 | + }) |
| 26 | +}) |
| 27 | + |
| 28 | +afterAll(() => { |
| 29 | + serverProcess.kill() |
| 30 | +}) |
| 31 | + |
| 32 | +it("Returns output of host on a valid git credential operation", async () => { |
| 33 | + // this test validates that the entire round trip is working: the client |
| 34 | + // makes a 'get' request, that is forwarded to the server, the server |
| 35 | + // passes it to `git`, gets the response, and passes it back to the |
| 36 | + // client |
| 37 | + return runClient({ |
| 38 | + operation: "get", |
| 39 | + input: "username=myUser", |
| 40 | + expectations: output => { |
| 41 | + expect(output).toEqual(`username=myUser\npassword=${TEST_PASSWORD}`) |
| 42 | + } |
| 43 | + }) |
| 44 | +}) |
| 45 | + |
| 46 | +it("Returns no output on unsupported git command", async () => { |
| 47 | + return runClient({ |
| 48 | + // not an actual git command |
| 49 | + operation: "move" as GitCredentialHelperOperation, |
| 50 | + input: "username=myUser", |
| 51 | + expectations: output => { |
| 52 | + expect(output).toEqual("") |
| 53 | + } |
| 54 | + }) |
| 55 | +}) |
| 56 | + |
| 57 | +/** |
| 58 | + * Helper function to run e2e tests |
| 59 | + * |
| 60 | + * @async |
| 61 | + * @param operation - the git credential helper operation to test |
| 62 | + * @param input - the git input string to pass |
| 63 | + * @param expectations - callback that allows running assertions on the output returned to the client |
| 64 | + */ |
| 65 | +async function runClient({ |
| 66 | + operation, |
| 67 | + input, |
| 68 | + expectations |
| 69 | +}: { |
| 70 | + operation: GitCredentialHelperOperation |
| 71 | + input: string |
| 72 | + expectations: (output: string) => void |
| 73 | +}): Promise<void> { |
| 74 | + const clientProcess = spawn("node", ["./dist/gcf-client.js", operation], { |
| 75 | + env: { |
| 76 | + [EnvKey.SERVER]: "localhost:" + PORT_FOR_TEST_SERVER.toString() |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + let outputData = "" |
| 81 | + clientProcess.stdout.on("data", data => { |
| 82 | + outputData += data.toString() |
| 83 | + }) |
| 84 | + |
| 85 | + // Promise that will resolve when the client process is complete (i.e., |
| 86 | + // when all output has been received back |
| 87 | + const done = new Promise((resolve, reject) => { |
| 88 | + clientProcess.on("close", () => { |
| 89 | + try { |
| 90 | + expectations(outputData) |
| 91 | + resolve(undefined) |
| 92 | + } catch (err) { |
| 93 | + reject(err) |
| 94 | + } |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + clientProcess.stdin.write(input) |
| 99 | + clientProcess.stdin.end() |
| 100 | + await done |
| 101 | +} |
0 commit comments