-
Notifications
You must be signed in to change notification settings - Fork 23
/
buf-setup.test.ts
157 lines (127 loc) · 4.44 KB
/
buf-setup.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
import * as core from "@actions/core";
import * as io from "@actions/io";
import fs from "fs";
import cp from "child_process";
import osm, { type } from "os";
import path from "path";
import * as run from "../src/run";
import * as buf from "../src/buf";
const win32Join = path.win32.join;
const posixJoin = path.posix.join;
//jest.setTimeout(10000);
describe("buf-setup", () => {
let inputs = {} as any;
let os = {} as any;
let inSpy: jest.SpyInstance;
let platSpy: jest.SpyInstance;
let archSpy: jest.SpyInstance;
let joinSpy: jest.SpyInstance;
let execSpy: jest.SpyInstance;
let logSpy: jest.SpyInstance;
let dbgSpy: jest.SpyInstance;
let warnSpy: jest.SpyInstance;
let existsSpy: jest.SpyInstance;
let readFileSpy: jest.SpyInstance;
let getSpy: jest.SpyInstance;
let whichSpy: jest.SpyInstance;
let cnSpy: jest.SpyInstance;
let failedSpy: jest.SpyInstance;
beforeAll(async () => {
process.env["GITHUB_ENV"] = ""; // Stub out Environment file functionality so we can verify it writes to standard out (toolkit is backwards compatible)
}, 100000);
beforeEach(() => {
process.env["GITHUB_PATH"] = ""; // Stub out ENV file functionality so we can verify it writes to standard out
// @actions/core
inputs = {};
// Defaults as per action.yml
inputs["version"] = "1.26.0";
inputs["buf_domain"] = "buf.build";
inSpy = jest.spyOn(core, "getInput");
inSpy.mockImplementation((name) => inputs[name] ?? "");
failedSpy = jest.spyOn(core, "setFailed");
// buf
getSpy = jest.spyOn(buf, "getBuf");
getSpy.mockImplementation(
async (): Promise<string | Error> => "/usr/local"
);
// os
os = {};
platSpy = jest.spyOn(osm, "platform");
platSpy.mockImplementation(() => os["platform"]);
archSpy = jest.spyOn(osm, "arch");
// cp
archSpy.mockImplementation(() => os["arch"]);
execSpy = jest.spyOn(cp, "execSync");
// switch path join behaviour based on set os.platform
joinSpy = jest.spyOn(path, "join");
joinSpy.mockImplementation((...paths: string[]): string => {
if (os["platform"] == "win32") {
return win32Join(...paths);
}
return posixJoin(...paths);
});
// fs
existsSpy = jest.spyOn(fs, "existsSync");
readFileSpy = jest.spyOn(fs, "readFileSync");
// io
whichSpy = jest.spyOn(io, "which");
// writes
cnSpy = jest.spyOn(process.stdout, "write");
logSpy = jest.spyOn(core, "info");
dbgSpy = jest.spyOn(core, "debug");
warnSpy = jest.spyOn(core, "warning");
});
afterEach(() => {
jest.clearAllMocks();
process.exitCode = 0; // Prevent non-zero exit code from failing test.
});
afterAll(async () => {
jest.restoreAllMocks();
}, 100000);
describe("go-version-file", () => {
const goModContents = `module example.com/mymodule
go 1.14
require (
example.com/othermodule v1.2.3
example.com/thismodule v1.2.3
github.com/bufbuild/buf v1.26.1
)
replace example.com/thatmodule => ../thatmodule
exclude example.com/thismodule v1.3.0
`;
it("return version if go_version_file isn't set", async () => {
existsSpy.mockImplementation(() => false);
await run.run();
expect(logSpy).toHaveBeenCalledWith(
`Setting up buf version "${inputs["version"]}"`
);
expect(logSpy).toHaveBeenCalledWith(
`Successfully setup buf version ${inputs["version"]}`
);
});
it("reads version from go_version_file if set", async () => {
inputs["go_version_file"] = "go.mod";
existsSpy.mockImplementation(() => true);
readFileSpy.mockImplementation(() => Buffer.from(goModContents));
await run.run();
expect(warnSpy).toHaveBeenCalledWith(
"Both version and go_version_file inputs are specified, go_version_file will be preferred"
);
expect(logSpy).toHaveBeenCalledWith('Setting up buf version "1.26.1"');
expect(logSpy).toHaveBeenCalledWith(
"Successfully setup buf version 1.26.1"
);
});
it("reports failure if go_version_file doesn't exist", async () => {
inputs["go_version_file"] = "go.mod";
existsSpy.mockImplementation(() => false);
await run.run();
expect(warnSpy).toHaveBeenCalledWith(
"Both version and go_version_file inputs are specified, go_version_file will be preferred"
);
expect(cnSpy).toHaveBeenCalledWith(
`::error::The specified go version file: "go.mod" does not exist${osm.EOL}`
);
});
});
});