/
sbt.test.ts
213 lines (179 loc) · 5.62 KB
/
sbt.test.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import * as Auto from "@auto-it/core";
import { dummyLog } from "@auto-it/core/dist/utils/logger";
import { makeHooks } from "@auto-it/core/dist/utils/make-hooks";
import SbtPlugin, { ISbtPluginOptions, sbtClient, sbtGetVersion } from "../src";
const exec = jest.fn();
jest.mock(
"../../../packages/core/dist/utils/exec-promise",
() => (...args: any[]) => exec(...args),
);
const rawOutput =
`[[0minfo[0m] entering *experimental* thin client - BEEP WHIRR
[[0minfo[0m] terminate the server with \`shutdown\`
> print version
1.2.3[0J
[0J[[32msuccess[0m] Total time: 2 s, completed Apr 27, 2021 3:39:23 AM
[0J`;
const cleanedOutput = `1.2.3
[success] Total time: 2 s, completed Apr 27, 2021 3:39:23 AM`;
const rawAggregationOutput =
`[[0minfo[0m] entering *experimental* thin client - BEEP WHIRR
[[0minfo[0m] terminate the server with \`shutdown\`
> set version/aggregate := false
[info] Defining version / aggregate[0J
[0J[info] The new value will be used by no settings or tasks.[0J
[0J[info] Reapplying settings...[0J
[0J[info] set current project to auto-release-test-scala (in build file:/Users/user/project/)[0J
[0J[[32msuccess[0m] Total time: 2 s, completed Apr 27, 2021 3:52:04 AM
[0Jv`;
describe("sbt plugin", () => {
let hooks: Auto.IAutoHooks;
const prefixRelease: (a: string) => string = jest.fn(
(version) => `v${version}`,
);
const options: ISbtPluginOptions = {};
const logger = dummyLog();
const setup = (options: ISbtPluginOptions) => {
const plugin = new SbtPlugin(options);
hooks = makeHooks();
plugin.apply(
({
hooks,
logger,
remote: "stubRemote",
prefixRelease,
git: {
getLastTagNotInBaseBranch: async () => undefined,
getLatestRelease: async () => "0.0.1",
},
getCurrentVersion: async () => "0.0.1",
} as unknown) as Auto.Auto,
);
};
beforeEach(() => {
exec.mockClear();
setup(options);
});
describe("sbt client", () => {
test("should clean output", async () => {
exec.mockReturnValueOnce(rawOutput);
const output = await sbtClient("");
expect(output).toBe(cleanedOutput);
});
test("should parse version value", async () => {
exec
.mockReturnValueOnce(rawAggregationOutput)
.mockReturnValueOnce(rawOutput);
const output = await sbtGetVersion();
expect(output).toBe("1.2.3");
});
test("should error if it can't parse version value", async () => {
exec
.mockReturnValueOnce(rawAggregationOutput)
.mockReturnValueOnce("");
await expect(sbtGetVersion()).rejects.toThrowError(
`Failed to read version from sbt: `,
);
});
});
describe("version hook", () => {
test("should set version in sbt", async () => {
exec.mockReturnValue("");
await hooks.version.promise({
bump: Auto.SEMVER.minor,
});
expect(exec).toHaveBeenCalledTimes(2);
expect(exec).lastCalledWith("sbt", [
"--client",
'set every version := \\"0.1.0\\"',
]);
});
});
describe("publish hook", () => {
test("should call sbt publish", async () => {
exec.mockReturnValue("");
await hooks.publish.promise({
bump: Auto.SEMVER.minor,
});
expect(exec).toHaveBeenCalledWith("sbt", [
"--client",
"publish",
]);
});
test("should call sbt publish with custom command", async () => {
setup({
publishCommand: "+publishLocal",
});
exec.mockReturnValue("");
await hooks.publish.promise({
bump: Auto.SEMVER.minor,
});
expect(exec).toHaveBeenCalledWith("sbt", [
"--client",
"+publishLocal",
]);
});
});
describe("canary hook", () => {
test("should only read version from sbt on dry run", async () => {
exec
.mockReturnValueOnce(rawAggregationOutput)
.mockReturnValueOnce(rawOutput);
await hooks.canary.promise({
bump: Auto.SEMVER.minor,
canaryIdentifier: "-canary.42.1",
dryRun: true,
});
expect(exec).toHaveBeenCalledTimes(2); // 2 calls in sbtGetVersion
});
test("should return version from sbt as canary", async () => {
exec.mockReturnValue(rawOutput);
const result = await hooks.canary.promise({
bump: Auto.SEMVER.minor,
canaryIdentifier: "-canary.42.1",
});
expect(exec).not.toHaveBeenCalledWith("sbt", [
"--client",
'set every version := \\"0.1.0\\"',
]);
expect(result).toMatchObject({
newVersion: "1.2.3",
details: [
"```",
cleanedOutput,
"```",
].join("\n"),
});
});
test("should construct canary version when configured", async () => {
setup({
setCanaryVersion: true,
});
exec.mockReturnValue(rawOutput);
const result = await hooks.canary.promise({
bump: Auto.SEMVER.minor,
canaryIdentifier: "-canary.42.1",
});
const newVersion = "0.0.0-canary.42.1-SNAPSHOT";
expect(exec).toHaveBeenCalledWith("sbt", [
"--client",
`set every version := \\"${newVersion}\\"`,
]);
expect(result).toMatchObject({ newVersion });
});
test("should call sbt publish with custom command", async () => {
setup({
publishCommand: "+publishLocal",
});
exec.mockReturnValue(rawOutput);
await hooks.canary.promise({
bump: Auto.SEMVER.minor,
canaryIdentifier: "-canary.42.1",
});
expect(exec).toHaveBeenCalledWith("sbt", [
"--client",
"+publishLocal",
]);
});
});
});