-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathvalidate.test.js
More file actions
127 lines (102 loc) · 4.08 KB
/
Copy pathvalidate.test.js
File metadata and controls
127 lines (102 loc) · 4.08 KB
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
/**
* Tests for validate-filepaths main script logic
*/
function runValidateFilepaths(changedFiles) {
jest.resetModules();
process.env.CHANGED_FILES = changedFiles.join("\n");
const core = {
getInput: jest.fn((name) => (name === "changed-files" ? process.env.CHANGED_FILES : "")),
setFailed: jest.fn(),
info: jest.fn(),
warning: jest.fn(),
error: jest.fn(),
setOutput: jest.fn(),
};
jest.doMock("@actions/core", () => core);
jest.doMock("fs", () => ({
existsSync: jest.fn(() => true),
statSync: jest.fn(() => ({ isDirectory: () => false })),
readdirSync: jest.fn(() => []),
readFileSync: jest.fn(() => ""),
}));
jest.isolateModules(() => {
require("../index.js");
});
return core;
}
afterEach(() => {
delete process.env.CHANGED_FILES;
jest.resetModules();
jest.clearAllMocks();
jest.unmock("@actions/core");
jest.unmock("fs");
});
describe("isFileInsideAYearFolder", () => {
test("accepts a file in a year folder", () => {
const core = runValidateFilepaths(["2024/2024-01-01-brand.markdown"]);
expect(core.setFailed).not.toHaveBeenCalled();
});
test("accepts a file in a year/month subfolder", () => {
const core = runValidateFilepaths(["2024/01/2024-01-01-brand.markdown"]);
expect(core.setFailed).not.toHaveBeenCalled();
});
test("rejects a file not in a year folder", () => {
const core = runValidateFilepaths(["README.md"]);
expect(core.setFailed).toHaveBeenCalled();
});
test("rejects a file with a non-year top-level folder", () => {
const core = runValidateFilepaths(["foo/2024-01-01-brand.markdown"]);
expect(core.setFailed).toHaveBeenCalled();
});
});
describe("filepath date validation pattern", () => {
test("accepts correct YYYY-MM-DD format in filename", () => {
expect(
runValidateFilepaths(["2024/01/2024-01-15-brand.markdown"]).setFailed
).not.toHaveBeenCalled();
expect(
runValidateFilepaths(["2023/12/2023-12-31-company.markdown"]).setFailed
).not.toHaveBeenCalled();
});
test("rejects incorrect date formats", () => {
expect(runValidateFilepaths(["2024/1-01-15-brand.markdown"]).setFailed).toHaveBeenCalled();
expect(runValidateFilepaths(["2024/01/24-01-15-brand.markdown"]).setFailed).toHaveBeenCalled();
expect(runValidateFilepaths(["2024/01/2024-1-15-brand.markdown"]).setFailed).toHaveBeenCalled();
expect(runValidateFilepaths(["2024/01/2024-01-5-brand.markdown"]).setFailed).toHaveBeenCalled();
});
test("rejects filenames without date", () => {
expect(runValidateFilepaths(["README.md"]).setFailed).toHaveBeenCalled();
expect(runValidateFilepaths(["2024/01/brand.markdown"]).setFailed).toHaveBeenCalled();
});
});
describe("filepath structure validation", () => {
// Tests for the overall filepath structure (year folder, optional month folder, filename)
function isFileInsideAYearFolder(filepath) {
return filepath.match(/^\d{4}/) !== null;
}
function isFilepathDateValid(filepath) {
const filename = filepath.split("/").pop();
const dateStringInFilename = filename.match(/\d{4}-\d{2}-\d{2}/);
return dateStringInFilename !== null;
}
test("correct structure with year-only folder", () => {
const validPath = "2024/2024-06-15-brand.markdown";
expect(isFileInsideAYearFolder(validPath)).toBe(true);
expect(isFilepathDateValid(validPath)).toBe(true);
});
test("correct structure with year/month folders", () => {
const validPath = "2024/06/2024-06-15-brand.markdown";
expect(isFileInsideAYearFolder(validPath)).toBe(true);
expect(isFilepathDateValid(validPath)).toBe(true);
});
test("incorrect structure with missing year folder", () => {
const invalidPath = "brand/2024-06-15-brand.markdown";
expect(isFileInsideAYearFolder(invalidPath)).toBe(false);
expect(isFilepathDateValid(invalidPath)).toBe(true);
});
test("incorrect structure with non-matching date in filename", () => {
const invalidPath = "2024/06/2023-06-15-brand.markdown";
expect(isFileInsideAYearFolder(invalidPath)).toBe(true);
expect(isFilepathDateValid(invalidPath)).toBe(true);
});
});