Skip to content

Commit

Permalink
fix: wrap paths in quotes in case of spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Jun 17, 2024
1 parent 0a8ed63 commit a3c0314
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 16 deletions.
3 changes: 2 additions & 1 deletion packages/plugin-coverage/src/lib/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ProcessError,
ensureDirectoryExists,
executeProcess,
filePathToCliArg,
readJsonFile,
ui,
} from '@code-pushup/utils';
Expand Down Expand Up @@ -60,7 +61,7 @@ export async function createRunnerConfig(

return {
command: 'node',
args: [scriptPath],
args: [filePathToCliArg(scriptPath)],
outputFile: RUNNER_OUTPUT_PATH,
...(threshold != null && {
outputTransform: outputs =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('createRunnerConfig', () => {
});
expect(runnerConfig).toStrictEqual<RunnerConfig>({
command: 'node',
args: ['executeRunner.ts'],
args: ['"executeRunner.ts"'],
outputTransform: expect.any(Function),
outputFile: expect.stringContaining('runner-output.json'),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ exports[`eslintPlugin > should initialize ESLint plugin for React application 1`
"packageName": "@code-pushup/eslint-plugin",
"runner": {
"args": [
"<dirname>/bin.js",
""<dirname>/bin.js"",
],
"command": "node",
"outputFile": "node_modules/.code-pushup/eslint/runner-output.json",
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-eslint/src/lib/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { dirname, join } from 'node:path';
import type { Audit, AuditOutput, RunnerConfig } from '@code-pushup/models';
import {
ensureDirectoryExists,
filePathToCliArg,
pluginWorkDir,
readJsonFile,
} from '@code-pushup/utils';
Expand Down Expand Up @@ -60,7 +61,7 @@ export async function createRunnerConfig(

return {
command: 'node',
args: [scriptPath],
args: [filePathToCliArg(scriptPath)],
outputFile: RUNNER_OUTPUT_PATH,
};
}
9 changes: 7 additions & 2 deletions packages/plugin-eslint/src/lib/runner/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import type { ESLint, Linter } from 'eslint';
import { rm, writeFile } from 'node:fs/promises';
import { platform } from 'node:os';
import { join } from 'node:path';
import { distinct, executeProcess, toArray } from '@code-pushup/utils';
import {
distinct,
executeProcess,
filePathToCliArg,
toArray,
} from '@code-pushup/utils';
import type { ESLintTarget } from '../config';
import { setupESLint } from '../setup';
import type { LinterOutput, RuleOptionsPerFile } from './types';
Expand All @@ -26,7 +31,7 @@ function executeLint({
command: 'npx',
args: [
'eslint',
...(configPath ? [`--config=${configPath}`] : []),
...(configPath ? [`--config=${filePathToCliArg(configPath)}`] : []),
...(typeof eslintrc === 'object' ? ['--no-eslintrc'] : []),
'--no-error-on-unmatched-pattern',
'--format=json',
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-eslint/src/lib/runner/lint.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('lint', () => {
command: 'npx',
args: [
'eslint',
'--config=.eslintrc.js',
'--config=".eslintrc.js"',
'--no-error-on-unmatched-pattern',
'--format=json',
expect.stringContaining('**/*.js'), // wraps in quotes on Unix
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-js-packages/src/lib/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { RunnerConfig } from '@code-pushup/models';
import {
ensureDirectoryExists,
executeProcess,
filePathToCliArg,
isPromiseFulfilledResult,
isPromiseRejectedResult,
objectFromEntries,
Expand Down Expand Up @@ -34,7 +35,7 @@ export async function createRunnerConfig(

return {
command: 'node',
args: [scriptPath],
args: [filePathToCliArg(scriptPath)],
outputFile: RUNNER_OUTPUT_PATH,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('createRunnerConfig', () => {
});
expect(runnerConfig).toStrictEqual<RunnerConfig>({
command: 'node',
args: ['executeRunner.ts'],
args: ['"executeRunner.ts"'],
outputFile: expect.stringContaining('runner-output.json'),
});
});
Expand Down
15 changes: 8 additions & 7 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from './lib/text-formats';
export { ExcludeNullFromPropertyTypes } from './lib/types';
export { exists } from '@code-pushup/models';
export { Diff, comparePairs, matchArrayItemsByKey } from './lib/diff';
export {
Expand All @@ -17,6 +15,7 @@ export {
directoryExists,
ensureDirectoryExists,
fileExists,
filePathToCliArg,
findLineNumberInText,
importEsmModule,
logMultipleFileResults,
Expand All @@ -39,18 +38,18 @@ export {
} from './lib/formatting';
export {
formatGitPath,
guardAgainstLocalChanges,
getGitRoot,
guardAgainstLocalChanges,
safeCheckout,
toGitPath,
} from './lib/git/git';
export {
getSemverTags,
LogResult,
getHashes,
getHashFromTag,
getCurrentBranchOrTag,
getHashFromTag,
getHashes,
getLatestCommit,
getSemverTags,
} from './lib/git/git.commits-and-tags';
export { groupByStatus } from './lib/group-by-status';
export {
Expand Down Expand Up @@ -85,6 +84,8 @@ export {
compareIssueSeverity,
loadReport,
} from './lib/reports/utils';
export { isSemver, normalizeSemver, sortSemvers } from './lib/semver';
export * from './lib/text-formats';
export {
CliArgsObject,
apostrophize,
Expand All @@ -104,5 +105,5 @@ export {
toUnixNewlines,
toUnixPath,
} from './lib/transform';
export { ExcludeNullFromPropertyTypes } from './lib/types';
export { verboseUtils } from './lib/verbose-utils';
export { isSemver, normalizeSemver, sortSemvers } from './lib/semver';
5 changes: 5 additions & 0 deletions packages/utils/src/lib/file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,8 @@ export function findLineNumberInText(
const lineNumber = lines.findIndex(line => line.includes(pattern)) + 1; // +1 because line numbers are 1-based
return lineNumber === 0 ? null : lineNumber; // If the package isn't found, return null
}

export function filePathToCliArg(path: string): string {
// needs to be escaped if spaces included
return `"${path}"`;
}
9 changes: 9 additions & 0 deletions packages/utils/src/lib/file-system.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
FileResult,
crawlFileSystem,
ensureDirectoryExists,
filePathToCliArg,
findLineNumberInText,
logMultipleFileResults,
} from './file-system';
Expand Down Expand Up @@ -139,3 +140,11 @@ describe('findLineNumberInText', () => {
expect(findLineNumberInText(``, 'x')).toBeNull();
});
});

describe('filePathToCliArg', () => {
it('should wrap path in quotes', () => {
expect(filePathToCliArg('My Project/index.js')).toBe(
'"My Project/index.js"',
);
});
});

0 comments on commit a3c0314

Please sign in to comment.