forked from ronomon/utimes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
197 lines (179 loc) · 5.19 KB
/
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
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
var Node = {
fs: require('fs'),
path: require('path'),
process: process
};
var random = Math.random.bind(Math);
var Queue = require('@ronomon/queue');
var namespace = 'Utimes';
var Test = {};
Test.equal = function(value, expected, namespace, description) {
value = JSON.stringify(value) + '';
expected = JSON.stringify(expected) + '';
if (value === expected) {
Test.pass(namespace, description, expected);
} else {
Test.fail(namespace, description, value + ' !== ' + expected);
}
};
Test.fail = function(namespace, description, message) {
console.log('');
throw 'FAIL: ' + Test.message(namespace, description, message);
};
Test.message = function(namespace, description, message) {
if ((namespace = namespace || '')) namespace += ': ';
if ((description = description || '')) description += ': ';
return namespace + description + (message || '');
};
Test.pass = function(namespace, description, message) {
console.log('PASS: ' + Test.message(namespace, description, message));
};
var root = Node.path.resolve(module.filename, '..');
if (!root) throw new Error('root must not be empty');
var fixtures = Node.path.join(root, 'utimes_fixtures');
var binding = require(Node.path.join(root, 'index.js'));
var ALPHABET = '';
ALPHABET += 'abcdefghijklmnopqrstuvwxyz';
ALPHABET += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
ALPHABET += '0123456789';
ALPHABET += 'àáâäæãåāèéêëēėę';
// Do not use extensions.
// Some extensions (such as .eml) cause timestamp updates on Windows.
// (MAX_TIME + 1) overflows NTFS and returns a negative timestamp:
var MAX_TIME = 2147483647999;
function generatePath() {
var chars = 1 + Math.floor(random() * 16);
var string = '';
while (chars--) {
string += ALPHABET[Math.floor(random() * ALPHABET.length)];
}
if (random() < 0.5) {
if (random() < 0.5) {
string = string.normalize('NFC');
} else {
string = string.normalize('NFD');
}
}
return string;
}
function generateTime() {
if (random() < 0.1) return undefined;
var time = Math.floor(random() * MAX_TIME);
if (Node.process.platform !== 'win32') {
time = Math.floor(time / 1000) * 1000;
}
return time;
}
function removeFixtures(callback) {
function remove() {
try {
var names = Node.fs.readdirSync(fixtures);
} catch (error) {
if (error.code !== 'ENOENT') throw error;
var names = [];
}
names.forEach(
function(name) {
Node.fs.unlinkSync(Node.path.join(fixtures, name));
}
);
try {
Node.fs.rmdirSync(fixtures);
} catch (error) {
if (error.code !== 'ENOENT') throw error;
}
}
if (callback) {
setTimeout(
function() {
remove();
callback();
},
// Wait for handles to be released by antivirus:
Node.process.platform === 'win32' ? 2000 : 0
);
} else {
remove();
}
}
function times(stats) {
return {
btime: stats.birthtime.getTime(),
mtime: stats.mtime.getTime(),
atime: stats.atime.getTime()
};
}
removeFixtures();
try {
Node.fs.mkdirSync(fixtures);
} catch (error) {
if (error.code !== 'EEXIST') throw error;
}
var queue = new Queue(1);
queue.onData = function(test, end) {
var path = generatePath();
var target = Node.path.join(fixtures, path);
var btime = generateTime();
var mtime = generateTime();
var atime = generateTime();
Node.fs.writeFileSync(target, '');
var expect = times(Node.fs.statSync(target));
binding.utimes(target, btime, mtime, atime,
function(error) {
try {
if (error) throw error;
if (btime !== undefined) expect.btime = btime;
if (mtime !== undefined) expect.mtime = mtime;
if (atime !== undefined) expect.atime = atime;
var actual = times(Node.fs.statSync(target));
if (Node.process.platform !== 'win32') {
if (btime === undefined && actual.btime < expect.btime) {
if (
actual.btime === expect.mtime ||
actual.btime === expect.atime
) {
expect.btime = actual.btime;
}
}
if (actual.atime !== expect.atime) {
if (
actual.atime === expect.btime ||
actual.atime === expect.mtime
) {
expect.atime = actual.atime;
}
}
}
if (
Node.process.platform !== 'win32' &&
Node.process.platform !== 'darwin'
) {
expect.btime = actual.btime;
}
Test.equal(path, path, namespace, 'path');
Test.equal(actual.btime, expect.btime, namespace, 'btime=' + btime);
Test.equal(actual.mtime, expect.mtime, namespace, 'mtime=' + mtime);
Test.equal(actual.atime, expect.atime, namespace, 'atime=' + atime);
} catch (error) {
console.log('Actual: ' + JSON.stringify(actual));
console.log('Expect: ' + JSON.stringify(expect));
return end(error);
}
end();
}
);
};
queue.onEnd = function(error) {
removeFixtures(
function() {
if (error) throw error;
console.log('================');
console.log('PASSED ALL TESTS');
console.log('================');
}
);
};
for (var test = 0; test < 1000; test++) {
queue.push(test);
}
queue.end();