-
-
Notifications
You must be signed in to change notification settings - Fork 511
/
shell.js
115 lines (103 loc) 路 3.28 KB
/
shell.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
const test = require('tape');
const shell = require('shelljs');
const mockStdIo = require('mock-stdio');
const path = require('path');
const { EOL } = require('os');
const { readFile, readJSON } = require('./util/index');
const { config } = require('../lib/config');
const { run, runTemplateCommand, pushd, popd, copy, isSubDir, bump } = require('../lib/shell');
const dir = 'test/resources';
const cwd = process.cwd();
test('run (shell.exec)', async t => {
t.equal(await run('echo bar'), 'bar');
t.end();
});
test('run (shelljs command)', async t => {
mockStdIo.start();
await run('!echo foo');
const { stdout } = mockStdIo.end();
t.equal(stdout, 'foo\n');
t.end();
});
test('run (dry run)', async t => {
const { 'dry-run': dryRun } = config.options;
mockStdIo.start();
config.options['dry-run'] = true;
const pwd = await run('!pwd');
const { stdout } = mockStdIo.end();
t.ok(/not executed in dry run/.test(stdout));
t.equal(pwd, undefined);
config.options['dry-run'] = dryRun;
t.end();
});
test('run (verbose)', async t => {
const { verbose } = config.options;
mockStdIo.start();
config.options.verbose = true;
const actual = await run('echo foo');
const { stdout } = mockStdIo.end();
t.equal(stdout, `$ echo foo\nfoo${EOL}`);
t.equal(actual, 'foo');
config.options.verbose = verbose;
t.end();
});
test.skip('run (read-only command)', async t => {
t.equal(await run('!pwd', { isReadOnly: true }), cwd);
t.end();
});
test('runTemplateCommand', async t => {
const run = cmd => runTemplateCommand(cmd, { verbose: false });
t.notOk(await run(''));
t.equal(await run('!pwd'), cwd);
t.equal(await run('echo ${git.pushRepo}'), 'origin');
t.equal(await run('echo -*- ${github.tokenRef} -*-'), '-*- GITHUB_TOKEN -*-');
t.end();
});
test('pushd + popd', async t => {
const outputPush = await pushd(dir);
const [to, from] = outputPush.split(',');
const diff = to
.replace(from, '')
.replace(/^[/|\\\\]/, '')
.replace(/\\/g, '/');
t.equal(diff, dir);
const popOutput = await popd();
const trail = popOutput.split(',');
t.equal(trail.length, 1);
t.end();
});
test('copy', async t => {
shell.pushd('-q', dir);
shell.mkdir('tmp');
await copy(['file*'], {}, 'tmp');
t.equal(await readFile('file1'), await readFile('tmp/file1'));
t.equal(await readFile('file2'), await readFile('tmp/file2'));
shell.rm('-rf', 'tmp');
shell.popd('-q');
t.end();
});
test('bump', async t => {
const target = path.resolve(dir);
const manifestA = path.join(target, 'package.json');
const manifestB = path.join(target, 'lockfile.json');
await copy('package.json', {}, target);
await copy('package.json', { rename: () => 'lockfile.json' }, target);
await bump(manifestA, '1.0.0');
const pkg = await readJSON(manifestA);
t.equal(pkg.version, '1.0.0');
await bump([manifestA, manifestB], '2.0.0');
const pkgA = await readJSON(manifestA);
const pkgB = await readJSON(manifestB);
t.equal(pkgA.version, '2.0.0');
t.equal(pkgB.version, '2.0.0');
await run(`!rm ${manifestA} ${manifestB}`);
t.end();
});
test('bump (file not found)', async t => {
await t.shouldReject(bump('foo.json'), /no such file/i);
t.end();
});
test('bump (invalid)', async t => {
await t.shouldReject(bump('test/resources/file1'), /unexpected token/i);
t.end();
});