/
echo.js
151 lines (135 loc) · 3.69 KB
/
echo.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
import test from 'ava';
import shell from '..';
import utils from './utils/utils';
import mocks from './utils/mocks';
shell.config.silent = true;
test.beforeEach(t => {
t.context.tmp = utils.getTempDir();
mocks.init();
});
test.afterEach.always(t => {
shell.rm('-rf', t.context.tmp);
mocks.restore();
});
//
// Valids
//
test('simple test with defaults', t => {
const result = shell.echo('hello', 'world');
const stdout = mocks.stdout();
const stderr = mocks.stderr();
t.falsy(shell.error());
t.is(result.code, 0);
t.is(stdout, 'hello world\n');
t.is(stderr, '');
});
test('allow arguments to begin with a hyphen', t => {
// Github issue #20
const result = shell.echo('-asdf', '111');
const stdout = mocks.stdout();
const stderr = mocks.stderr();
t.falsy(shell.error());
t.is(result.code, 1);
t.is(stdout, '-asdf 111\n');
t.is(stderr, '');
});
test("using null as an explicit argument doesn't crash the function", t => {
const result = shell.echo(null);
const stdout = mocks.stdout();
const stderr = mocks.stderr();
t.falsy(shell.error());
t.is(result.code, 0);
t.is(stdout, 'null\n');
t.is(stderr, '');
});
test('-e option', t => {
const result = shell.echo('-e', '\tmessage');
const stdout = mocks.stdout();
const stderr = mocks.stderr();
t.falsy(shell.error());
t.is(result.code, 0);
t.is(stdout, '\tmessage\n');
t.is(stderr, '');
});
test('piping to a file', t => {
// Github issue #476
shell.mkdir(t.context.tmp);
const tmp = `${t.context.tmp}/echo.txt`;
const resultA = shell.echo('A').toEnd(tmp);
t.falsy(shell.error());
t.is(resultA.code, 0);
const resultB = shell.echo('B').toEnd(tmp);
t.falsy(shell.error());
t.is(resultB.code, 0);
const result = shell.cat(tmp);
const stdout = mocks.stdout();
const stderr = mocks.stderr();
t.falsy(shell.error());
t.is(stdout, 'A\nB\n');
t.is(stderr, '');
t.is(result.toString(), 'A\nB\n');
});
test('-n option', t => {
const result = shell.echo('-n', 'message');
const stdout = mocks.stdout();
const stderr = mocks.stderr();
t.falsy(shell.error());
t.is(result.code, 0);
t.is(stdout, 'message');
t.is(stderr, '');
});
test('-ne option', t => {
const result = shell.echo('-ne', 'message');
const stdout = mocks.stdout();
const stderr = mocks.stderr();
t.falsy(shell.error());
t.is(result.code, 0);
t.is(stdout, 'message');
t.is(stderr, '');
});
test('-en option', t => {
const result = shell.echo('-en', 'message');
const stdout = mocks.stdout();
const stderr = mocks.stderr();
t.falsy(shell.error());
t.is(result.code, 0);
t.is(stdout, 'message');
t.is(stderr, '');
});
test('-en option with escaped characters', t => {
const result = shell.echo('-en', '\tmessage\n');
const stdout = mocks.stdout();
const stderr = mocks.stderr();
t.falsy(shell.error());
t.is(result.code, 0);
t.is(stdout, '\tmessage\n');
t.is(stderr, '');
});
test('piping to a file with -n', t => {
// Github issue #476
shell.mkdir(t.context.tmp);
const tmp = `${t.context.tmp}/echo.txt`;
const resultA = shell.echo('-n', 'A').toEnd(tmp);
t.falsy(shell.error());
t.is(resultA.code, 0);
const resultB = shell.echo('-n', 'B').toEnd(tmp);
t.falsy(shell.error());
t.is(resultB.code, 0);
const result = shell.cat(tmp);
const stdout = mocks.stdout();
const stderr = mocks.stderr();
t.falsy(shell.error());
t.is(stdout, 'AB');
t.is(stderr, '');
t.is(result.toString(), 'AB');
});
test('stderr with unrecognized options is empty', t => {
const result = shell.echo('-asdf');
const stdout = mocks.stdout();
const stderr = mocks.stderr();
t.falsy(shell.error());
t.is(result.code, 1);
t.falsy(result.stderr);
t.is(stdout, '-asdf\n');
t.is(stderr, '');
});