This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmve.test.js
147 lines (122 loc) · 3.11 KB
/
mve.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
const {resolve} = require('path');
const {
execFile,
execSync: $
} = require('child_process');
const {readFileSync, readdirSync} = require('fs');
const tape = require('tape-catch');
const {test, plus, curry} = require('1-liners');
const title = curry(plus)('The CLI program: ');
const mve = resolve(__dirname, '../../module/bin/mve.js');
const $mve = curry(execFile)(mve);
const cwd = resolve(__dirname, '../mock-cwd');
const cat = (filename) => {
try {
return readFileSync(resolve(cwd, filename), {encoding: 'utf8'});
} catch (error) {
if (error.code === 'ENOENT') return null;
else throw error;
}
};
const ls = (directory = cwd) => readdirSync(directory);
tape(title('Prints usage'), (is) => {
is.plan(10);
$mve([], (error, _, stderr) => {
is.equal(error && error.code, 1,
'`mve` fails…'
);
is.ok(
test(stderr, /^usage:/i),
'…and prints usage to stderr'
);
});
$mve(['--invalid', '--options'], (error, _, stderr) => {
is.equal(error && error.code, 1,
'`mve --invalid --options` fails…'
);
is.ok(
test(stderr, /^usage:/i),
'…and prints usage to stderr'
);
});
$mve(['a'], {cwd}, (error, _, stderr) => {
is.equal(error && error.code, 1,
'`mve <just one file>` fails…'
);
is.ok(
test(stderr, /^usage:/i),
'…and prints usage to stderr'
);
});
$mve(['-h'], (error, stdout) => {
is.equal(error, null,
'`mve -h` succeeds…'
);
is.ok(
test(stdout, /^usage:/i),
'…and prints usage'
);
});
$mve(['--help'], (error, stdout) => {
is.equal(error, null,
'`mve --help` succeeds…'
);
is.ok(
test(stdout, /SYNOPSIS/),
'…and prints manpage-like help'
);
});
});
tape(title('Works for a single file'), (is) => {
$mve(['a', 'a.moved'], {cwd}, (error) => {
is.equal(error, null,
'`mve <source file> <new destination>` succeeds, …'
);
is.deepEqual(ls(),
['a.moved', 'b', 'c'],
'…moves the source file to the new destination…'
);
is.equal(cat('a.moved'),
'AAA\n',
'…and keeps its contents'
);
// Clean up.
$mve(['a.moved', 'a'], {cwd}, () => {
is.end();
});
});
});
tape(title('Fails for a non-existent file'), (is) => {
$mve(['non-existent', '_'], {cwd}, (error, _, stderr) => {
is.equal(error && error.code, 1,
'`mve <non-existent file> <…>` fails…'
);
is.ok(/file not found/i.test(stderr),
'…with a helpful message.'
);
is.end();
});
});
tape(title('Works for a single directory'), (is) => {
$mve(['c', 'c.moved'], {cwd}, (error) => {
is.equal(error, null,
'`mve <directory> <new name>` succeeds, …'
);
is.deepEqual(ls(),
['a', 'b', 'c.moved'],
'…renames a directory to a new name, …'
);
is.deepEqual(ls(resolve(cwd, 'c.moved')),
['c'],
'…keeps its contents…'
);
is.equal(cat('c.moved/c'),
'CCC\n',
'…and the contents of its files'
);
// Clean up.
$mve(['c.moved', 'c'], {cwd}, () => {
is.end();
});
});
});