fix: improve -f flag detection to prevent false positives

- Refine regex pattern to prevent false positives with flags like -foo, -from, -feature
- Change from \S (any non-whitespace) to [.~/]|$ (path characters or end of word)
- Add comprehensive tests for false positive prevention (4 test cases)
- Add path normalization tests for baseDirectory edge cases (6 test cases)
- Add @example documentation to injectDockerComposeFlags function

Prevents incorrect detection of:
- -foo, -from, -feature, -fast as the -f flag
- Ensures -f flag is only detected when followed by path characters or end of word

All 45 tests passing with 135 assertions.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Andras Bacsai
2025-11-18 13:49:46 +01:00
parent cfe3f5d8b9
commit e7fd1ba36a
2 changed files with 133 additions and 2 deletions

View File

@@ -1280,14 +1280,19 @@ function generateDockerEnvFlags($variables): string
* @param string $composeFilePath The path to the compose file
* @param string $envFilePath The path to the .env file
* @return string The modified command with injected flags
*
* @example
* Input: "docker compose build"
* Output: "docker compose -f ./docker-compose.yml --env-file .env build"
*/
function injectDockerComposeFlags(string $command, string $composeFilePath, string $envFilePath): string
{
$dockerComposeReplacement = 'docker compose';
// Add -f flag if not present (checks for both -f and --file with various formats)
// Detects: -f path, -f=path, -fpath (concatenated), --file path, --file=path with any whitespace (space, tab, newline)
if (! preg_match('/(?:^|\s)(?:-f(?:[=\s]|\S)|--file(?:=|\s))/', $command)) {
// Detects: -f path, -f=path, -fpath (concatenated with path chars: . / ~), --file path, --file=path
// Note: Uses [.~/]|$ instead of \S to prevent false positives with flags like -foo, -from, -feature
if (! preg_match('/(?:^|\s)(?:-f(?:[=\s]|[.\/~]|$)|--file(?:=|\s))/', $command)) {
$dockerComposeReplacement .= " -f {$composeFilePath}";
}