forked from sindresorhus/gulp-changed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
132 lines (111 loc) · 3.79 KB
/
test.js
File metadata and controls
132 lines (111 loc) · 3.79 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
import path from 'path';
import fs from 'fs';
import touch from 'touch';
import makeDir from 'make-dir';
import test from 'ava';
import gulp from 'gulp';
import Vinyl from 'vinyl';
import del from 'del';
import getStream from 'get-stream';
import figures from 'figures';
import chalk from 'chalk';
import changed from '.';
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
return 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 makeDir(dest);
await touch(path.join(dest, `foo${extension}`));
} catch (error) {
reject(error);
return;
}
stream.on('data', file => {
files.push(file);
fs.writeFileSync(path.join(dest, file.relative), file.contents);
});
stream.on('end', () => {
t.is(files.length, 1);
t.is(files[0].relative, 'bar.js');
del.sync(dest);
resolve();
});
stream.write(new Vinyl({
cwd: __dirname,
base: __dirname,
path: 'foo.js',
contents: Buffer.from(''),
stat: {
mtime: fs.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: changed.compareContents}));
const files = await getStream.array(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: changed.compareContents}));
const files = await getStream.array(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: changed.compareContents,
transformPath: newPath => {
const pathParsed = path.parse(newPath);
return path.join(pathParsed.dir, 'c', pathParsed.base);
}
}));
const files = await getStream.array(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: changed.compareContents,
extension: '.coffee'
}));
const files = await getStream.array(stream);
t.is(files.length, 1);
t.is(path.basename(files[0].path), 'b.typescript');
});