diff --git a/.circleci/config.yml b/.circleci/config.yml index 295dc0a..f3173ef 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -96,9 +96,9 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--force-push--circleci--[branch] \ --mode=force-push \ - --report=$HOME/report--mode--force-push.txt \ + --log=$HOME/report--mode--force-push.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" @@ -121,9 +121,9 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--force-push--circleci--[branch] \ --mode=force-push \ - --report=$HOME/report--mode--force-push.txt \ + --log=$HOME/report--mode--force-push.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" @@ -170,9 +170,9 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--branch--circleci--[branch]--[timestamp:Y-m-d_H-i] \ --mode=branch \ - --report=$HOME/report--mode--branch.txt \ + --log=$HOME/report--mode--branch.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--branch.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" @@ -187,9 +187,9 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--branch--circleci--[branch]--[timestamp:Y-m-d_H-i] \ --mode=branch \ - --report=$HOME/report--mode--branch.txt \ + --log=$HOME/report--mode--branch.txt \ --push \ - --debug \ + -vvv \ && { echo "Expected to fail as repeated pushes to the same branch are not allowed, but succeeded" >&2; exit 1; } || echo "Failed as expected" - run: @@ -207,9 +207,9 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--branch--circleci--[branch]--[timestamp:Y-m-d_H-i-s] \ --mode=branch \ - --report=$HOME/report--mode--branch.txt \ + --log=$HOME/report--mode--branch.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--branch.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" diff --git a/.github/workflows/test-php.yml b/.github/workflows/test-php.yml index b61da39..7215545 100644 --- a/.github/workflows/test-php.yml +++ b/.github/workflows/test-php.yml @@ -158,9 +158,9 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--force-push--gha--[branch] \ --mode=force-push \ - --report=$HOME/report--mode--force-push.txt \ + --log=$HOME/report--mode--force-push.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" @@ -181,9 +181,9 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--force-push--gha--[branch] \ --mode=force-push \ - --report=$HOME/report--mode--force-push.txt \ + --log=$HOME/report--mode--force-push.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" @@ -260,9 +260,9 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--branch--gha--[branch]--[timestamp:Y-m-d_H-i] \ --mode=branch \ - --report=$HOME/report--mode--branch.txt \ + --log=$HOME/report--mode--branch.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--branch.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" @@ -283,9 +283,9 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--branch--gha--[branch]--[timestamp:Y-m-d_H-i] \ --mode=branch \ - --report=$HOME/report--mode--branch.txt \ + --log=$HOME/report--mode--branch.txt \ --push \ - --debug \ + -vvv \ && { echo "Expected to fail as repeated pushes to the same branch are not allowed, but succeeded" >&2; exit 1; } || echo "Failed as expected" - name: Deployment 2 - new branch @@ -294,9 +294,9 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--branch--gha--[branch]--[timestamp:Y-m-d_H-i-s] \ --mode=branch \ - --report=$HOME/report--mode--branch.txt \ + --log=$HOME/report--mode--branch.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--branch.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" diff --git a/README.md b/README.md index 55ed662..fb8b1fc 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ fully-configured [example in the DrevOps project](https://github.com/drevops/dre --no-cleanup Do not cleanup after run. --now=NOW Internal value used to set internal time. --push Push artifact to the remote repository. Defaults to FALSE. - --report=REPORT Path to the report file. + --log=LOG Path to the log/report file. --root=ROOT Path to the root for file path resolution. If not specified, current directory is used. --show-changes Show changes made to the repo by the build in the output. --src=SRC Directory where source repository is located. If not specified, root directory is used. @@ -144,9 +144,6 @@ fully-configured [example in the DrevOps project](https://github.com/drevops/dre --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question - --simulate Run in simulated mode (show what would have happened). - --progress-delay=PROGRESS-DELAY Number of seconds before progress bar is displayed in long-running task collections. Default: 2s. [default: 2] - -D, --define=DEFINE Define a configuration item value. (multiple values allowed) -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Help: diff --git a/composer.json b/composer.json index a0d8c68..d35a71e 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "symfony/filesystem": "^6", "symfony/finder": "^6", "czproject/git-php": "^4.2", - "symfony/process": "^6" + "symfony/process": "^6", + "symfony/monolog-bridge": "^6" }, "require-dev": { "bamarni/composer-bin-plugin": "^1.8", @@ -52,7 +53,7 @@ "lint": [ "phpcs", "phpmd --exclude vendor,vendor-bin,node_modules . text phpmd.xml", - "phpstan", + "phpstan --memory-limit=-1", "rector --clear-cache --dry-run" ], "lint-fix": [ diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index b59607a..5c90bae 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -7,6 +7,7 @@ use DrevOps\GitArtifact\FilesystemTrait; use DrevOps\GitArtifact\GitArtifactGit; use DrevOps\GitArtifact\GitArtifactGitRepository; +use DrevOps\GitArtifact\LogTrait; use DrevOps\GitArtifact\TokenTrait; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -28,6 +29,7 @@ class ArtifactCommand extends Command { use TokenTrait; use FilesystemTrait; + use LogTrait; const GIT_REMOTE_NAME = 'dst'; @@ -96,9 +98,9 @@ class ArtifactCommand extends Command { protected bool $needCleanup = TRUE; /** - * Path to report file. + * Path to log file. */ - protected string $reportFile = ''; + protected string $logFile = ''; /** * Flag to show changes made to the repo by the build in the output. @@ -110,11 +112,6 @@ class ArtifactCommand extends Command { */ protected bool $result = FALSE; - /** - * Flag to print debug information. - */ - protected bool $debug = FALSE; - /** * Internal option to set current timestamp. */ @@ -162,7 +159,6 @@ protected function configure(): void { $this ->addOption('branch', NULL, InputOption::VALUE_REQUIRED, 'Destination branch with optional tokens.', '[branch]') - ->addOption('debug', NULL, InputOption::VALUE_NONE, 'Print debug information.') ->addOption( 'gitignore', NULL, @@ -186,7 +182,7 @@ protected function configure(): void { ->addOption('no-cleanup', NULL, InputOption::VALUE_NONE, 'Do not cleanup after run.') ->addOption('now', NULL, InputOption::VALUE_REQUIRED, 'Internal value used to set internal time.') ->addOption('push', NULL, InputOption::VALUE_NONE, 'Push artifact to the remote repository') - ->addOption('report', NULL, InputOption::VALUE_REQUIRED, 'Path to the report file.') + ->addOption('log', NULL, InputOption::VALUE_REQUIRED, 'Path to the log file.') ->addOption( 'root', NULL, @@ -221,19 +217,39 @@ protected function configure(): void { * @throws \Exception */ protected function execute(InputInterface $input, OutputInterface $output): int { + // If log option was set, we set verbosity is debug. + if ($input->getOption('log')) { + $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); + } $this->output = $output; + $tmpLogFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . time() . '-artifact-log.log'; + $this->logger = self::createLogger((string) $this->getName(), $output, $tmpLogFile); + $remote = $input->getArgument('remote'); try { + // Now we have all what we need. + // Let process artifact function. $this->checkRequirements(); - $remote = $input->getArgument('remote'); // @phpstan-ignore-next-line $this->processArtifact($remote, $input->getOptions()); + + // Dump log file and clean tmp log file. + if ($this->fsFileSystem->exists($tmpLogFile)) { + if (!empty($this->logFile)) { + $this->fsFileSystem->copy($tmpLogFile, $this->logFile); + } + $this->fsFileSystem->remove($tmpLogFile); + } } catch (\Exception $exception) { - $output->writeln('' . $exception->getMessage() . ''); - + $this->output->writeln([ + 'Deployment failed.', + '' . $exception->getMessage() . '', + ]); return Command::FAILURE; } + $this->output->writeln('Deployment finished successfully.'); + return Command::SUCCESS; } @@ -242,7 +258,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int * * @param string $remote * Path to the remote git repository. - * @param array $opts + * @param array $options * Options. * * @option $branch Destination branch with optional tokens. @@ -263,31 +279,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int * specified, root directory is used. * * @throws \Exception - * - * @phpstan-ignore-next-line */ - protected function processArtifact(string $remote, array $opts = [ - 'branch' => '[branch]', - 'debug' => FALSE, - 'gitignore' => '', - 'message' => 'Deployment commit', - 'mode' => 'force-push', - 'no-cleanup' => FALSE, - 'now' => '', - 'push' => FALSE, - 'report' => '', - 'root' => '', - 'show-changes' => FALSE, - 'src' => '', - ]): void { + protected function processArtifact(string $remote, array $options): void { try { $error = NULL; - $this->resolveOptions($remote, $opts); - - // Now we have all what we need. - // Let process artifact function. - $this->printDebug('Debug messages enabled'); - + $this->logDebug('Debug messages enabled'); + // Let resolve options into properties first. + $this->resolveOptions($remote, $options); $this->setupRemoteForRepository(); $this->showInfo(); $this->prepareArtifact(); @@ -296,7 +294,7 @@ protected function processArtifact(string $remote, array $opts = [ $this->doPush(); } else { - $this->yell('Cowardly refusing to push to remote. Use --push option to perform an actual push.'); + $this->output->writeln('Cowardly refusing to push to remote. Use --push option to perform an actual push.'); } $this->result = TRUE; } @@ -305,19 +303,13 @@ protected function processArtifact(string $remote, array $opts = [ $error = $exception->getMessage(); } - if (!empty($this->reportFile)) { - $this->dumpReport(); - } + $this->logReport(); if ($this->needCleanup) { $this->cleanup(); } - if ($this->result) { - $this->say('Deployment finished successfully.'); - } - else { - $this->say('Deployment failed.'); + if (!$this->result) { throw new \Exception((string) $error); } } @@ -382,7 +374,8 @@ protected function prepareArtifact(): void { $result = $this->commitAllChangesInGitRepository(); // Show all changes if needed. if ($this->showChanges) { - $this->say(sprintf('Added changes: %s', implode("\n", $result))); + $this->output->writeln(sprintf('Added changes: %s', implode("\n", $result))); + $this->logNotice(sprintf('Added changes: %s', implode("\n", $result))); } } @@ -468,7 +461,7 @@ protected function doPush(): void { $this->gitRepository->push([$this->remoteName, $refSpec]); } - $this->sayOkay(sprintf('Pushed branch "%s" with commit message "%s"', $this->destinationBranch, $this->message)); + $this->output->writeln(sprintf('Pushed branch "%s" with commit message "%s"', $this->destinationBranch, $this->message)); } catch (\Exception $exception) { // Re-throw the message with additional context. @@ -505,9 +498,8 @@ protected function resolveOptions(string $remote, array $options): void { $this->showChanges = !empty($options['show-changes']); $this->needCleanup = empty($options['no-cleanup']); $this->needsPush = !empty($options['push']); - $this->reportFile = empty($options['report']) ? '' : $options['report']; + $this->logFile = empty($options['log']) ? '' : $this->fsGetAbsolutePath($options['log']); $this->now = empty($options['now']) ? time() : (int) $options['now']; - $this->debug = !empty($options['debug']); $this->remoteName = self::GIT_REMOTE_NAME; $this->remoteUrl = $remote; $this->setMode($options['mode'], $options); @@ -563,13 +555,17 @@ protected function showInfo(): void { $lines[] = (' Gitignore file: ' . ($this->gitignoreFile ?: 'No')); $lines[] = (' Will push: ' . ($this->needsPush ? 'Yes' : 'No')); $lines[] = ('----------------------------------------------------------------------'); + $this->output->writeln($lines); + foreach ($lines as $line) { + $this->logNotice($line); + } } /** * Dump artifact report to a file. */ - protected function dumpReport(): void { + protected function logReport(): void { $lines[] = '----------------------------------------------------------------------'; $lines[] = ' Artifact report'; $lines[] = '----------------------------------------------------------------------'; @@ -583,7 +579,9 @@ protected function dumpReport(): void { $lines[] = ' Push result: ' . ($this->result ? 'Success' : 'Failure'); $lines[] = '----------------------------------------------------------------------'; - $this->fsFileSystem->dumpFile($this->reportFile, implode(PHP_EOL, $lines)); + foreach ($lines as $line) { + $this->logNotice($line); + } } /** @@ -597,8 +595,6 @@ protected function dumpReport(): void { * @phpstan-ignore-next-line */ protected function setMode(string $mode, array $options): void { - $this->say(sprintf('Running in "%s" mode', $mode)); - switch ($mode) { case self::modeForcePush(): // Intentionally empty. @@ -606,9 +602,9 @@ protected function setMode(string $mode, array $options): void { case self::modeBranch(): if (!$this->hasToken($options['branch'])) { - $this->say('WARNING! Provided branch name does not have a token. + $this->output->writeln('WARNING! Provided branch name does not have a token. Pushing of the artifact into this branch will fail on second and follow up pushes to remote. - Consider adding tokens with unique values to the branch name.'); + Consider adding tokens with unique values to the branch name.'); } break; @@ -706,11 +702,11 @@ protected function setGitignoreFile(string $path): void { */ protected function checkRequirements(): void { // @todo Refactor this into more generic implementation. - $this->say('Checking requirements'); + $this->logNotice('Checking requirements'); if (!$this->fsIsCommandAvailable('git')) { throw new \RuntimeException('At least one of the script running requirements was not met'); } - $this->sayOkay('All requirements were met'); + $this->logNotice('All requirements were met'); } /** @@ -721,7 +717,7 @@ protected function checkRequirements(): void { */ protected function replaceGitignoreInGitRepository(string $filename): void { $path = $this->getSourcePathGitRepository(); - $this->printDebug('Replacing .gitignore: %s with %s', $path . DIRECTORY_SEPARATOR . '.gitignore', $filename); + $this->logDebug(sprintf('Replacing .gitignore: %s with %s', $path . DIRECTORY_SEPARATOR . '.gitignore', $filename)); $this->fsFileSystem->copy($filename, $path . DIRECTORY_SEPARATOR . '.gitignore', TRUE); $this->fsFileSystem->remove($filename); } @@ -803,7 +799,7 @@ protected function disableLocalExclude(string $path): void { $filename = $this->getLocalExcludeFileName($path); $filenameDisabled = $filename . '.bak'; if ($this->fsFileSystem->exists($filename)) { - $this->printDebug('Disabling local exclude'); + $this->logDebug('Disabling local exclude'); $this->fsFileSystem->rename($filename, $filenameDisabled); } } @@ -818,7 +814,7 @@ protected function restoreLocalExclude(string $path): void { $filename = $this->getLocalExcludeFileName($path); $filenameDisabled = $filename . '.bak'; if ($this->fsFileSystem->exists($filenameDisabled)) { - $this->printDebug('Restoring local exclude'); + $this->logDebug('Restoring local exclude'); $this->fsFileSystem->rename($filenameDisabled, $filename); } } @@ -840,12 +836,12 @@ protected function removeIgnoredFiles(string $location, string $gitignorePath = $gitignoreContent = file_get_contents($gitignorePath); if (!$gitignoreContent) { - $this->printDebug('Unable to load ' . $gitignoreContent); + $this->logDebug('Unable to load ' . $gitignoreContent); } else { - $this->printDebug('-----.gitignore---------'); - $this->printDebug($gitignoreContent); - $this->printDebug('-----.gitignore---------'); + $this->logDebug('-----.gitignore---------'); + $this->logDebug($gitignoreContent); + $this->logDebug('-----.gitignore---------'); } $files = $this @@ -856,7 +852,7 @@ protected function removeIgnoredFiles(string $location, string $gitignorePath = $files = array_filter($files); foreach ($files as $file) { $fileName = $location . DIRECTORY_SEPARATOR . $file; - $this->printDebug('Removing excluded file %s', $fileName); + $this->logDebug(sprintf('Removing excluded file %s', $fileName)); if ($this->fsFileSystem->exists($fileName)) { $this->fsFileSystem->remove($fileName); } @@ -878,7 +874,7 @@ protected function removeOtherFilesInGitRepository(): void { $files = array_filter($files); foreach ($files as $file) { $fileName = $this->getSourcePathGitRepository() . DIRECTORY_SEPARATOR . $file; - $this->printDebug('Removing other file %s', $fileName); + $this->logDebug(sprintf('Removing other file %s', $fileName)); $this->fsFileSystem->remove($fileName); } } @@ -904,7 +900,7 @@ protected function removeSubReposInGitRepository(): void { $dir = $dir->getPathname(); } $this->fsFileSystem->remove($dir); - $this->printDebug('Removing sub-repository "%s"', (string) $dir); + $this->logDebug(sprintf('Removing sub-repository "%s"', (string) $dir)); } } @@ -955,71 +951,6 @@ protected function getTokenTimestamp(string $format = 'Y-m-d_H-i-s'): string { return date($format, $this->now); } - /** - * Check if running in debug mode. - * - * @return bool - * Check is debugging mode or not. - */ - protected function isDebug(): bool { - return $this->debug || $this->output->isDebug(); - } - - /** - * Write line as yell style. - * - * @param string $text - * Text yell. - */ - protected function yell(string $text): void { - $color = 'green'; - $char = $this->decorationCharacter('>', '➜'); - $format = sprintf('%%s %%s', $color, $color); - $this->writeln(sprintf($format, $char, $text)); - } - - /** - * Write line as say style. - * - * @param string $text - * Text. - */ - protected function say(string $text): void { - $char = $this->decorationCharacter('>', '➜'); - $this->writeln(sprintf('%s %s', $char, $text)); - } - - /** - * Print success message. - * - * Usually used to explicitly state that some action was successfully - * executed. - * - * @param string $text - * Message text. - */ - protected function sayOkay(string $text): void { - $color = 'green'; - $char = $this->decorationCharacter('V', '✔'); - $format = sprintf('%%s %%s', $color, $color); - $this->writeln(sprintf($format, $char, $text)); - } - - /** - * Print debug information. - * - * @param mixed ...$args - * The args. - */ - protected function printDebug(mixed ...$args): void { - if (!$this->isDebug()) { - return; - } - $message = array_shift($args); - /* @phpstan-ignore-next-line */ - $this->writeln(vsprintf($message, $args)); - } - /** * Write output. * @@ -1030,25 +961,6 @@ protected function writeln(string $text): void { $this->output->writeln($text); } - /** - * Decoration character. - * - * @param string $nonDecorated - * Non decorated. - * @param string $decorated - * Decorated. - * - * @return string - * The decoration character. - */ - protected function decorationCharacter(string $nonDecorated, string $decorated): string { - if (!$this->output->isDecorated() || (strncasecmp(PHP_OS, 'WIN', 3) === 0)) { - return $nonDecorated; - } - - return $decorated; - } - /** * Setup remote for current repository. * diff --git a/src/LogTrait.php b/src/LogTrait.php new file mode 100644 index 0000000..c2ce54f --- /dev/null +++ b/src/LogTrait.php @@ -0,0 +1,96 @@ +pushHandler($handler); + // Stream handler if needed. + if (!empty($logFile)) { + $verbosityMapping = [ + OutputInterface::VERBOSITY_QUIET => Level::Error, + OutputInterface::VERBOSITY_NORMAL => Level::Warning, + OutputInterface::VERBOSITY_VERBOSE => Level::Notice, + OutputInterface::VERBOSITY_VERY_VERBOSE => Level::Info, + OutputInterface::VERBOSITY_DEBUG => Level::Debug, + ]; + $verbosity = $output->getVerbosity(); + $level = $verbosityMapping[$verbosity] ?? Level::Debug; + $handler = new StreamHandler($logFile, $level); + $logger->pushHandler($handler); + } + + return $logger; + } + + /** + * Log debug. + * + * @param string|\Stringable $message + * Message. + * @param array $context + * Context. + */ + public function logDebug(string|\Stringable $message, array $context = []): void { + $this->logger->debug($message, $context); + } + + /** + * Log notice. + * + * @param string|\Stringable $message + * Message. + * @param array $context + * Context. + */ + public function logNotice(string|\Stringable $message, array $context = []): void { + $this->logger->notice($message, $context); + } + + /** + * Log error. + * + * @param string|\Stringable $message + * Message. + * @param array $context + * Context. + */ + public function logError(string|\Stringable $message, array $context = []): void { + $this->logger->error($message, $context); + } + +} diff --git a/tests/phpunit/AbstractTestCase.php b/tests/phpunit/AbstractTestCase.php index e48e6d6..931cc0f 100644 --- a/tests/phpunit/AbstractTestCase.php +++ b/tests/phpunit/AbstractTestCase.php @@ -56,7 +56,6 @@ protected function setUp(): void { $this->commandTraitSetUp( $this->fixtureDir . DIRECTORY_SEPARATOR . 'git_src', $this->fixtureDir . DIRECTORY_SEPARATOR . 'git_remote', - $this->isDebug() ); } @@ -71,14 +70,4 @@ protected function tearDown(): void { } } - /** - * Check if testing framework was ran with --debug option. - * - * @return bool - * TRUE if is in debug mode, FALSE otherwise. - */ - protected function isDebug(): bool { - return in_array('--debug', $_SERVER['argv'], TRUE); - } - } diff --git a/tests/phpunit/Functional/AbstractFunctionalTestCase.php b/tests/phpunit/Functional/AbstractFunctionalTestCase.php index d09c6d0..be63379 100644 --- a/tests/phpunit/Functional/AbstractFunctionalTestCase.php +++ b/tests/phpunit/Functional/AbstractFunctionalTestCase.php @@ -125,12 +125,6 @@ protected function runBuild(string $args = '', bool $expectFail = FALSE): string $output = $this->runGitArtifactCommandTimestamped(sprintf('--src=%s %s %s', $this->src, $this->dst, $args), $expectFail); - if ($this->isDebug()) { - print str_pad('', 80, '+') . PHP_EOL; - print implode(PHP_EOL, $output) . PHP_EOL; - print str_pad('', 80, '+') . PHP_EOL; - } - return implode(PHP_EOL, $output); } diff --git a/tests/phpunit/Functional/ForcePushTest.php b/tests/phpunit/Functional/ForcePushTest.php index 79d5e7c..b793780 100644 --- a/tests/phpunit/Functional/ForcePushTest.php +++ b/tests/phpunit/Functional/ForcePushTest.php @@ -75,7 +75,7 @@ public function testSubRepos(): void { $this->gitAssertFilesNotExist($this->src, ['r2/r21.git/index']); $this->gitAssertFilesNotExist($this->src, ['r3/r31/r311/.git/index']); - $output = $this->assertBuildSuccess('--debug'); + $output = $this->assertBuildSuccess('-vvv'); $this->assertStringContainsString(sprintf('Removing sub-repository "%s"', $this->src . DIRECTORY_SEPARATOR . 'r1/.git'), $output); $this->assertStringContainsString(sprintf('Removing sub-repository "%s"', $this->src . DIRECTORY_SEPARATOR . 'r2/r21/.git'), $output); $this->assertStringContainsString(sprintf('Removing sub-repository "%s"', $this->src . DIRECTORY_SEPARATOR . 'r3/r31/r311/.git'), $output); @@ -234,7 +234,7 @@ public function testGitignoreCustomWhitelisting(): void { ]); // Run the build. - $this->assertBuildSuccess('--debug --gitignore=' . $this->src . DIRECTORY_SEPARATOR . 'mygitignore'); + $this->assertBuildSuccess('-vvv --gitignore=' . $this->src . DIRECTORY_SEPARATOR . 'mygitignore'); $this->assertFixtureCommits(2, $this->dst, 'testbranch', ['Custom third commit', 'Deployment commit'], FALSE); diff --git a/tests/phpunit/Functional/GeneralTest.php b/tests/phpunit/Functional/GeneralTest.php index 7fdac32..adff138 100644 --- a/tests/phpunit/Functional/GeneralTest.php +++ b/tests/phpunit/Functional/GeneralTest.php @@ -62,15 +62,65 @@ public function testNoCleanup(): void { $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); } - public function testReport(): void { + public function testDebug(): void { + $this->gitCreateFixtureCommits(1); + $output = $this->runBuild('-vvv'); + + $this->assertStringContainsString('Debug messages enabled', $output); + $this->assertStringContainsString('Artifact information', $output); + $this->assertStringContainsString('Mode: force-push', $output); + $this->assertStringContainsString('Source repository: ' . $this->src, $output); + $this->assertStringContainsString('Remote repository: ' . $this->dst, $output); + $this->assertStringContainsString('Remote branch: ' . $this->currentBranch, $output); + $this->assertStringContainsString('Gitignore file: No', $output); + $this->assertStringContainsString('Will push: No', $output); + + $this->assertStringContainsString('Artifact report', $output); + $this->assertStringContainsString(sprintf('Source repository: %s', $this->src), $output); + $this->assertStringContainsString(sprintf('Remote repository: %s', $this->dst), $output); + $this->assertStringContainsString(sprintf('Remote branch: %s', $this->currentBranch), $output); + $this->assertStringContainsString('Gitignore file: No', $output); + $this->assertStringContainsString('Push result: Success', $output); + + $this->assertStringContainsString('Cowardly refusing to push to remote. Use --push option to perform an actual push.', $output); + $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); + } + + public function testDebugLogFile(): void { $report = $this->src . DIRECTORY_SEPARATOR . 'report.txt'; $this->gitCreateFixtureCommits(1); - $this->runBuild(sprintf('--report=%s', $report)); + $commandOutput = $this->runBuild(sprintf('--log=%s', $report)); + + $this->assertStringContainsString('Debug messages enabled', $commandOutput); + $this->assertStringContainsString('Artifact information', $commandOutput); + $this->assertStringContainsString('Mode: force-push', $commandOutput); + $this->assertStringContainsString('Source repository: ' . $this->src, $commandOutput); + $this->assertStringContainsString('Remote repository: ' . $this->dst, $commandOutput); + $this->assertStringContainsString('Remote branch: ' . $this->currentBranch, $commandOutput); + $this->assertStringContainsString('Gitignore file: No', $commandOutput); + $this->assertStringContainsString('Will push: No', $commandOutput); + + $this->assertStringContainsString('Artifact report', $commandOutput); + $this->assertStringContainsString(sprintf('Source repository: %s', $this->src), $commandOutput); + $this->assertStringContainsString(sprintf('Remote repository: %s', $this->dst), $commandOutput); + $this->assertStringContainsString(sprintf('Remote branch: %s', $this->currentBranch), $commandOutput); + $this->assertStringContainsString('Gitignore file: No', $commandOutput); + $this->assertStringContainsString('Push result: Success', $commandOutput); $this->assertFileExists($report); $output = file_get_contents($report); + $this->assertStringContainsString('Debug messages enabled', (string) $output); + $this->assertStringContainsString('Artifact information', (string) $output); + $this->assertStringContainsString('Mode: force-push', (string) $output); + $this->assertStringContainsString('Source repository: ' . $this->src, (string) $output); + $this->assertStringContainsString('Remote repository: ' . $this->dst, (string) $output); + $this->assertStringContainsString('Remote branch: ' . $this->currentBranch, (string) $output); + $this->assertStringContainsString('Gitignore file: No', (string) $output); + $this->assertStringContainsString('Will push: No', (string) $output); + $this->assertStringNotContainsString('Added changes:', (string) $output); + $this->assertStringContainsString('Artifact report', (string) $output); $this->assertStringContainsString(sprintf('Source repository: %s', $this->src), (string) $output); $this->assertStringContainsString(sprintf('Remote repository: %s', $this->dst), (string) $output); @@ -79,16 +129,6 @@ public function testReport(): void { $this->assertStringContainsString('Push result: Success', (string) $output); } - public function testDebug(): void { - $this->gitCreateFixtureCommits(1); - $output = $this->runBuild('--debug'); - - $this->assertStringContainsString('Debug messages enabled', $output); - - $this->assertStringContainsString('Cowardly refusing to push to remote. Use --push option to perform an actual push.', $output); - $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); - } - public function testDebugDisabled(): void { $this->gitCreateFixtureCommits(1); $output = $this->runBuild(); diff --git a/tests/phpunit/Traits/CommandTrait.php b/tests/phpunit/Traits/CommandTrait.php index 2efbba8..2674573 100644 --- a/tests/phpunit/Traits/CommandTrait.php +++ b/tests/phpunit/Traits/CommandTrait.php @@ -37,13 +37,6 @@ trait CommandTrait { */ protected $fs; - /** - * Flag to denote that debug information should be printed. - * - * @var bool - */ - protected $printDebug; - /** * Artifact git. */ @@ -58,12 +51,8 @@ trait CommandTrait { * Source path. * @param string $remote * Remote path. - * @param bool $printDebug - * Optional flag to print debug information when running commands. - * Defaults to FALSE. */ - protected function setUp(string $src, string $remote, bool $printDebug = FALSE): void { - $this->printDebug = $printDebug; + protected function setUp(string $src, string $remote): void { $this->fs = new Filesystem(); $this->src = $src; $this->gitInitRepo($this->src); @@ -511,17 +500,11 @@ public function runGitArtifactCommand(string $argsAndOptions, bool $expectFail = * If commands exists with non-zero status. */ protected function runCliCommand(string $command): array { - if ($this->printDebug) { - print '++ ' . $command . PHP_EOL; - } exec($command . ' 2>&1', $output, $code); if ($code !== 0) { throw new ErrorException(sprintf('Command "%s" exited with non-zero status', $command), $code, '', -1, new ErrorException(implode(PHP_EOL, $output), $code, '', -1)); } - if ($this->printDebug) { - print '++++ ' . implode(PHP_EOL, $output) . PHP_EOL; - } return $output; }