-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathintegration.cli-params.test.js
More file actions
288 lines (257 loc) · 8.6 KB
/
Copy pathintegration.cli-params.test.js
File metadata and controls
288 lines (257 loc) · 8.6 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { spawnSync } from 'node:child_process';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const thisFilePath = fileURLToPath(import.meta.url);
const thisDir = path.dirname(thisFilePath);
const repoRoot = path.resolve(thisDir, '../../../../');
const cliEntry = path.join(repoRoot, 'apps', 'cli', 'src', 'node-entry.js');
const amendFixturesDir = path.join(repoRoot, 'test-fixtures', 'amend-cross-format');
const tempPaths = new Set();
const defaultOutputLineEnding = process.platform === 'win32' ? '\r\n' : '\n';
function normalizeToDefaultOutputLineEnding(text) {
return text.replace(/\r\n|\r|\n/g, defaultOutputLineEnding);
}
function runCli(args) {
return spawnSync('node', [cliEntry, ...args], {
cwd: repoRoot,
encoding: 'utf8',
});
}
function tempFile(name) {
const tempPath = path.join(
os.tmpdir(),
`anywaydata-${name}-${Date.now()}-${Math.random().toString(16).slice(2)}.txt`
);
tempPaths.add(tempPath);
return tempPath;
}
afterAll(async () => {
await Promise.all(
[...tempPaths].map(async (tempPath) => {
try {
await fs.unlink(tempPath);
} catch {
// Ignore missing files or cleanup race conditions.
}
})
);
});
test('param -h shows help', () => {
const result = runCli(['-h']);
expect(result.status).toBe(0);
expect(result.stdout).toContain('Usage: anywaydata <generate|amend>');
});
test('param -i is required', () => {
const result = runCli(['generate', '-n', '1']);
expect(result.status).toBe(1);
expect(result.stderr).toContain('Missing required argument: input file');
});
test('params -i and -n drive row count', () => {
const inputPath = path.join(repoRoot, 'apps', 'cli', 'examples', 'company-literal.txt');
const result = runCli(['generate', '-i', inputPath, '-n', '3', '-f', 'csv', '--show-progress', 'false']);
expect(result.status).toBe(0);
const lines = result.stdout.trim().split(/\r?\n/);
expect(lines.length).toBe(4);
expect(lines[0]).toContain('"Company"');
});
test('param -f controls output format (jsonl)', () => {
const inputPath = path.join(repoRoot, 'apps', 'cli', 'examples', 'company-literal.txt');
const result = runCli(['generate', '-i', inputPath, '-n', '2', '-f', 'jsonl', '--show-progress', 'false']);
expect(result.status).toBe(0);
const lines = result.stdout.trim().split(/\r?\n/);
expect(lines.length).toBe(2);
for (const line of lines) {
expect(() => JSON.parse(line)).not.toThrow();
}
});
test('param -o writes output file', async () => {
const inputPath = path.join(repoRoot, 'apps', 'cli', 'examples', 'company-literal.txt');
const outputPath = tempFile('out');
const result = runCli(['generate', '-i', inputPath, '-n', '2', '-f', 'csv', '-o', outputPath]);
expect(result.status).toBe(0);
const written = await fs.readFile(outputPath, 'utf8');
expect(written).toContain('"Company"');
expect(written).toContain('AnyWayData');
});
test('param -t forces single row and enables test-mode diagnostics', () => {
const inputPath = path.join(repoRoot, 'apps', 'cli', 'examples', 'company-literal.txt');
const result = runCli(['generate', '-i', inputPath, '-n', '8', '-f', 'csv', '-t']);
expect(result.status).toBe(0);
expect(result.stdout).toContain('> Operating in Test Mode - generating 1 entry');
const csvLines = result.stdout.split(/\r?\n/).filter((line) => line.startsWith('"') && line.includes('Company'));
expect(csvLines.length).toBeGreaterThan(0);
});
test('param --show-progress false suppresses progress logs', () => {
const inputPath = path.join(repoRoot, 'apps', 'cli', 'examples', 'company-literal.txt');
const result = runCli(['generate', '-i', inputPath, '-n', '1', '-f', 'csv', '--show-progress', 'false']);
expect(result.status).toBe(0);
expect(result.stdout).not.toContain('> Processing Input File');
expect(result.stdout).toContain('"Company"');
});
test('params --stream and --show-progress true use streaming path for csv/jsonl/dsv/json/xml', async () => {
const inputPath = path.join(repoRoot, 'apps', 'cli', 'examples', 'company-literal.txt');
const formats = ['csv', 'jsonl', 'dsv', 'json', 'xml'];
for (const format of formats) {
const outputPath = tempFile(`stream-${format}`);
const result = runCli([
'generate',
'-i',
inputPath,
'-n',
'2',
'-f',
format,
'-o',
outputPath,
'--stream',
'--show-progress',
'true',
]);
expect(result.status).toBe(0);
expect(result.stdout).toContain('using stream mode');
const written = await fs.readFile(outputPath, 'utf8');
expect(written.length).toBeGreaterThan(0);
}
}, 30000);
test('param --stream-threshold auto-enables stream mode when threshold reached', () => {
const inputPath = path.join(repoRoot, 'apps', 'cli', 'examples', 'company-literal.txt');
const outputPath = tempFile('threshold');
const result = runCli([
'generate',
'-i',
inputPath,
'-n',
'1',
'-f',
'csv',
'-o',
outputPath,
'--stream-threshold',
'1',
'--show-progress',
'true',
]);
expect(result.status).toBe(0);
expect(result.stdout).toContain('using stream mode');
});
test('param --unsafe-faker-expressions is accepted', async () => {
const specPath = tempFile('unsafe-flag');
await fs.writeFile(specPath, 'Company\nAnyWayData', 'utf8');
const withoutFlag = runCli(['generate', '-i', specPath, '-n', '1', '-f', 'csv', '--show-progress', 'false']);
expect(withoutFlag.status).toBe(0);
expect(withoutFlag.stdout).toContain('"Company"');
const withFlag = runCli([
'generate',
'-i',
specPath,
'-n',
'1',
'-f',
'csv',
'--unsafe-faker-expressions',
'true',
'--show-progress',
'false',
]);
expect(withFlag.status).toBe(0);
expect(withFlag.stdout).toContain('"Company"');
});
test('amend command applies schema to existing data', async () => {
const schemaPath = tempFile('schema');
const dataPath = tempFile('data');
await fs.writeFile(schemaPath, 'Name\nBob', 'utf8');
await fs.writeFile(dataPath, '"Name"\n"Alice"\n"Eve"', 'utf8');
const result = runCli([
'amend',
'--schema-file',
schemaPath,
'--data-file',
dataPath,
'--input-format',
'csv',
'-n',
'1',
'-f',
'json',
'--show-progress',
'false',
]);
expect(result.status).toBe(0);
expect(JSON.parse(result.stdout.trim())).toEqual([{ Name: 'Bob' }, { Name: 'Eve' }]);
});
test('amend command trim flags affect imported input values before amend processing', async () => {
const schemaPath = tempFile('trim-schema');
const dataPath = tempFile('trim-data');
await fs.writeFile(schemaPath, 'Status\nActive', 'utf8');
await fs.writeFile(dataPath, '"Name","Role"\n" Alice "," Engineer "', 'utf8');
const result = runCli([
'amend',
'--schema-file',
schemaPath,
'--data-file',
dataPath,
'--input-format',
'csv',
'--trim-input-fields',
'Name',
'-n',
'0',
'-f',
'json',
'--show-progress',
'false',
]);
expect(result.status).toBe(0);
expect(JSON.parse(result.stdout.trim())).toEqual([{ Name: 'Alice', Role: ' Engineer ', Status: '' }]);
});
test('amend command fixture flow: CSV input to DSV output on stdout', async () => {
const schemaPath = path.join(amendFixturesDir, 'schema.txt');
const dataPath = path.join(amendFixturesDir, 'input.csv');
const expectedPath = path.join(amendFixturesDir, 'expected-output.dsv');
const result = runCli([
'amend',
'--schema-file',
schemaPath,
'--data-file',
dataPath,
'--input-format',
'csv',
'-n',
'2',
'-f',
'dsv',
'--show-progress',
'false',
]);
expect(result.status).toBe(0);
const expected = await fs.readFile(expectedPath, 'utf8');
expect(result.stdout.trimEnd()).toBe(expected.trimEnd());
});
test('amend command fixture flow: DSV input to CSV output file', async () => {
const schemaPath = path.join(amendFixturesDir, 'schema.txt');
const dataPath = path.join(amendFixturesDir, 'input.dsv');
const expectedPath = path.join(amendFixturesDir, 'expected-output.csv');
const outputPath = tempFile('amend-cross-format-output');
const result = runCli([
'amend',
'--schema-file',
schemaPath,
'--data-file',
dataPath,
'--input-format',
'dsv',
'-n',
'2',
'-f',
'csv',
'-o',
outputPath,
'--show-progress',
'false',
]);
expect(result.status).toBe(0);
const [actual, expected] = await Promise.all([fs.readFile(outputPath, 'utf8'), fs.readFile(expectedPath, 'utf8')]);
expect(actual.trimEnd()).toBe(normalizeToDefaultOutputLineEnding(expected).trimEnd());
});