-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstdin.test.ts
More file actions
188 lines (154 loc) · 4.77 KB
/
Copy pathstdin.test.ts
File metadata and controls
188 lines (154 loc) · 4.77 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
import { expect, test } from 'rstack/test';
import {
packageJsonSource,
setupFmtTest,
sortedPackageJson,
} from './helpers.ts';
const { projectFileExists, runFmtStdin, writeProjectFile } = setupFmtTest();
test('formats stdin for the given filepath', () => {
const result = runFmtStdin(
['--stdin-filepath', 'src/index.ts'],
'const message="hello"',
);
expect(result.status).toBe(0);
expect(result.stdout).toBe('const message = "hello";\n');
expect(result.stderr).toBe('');
expect(projectFileExists('.rstack')).toBe(false);
});
test('applies define.fmt options and overrides to stdin', () => {
writeProjectFile(
'rstack.config.ts',
`import { define } from 'rstack';
define.fmt({
singleQuote: true,
overrides: [
{
files: '*.test.ts',
options: {
semi: false,
},
},
],
});
`,
);
const result = runFmtStdin(
['--stdin-filepath', 'src/index.test.ts'],
'const test="test"',
);
expect(result.status).toBe(0);
expect(result.stdout).toBe("const test = 'test'\n");
expect(result.stderr).toBe('');
});
test('sorts package.json from stdin', () => {
writeProjectFile(
'rstack.config.ts',
`import { define } from 'rstack';
define.fmt({ sortPackageJson: true });
`,
);
const result = runFmtStdin(
['--stdin-filepath', 'package.json'],
packageJsonSource,
);
expect(result.status).toBe(0);
expect(result.stdout).toBe(sortedPackageJson);
expect(result.stderr).toBe('');
});
test('echoes ignored stdin paths verbatim', () => {
writeProjectFile(
'rstack.config.ts',
`import { define } from 'rstack';
define.fmt({ ignorePatterns: ['src/ignored.ts'] });
`,
);
const source = 'const ignored="ignored"';
const result = runFmtStdin(['--stdin-filepath', 'src/ignored.ts'], source);
expect(result.status).toBe(0);
expect(result.stdout).toBe(source);
expect(result.stderr).toBe('');
});
test('echoes stdin paths ignored by --ignore-path', () => {
writeProjectFile('.prettierignore', 'src/ignored.ts\n');
const source = 'const ignored="ignored"';
const result = runFmtStdin(
['--ignore-path', '.prettierignore', '--stdin-filepath', 'src/ignored.ts'],
source,
);
expect(result.status).toBe(0);
expect(result.stdout).toBe(source);
expect(result.stderr).toBe('');
});
test('echoes stdin for default ignored lock files', () => {
const source = 'lockfileVersion: "9.0"\n';
const result = runFmtStdin(['--stdin-filepath', 'pnpm-lock.yaml'], source);
expect(result.status).toBe(0);
expect(result.stdout).toBe(source);
expect(result.stderr).toBe('');
});
test('returns exit code 2 when no parser can be inferred for stdin', () => {
const result = runFmtStdin(['--stdin-filepath', 'data.unknown'], 'value');
expect(result.status).toBe(2);
expect(result.stdout).toBe('');
expect(result.stderr).toContain(
'No parser could be inferred for "data.unknown".',
);
});
test('ignores stdin when no parser can be inferred with --ignore-unknown', () => {
const result = runFmtStdin(
['--stdin-filepath', 'data.unknown', '--ignore-unknown'],
'value',
);
expect(result.status).toBe(0);
expect(result.stdout).toBe('');
expect(result.stderr).toBe('');
});
test('returns exit code 2 for stdin parse errors', () => {
const result = runFmtStdin(
['--stdin-filepath', 'index.ts'],
'const value = ;',
);
expect(result.status).toBe(2);
expect(result.stdout).toBe('');
expect(result.stderr).toContain("Unexpected token ';'");
});
test.each(['--write', '--check', '--list-different'])(
'returns exit code 2 for %s with --stdin-filepath',
(option) => {
const result = runFmtStdin(
['--stdin-filepath', 'index.ts', option],
'const value=1',
);
expect(result.status).toBe(2);
expect(result.stdout).toBe('');
expect(result.stderr).toContain(
'The --stdin-filepath option cannot be used with --write, --check, or --list-different.',
);
},
);
test('returns exit code 2 for file arguments with --stdin-filepath', () => {
const result = runFmtStdin(
['--stdin-filepath', 'index.ts', 'src/other.ts'],
'const value=1',
);
expect(result.status).toBe(2);
expect(result.stdout).toBe('');
expect(result.stderr).toContain(
'The --stdin-filepath option cannot be used with file arguments.',
);
});
test('accepts --parallel-workers with --stdin-filepath', () => {
const result = runFmtStdin(
['--stdin-filepath', 'index.ts', '--parallel-workers', '2'],
'const value=1',
);
expect(result.status).toBe(0);
expect(result.stdout).toBe('const value = 1;\n');
expect(result.stderr).toBe('');
});
test('writes nothing for empty stdin', () => {
const result = runFmtStdin(['--stdin-filepath', 'index.ts'], '');
expect(result.status).toBe(0);
expect(result.stdout).toBe('');
expect(result.stderr).toBe('');
});