-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles-to-prompt.test.ts
323 lines (273 loc) · 13.4 KB
/
files-to-prompt.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
import { describe, beforeEach, afterEach, expect, test, spyOn } from "bun:test";
import { main, isBinaryFile, parseFilePathsFromStdin } from "./files-to-prompt";
import * as ftp from "./files-to-prompt";
// Looks like 1ms not enough delay for tests to pass reliably, 5ms seems OK on my M1
// In doubt, set to 50ms
// Without this delay the afterEach() hook can run and delete files before the main() call has completed
const sleepTime = 5;
describe('files-to-prompt.ts', () => {
const testDir = path.join(__dirname, 'test-data');
let stdoutOutput: string = '';
let stderrOutput: string = '';
// Overwrite / mock output functions in main script to capture output in variables
// Earlier versions of this test script used execSync() and direct stdout direction
// But this makes it hard to capture stderr output (stderr gets redirected to the parent process
// and then annoyingly error messages show up on the console when the test script runs)
// Also child_process testing prevents the test framework from tracing codepaths and this disables coverage
spyOn(ftp, 'output').mockImplementation((...args: any[]) => { stdoutOutput += args.join(' ') + '\n' });
spyOn(ftp, 'error').mockImplementation((...args: any[]) => { stderrOutput += args.join(' ') + '\n' });
beforeEach(() => {
fs.mkdirSync(testDir, { recursive: true });
});
afterEach(() => {
fs.rmSync(testDir, { recursive: true, force: true });
stdoutOutput = '';
stderrOutput = '';
});
async function runScript(args: string[]): Promise<void> {
await main(args);
await Bun.sleep(sleepTime); // The joy of asynchrony, tests will fail without sleeping a few ms here
}
test('should include single file passed on the command line', async () => {
const filePath = path.join(testDir, 'file1.txt');
fs.writeFileSync(filePath, 'File 1 contents');
const args = [filePath];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).toContain(filePath);
expect(stdoutOutput).toContain("File 1 contents");
});
test('should include multiple files passed on the command line', async () => {
const file1Path = path.join(testDir, 'file1.txt');
const file2Path = path.join(testDir, 'file2.txt');
fs.writeFileSync(file1Path, 'File 1 contents');
fs.writeFileSync(file2Path, 'File 2 contents');
const args = [file1Path, file2Path];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).toContain(file1Path);
expect(stdoutOutput).toContain('File 1 contents');
expect(stdoutOutput).toContain(file2Path);
expect(stdoutOutput).toContain('File 2 contents');
});
test('should include files in directories passed on the command line', async () => {
const dirPath = path.join(testDir, 'dir');
fs.mkdirSync(dirPath);
const filePath = path.join(dirPath, 'file.txt');
fs.writeFileSync(filePath, 'File contents');
const args = [dirPath];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).toContain(filePath);
expect(stdoutOutput).toContain('File contents');
});
test('should include files a few levels deep in a directory structure', async () => {
const dir1Path = path.join(testDir, 'dir1');
const dir2Path = path.join(dir1Path, 'dir2');
fs.mkdirSync(dir1Path);
fs.mkdirSync(dir2Path);
const filePath = path.join(dir2Path, 'file.txt');
fs.writeFileSync(filePath, 'File contents');
const args = [testDir];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).toContain(filePath);
expect(stdoutOutput).toContain('File contents');
});
test('should exclude files matching patterns passed via --ignore', async () => {
const file1Path = path.join(testDir, 'file1.txt');
const file2Path = path.join(testDir, 'file2.txt');
fs.writeFileSync(file1Path, 'File 1 contents');
fs.writeFileSync(file2Path, 'File 2 contents');
const args = [testDir, '--ignore', 'file1.txt'];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).not.toContain(file1Path);
expect(stdoutOutput).toContain(file2Path);
});
test('should exclude files matching patterns passed via multiple --ignore', async () => {
const file1Path = path.join(testDir, 'file1.txt');
const file2Path = path.join(testDir, 'file2.txt');
const file3Path = path.join(testDir, 'file3.txt');
fs.writeFileSync(file1Path, 'File 1 contents');
fs.writeFileSync(file2Path, 'File 2 contents');
fs.writeFileSync(file3Path, 'File 3 contents');
const args = [testDir, '--ignore', 'file1.txt', '--ignore', 'file2.txt'];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).not.toContain(file1Path);
expect(stdoutOutput).not.toContain(file2Path);
expect(stdoutOutput).toContain(file3Path);
});
test('should fail when --ignore gets passed without an argument', async () => {
const file1Path = path.join(testDir, 'file1.txt');
fs.writeFileSync(file1Path, 'File 1 contents');
const args = [testDir, '--ignore'];
await runScript(args);
expect(stderrOutput).toContain('--ignore option requires a pattern');
expect(stdoutOutput).not.toContain(file1Path);
});
test('should exclude files matching patterns in .gitignore', async () => {
const file1Path = path.join(testDir, 'file1.txt');
const file2Path = path.join(testDir, 'file2.txt');
fs.writeFileSync(file1Path, 'File 1 contents');
fs.writeFileSync(file2Path, 'File 2 contents');
fs.writeFileSync(path.join(testDir, '.gitignore'), 'file1.txt');
const args = [testDir];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).not.toContain(file1Path);
expect(stdoutOutput).toContain(file2Path);
});
test('should exclude directory matching patterns in .gitignore', async () => {
const dir1Path = path.join(testDir, 'dir1');
const dir2Path = path.join(dir1Path, 'dir2');
fs.mkdirSync(dir1Path);
fs.mkdirSync(dir2Path);
const file1Path = path.join(dir2Path, 'file.txt');
fs.writeFileSync(file1Path, 'File 1 contents');
const file2Path = path.join(testDir, 'file2.txt');
fs.writeFileSync(file2Path, 'File 2 contents');
fs.writeFileSync(path.join(testDir, '.gitignore'), 'dir1/');
const args = [testDir];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).not.toContain(file1Path);
expect(stdoutOutput).toContain(file2Path);
});
test('should exclude directory matching patterns in .gitignore in different directories', async () => {
const dir1Path = path.join(testDir, 'dir1');
const dir2Path = path.join(testDir, 'dir2');
fs.mkdirSync(dir1Path);
fs.mkdirSync(dir2Path);
const file1Path = path.join(dir1Path, 'file1.txt');
fs.writeFileSync(file1Path, 'File 1 contents');
const file2Path = path.join(dir2Path, 'file2.txt');
fs.writeFileSync(file2Path, 'File 2 contents');
fs.writeFileSync(path.join(dir1Path, '.gitignore'), 'file1.txt');
fs.writeFileSync(path.join(dir2Path, '.gitignore'), 'file2.txt');
const args = [testDir];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).not.toContain(file1Path);
expect(stdoutOutput).not.toContain(file2Path);
});
test('should include hidden files and directories when --include-hidden is passed', async () => {
const hiddenFilePath = path.join(testDir, '.hidden-file.txt');
const hiddenDirPath = path.join(testDir, '.hidden-dir');
const hiddenDirFilePath = path.join(hiddenDirPath, 'file.txt');
fs.writeFileSync(hiddenFilePath, 'Hidden file contents');
fs.mkdirSync(hiddenDirPath);
fs.writeFileSync(hiddenDirFilePath, 'Hidden dir file contents');
const args = [testDir, '--include-hidden'];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).toContain(hiddenFilePath);
expect(stdoutOutput).toContain('Hidden file contents');
expect(stdoutOutput).toContain(hiddenDirFilePath);
expect(stdoutOutput).toContain('Hidden dir file contents');
});
test('should ignore .gitignore files when --ignore-gitignore is passed', async () => {
const file1Path = path.join(testDir, 'file1.txt');
fs.writeFileSync(file1Path, 'File 1 contents');
fs.writeFileSync(path.join(testDir, '.gitignore'), 'file1.txt');
const args = [testDir, '--ignore-gitignore'];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).toContain(file1Path);
expect(stdoutOutput).toContain('File 1 contents');
});
test('should skip binary files', async () => {
const binaryFilePath = path.join(testDir, 'binary.data');
const binaryData = Buffer.from([0x80, 0x81, 0x82, 0x83, 0x84, 0x85]);
fs.writeFileSync(binaryFilePath, binaryData);
const args = [testDir];
await runScript(args);
expect(stderrOutput).toContain('Warning: Skipping binary file');
expect(stdoutOutput).not.toContain(binaryFilePath);
});
test('should fail silently if isBinaryFile() gets called with invalid path', async () => {
const result = await isBinaryFile('./file-does-not-exist.txt');
expect(result).toBeFalse;
});
test("should skip FIFOs", async () => {
const fifoFilePath = path.join(testDir, "fifo");
execSync(`mkfifo ${fifoFilePath}`);
const args = [fifoFilePath];
await runScript(args);
expect(stderrOutput).toContain('unsupported file type');
expect(stdoutOutput).not.toContain(fifoFilePath);
});
test("should fail with error message if path does not exist", async () => {
const args = ['./file-does-not-exist.txt'];
await runScript(args);
expect(stderrOutput).toContain('Path does not exist');
});
test('should parse file paths with parseFilePathsFromStdin() correctly', () => {
const stdinData = `file1.txt:File 1 contents.\nfile2.txt:File 2 contents.`;
const filePathsFromStdin = parseFilePathsFromStdin(stdinData);
expect(filePathsFromStdin).toEqual(['file1.txt', 'file2.txt']);
});
test('should de-duplicate file paths with parseFilePathsFromStdin()', () => {
const stdinData = `file1.txt:File 1 contents.\nfile2.txt:File 2 contents.\nfile1.txt:Another match in file1.txt.`;
const filePathsFromStdin = parseFilePathsFromStdin(stdinData);
expect(filePathsFromStdin).toEqual(['file1.txt', 'file2.txt']);
});
test('should parse file paths with one file path per line', () => {
const stdinData = `file1.txt\nfile2.txt\nfile3.txt`;
const filePathsFromStdin = parseFilePathsFromStdin(stdinData);
expect(filePathsFromStdin).toEqual(['file1.txt', 'file2.txt', 'file3.txt']);
});
test('should handle mixed input formats', () => {
const stdinData = `file1.txt:File 1 contents.\nfile2.txt\nfile3.txt:File 3 contents.`;
const filePathsFromStdin = parseFilePathsFromStdin(stdinData);
expect(filePathsFromStdin).toEqual(['file1.txt', 'file2.txt', 'file3.txt']);
});
test('should handle empty lines in stdin data', () => {
const stdinData = `file1.txt:File 1 contents.\n\nfile2.txt:File 2 contents.\n`;
const filePathsFromStdin = parseFilePathsFromStdin(stdinData);
expect(filePathsFromStdin).toEqual(['file1.txt', 'file2.txt']);
});
test('should handle binary data in stdin', () => {
const binaryData = Buffer.from([0x80, 0x81, 0x82, 0x83, 0x84, 0x85]);
const stdinData = `file1.txt:File 1 contents.\n${binaryData.toString('utf8')}\nfile2.txt:File 2 contents.`;
const filePathsFromStdin = parseFilePathsFromStdin(stdinData);
expect(filePathsFromStdin).toEqual(['file1.txt', 'file2.txt']);
});
test('should handle common text/code files in stdin', () => {
const textData = `console.log('Hello, world\!');`;
const stdinData = `file1.txt:File 1 contents.\n${textData}\nfile2.txt:File 2 contents.`;
const filePathsFromStdin = parseFilePathsFromStdin(stdinData);
expect(filePathsFromStdin).toEqual(['file1.txt', textData, 'file2.txt']);
});
test('should handle long file paths in stdin', () => {
const longFilePath = 'a'.repeat(1025);
const stdinData = `file1.txt:File 1 contents.\n${longFilePath}\nfile2.txt:File 2 contents.`;
const filePathsFromStdin = parseFilePathsFromStdin(stdinData);
expect(filePathsFromStdin).toEqual(['file1.txt', 'file2.txt']);
});
test('should ignore file paths with the null character', () => {
const invalidFilePath = 'invalid_file\0.txt';
const stdinData = `file1.txt:File 1 contents.\n${invalidFilePath}\nfile2.txt:File 2 contents.`;
const filePathsFromStdin = parseFilePathsFromStdin(stdinData);
expect(filePathsFromStdin).toEqual(['file1.txt', 'file2.txt']);
});
test('should ignore file paths with control characters', () => {
const stdinData = `file1.txt:File 1 contents.\nfile2.txt\x07.txt:File 2 contents.\nfile3.txt:File 3 contents.`;
const filePathsFromStdin = parseFilePathsFromStdin(stdinData);
expect(filePathsFromStdin).toEqual(['file1.txt', 'file3.txt']);
});
test('should output version string when --version is passed', async () => {
await main(['--version']);
expect(stdoutOutput).toContain(`files-to-prompt.ts version`);
expect(stderrOutput).toBeEmpty();
});
test('should output error for unsupported options', async () => {
await main(['--unsupported-option']);
expect(stdoutOutput).toBeEmpty();
expect(stderrOutput).toContain('Unsupported option');
});
});