-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathtest.js
More file actions
269 lines (223 loc) · 7.75 KB
/
test.js
File metadata and controls
269 lines (223 loc) · 7.75 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
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
import {Buffer} from 'node:buffer';
import path from 'node:path';
import fs from 'node:fs/promises';
import fsSync from 'node:fs';
import {fileURLToPath} from 'node:url';
import {setTimeout} from 'node:timers/promises';
import touch from 'touch';
import test from 'ava';
import gulp from 'gulp';
import Vinyl from 'vinyl';
import {deleteSync} from 'del';
import {getStreamAsArray} from 'get-stream';
import figures from 'figures';
import chalk from 'chalk';
import changed, {compareContents} from './index.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const pointer = chalk.gray.dim(figures.pointerSmall);
const macro = async (t, options) =>
// TODO: Use the `p-event` package here instead of the promise constructor
// eslint-disable-next-line no-async-promise-executor
new Promise(async (resolve, reject) => {
let {dest} = options;
const extension = options.extension || '.js';
const stream = changed(dest, options);
const files = [];
if (typeof dest === 'function') {
dest = dest();
}
try {
await fs.mkdir(dest, {recursive: true});
await touch(path.join(dest, `foo${extension}`));
} catch (error) {
reject(error);
return;
}
stream.on('data', file => {
files.push(file);
fsSync.writeFileSync(path.join(dest, file.relative), file.contents);
});
stream.on('end', () => {
t.is(files.length, 1);
t.is(files[0].relative, 'bar.js');
deleteSync(dest);
resolve();
});
stream.write(new Vinyl({
cwd: __dirname,
base: __dirname,
path: 'foo.js',
contents: Buffer.from(''),
stat: {
mtime: fsSync.statSync(path.join(dest, 'foo' + extension)),
},
}));
stream.write(new Vinyl({
base: __dirname,
path: 'bar.js',
contents: Buffer.from(''),
stat: {
mtime: new Date(),
},
}));
stream.end();
});
macro.title = (providedTitle, options) => {
let desc = 'should only pass through changed files';
if (options && options.extension) {
desc += ' using extension ' + options.extension;
} else if (options.absolute) {
desc += ' with a absolute path';
} else {
desc += ' using file extension';
}
return [providedTitle, desc].join(` ${pointer} `);
};
test.serial(`compareLastModifiedTime ${pointer} using relative dest`, macro, {dest: 'tmp', absolute: true});
test.serial(`compareLastModifiedTime ${pointer} using relative dest`, macro, {dest: 'tmp', absolute: true, extension: '.coffee'});
test.serial(`compareLastModifiedTime ${pointer} dest can be a function`, macro, {dest: () => 'tmp'});
test(`compareContents ${pointer} should not pass any files through in identical directories`, async t => {
const stream = gulp.src('fixture/identical/src/*')
.pipe(changed('fixture/identical/trg', {hasChanged: compareContents}));
const files = await getStreamAsArray(stream);
t.is(files.length, 0);
});
test(`compareContents ${pointer} should only pass through changed files using file extension`, async t => {
const stream = gulp.src('fixture/different/src/*')
.pipe(changed('fixture/different/trg', {hasChanged: compareContents}));
const files = await getStreamAsArray(stream);
t.is(files.length, 1);
t.is(path.basename(files[0].path), 'b');
});
test(`compareContents ${pointer} should only pass through changed files using transformPath`, async t => {
const stream = gulp.src('fixture/different.transformPath/src/*')
.pipe(changed('fixture/different.transformPath/trg', {
hasChanged: compareContents,
transformPath(newPath) {
const pathParsed = path.parse(newPath);
return path.join(pathParsed.dir, 'c', pathParsed.base);
},
}));
const files = await getStreamAsArray(stream);
t.is(files.length, 1);
t.is(path.basename(files[0].path), 'b');
});
test(`compareContents ${pointer} should only pass through changed files using extension .coffee`, async t => {
const stream = gulp.src('fixture/different.ext/src/*')
.pipe(changed('fixture/different.ext/trg', {
hasChanged: compareContents,
extension: '.coffee',
}));
const files = await getStreamAsArray(stream);
t.is(files.length, 1);
t.is(path.basename(files[0].path), 'b.typescript');
});
test.serial(`compareLastModifiedTime ${pointer} detects file replacement with older file via ctime`, async t => {
const testDir = 'tmp-replacement';
const srcDir = path.join(testDir, 'src');
const destDir = path.join(testDir, 'dest');
// Clean up and create directories
deleteSync(testDir);
await fs.mkdir(srcDir, {recursive: true});
await fs.mkdir(destDir, {recursive: true});
const srcPath = path.join(srcDir, 'app.js');
const destPath = path.join(destDir, 'app.js');
// Create initial file and process it
await fs.writeFile(srcPath, 'console.log("v1");');
const stream1 = changed(destDir);
stream1.on('data', file => {
fsSync.writeFileSync(destPath, file.contents);
});
stream1.write(new Vinyl({
cwd: testDir,
base: srcDir,
path: srcPath,
contents: Buffer.from('console.log("v1");'),
stat: await fs.stat(srcPath),
}));
stream1.end();
await new Promise(resolve => {
stream1.on('end', resolve);
});
// Add a small delay to ensure dest file's mtime is properly set
await setTimeout(10);
// Replace source with older file
await fs.writeFile(srcPath, 'console.log("old");');
const oldTime = Date.now() - 10_000;
await fs.utimes(srcPath, oldTime / 1000, oldTime / 1000);
// Verify ctime is newer than dest mtime
const srcStat = await fs.stat(srcPath);
const destStat = await fs.stat(destPath);
t.true(srcStat.ctimeMs > destStat.mtimeMs, 'Source ctime should be newer than dest mtime');
// Process replaced file - should be detected
const stream2 = changed(destDir);
const filesProcessed = [];
stream2.on('data', file => filesProcessed.push(file));
stream2.write(new Vinyl({
cwd: testDir,
base: srcDir,
path: srcPath,
contents: await fs.readFile(srcPath),
stat: await fs.stat(srcPath),
}));
stream2.end();
await new Promise(resolve => {
stream2.on('end', resolve);
});
t.is(filesProcessed.length, 1, 'Replaced file with older mtime should be detected via ctime');
// Clean up
deleteSync(testDir);
});
test.serial(`compareLastModifiedTime ${pointer} should not pass through files when mtime is old but ctime is current (issue #94)`, async t => {
const testDir = 'tmp-issue-94';
const srcDir = path.join(testDir, 'src');
const destDir = path.join(testDir, 'dest');
deleteSync(testDir);
await fs.mkdir(srcDir, {recursive: true});
await fs.mkdir(destDir, {recursive: true});
const srcPath = path.join(srcDir, 'app.js');
const destPath = path.join(destDir, 'app.js');
// Create file with old mtime (simulates git checkout/archive extraction)
await fs.writeFile(srcPath, 'console.log("test");');
const oldTime = Date.now() - 10_000;
await fs.utimes(srcPath, oldTime / 1000, oldTime / 1000);
// First pass - should pass through (destination doesn't exist)
const stream1 = changed(destDir);
let passedThrough = false;
stream1.on('data', file => {
passedThrough = true;
fsSync.writeFileSync(destPath, file.contents);
fsSync.utimesSync(destPath, file.stat.atime, file.stat.mtime);
});
stream1.write(new Vinyl({
cwd: testDir,
base: srcDir,
path: srcPath,
contents: await fs.readFile(srcPath),
stat: await fs.stat(srcPath),
}));
stream1.end();
await new Promise(resolve => {
stream1.on('end', resolve);
});
t.true(passedThrough, 'First run should pass through');
// Second pass - should NOT pass through (file unchanged, despite ctime > mtime)
const stream2 = changed(destDir);
passedThrough = false;
stream2.on('data', () => {
passedThrough = true;
});
stream2.write(new Vinyl({
cwd: testDir,
base: srcDir,
path: srcPath,
contents: await fs.readFile(srcPath),
stat: await fs.stat(srcPath),
}));
stream2.end();
await new Promise(resolve => {
stream2.on('end', resolve);
});
t.false(passedThrough, 'Second run should not pass through unchanged file');
deleteSync(testDir);
});