Skip to content

Commit

Permalink
fix: grammar tests on Windows (#955)
Browse files Browse the repository at this point in the history
* fix: grammar tests on Windows

* fix: only do on Windows

* nit:
  • Loading branch information
Princesseuh authored Sep 20, 2024
1 parent 7b51794 commit 0ef6401
Showing 1 changed file with 1 addition and 37 deletions.
38 changes: 1 addition & 37 deletions packages/vscode/test/grammar/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,6 @@ const grammars = readdirSync(grammarDir)

const allGrammars = [...grammars, ...dummyGrammars];

/**
* Escapes command line arguments for Windows
* @param {string} arg
* @returns {string}
*
* Escaping sequences obtained trusting https://www.robvanderwoude.com/escapechars.php#:~:text=In%20batch%20files%2C%20the%20percent,instead%20of%20being%20further%20interpreted.
*/
function escapeWindowsArg(arg) {
// If the argument doesn't contain spaces or special characters, return it as is
const single_charac_escape = /(['`,;=()<>|&^])/g;
const double_carat_escape = /(!)/g;
const percentage_escape = /(%)/g;
const backlash_escape = /([\\"[\].*?])/g;
if (
![single_charac_escape, double_carat_escape, percentage_escape, backlash_escape].some((regex) =>
regex.test(arg),
)
) {
return arg;
}

// This one must go first, since it would otherwise conflict with the ! rule
arg = arg.replace(single_charac_escape, '^$1');
arg = arg.replace(double_carat_escape, '^^$1');
arg = arg.replace(percentage_escape, '%$1');
arg = arg.replace(backlash_escape, '\\$1');

// Wrap the entire argument in double quotes
return `"${arg}"`;
}

/**
* @param {Parameters<typeof spawn>} arg
* @returns {Promise<number | null>}
Expand Down Expand Up @@ -75,15 +44,10 @@ async function snapShotTest() {
'./test/grammar/fixtures/**/*.astro',
...allGrammars.reduce((/** @type string[] */ previous, path) => [...previous, '-g', path], []),
...extraArgs,
].map((arg) => {
if (isWindows) return escapeWindowsArg(arg);
return arg;
});
].map((arg) => (isWindows && arg.includes(' ') ? `"${arg}"` : arg))

const code = await promisifySpawn(isWindows ? 'pnpm.cmd' : 'pnpm', args, {
stdio: 'inherit',
// windows shell escaping on nodejs doesn't work
// See https://github.com/nodejs/node/issues/52554 and https://github.com/withastro/language-tools/pull/893
shell: isWindows,
});

Expand Down

0 comments on commit 0ef6401

Please sign in to comment.