Skip to content

Commit

Permalink
Fix Running Executables With Spaces (#44)
Browse files Browse the repository at this point in the history
This commit adds support for running PHPCS
executables that have spaces in them. We break
the executable apart on spaces, with support
for quoted strings. The first segment is the
executable while the rest are arguments to pass.
  • Loading branch information
ObliviousHarmony authored May 6, 2022
1 parent 161844e commit 3387cc8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- `phpCodeSniffer.executable` options with spaces throwing errors.

## [1.5.0] - 2021-12-10
### Fixed
Expand Down
23 changes: 23 additions & 0 deletions src/phpcs-report/__tests__/worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,27 @@ describe('Worker', () => {
expect(onActiveChanged).toHaveBeenCalledTimes(2);
expect(onActiveChanged).toHaveBeenLastCalledWith(worker);
});

it('should support executables with spaces', async () => {
const worker = new Worker();

const request: Request<ReportType.Diagnostic> = {
type: ReportType.Diagnostic,
documentPath: 'Test.php',
documentContent: '<?php class Test {}',
options: {
workingDirectory: __dirname,
// Adding the PHP executable won't break the PHPCS script.
executable: '/usr/bin/env php ' + phpcsPath,
standard: StandardType.PSR12,
},
data: null,
};

const response = await worker.execute(request);

expect(response).toHaveProperty('type', ReportType.Diagnostic);
expect(response).toHaveProperty('report');
expect(response.report).not.toBeUndefined();
});
});
20 changes: 19 additions & 1 deletion src/phpcs-report/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ export class Worker {
'1',
];

// Since the executable may also include arguments, we need to break the given option
// apart and track the specific process and add the args to the array we will use
// to spawn the process. We will break on spaces but also support quoted strings.
const executableMatches = request.options.executable.match( /`((?:[^`\\]|\\`)*)`|'((?:[^'\\]|\\')*)'|"((?:[^"\\]|\\")*)"|([^\s"]+)/g );
if (!executableMatches) {
throw new Error('No executable was given.');
}

// The first segment will always be the executable.
const executable = executableMatches.shift();
if (!executable) {
throw new Error('No executable was given.');
}

// Any remaining matches will be arguments that we pass to the executable.
// Make sure to add them to the front so it runs PHPCS correctly.
processArguments.unshift(...executableMatches);

// Only set the standard when the user has selected one.
if (request.options.standard !== StandardType.Default) {
processArguments.push('--standard=' + request.options.standard);
Expand All @@ -203,7 +221,7 @@ export class Worker {

// Create a new process to fetch the report.
const phpcsProcess = spawn(
request.options.executable,
executable,
processArguments,
processOptions
);
Expand Down

0 comments on commit 3387cc8

Please sign in to comment.