Repository navigation
Expand file tree
/
Copy pathindex.test.js
More file actions
255 lines (217 loc) · 7.64 KB
/
Copy pathindex.test.js
File metadata and controls
255 lines (217 loc) · 7.64 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const fire = require("./");
const util = require("util");
const fs = require("fs");
const execCb = require("child_process").exec;
const exec = util.promisify(execCb);
describe("js-fire", () => {
describe("function", () => {
test("functions works with default as", async () => {
const { stdout } = await exec("node examples/function.js");
expect(stdout.trim()).toEqual("40");
});
test("helpText", async () => {
const { stdout } = await exec("node examples/function.js --help");
expect(stdout).toMatchSnapshot();
});
test("flag typo", async () => {
try {
await exec("node examples/function.js --numer=2");
} catch (err) {
expect(err.stdout.trim()).toMatchSnapshot();
}
});
});
describe("arrow function", () => {
test("functions works unnamed arg", async () => {
const { stdout } = await exec("node examples/arrow-function.js 20");
expect(stdout.trim()).toEqual("40");
});
test("helpText", async () => {
const { stdout } = await exec("node examples/arrow-function.js --help");
expect(stdout).toMatchSnapshot();
});
});
describe("circular reference", () => {
test("should compile", async () => {
const { stdout } = await exec(
"node examples/circular-reference.js half --number 20"
);
expect(stdout.trim()).toEqual("10");
});
});
describe("lib", () => {
// tests whether you can call a arbitratry library via the js-fire api.
test("write a file with fs", async () => {
const path = __dirname + "/test.txt";
await exec(
`node bin/index.js fs -- writeFileSync --path ${path} --data "hiii"`
);
const constents = fs.readFileSync(path);
expect(constents.toString()).toEqual("hiii");
// cleanup
fs.unlinkSync(path);
});
test("helpText", async () => {
const { stdout } = await exec("node bin/index.js fs -h");
expect(stdout.trim()).toMatchSnapshot();
});
});
describe("cli", () => {
const opts = { cwd: "examples/cli" };
beforeAll(async () => {
await exec(`yarn link`, opts);
});
test("helpText", async () => {
const { stdout } = await exec("js-fire-example -h");
// Command usage should ref js-fire
expect(stdout.trim()).toMatchSnapshot();
});
});
describe("interactive", () => {
const keys = {
up: "\x1B\x5B\x41",
down: "\x1B\x5B\x42",
enter: "\x0D",
space: "\x20",
};
test("object with subcommand", (done) => {
const ps = execCb("node examples/object.js half -i", (err, stdout) => {
expect(stdout.trim()).toContain("10");
done();
});
ps.stdin.write(`20${keys.enter}`);
});
test("object from root", (done) => {
const ps = execCb("node examples/object.js misc -i", (err, stdout) => {
console.log(stdout);
expect(stdout.trim()).toContain("greetings hobo!");
done();
});
setTimeout(() => ps.stdin.write(`info\r\n`), 100);
setTimeout(() => ps.stdin.write(`greeting\r\n`), 200);
setTimeout(() => ps.stdin.write(`hobo\r\n`), 400);
});
test("object from root", (done) => {
const ps = execCb("node examples/object.js -i", (err, stdout) => {
console.log(stdout);
expect(stdout.trim()).toContain("greetings hobo!");
done();
});
setTimeout(() => ps.stdin.write(`misc\r\n`), 100);
setTimeout(() => ps.stdin.write(`info\r\n`), 200);
setTimeout(() => ps.stdin.write(`greeting\r\n`), 400);
setTimeout(() => ps.stdin.write(`hobo\r\n`), 600);
});
});
describe("bin", () => {
beforeAll(async () => {
await exec(`yarn link`);
});
test("helpText", async () => {
const { stdout } = await exec("js-fire -h");
// Command usage should ref js-fire
expect(stdout.trim()).toMatchSnapshot();
});
test("helpText with subcommand without help flag", async () => {
const { stdout } = await exec("js-fire fs -h");
// Command usage should ref js-fire
expect(stdout.trim()).toMatchSnapshot();
});
test("helpText with subcommand with help flag", async () => {
const { stdout } = await exec("js-fire fs -h");
expect(stdout.trim()).toMatchSnapshot();
});
test("helpText with incorrect subcommand name", async () => {
// this should return a suggested command in the output
try {
const { stdout } = await exec("js-fire fs appendF");
// this should return an exit code 1
} catch (err) {
expect(err.stdout.trim()).toMatchSnapshot();
}
});
test("bin with named args", async () => {
const { stdout } = await exec(
`js-fire fs -- existsSync --path ${__filename}`
);
expect(stdout.trim()).toEqual("true");
});
test("bin with positional args", async () => {
const { stdout } = await exec(`js-fire fs existsSync ${__filename}`);
expect(stdout.trim()).toEqual("true");
});
});
describe("object", () => {
test("half", async () => {
const { stdout } = await exec("node examples/object.js half --number 20");
expect(stdout.trim()).toEqual("10");
});
test("double", async () => {
const { stdout } = await exec(
"node examples/object.js double --number 20"
);
expect(stdout.trim()).toEqual("40");
});
test("works without named args", async () => {
const { stdout } = await exec("node examples/object.js double 20");
expect(stdout.trim()).toEqual("40");
});
test("works with deeply nested objects", async () => {
const { stdout } = await exec(
"node examples/object.js misc hello --name hobochild"
);
expect(stdout.trim()).toEqual("hi hobochild");
});
test("helpText", async () => {
const { stdout } = await exec("node examples/object.js --help");
expect(stdout).toMatchSnapshot();
});
test("helpText with subcommand", async () => {
const { stdout } = await exec("node examples/object.js double --help");
expect(stdout).toMatchSnapshot();
});
test("command not found", async () => {
// test case for:
// https://github.com/hobochild/js-fire/issues/22
const { stdout } = await exec(
'node examples/object.js misc; echo "exit code $?"'
);
expect(stdout).toMatchSnapshot();
});
test("deeply nested", async () => {
const { stdout } = await exec(
"node examples/object.js misc info greeting --name hobochild"
);
expect(stdout.trim()).toEqual("greetings hobochild!");
});
test("deeply nested with typo", async () => {
const { stdout } = await exec(
"node examples/object.js misc info greeting --name hobochild"
);
expect(stdout).toMatchSnapshot();
});
});
test("async functions", async () => {
const { stdout } = await exec("node examples/async.js --number 20");
expect(stdout.trim()).toEqual("40");
});
describe("multi-args", () => {
test("multi unname args", async () => {
const { stdout } = await exec("node examples/multi-args.js 30 20");
expect(stdout.trim()).toEqual("10");
});
});
test("multi named args", async () => {
const { stdout } = await exec("node examples/multi-args.js --b 30 --a 20");
expect(stdout.trim()).toEqual("-10");
});
test("works with default (eval) values", async () => {
const { stdout } = await exec("node examples/multi-args.js --b 1");
const answer = Math.PI - 1;
expect(stdout.trim()).toEqual(answer.toString());
});
test("helpText", async () => {
const { stdout } = await exec("node examples/multi-args.js --help");
expect(stdout.trim()).toMatchSnapshot();
});
});