Skip to content

Commit 9c7f52c

Browse files
committed
test: cover prettier plugin config flags
1 parent a8e677d commit 9c7f52c

3 files changed

Lines changed: 97 additions & 2 deletions

File tree

AGENTS.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ plugin versions:
123123
behind expected plugin-language failures.
124124
- CI now runs an explicit `vim --version` capability check for `+job` and
125125
`+channel` in the blocking Vim jobs, plus a blocking `git diff --check` job.
126+
- Added targeted config resolver tests for `g:prettier#config#plugins` and made
127+
the smoke lane run config resolver tests, not only the original Vim 9
128+
regression.
126129

127130
## Review Findings After b538e29
128131

@@ -174,7 +177,7 @@ plugin versions:
174177
- [x] Update classified Prettier 3.0.3 core snapshots for GraphQL, SCSS, Vue,
175178
and YAML without changing plugin-language snapshots broadly.
176179
- [x] Add explicit plugin argument support for PHP/XML bundled fallback tests.
177-
- [ ] Add targeted plugin config tests for `g:prettier#config#plugins`, including
180+
- [x] Add targeted plugin config tests for `g:prettier#config#plugins`, including
178181
string, list, empty, invalid, and paths with spaces.
179182
- [ ] Add project-local Prettier tests proving bundled plugin injection does not
180183
override local Prettier/plugins.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"scripts": {
1212
"test": "node scripts/vim-version.js && LOG_LEVEL=error PRETTIER_FORMATTING_FIXTURE_LANE=all jest",
13-
"test:smoke": "node scripts/vim-version.js && LOG_LEVEL=error PRETTIER_FORMATTING_FIXTURE_LANE=all jest tests/formatting.test.js --runInBand --testNamePattern=\"Prettier config version detection works inside vim-driver execute\"",
13+
"test:smoke": "node scripts/vim-version.js && LOG_LEVEL=error PRETTIER_FORMATTING_FIXTURE_LANE=all jest tests/formatting.test.js --runInBand --testNamePattern=\"Prettier config\"",
1414
"test:formatting:passing": "node scripts/vim-version.js && LOG_LEVEL=error PRETTIER_FORMATTING_FIXTURE_LANE=known-passing jest tests/formatting.test.js --runInBand",
1515
"test:formatting:quarantined": "node scripts/vim-version.js && LOG_LEVEL=error PRETTIER_FORMATTING_FIXTURE_LANE=quarantined jest tests/formatting.test.js --runInBand",
1616
"lint": "vint --version && vint ."

tests/formatting.test.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ jest.setTimeout(15000);
4444
const getBufferContents = async remote =>
4545
(await remote.call('getline', [1, '$'])).join('\n');
4646

47+
const vimString = value => `'${value.replace(/'/g, "''")}'`;
48+
49+
const resolveConfigFlags = config =>
50+
remote.eval(`prettier#resolver#config#resolve(${config}, 0, 1, 1)`);
51+
52+
const expectNoPluginFlags = flags => {
53+
expect(flags).not.toContain('--plugin=');
54+
};
55+
56+
const countPluginFlags = flags => (flags.match(/--plugin=/g) || []).length;
57+
4758
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
4859
const waitUntil = (condition, timeout = 2000) => {
4960
return new Promise(resolve => {
@@ -164,6 +175,87 @@ if (FORMAT_FIXTURE_LANE === 'all') {
164175
remote.execute('call prettier#resolver#config#resolve({}, 0, 1, 1)')
165176
).resolves.toBeDefined();
166177
});
178+
179+
describe('Prettier config plugins flags', () => {
180+
test('ignores empty string plugin config', async () => {
181+
const flags = await resolveConfigFlags(`{'plugins': ''}`);
182+
183+
expectNoPluginFlags(flags);
184+
});
185+
186+
test('ignores empty list plugin config', async () => {
187+
const flags = await resolveConfigFlags(`{'plugins': []}`);
188+
189+
expectNoPluginFlags(flags);
190+
});
191+
192+
test('adds a string plugin path', async () => {
193+
const pluginPath = '/tmp/prettier-plugin-example.js';
194+
const flags = await resolveConfigFlags(
195+
`{'plugins': ${vimString(pluginPath)}}`
196+
);
197+
198+
expect(flags).toContain(`--plugin='${pluginPath}'`);
199+
expect(countPluginFlags(flags)).toBe(1);
200+
});
201+
202+
test('adds a list of plugin paths', async () => {
203+
const pluginPaths = [
204+
'/tmp/prettier-plugin-one.js',
205+
'/tmp/prettier-plugin-two.js',
206+
];
207+
const flags = await resolveConfigFlags(
208+
`{'plugins': [${pluginPaths.map(vimString).join(', ')}]}`
209+
);
210+
211+
expect(flags).toContain(`--plugin='${pluginPaths[0]}'`);
212+
expect(flags).toContain(`--plugin='${pluginPaths[1]}'`);
213+
expect(countPluginFlags(flags)).toBe(2);
214+
});
215+
216+
test('ignores invalid plugin config type', async () => {
217+
const flags = await resolveConfigFlags(`{'plugins': 1}`);
218+
219+
expectNoPluginFlags(flags);
220+
});
221+
222+
test('shellescapes plugin paths containing spaces', async () => {
223+
const pluginPath = path.join(
224+
FIXTURES_DIR,
225+
'plugin path',
226+
'prettier-plugin-example.js'
227+
);
228+
const flags = await resolveConfigFlags(
229+
`{'plugins': ${vimString(pluginPath)}}`
230+
);
231+
232+
expect(flags).toContain(`--plugin='${pluginPath}'`);
233+
expect(countPluginFlags(flags)).toBe(1);
234+
});
235+
236+
test('falls back to restored global plugin config', async () => {
237+
const pluginPath = '/tmp/prettier-plugin-global.js';
238+
239+
await remote.execute(
240+
'let g:prettier_test_plugins = deepcopy(g:prettier#config#plugins)'
241+
);
242+
243+
try {
244+
await remote.execute(
245+
`let g:prettier#config#plugins = ${vimString(pluginPath)}`
246+
);
247+
248+
const flags = await resolveConfigFlags('{}');
249+
250+
expect(flags).toContain(`--plugin='${pluginPath}'`);
251+
expect(countPluginFlags(flags)).toBe(1);
252+
} finally {
253+
await remote.execute(
254+
'let g:prettier#config#plugins = g:prettier_test_plugins | unlet g:prettier_test_plugins'
255+
);
256+
}
257+
});
258+
});
167259
}
168260

169261
// run formatting tests in all fixtures

0 commit comments

Comments
 (0)