-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles-to-prompt.test.ts
487 lines (413 loc) · 19.6 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
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";
describe('files-to-prompt.ts', () => {
const testDir = path.join(__dirname, 'test-data');
const outDir = path.join(__dirname, 'test-output');
let stdoutOutput: string = '';
let stderrOutput: string = '';
// Overwrite / mock console 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, 'consoleOutput').mockImplementation((...args: any[]) => { stdoutOutput += args.join(' ') + '\n' });
spyOn(ftp, 'consoleError').mockImplementation((...args: any[]) => { stderrOutput += args.join(' ') + '\n' });
beforeEach(() => {
fs.mkdirSync(testDir, { recursive: true });
fs.mkdirSync(outDir, { recursive: true });
});
afterEach(() => {
fs.rmSync(testDir, { recursive: true, force: true });
fs.rmSync(outDir, { recursive: true, force: true });
stdoutOutput = '';
stderrOutput = '';
});
async function runScript(args: string[]): Promise<void> {
await main(args);
}
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 v`);
expect(stderrOutput).toBeEmpty();
});
test('should output error for unsupported options', async () => {
await main(['--unsupported-option']);
expect(stdoutOutput).toBeEmpty();
expect(stderrOutput).toContain('Unsupported option');
});
test('should output to a file when --output is passed', async () => {
const filePath = path.join(testDir, 'file1.txt');
fs.writeFileSync(filePath, 'File 1 contents');
const outputFilePath = path.join(outDir, 'output.txt');
const args = [filePath, '--output', outputFilePath];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).toBeEmpty();
const outputFileContents = fs.readFileSync(outputFilePath, 'utf8');
expect(outputFileContents).toContain(filePath);
expect(outputFileContents).toContain('File 1 contents');
});
test('should output to a file when -o is passed', async () => {
const filePath = path.join(testDir, 'file1.txt');
fs.writeFileSync(filePath, 'File 1 contents');
const outputFilePath = path.join(outDir, 'output.txt');
const args = [filePath, '-o', outputFilePath];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).toBeEmpty();
const outputFileContents = fs.readFileSync(outputFilePath, 'utf8');
expect(outputFileContents).toContain(filePath);
expect(outputFileContents).toContain('File 1 contents');
});
test('should output error if --output is passed without a file path', async () => {
const filePath = path.join(testDir, 'file1.txt');
fs.writeFileSync(filePath, 'File 1 contents');
const args = [filePath, '--output'];
await runScript(args);
expect(stderrOutput).toContain('option requires a file path');
expect(stdoutOutput).toBeEmpty();
});
test('should output error if -o is passed without a file path', async () => {
const filePath = path.join(testDir, 'file1.txt');
fs.writeFileSync(filePath, 'File 1 contents');
const args = [filePath, '-o'];
await runScript(args);
expect(stderrOutput).toContain('option requires a file path');
expect(stdoutOutput).toBeEmpty();
});
test('should output error if output file cannot be written', async () => {
const filePath = path.join(testDir, 'file1.txt');
fs.writeFileSync(filePath, 'File 1 contents');
const outputFilePath = path.join('/non-existent-directory', 'output.txt');
const args = [filePath, '--output', outputFilePath];
await runScript(args);
expect(stderrOutput).toContain('Error writing to output file');
expect(stdoutOutput).toBeEmpty();
});
// const nbconvertTool = 'jupyter-nbconvert'; // real nbconvert tool (no point in running it for every test)
const nbconvertTool = './nbconvert-shim.ts'; // fast fake tool for testing, with relative path
// const nbconvertTool = path.join(__dirname, 'nbconvert-shim.ts'); // same as above, with absolute path
const ipynbFileContents = JSON.stringify({
cells: [
{
cell_type: 'code',
execution_count: 1,
metadata: {},
outputs: [],
source: ['print(\'Hello, World!\')'],
},
],
metadata: {
kernelspec: {
display_name: 'Python 3 (ipykernel)',
language: 'python',
name: 'python3',
},
language_info: {
codemirror_mode: {
name: 'ipython',
version: 3,
},
file_extension: '.py',
mimetype: 'text/x-python',
name: 'python',
nbconvert_exporter: 'python',
pygments_lexer: 'ipython3',
version: '3.9.7',
},
},
nbformat: 4,
nbformat_minor: 4,
});
test('should include .ipynb files verbatim whitout --nbconvert', async () => {
const ipynbFilePath = path.join(testDir, 'notebook.ipynb');
fs.writeFileSync(ipynbFilePath, ipynbFileContents);
const args = [testDir];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).toContain(ipynbFilePath);
expect(stdoutOutput).toContain(ipynbFileContents);
});
test('should include .ipynb files verbatim when --nbconvert is set to invalid command', async () => {
const ipynbFilePath = path.join(testDir, 'notebook.ipynb');
fs.writeFileSync(ipynbFilePath, ipynbFileContents);
const args = [testDir, '--nbconvert', 'invalid_command'];
await runScript(args);
expect(stderrOutput).toContain('command not found');
expect(stdoutOutput).toContain(ipynbFilePath);
expect(stdoutOutput).toContain(ipynbFileContents);
});
test('should convert .ipynb files to ASCII when --nbconvert --format asciidoc is passed', async () => {
const ipynbFilePath = path.join(testDir, 'notebook.ipynb');
fs.writeFileSync(ipynbFilePath, ipynbFileContents);
const args = [testDir, '--nbconvert', nbconvertTool, '--format', 'asciidoc'];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).toContain(ipynbFilePath);
expect(stdoutOutput).toContain('+*In[1]:*+');
expect(stdoutOutput).toContain('print(\'Hello, World!\')');
});
test('should convert .ipynb files to Markdown when --nbconvert --format markdown is passed', async () => {
const ipynbFilePath = path.join(testDir, 'notebook.ipynb');
fs.writeFileSync(ipynbFilePath, ipynbFileContents);
const args = [testDir, '--nbconvert', nbconvertTool, '--format', 'markdown'];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).toContain(ipynbFilePath);
expect(stdoutOutput).toContain('```python');
expect(stdoutOutput).toContain('print(\'Hello, World!\')');
});
test('should convert .ipynb files to ASCII when --nbconvert --format asciidoc is passed using internal converter', async () => {
const ipynbFilePath = path.join(testDir, 'notebook.ipynb');
fs.writeFileSync(ipynbFilePath, ipynbFileContents);
const args = [testDir, '--nbconvert', 'internal', '--format', 'asciidoc'];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).toContain(ipynbFilePath);
expect(stdoutOutput).toContain('+*In[1]:*+');
expect(stdoutOutput).toContain('print(\'Hello, World!\')');
});
test('should convert .ipynb files to Markdown when --nbconvert --format markdown is passed using internal converter', async () => {
const ipynbFilePath = path.join(testDir, 'notebook.ipynb');
fs.writeFileSync(ipynbFilePath, ipynbFileContents);
const args = [testDir, '--nbconvert', 'internal', '--format', 'markdown'];
await runScript(args);
expect(stderrOutput).toBeEmpty();
expect(stdoutOutput).toContain(ipynbFilePath);
expect(stdoutOutput).toContain('```python');
expect(stdoutOutput).toContain('print(\'Hello, World!\')');
});
});