-
Notifications
You must be signed in to change notification settings - Fork 722
/
touch.js
195 lines (171 loc) · 5.61 KB
/
touch.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
import crypto from 'crypto';
import fs from 'fs';
import test from 'ava';
import shell from '..';
import common from '../src/common';
import utils from './utils/utils';
test.beforeEach(t => {
t.context.tmp = utils.getTempDir();
shell.config.resetForTesting();
shell.mkdir(t.context.tmp);
});
test.afterEach.always(t => {
shell.rm('-rf', t.context.tmp);
});
// Helper functions
function resetUtimes(f) {
const d = new Date();
d.setYear(2000);
fs.utimesSync(f, d, d);
return common.statFollowLinks(f);
}
function tmpFile(t, noCreate) {
const str = crypto.randomBytes(Math.ceil(10 / 2)).toString('hex');
const file = `${t.context.tmp}/${str}`;
if (!noCreate) {
fs.closeSync(fs.openSync(file, 'a'));
}
return file;
}
//
// Valids
//
test('should handle args', t => {
const result = shell.touch();
t.truthy(shell.error());
t.is(result.code, 1);
});
test('arguments must be strings', t => {
const result = shell.touch(1);
t.truthy(shell.error());
t.is(result.code, 1);
});
test('exits without error when trying to touch a directory', t => {
const result = shell.touch(t.context.tmp);
t.falsy(shell.error());
t.is(result.code, 0);
});
test('creates new files', t => {
const testFile = tmpFile(t);
const result = shell.touch(testFile);
t.truthy(fs.existsSync(testFile));
t.is(result.code, 0);
});
test('does not create a file if told not to', t => {
const testFile = tmpFile(t, true);
const result = shell.touch('-c', testFile);
t.is(result.code, 0);
t.falsy(fs.existsSync(testFile));
});
test('handles globs correctly', t => {
shell.touch(`${t.context.tmp}/file.txt`);
shell.touch(`${t.context.tmp}/file.js`);
const result = shell.touch(`${t.context.tmp}/file*`);
t.is(result.code, 0);
const files = shell.ls(`${t.context.tmp}/file*`);
t.truthy(files.indexOf(`${t.context.tmp}/file.txt`) > -1);
t.truthy(files.indexOf(`${t.context.tmp}/file.js`) > -1);
t.is(files.length, 2);
});
test('errors if reference file is not found', t => {
const testFile = tmpFile(t);
const refFile = tmpFile(t, true);
const result = shell.touch({ '-r': refFile }, testFile);
t.is(result.code, 1);
t.truthy(shell.error());
});
test('uses a reference file for mtime', t => {
const testFile = tmpFile(t);
const testFile2 = tmpFile(t);
shell.touch(testFile2);
utils.sleep(1000);
let result = shell.touch(testFile);
t.falsy(shell.error());
t.is(result.code, 0);
t.not(
common.statFollowLinks(testFile).mtime.getTime(),
common.statFollowLinks(testFile2).mtime.getTime()
);
t.not(
common.statFollowLinks(testFile).atime.getTime(),
common.statFollowLinks(testFile2).atime.getTime()
);
result = shell.touch({ '-r': testFile2 }, testFile);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(
common.statFollowLinks(testFile).mtime.getTime(),
common.statFollowLinks(testFile2).mtime.getTime()
);
t.is(
common.statFollowLinks(testFile).atime.getTime(),
common.statFollowLinks(testFile2).atime.getTime()
);
});
test('accepts -d flag', t => {
const testFile = tmpFile(t);
const date = new Date('December 17, 1995 03:24:00');
const result = shell.touch({ '-d': date }, testFile);
t.is(result.code, 0);
// Compare getTime(), because Date can't be compared with triple-equals.
t.is(common.statFollowLinks(testFile).mtime.getTime(), date.getTime());
t.is(common.statFollowLinks(testFile).atime.getTime(), date.getTime());
});
test('accepts long option (--date)', t => {
const testFile = tmpFile(t);
const someDate = new Date('December 17, 1995 03:24:00');
const result = shell.touch({ date: someDate }, testFile);
t.is(result.code, 0);
// Compare getTime(), because Date can't be compared with triple-equals.
t.is(common.statFollowLinks(testFile).mtime.getTime(), someDate.getTime());
t.is(common.statFollowLinks(testFile).atime.getTime(), someDate.getTime());
});
test('sets mtime and atime by default', t => {
const testFile = tmpFile(t);
const oldStat = resetUtimes(testFile);
const result = shell.touch(testFile);
t.is(result.code, 0);
t.truthy(oldStat.mtime < common.statFollowLinks(testFile).mtime);
t.truthy(oldStat.atime < common.statFollowLinks(testFile).atime);
});
test('does not set mtime if told not to', t => {
const testFile = tmpFile(t);
const oldStat = resetUtimes(testFile);
const result = shell.touch('-a', testFile);
t.is(result.code, 0);
t.is(oldStat.mtime.getTime(), common.statFollowLinks(testFile).mtime.getTime());
});
test('does not set atime if told not to', t => {
const testFile = tmpFile(t);
const oldStat = resetUtimes(testFile);
const result = shell.touch('-m', testFile);
t.is(result.code, 0);
t.is(oldStat.atime.getTime(), common.statFollowLinks(testFile).atime.getTime());
});
test('multiple files', t => {
const testFile = tmpFile(t, true);
const testFile2 = tmpFile(t, true);
shell.rm('-f', testFile, testFile2);
const result = shell.touch(testFile, testFile2);
t.is(result.code, 0);
t.truthy(fs.existsSync(testFile));
t.truthy(fs.existsSync(testFile2));
});
test('file array', t => {
const testFile = tmpFile(t, true);
const testFile2 = tmpFile(t, true);
shell.rm('-f', testFile, testFile2);
const result = shell.touch([testFile, testFile2]);
t.is(result.code, 0);
t.truthy(fs.existsSync(testFile));
t.truthy(fs.existsSync(testFile2));
});
test('touching broken link creates a new file', t => {
utils.skipOnWin(t, () => {
shell.ln('-s', 'not_existed_file', `${t.context.tmp}/badlink2`);
const result = shell.touch(`${t.context.tmp}/badlink2`);
t.is(result.code, 0);
t.falsy(shell.error());
t.truthy(fs.existsSync(`${t.context.tmp}/not_existed_file`));
});
});