summaryrefslogtreecommitdiff
path: root/src/lib/git/index.test.ts
blob: 4eedacad4146a549966f93378474f159d75d9779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { describe, it } from "@std/testing/bdd";
import { assertEquals } from "@std/assert";
import {
  assertSpyCall,
  assertSpyCalls,
  returnsNext,
  stub,
} from "@std/testing/mock";

// IMPORTANT: Delay the import of `gitDir` to after the stub
let gitDir: typeof import("./index.ts").gitDir;

describe("gitDir", () => {
  it("resolves with trimmed decoded stdout", async () => {
    const encoded = new TextEncoder().encode(
      " /home/user/project \n",
    ) as Uint8Array<ArrayBuffer>;
    const fakeOutput = Promise.resolve({
      success: true,
      code: 0,
      stdout: encoded,
      stderr: new Uint8Array(),
      signal: null,
    });

    using outputStub = stub(
      Deno.Command.prototype,
      "output",
      returnsNext([fakeOutput]),
    );

    // Now import gitDir AFTER stubbing
    ({ gitDir } = await import("./index.ts"));

    const result = await gitDir();
    assertEquals(result.pathname, "/home/user/project");
    assertSpyCall(outputStub, 0, { args: [], returned: fakeOutput });
    assertSpyCalls(outputStub, 1);
  });
});