-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsvz.test.js
241 lines (214 loc) · 9.22 KB
/
fsvz.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const micromatch = require("micromatch");
describe("globToRegex", () => {
const { globToRegex } = require("./fsvz");
test("converts simple glob to regex", () => {
const regex = globToRegex("*.js");
expect(regex.test("test.js")).toBe(true); // Positive
expect(regex.test("example.js")).toBe(true); // Positive
expect(regex.test("test.txt")).toBe(false); // Negative
expect(regex.test(".js")).toBe(false); // Negative
});
test("handles special characters", () => {
const regex = globToRegex("file?.js");
expect(regex.test("file1.js")).toBe(true); // Positive
expect(regex.test("filea.js")).toBe(true); // Positive
expect(regex.test("file12.js")).toBe(false); // Negative
expect(regex.test("file.js")).toBe(false); // Negative
});
test("handles multiple patterns", () => {
const regex = globToRegex("*.js|*.ts");
expect(regex.test("test.js")).toBe(true); // Positive
expect(regex.test("example.ts")).toBe(true); // Positive
expect(regex.test("test.txt")).toBe(false); // Negative
expect(regex.test("example.jsx")).toBe(false); // Negative
});
test("handles dotfiles", () => {
const regex = globToRegex(".*");
expect(regex.test(".gitignore")).toBe(true); // Positive
expect(regex.test(".env")).toBe(true); // Positive
expect(regex.test("file.txt")).toBe(false); // Negative
expect(regex.test("anotherfile")).toBe(false); // Negative
});
test("handles complex patterns", () => {
const regex = globToRegex("*.{js,jsx}");
expect(regex.test("file.js")).toBe(true); // Positive
expect(regex.test("file.jsx")).toBe(true); // Positive
expect(regex.test("file.ts")).toBe(false); // Negative
expect(regex.test("filejs")).toBe(false); // Negative
});
test("handles directory patterns", () => {
const regex = globToRegex("src/**/*.js");
expect(regex.test("src/components/test.js")).toBe(true); // Positive
expect(regex.test("src/index.js")).toBe(true); // Positive
expect(regex.test("src/index.ts")).toBe(false); // Negative
expect(regex.test("src/test.txt")).toBe(false); // Negative
});
test("handles directory patterns (positive cases)", () => {
const regex = globToRegex("src/**/*.js");
expect(regex.test("src/components/test.js")).toBe(true); // Positive
expect(regex.test("src/index.js")).toBe(true); // Positive
expect(regex.test("src/dir1/dir2/test.js")).toBe(true); // Positive
expect(regex.test("src/dir1/dir2/dir3/test.js")).toBe(true); // Positive
expect(regex.test("src/dir1/test.js")).toBe(true); // Positive
expect(regex.test("src/test.js")).toBe(true); // Positive
expect(regex.test("src/subdir/file.js")).toBe(true); // Positive
expect(regex.test("src/anotherdir/anotherfile.js")).toBe(true); // Positive
expect(regex.test("src/mixedContent123/file.js")).toBe(true); // Positive
expect(regex.test("src/a/b/c/d/e/f/g/h/i/j/file.js")).toBe(true); // Positive
});
test("handles directory patterns (negative cases)", () => {
const regex = globToRegex("src/**/*.js");
expect(regex.test("src/index.ts")).toBe(false); // Negative
expect(regex.test("src/indexjsx")).toBe(false); // Negative
expect(regex.test("src/components/test.jsx")).toBe(false); // Negative
expect(regex.test("src/components/test.js.txt")).toBe(false); // Negative
expect(regex.test("src/dir1/dir2/test.ts")).toBe(false); // Negative
expect(regex.test("source/components/test.js")).toBe(false); // Negative
expect(regex.test("srcdir1/dir2/test.js")).toBe(false); // Negative
expect(regex.test("src2/components/test.js")).toBe(false); // Negative
expect(regex.test("src/dir1/dir2/")).toBe(false); // Negative (Just a directory, no file)
expect(regex.test("src/dir1/dir2/testfile")).toBe(false); // Negative (No .js extension)
});
});
describe("getOptions", () => {
const getOptions = require("./fsvz").getOptions;
test("parses simple flags", () => {
const options = getOptions(["-s", "-d"]);
expect(options.simple).toBe(true);
expect(options.dirsOnly).toBe(true);
});
test("parses ignore pattern", () => {
const options = getOptions(["-i", "*.js"]);
expect(options.ignorePatterns).toEqual(["*.js"]);
expect(micromatch.isMatch("test.js", options.ignorePatterns)).toBe(true);
expect(micromatch.isMatch("test.txt", options.ignorePatterns)).toBe(false);
});
test("parses multiple ignore patterns", () => {
const options = getOptions(["-i", "*.js,node_modules"]);
expect(options.ignorePatterns).toEqual(["*.js", "node_modules"]);
expect(micromatch.isMatch("test.js", options.ignorePatterns)).toBe(true);
expect(micromatch.isMatch("test.txt", options.ignorePatterns)).toBe(false);
});
test("parses ignore pattern with equal sign", () => {
const options = getOptions(["--ignore=*.js"]);
expect(options.ignorePatterns).toEqual(["*.js"]);
expect(micromatch.isMatch("test.js", options.ignorePatterns)).toBe(true);
expect(micromatch.isMatch("test.txt", options.ignorePatterns)).toBe(false);
});
test("handles invalid pattern gracefully", () => {
const logSpy = jest.spyOn(console, "error").mockImplementation(() => {});
const exitSpy = jest.spyOn(process, "exit").mockImplementation(() => {});
getOptions(["-i", "[invalid"]);
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("Invalid pattern"));
expect(exitSpy).toHaveBeenCalledWith(1);
logSpy.mockRestore();
exitSpy.mockRestore();
});
test("handles invalid filename gracefully", () => {
const logSpy = jest.spyOn(console, "error").mockImplementation(() => {});
const exitSpy = jest.spyOn(process, "exit").mockImplementation(() => {});
getOptions(["-r"]);
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("Please provide a filename"));
expect(exitSpy).toHaveBeenCalledWith(1);
logSpy.mockRestore();
exitSpy.mockRestore();
});
});
describe("getDirectoryStructure", () => {
const fs = require("fs");
const path = require("path");
const tmpDir = path.join(__dirname, "tmp");
const { getDirectoryStructure } = require("./fsvz");
beforeAll(() => {
// Clean up if the directory already exists
if (fs.existsSync(tmpDir)) {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
// Create a temporary directory structure
fs.mkdirSync(path.join(tmpDir, "dir"), { recursive: true });
fs.writeFileSync(path.join(tmpDir, "dir", "file1.txt"), "");
fs.writeFileSync(path.join(tmpDir, "dir", "file2.txt"), "");
fs.mkdirSync(path.join(tmpDir, "dir", "subdir"), { recursive: true });
fs.mkdirSync(path.join(tmpDir, "dir", "subdir2"), { recursive: true });
fs.writeFileSync(path.join(tmpDir, "dir", "subdir2", "file3.txt"), "");
fs.symlinkSync(path.join(tmpDir, "target"), path.join(tmpDir, "dir", "symlink"));
});
afterAll(() => {
// Remove the temporary directory
fs.rmSync(tmpDir, { recursive: true, force: true });
});
test("returns an empty array for an empty directory", () => {
fs.mkdirSync(path.join(tmpDir, "emptydir"), { recursive: true });
const structure = getDirectoryStructure(path.join(tmpDir, "emptydir"), {});
expect(structure).toEqual([]);
fs.rmdirSync(path.join(tmpDir, "emptydir"));
});
test("returns the correct structure for a directory with files and subdirectories", () => {
const dirPath = path.join(tmpDir, "dir");
const options = { path: dirPath };
const structure = getDirectoryStructure(dirPath, options);
const expectedStructure = [
{
name: "subdir",
path: path.join(dirPath, "subdir"),
type: "directory",
children: [],
},
{
name: "subdir2",
path: path.join(dirPath, "subdir2"),
type: "directory",
children: [
{
name: "file3.txt",
path: path.join(dirPath, "subdir2", "file3.txt"),
type: "file",
},
],
},
{
name: "file1.txt",
path: path.join(dirPath, "file1.txt"),
type: "file",
},
{
name: "file2.txt",
path: path.join(dirPath, "file2.txt"),
type: "file",
},
{
name: "symlink",
path: path.join(dirPath, "symlink"),
type: "symbolic link",
target: path.join(tmpDir, "target"),
},
];
expect(structure).toMatchObject(expectedStructure);
});
test("excludes files when 'dirsOnly' option is true", () => {
const options = { dirsOnly: true };
const structure = getDirectoryStructure(path.join(tmpDir, "dir"), options);
const expectedStructure = [
{ name: "subdir", type: "directory" },
{ name: "subdir2", type: "directory" },
];
expectedStructure.forEach((expectedItem) => {
const item = structure.find(
(i) => i.name === expectedItem.name && i.type === expectedItem.type
);
expect(item).toMatchObject(expectedItem);
});
});
test("handles symbolic links correctly", () => {
const structure = getDirectoryStructure(path.join(tmpDir, "dir"), {});
const expectedItem = {
name: "symlink",
type: "symbolic link",
target: path.join(tmpDir, "target"),
};
const item = structure.find(
(i) => i.name === expectedItem.name && i.type === expectedItem.type
);
expect(item).toMatchObject(expectedItem);
});
});