summaryrefslogtreecommitdiff
path: root/src/lib/git/log.test.ts
blob: 09acb1c553028e0d666aca21a8438765f5dcb3fc (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { describe, it } from "@std/testing/bdd";
import { assertEquals, assertExists } from "@std/assert";
import {
  assertSpyCall,
  assertSpyCalls,
  returnsNext,
  stub,
} from "@std/testing/mock";
import { getLastCommitForOneOfFiles } from "./log.ts";
import {
  emptyCommandOutput,
  gitDiffTreeCommandOutput,
  gitDir,
  gitLogPrettyCommandOutput,
  gitRevParseCommandOutput,
} from "../../../tests/fixtures/test_data.ts";

describe("getLastCommitForOneOfFiles", () => {
  it("returns parsed commit with signature and file info", async () => {
    const outputs = [
      gitLogPrettyCommandOutput,
      gitDiffTreeCommandOutput,
      gitRevParseCommandOutput,
    ];
    using logStub = stub(
      Deno.Command.prototype,
      "output",
      returnsNext(outputs),
    );

    const file = new URL("file.ts", gitDir);
    const result = await getLastCommitForOneOfFiles(file);

    assertExists(result);
    assertEquals(result.hash.short, "abcdef1");
    assertEquals(result.hash.long, "abcdef1234567890abcdef1234567890abcdef12");

    assertEquals(result.author.name, "Alice");
    assertEquals(result.committer.email, "bob@example.com");

    assertEquals(result.files.length, 1);
    assertEquals(result.files[0], {
      path: file,
      status: "modified",
    });

    assertEquals(result.signature?.type, "gpg");
    assertEquals(result.signature?.signer, "bob@example.com");

    for (let i = 0; i < outputs.length; i++) {
      assertSpyCall(logStub, i, { args: [], returned: outputs[i] });
    }
    assertSpyCalls(logStub, outputs.length);
  });

  it("returns undefined for empty commit output", async () => {
    using logStub = stub(
      Deno.Command.prototype,
      "output",
      returnsNext([emptyCommandOutput]),
    );

    const result = await getLastCommitForOneOfFiles(
      [new URL("nonexistent.ts", gitDir)],
    );

    assertEquals(result, undefined);
    assertSpyCall(logStub, 0, { args: [], returned: emptyCommandOutput });
    assertSpyCalls(logStub, 1);
  });
});