-
Notifications
You must be signed in to change notification settings - Fork 722
/
ln.js
211 lines (185 loc) · 6.65 KB
/
ln.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import fs from 'fs';
import path from 'path';
import test from 'ava';
import shell from '..';
import utils from './utils/utils';
const CWD = process.cwd();
test.beforeEach(t => {
t.context.tmp = utils.getTempDir();
shell.config.resetForTesting();
shell.cp('-r', 'test/resources', t.context.tmp);
process.chdir(CWD);
});
test.afterEach.always(t => {
shell.rm('-rf', t.context.tmp);
});
//
// Invalids
//
test('no args', t => {
const result = shell.ln();
t.truthy(shell.error());
t.is(result.code, 1);
});
test('too few args', t => {
const result = shell.ln('file');
t.truthy(shell.error());
t.is(result.code, 1);
});
test('only an option', t => {
const result = shell.ln('-f');
t.truthy(shell.error());
t.is(result.code, 1);
});
test('destination already exists', t => {
const result = shell.ln(`${t.context.tmp}/file1`, `${t.context.tmp}/file2`);
t.truthy(shell.error());
t.is(result.code, 1);
});
test('destination already exists inside directory', t => {
shell.cd(t.context.tmp);
const result = shell.ln('-s', 'file1', './');
t.truthy(shell.error());
t.is(result.code, 1);
shell.cd('..');
});
test('non-existent source', t => {
const result = shell.ln(`${t.context.tmp}/noexist`, `${t.context.tmp}/linkfile1`);
t.truthy(shell.error());
t.is(result.code, 1);
});
test('non-existent source (-sf)', t => {
const result = shell.ln('-sf', 'no/exist', `${t.context.tmp}/badlink`);
t.truthy(shell.error());
t.is(result.code, 1);
});
test('non-existent source (-f)', t => {
const result = shell.ln('-f', 'noexist', `${t.context.tmp}/badlink`);
t.truthy(shell.error());
t.is(result.code, 1);
});
//
// Valids
//
test('basic usage', t => {
const result = shell.ln(`${t.context.tmp}/file1`, `${t.context.tmp}/linkfile1`);
t.truthy(fs.existsSync(`${t.context.tmp}/linkfile1`));
t.is(
fs.readFileSync(`${t.context.tmp}/file1`).toString(),
fs.readFileSync(`${t.context.tmp}/linkfile1`).toString()
);
fs.writeFileSync(`${t.context.tmp}/file1`, 'new content 1');
t.is(fs.readFileSync(`${t.context.tmp}/linkfile1`).toString(), 'new content 1');
t.is(result.code, 0);
});
test('With glob', t => {
shell.rm(`${t.context.tmp}/linkfile1`);
const result = shell.ln(`${t.context.tmp}/fi*1`, `${t.context.tmp}/linkfile1`);
t.truthy(fs.existsSync(`${t.context.tmp}/linkfile1`));
t.is(
fs.readFileSync(`${t.context.tmp}/file1`).toString(),
fs.readFileSync(`${t.context.tmp}/linkfile1`).toString()
);
fs.writeFileSync(`${t.context.tmp}/file1`, 'new content 1');
t.is(fs.readFileSync(`${t.context.tmp}/linkfile1`).toString(), 'new content 1');
t.is(result.code, 0);
});
test('-s option', t => {
utils.skipOnWinForEPERM(shell.ln.bind(shell, '-s', 'file2', `${t.context.tmp}/linkfile2`), () => {
t.truthy(fs.existsSync(`${t.context.tmp}/linkfile2`));
t.is(
fs.readFileSync(`${t.context.tmp}/file2`).toString(),
fs.readFileSync(`${t.context.tmp}/linkfile2`).toString()
);
fs.writeFileSync(`${t.context.tmp}/file2`, 'new content 2');
t.is(fs.readFileSync(`${t.context.tmp}/linkfile2`).toString(), 'new content 2');
});
});
test('Symbolic link directory test', t => {
shell.mkdir(`${t.context.tmp}/ln`);
shell.touch(`${t.context.tmp}/ln/hello`);
const result = shell.ln('-s', 'ln', `${t.context.tmp}/dir1`);
t.truthy(fs.existsSync(`${t.context.tmp}/ln/hello`));
t.truthy(fs.existsSync(`${t.context.tmp}/dir1/hello`));
t.is(result.code, 0);
});
test('To current directory', t => {
shell.cd(t.context.tmp);
let result = shell.ln('-s', './', 'dest');
t.is(result.code, 0);
shell.touch('testfile.txt');
t.truthy(fs.existsSync('testfile.txt'));
t.truthy(fs.existsSync('dest/testfile.txt'));
shell.rm('-f', 'dest');
shell.mkdir('dir1');
shell.cd('dir1');
result = shell.ln('-s', './', '../dest');
t.is(result.code, 0);
shell.touch('insideDir.txt');
shell.cd('..');
t.truthy(fs.existsSync('testfile.txt'));
t.truthy(fs.existsSync('dest/testfile.txt'));
t.truthy(fs.existsSync('dir1/insideDir.txt'));
t.falsy(fs.existsSync('dest/insideDir.txt'));
shell.cd('..');
});
test('Inside existing directory', t => {
shell.cd(t.context.tmp);
const result = shell.ln('-s', 'external/node_script.js', './');
t.is(result.code, 0);
t.truthy(fs.existsSync('node_script.js'));
t.is(
fs.readFileSync('external/node_script.js').toString(),
fs.readFileSync('node_script.js').toString()
);
shell.rm('node_script.js');
shell.cd('..');
});
test('-f option', t => {
const result = shell.ln('-f', `${t.context.tmp}/file1.js`, `${t.context.tmp}/file2.js`);
t.is(result.code, 0);
t.truthy(fs.existsSync(`${t.context.tmp}/file2.js`));
t.is(
fs.readFileSync(`${t.context.tmp}/file1.js`).toString(),
fs.readFileSync(`${t.context.tmp}/file2.js`).toString()
);
fs.writeFileSync(`${t.context.tmp}/file1.js`, 'new content js');
t.is(fs.readFileSync(`${t.context.tmp}/file2.js`).toString(), 'new content js');
});
test('-sf option', t => {
utils.skipOnWinForEPERM(shell.ln.bind(shell, '-sf', 'file1.txt', `${t.context.tmp}/file2.txt`), () => {
t.truthy(fs.existsSync(`${t.context.tmp}/file2.txt`));
t.is(
fs.readFileSync(`${t.context.tmp}/file1.txt`).toString(),
fs.readFileSync(`${t.context.tmp}/file2.txt`).toString()
);
fs.writeFileSync(`${t.context.tmp}/file1.txt`, 'new content txt');
t.is(fs.readFileSync(`${t.context.tmp}/file2.txt`).toString(), 'new content txt');
});
});
test('Abspath regression', t => {
utils.skipOnWinForEPERM(shell.ln.bind(shell, '-sf', 'file1', path.resolve(`${t.context.tmp}/abspath`)), () => {
t.truthy(fs.existsSync(`${t.context.tmp}/abspath`));
t.is(
fs.readFileSync(`${t.context.tmp}/file1`).toString(),
fs.readFileSync(`${t.context.tmp}/abspath`).toString()
);
fs.writeFileSync(`${t.context.tmp}/file1`, 'new content 3');
t.is(fs.readFileSync(`${t.context.tmp}/abspath`).toString(), 'new content 3');
});
});
test('Relative regression', t => {
utils.skipOnWinForEPERM(shell.ln.bind(shell, '-sf', 'file1.txt', `${t.context.tmp}/file2.txt`), () => {
shell.mkdir('-p', `${t.context.tmp}/new`);
// Move the symlink first, as the reverse confuses `mv`.
shell.mv(`${t.context.tmp}/file2.txt`, `${t.context.tmp}/new/file2.txt`);
shell.mv(`${t.context.tmp}/file1.txt`, `${t.context.tmp}/new/file1.txt`);
t.truthy(fs.existsSync(`${t.context.tmp}/new/file2.txt`));
t.is(
fs.readFileSync(`${t.context.tmp}/new/file1.txt`).toString(),
fs.readFileSync(`${t.context.tmp}/new/file2.txt`).toString()
);
fs.writeFileSync(`${t.context.tmp}/new/file1.txt`, 'new content txt');
t.is(fs.readFileSync(`${t.context.tmp}/new/file2.txt`).toString(), 'new content txt');
});
});