-
Notifications
You must be signed in to change notification settings - Fork 5
/
tested-version.test.ts
146 lines (117 loc) · 4.14 KB
/
tested-version.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
import nock from "nock";
import { mocked } from "jest-mock";
import mockedEnv from "mocked-env";
import * as core from "@actions/core";
import { testedVersion } from "../src/tested-version";
import { InvalidReadmeError } from "../src/exceptions/InvalidReadmeError";
jest.mock("@actions/core");
describe("[env variable mock]", () => {
let restore: () => void;
beforeEach(() => {
restore = mockedEnv({ GITHUB_REPOSITORY: "OWNER/REPO" });
mocked(core).getInput.mockReturnValue("GH_TOKEN");
});
afterEach(() => {
restore();
});
test.each([
"Tested up to: 0.42",
"Tested up to:0.42",
"LINE1\nNot Tested up to: 0.41\nTested up to: 0.42\nLINE2",
" Tested up to: 0.42",
" Tested up to: 0.42",
"\tTested up to: 0.42",
"\nTested up to: 0.42\n",
])("testedVersion works correctly", async (readme) => {
const readmePath = "path/to/readme.txt";
const config = {
readme: readmePath,
};
nock("https://api.github.com")
.get("/repos/OWNER/REPO/contents/" + encodeURIComponent(readmePath))
.reply(200, {
content: Buffer.from(readme).toString("base64"),
});
await expect(testedVersion(config)).resolves.toStrictEqual("0.42");
});
test("testedVersion works correctly with no config and readme.txt in repo root", async () => {
const readme = "Tested up to: 0.42";
nock("https://api.github.com")
.get("/repos/OWNER/REPO/contents/readme.txt")
.reply(200, {
content: Buffer.from(readme).toString("base64"),
});
await expect(testedVersion(null)).resolves.toStrictEqual("0.42");
});
test("testedVersion works correctly with no config and readme.txt in the plugin folder", async () => {
const readme = "Tested up to: 0.42";
nock("https://api.github.com")
.get("/repos/OWNER/REPO/contents/readme.txt")
.reply(404);
nock("https://api.github.com")
.get(
"/repos/OWNER/REPO/contents/" + encodeURIComponent("plugin/readme.txt")
)
.reply(200, {
content: Buffer.from(readme).toString("base64"),
});
await expect(testedVersion(null)).resolves.toStrictEqual("0.42");
});
test("testedVersion fails gracefully on connection issues", async () => {
const config = {
readme: "path/to/readme.txt",
};
await expect(testedVersion(config)).rejects.toThrow(InvalidReadmeError);
});
test("testedVersion fails gracefully on no readme", async () => {
const readmePath = "path/to/readme.txt";
const config = {
readme: readmePath,
};
nock("https://api.github.com")
.get("/repos/OWNER/REPO/contents/" + encodeURIComponent(readmePath))
.reply(404);
await expect(testedVersion(config)).rejects.toThrow(InvalidReadmeError);
});
test("testedVersion fails gracefully on invalid response", async () => {
const readmePath = "path/to/readme.txt";
const config = {
readme: readmePath,
};
nock("https://api.github.com")
.get("/repos/OWNER/REPO/contents/" + encodeURIComponent(readmePath))
.reply(200);
await expect(testedVersion(config)).rejects.toThrow(InvalidReadmeError);
});
test("testedVersion fails gracefully on invalid response 2", async () => {
const readmePath = "path/to/readme.txt";
const config = {
readme: readmePath,
};
nock("https://api.github.com")
.get("/repos/OWNER/REPO/contents/" + encodeURIComponent(readmePath))
.reply(200, {
content: "OOPS",
});
await expect(testedVersion(config)).rejects.toThrow(InvalidReadmeError);
});
test.each([
"Not Tested up to: 0.42",
"Tested up to:",
"Tested up to: 0.42:",
"Tested up to 0.42",
"Tested up to 0.42:",
"Tested up to: 0.41: 0.42",
])("testedVersion fails gracefully on invalid readme", async (readme) => {
const readmePath = "path/to/readme.txt";
const config = {
readme: readmePath,
};
nock("https://api.github.com")
.get("/repos/OWNER/REPO/contents/" + encodeURIComponent(readmePath))
.reply(200, {
content: Buffer.from(readme).toString("base64"),
});
await expect(testedVersion(config)).rejects.toThrow(InvalidReadmeError);
});
});