Skip to content

Commit

Permalink
Displaying version information in tests was going to go stale quick
Browse files Browse the repository at this point in the history
Ideally the tests would use a snapshot of packagist / advisories
so that they can never go wrong.
  • Loading branch information
cs278 committed Jul 27, 2021
1 parent 5cbb58d commit 96802c5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
17 changes: 15 additions & 2 deletions src/AuditCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln(sprintf(
'Checking <info>%s</info> (<info>%s</info>) for advisories...',
$name,
$name !== 'cs278/composer-audit' ? $version : 'N/A'
!self::isUnderTest() ? $version : 'N/A'
), OutputInterface::VERBOSITY_DEBUG);

foreach ($advisoriesManager->findByPackageNameAndVersion($name, $version) as $advisory) {
Expand All @@ -139,7 +139,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
'Found %u advisories for <info>%s</info> (<info>%s</info>)',
\count($advisories[$name]),
$name,
$name !== 'cs278/composer-audit' ? $version : 'N/A'
!self::isUnderTest() ? $version : 'N/A'
), OutputInterface::VERBOSITY_VERY_VERBOSE);
}
}
Expand Down Expand Up @@ -300,4 +300,17 @@ private static function clearLine(OutputInterface $output): void
$output->writeln('');
}
}

/**
* Check if the command is being run by the test suite.
*
* This is used to hide version information which will go stale as new packages
* are published.
*/
private static function isUnderTest(): bool
{
$value = filter_var(getenv('COMPOSER_AUDIT_TEST'), \FILTER_VALIDATE_BOOLEAN);

return $value !== null ? $value : false;
}
}
1 change: 1 addition & 0 deletions tests/integration/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public function testRun(int $expectedExit, string $expectedOutput, string $condi
return new Process($args, $workingDir, [
'COMPOSER_HOME' => $workingDir.'/.composer',
'COMPOSER_CACHE_DIR' => self::$cacheDir,
'COMPOSER_AUDIT_TEST' => 1,
]);
};

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/ignore--broken-show-warnings.test
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ true
Ignoring invalid ignore rule: `{"type":"package","value":"foo\/bar"}`
Ignoring invalid ignore rule: `{"type":"cve","value":""}`
Ignoring invalid ignore rule: `{"type":"","value":"test"}`
Found 9 advisories for symfony/http-foundation (2.0.4)
Found 9 advisories for symfony/http-foundation (N/A)
Found 9 advisories affecting 1 package(s).

composer://symfony/http-foundation (2.0.4)
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/verbose_debug_no_errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ true
0
--EXPECT-OUTPUT--
Checking cs278/composer-audit (N/A) for advisories...
Checking symfony/deprecation-contracts (v2.4.0) for advisories...
Checking symfony/polyfill-ctype (v1.23.0) for advisories...
Checking symfony/polyfill-php80 (v1.23.0) for advisories...
Checking symfony/yaml (v5.3.4) for advisories...
Checking symfony/deprecation-contracts (N/A) for advisories...
Checking symfony/polyfill-ctype (N/A) for advisories...
Checking symfony/polyfill-php80 (N/A) for advisories...
Checking symfony/yaml (N/A) for advisories...
No advisories found for any packages.

0 comments on commit 96802c5

Please sign in to comment.