-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.test.js
217 lines (211 loc) · 5.73 KB
/
cli.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
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
const cp = require("child_process");
const util = require("util");
const fs = require("fs");
const path = require("path");
describe("read", () => {
it("should run read", async () => {
const output = await invokeCli(JSON.stringify(["hello"]), "read", ["[0]"]);
expect(output).toBe(JSON.stringify("hello") + "\n");
});
it("should write to file", async () => {
let resolve;
const writeFileSpy = jest
.spyOn(fs, "writeFile")
.mockImplementation((filename, data, callback) => {
callback(null);
if (resolve) {
resolve();
}
});
const writable = new (require("stream").Duplex)();
writable._write = function (chunk, encoding, done) {
done();
};
writable._read = function (chunk, encoding, done) {
this.push(JSON.stringify(["hello"]));
this.push(null);
this.end();
};
const defineProcess = (ob) => {
Object.entries(ob).forEach(([key, value]) => {
Object.defineProperty(process, key, {
value: value,
configurable: true,
writable: false,
});
});
};
defineProcess({
stdin: writable,
argv: ["node", "./cli.js", "read", "[0]", "-r", "-n", "-f", "output.txt"],
});
const origs = { stdin: process.stdin, argv: process.argv };
await new Promise((_resolve) => {
resolve = _resolve;
require("./cli");
});
expect(writeFileSpy).toHaveBeenCalledWith(
"output.txt",
"hello",
expect.any(Function)
);
defineProcess(origs);
});
describe("default positional argument", () => {
it("should run read from the root", async () => {
const output = await invokeCli(JSON.stringify(["hello"]), "read", ["[]"]);
expect(output).toBe(JSON.stringify(["hello"]) + "\n");
});
it("should run read from the root", async () => {
const output = await invokeCli(JSON.stringify(["hello"]), "read");
expect(output).toBe(JSON.stringify(["hello"]) + "\n");
});
});
describe("raw output", () => {
it("should output string without quotes", async () => {
const output = await invokeCli(JSON.stringify(["hello"]), "read", [
"[0]",
"-r",
]);
expect(output).toBe("hello" + "\n");
});
it("should output string without quotes", async () => {
const output = await invokeCli(JSON.stringify(["hello"]), "read", [
"[0]",
"--raw",
]);
expect(output).toBe("hello" + "\n");
});
});
it("should print without newline", async () => {
const output = await invokeCli(JSON.stringify(["hello"]), "read", [
"[0]",
"-r",
"-n",
]);
expect(output).toBe("hello");
});
it("should print with formatting", async () => {
const output = await invokeCli(JSON.stringify(["hello"]), "read", [
"-n",
"-m",
]);
expect(output).toBe(JSON.stringify(["hello"], null, 2));
});
});
describe("modify", () => {
it("should modify", async () => {
const output = await invokeCli(JSON.stringify(["hello"]), "modify", [
"-p",
"[0]",
"-v",
"123",
"-n",
]);
expect(output).toBe(JSON.stringify([123]));
});
it("should persist rest of the object", async () => {
const output = await invokeCli(
JSON.stringify({ a: ["hello"], b: ["please change me"] }),
"modify",
["-p", `'["b", 0]'`, "-v", "123", "-n"]
);
expect(output).toBe(JSON.stringify({ a: ["hello"], b: [123] }));
});
describe("respect trailing new line", () => {
it("should preserve new-line", async () => {
const output = await invokeCli(
JSON.stringify(["hello"], null, 2),
"modify",
["-p", "[0]", "-v", "123", "-n"]
);
expect(output).toBe(JSON.stringify([123], null, 2));
});
it("should preserve an extra new-line", async () => {
const output = await invokeCli(
JSON.stringify(["hello"], null, 2) + "\n\n",
"modify",
["-p", "[0]", "-v", "123", "-n"]
);
expect(output).toBe(JSON.stringify([123], null, 2) + "\n\n");
});
});
it("should insert into an array", async () => {
const output = await invokeCli(JSON.stringify(["hello"]), "modify", [
"-p",
"[0]",
"-v",
"123",
"-n",
"-i",
]);
expect(output).toBe(JSON.stringify([123, "hello"]));
});
it("should delete item in array", async () => {
const output = await invokeCli(JSON.stringify(["hello"]), "modify", [
"-p",
"[0]",
"-n",
"-d",
]);
expect(output).toBe(JSON.stringify([]));
});
it("should delete item in ob", async () => {
const output = await invokeCli(JSON.stringify({ a: 1 }), "modify", [
"-p",
`'["a"]'`,
"-n",
"-d",
]);
expect(output).toBe(JSON.stringify({}));
});
});
const formatFixture = async (fixtureName) => {
return await invokeCli(
(
await fs.promises.readFile(
path.join(__dirname, "__fixtures__", fixtureName + ".jsonc")
)
).toString(),
"format",
[]
);
};
describe("format", () => {
it("should format unformated1", async () => {
const output = await formatFixture("unformated1");
expect(output).toMatchInlineSnapshot(`
"//comment
{
"key": "123"
}
"
`);
});
it("should format unformated2", async () => {
const output = await formatFixture("unformated2");
expect(output).toMatchInlineSnapshot(`
"{
"animal": "cat"
}
"
`);
});
});
async function invokeCli(stdin, command, args = []) {
return new Promise((resolve, reject) => {
const p = cp.exec(
`node ./cli.js ${command} ${args.join(" ")}`,
{},
(err, stdout) => {
if (err) {
reject(err);
} else {
resolve(stdout);
}
}
);
p.stdin.write(stdin);
p.stdin.end();
});
}