-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
142 lines (109 loc) · 4.05 KB
/
index.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
const fs = require('fs').promises;
const path = require('path');
jest.mock('fs', () => ({
promises: {
readFile: jest.fn(),
writeFile: jest.fn(),
readdir: jest.fn(),
},
}));
const { getIgnoreInstance, convertCRLFtoLF, processFile } = require('./index.cjs');
describe('CRLF to LF Converter', () => {
beforeEach(() => {
// Clear all mocks before each test
jest.clearAllMocks();
});
describe('getIgnoreInstance', () => {
it('should create ignore instance with gitignore content', async () => {
fs.readFile.mockResolvedValue('node_modules\n.git\n*.log');
const ig = await getIgnoreInstance('/test/path');
expect(fs.readFile).toHaveBeenCalledWith(
path.join('/test/path', '.gitignore'),
'utf8'
);
expect(ig.ignores('node_modules')).toBe(true);
expect(ig.ignores('test.log')).toBe(true);
expect(ig.ignores('src/index.js')).toBe(false);
});
it('should handle missing gitignore file', async () => {
const error = new Error('ENOENT');
error.code = 'ENOENT';
fs.readFile.mockRejectedValue(error);
const consoleSpy = jest.spyOn(console, 'warn');
const ig = await getIgnoreInstance('/test/path');
expect(consoleSpy).toHaveBeenCalledWith(
'.gitignore file not found. processing all files. /test/path/.gitignore'
);
expect(ig.ignores('node_modules')).toBe(false);
});
});
describe('processFile', () => {
it('should convert CRLF to LF in text files', async () => {
const testContent = 'line1\r\nline2\r\nline3';
const expectedContent = 'line1\nline2\nline3';
fs.readFile.mockResolvedValue(testContent);
await processFile('test.js');
expect(fs.writeFile).toHaveBeenCalledWith(
'test.js',
expectedContent,
'utf8'
);
});
it('should skip files with unsupported extensions', async () => {
const consoleSpy = jest.spyOn(console, 'log');
await processFile('test.exe');
expect(fs.readFile).not.toHaveBeenCalled();
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('skipped (extension)')
);
});
it('should handle files that dont need conversion', async () => {
const content = 'line1\nline2\nline3';
fs.readFile.mockResolvedValue(content);
const consoleSpy = jest.spyOn(console, 'log');
await processFile('test.js');
expect(fs.writeFile).not.toHaveBeenCalled();
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('no need to convert')
);
});
});
describe('convertCRLFtoLF', () => {
const createDirent = (name, isDir) => ({
name,
isDirectory: () => isDir,
isFile: () => !isDir,
});
it('should process all files in directory recursively', async () => {
const mockEntries = [
createDirent('test.js', false),
createDirent('test.md', false),
createDirent('subfolder', true),
];
fs.readdir.mockResolvedValueOnce(mockEntries);
fs.readdir.mockResolvedValueOnce([
createDirent('test.js', false)
]);
fs.readFile.mockResolvedValue('test\r\ndata');
const ig = { ignores: jest.fn().mockReturnValue(false) };
await convertCRLFtoLF('/test/path', ig);
expect(fs.readdir).toHaveBeenCalledTimes(2);
expect(fs.readFile).toHaveBeenCalledTimes(3);
});
it('should skip ignored files and directories', async () => {
const mockEntries = [
createDirent('.git', true),
createDirent('node_modules', true),
createDirent('test.log', false),
];
fs.readdir.mockResolvedValue(mockEntries);
const ig = { ignores: jest.fn().mockReturnValue(true) };
const consoleSpy = jest.spyOn(console, 'log');
await convertCRLFtoLF('/test/path', ig);
expect(fs.readFile).not.toHaveBeenCalled();
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('excluded')
);
});
});
});