forked from lerna/lerna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
describe-ref.test.js
142 lines (112 loc) · 3.79 KB
/
describe-ref.test.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"use strict";
jest.mock("@lerna/child-process");
const childProcess = require("@lerna/child-process");
const describeRef = require("../lib/describe-ref");
const DEFAULT_ARGS = ["describe", "--tags", "--always", "--long", "--dirty", "--first-parent"];
childProcess.exec.mockResolvedValue({ stdout: "v1.2.3-4-g567890a" });
childProcess.execSync.mockReturnValue("v1.2.3-4-g567890a");
describe("describeRef()", () => {
it("resolves parsed metadata", async () => {
const result = await describeRef();
expect(childProcess.exec).toHaveBeenLastCalledWith("git", DEFAULT_ARGS, {});
expect(result).toEqual({
isDirty: false,
lastTagName: "v1.2.3",
lastVersion: "v1.2.3",
refCount: "4",
sha: "567890a",
});
});
it("accepts options.cwd", async () => {
const options = { cwd: "foo" };
await describeRef(options);
expect(childProcess.exec).toHaveBeenLastCalledWith("git", DEFAULT_ARGS, options);
});
it("accepts options.match", async () => {
const options = { match: "v*.*.*" };
await describeRef(options);
expect(childProcess.exec).toHaveBeenLastCalledWith(
"git",
DEFAULT_ARGS.concat(["--match", "v*.*.*"]),
options
);
});
});
describe("describeRef.sync()", () => {
it("returns parsed metadata", () => {
const result = describeRef.sync();
expect(childProcess.execSync).toHaveBeenLastCalledWith("git", DEFAULT_ARGS, {});
expect(result).toEqual({
isDirty: false,
lastTagName: "v1.2.3",
lastVersion: "v1.2.3",
refCount: "4",
sha: "567890a",
});
});
it("accepts options.cwd", () => {
const options = { cwd: "foo" };
describeRef.sync(options);
expect(childProcess.execSync).toHaveBeenLastCalledWith("git", DEFAULT_ARGS, options);
});
it("accepts options.match", () => {
const options = { match: "v*.*.*" };
describeRef.sync(options);
expect(childProcess.execSync).toHaveBeenLastCalledWith(
"git",
DEFAULT_ARGS.concat(["--match", "v*.*.*"]),
options
);
});
});
describe("describeRef.parse()", () => {
it("matches independent tags", () => {
const result = describeRef.parse("pkg-name@1.2.3-4-g567890a");
expect(result.lastTagName).toBe("pkg-name@1.2.3");
expect(result.lastVersion).toBe("1.2.3");
});
it("matches independent tags for scoped packages", () => {
const result = describeRef.parse("@scope/pkg-name@1.2.3-4-g567890a");
expect(result.lastTagName).toBe("@scope/pkg-name@1.2.3");
expect(result.lastVersion).toBe("1.2.3");
});
it("matches dirty annotations", () => {
const result = describeRef.parse("pkg-name@1.2.3-4-g567890a-dirty");
expect(result.isDirty).toBe(true);
});
it("handles non-matching strings safely", () => {
const result = describeRef.parse("poopy-pants");
expect(result).toEqual({
isDirty: false,
lastTagName: undefined,
lastVersion: undefined,
refCount: undefined,
sha: undefined,
});
});
it("detects fallback and returns partial metadata", () => {
childProcess.execSync.mockReturnValueOnce("123");
const options = { cwd: "bar" };
const result = describeRef.parse("a1b2c3d", options);
expect(childProcess.execSync).toHaveBeenLastCalledWith(
"git",
["rev-list", "--count", "a1b2c3d"],
options
);
expect(result).toEqual({
isDirty: false,
refCount: "123",
sha: "a1b2c3d",
});
});
it("detects dirty fallback and returns partial metadata", () => {
childProcess.execSync.mockReturnValueOnce("456");
const result = describeRef.parse("a1b2c3d-dirty");
expect(childProcess.execSync).toHaveBeenLastCalledWith("git", ["rev-list", "--count", "a1b2c3d"], {});
expect(result).toEqual({
isDirty: true,
refCount: "456",
sha: "a1b2c3d",
});
});
});