Skip to content

Commit

Permalink
Add option to keep exit code in build command (massiveart#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
mamazu authored Oct 19, 2020
1 parent ba39415 commit 7a9220f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Build/BuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function getDependencies();

/**
* Execute the build logic
*
* @return int|null
*/
public function build();

Expand Down
16 changes: 13 additions & 3 deletions Command/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function configure()

$this->addArgument('target', InputArgument::OPTIONAL, 'Target to build', null);
$this->addOption('nodeps', 'D', InputOption::VALUE_NONE, 'Ignore dependencies');
$this->addOption('keep-exit-code', '-k', InputOption::VALUE_NONE, 'Keep the exit code of a job if it fails');
}

/**
Expand Down Expand Up @@ -114,17 +115,20 @@ public function execute(InputInterface $input, OutputInterface $output)
if (!$this->question->ask($input, $output, $question)) {
$this->output->writeln('Bye!');

return;
return 0;
}
}

$this->output->writeln('');
$this->runBuilders($builders);
$exitCode = $this->runBuilders($builders);

$end = microtime(true);

$this->output->writeln(sprintf('<info>Done (%ss)</info>', number_format($end - $start, 2)));

if($exitCode !== 0 && $input->getOption('keep-exit-code')) {
return $exitCode;
}
return 0;
}

Expand Down Expand Up @@ -172,6 +176,7 @@ protected function runBuilders($builders)

$builderContext = new BuilderContext($this->input, $this->output, $this->getApplication());

$combinedExitCode = 0;
foreach ($builders as $builder) {
$this->output->getFormatter()->setIndentLevel(0);

Expand All @@ -187,8 +192,13 @@ protected function runBuilders($builders)
$this->output->writeln('');

$this->output->getFormatter()->setIndentLevel(1);
$builder->build();
$exitCode = $builder->build();
if($exitCode !== null && $exitCode !== 0) {
$combinedExitCode = $exitCode;
}
}

return $combinedExitCode;
}

protected function writeTitle($title)
Expand Down
20 changes: 20 additions & 0 deletions Tests/Command/BuildCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ public function testBuildTarget()
$this->assertEquals(0, $res);
}

public function testBuildTargetThatFails()
{
$this->buildRegistry->getBuilders('Builder 1')->willReturn(array(
$this->builder1->reveal(),
$this->builder2->reveal()
));

$this->builder2->setContainer($this->container)->shouldBeCalled();
$this->builder1->setContext(Argument::type('Massive\Bundle\BuildBundle\Build\BuilderContext'))
->shouldBeCalled();
$this->builder2->setContext(Argument::type('Massive\Bundle\BuildBundle\Build\BuilderContext'))
->shouldBeCalled();

$this->builder1->build()->shouldBeCalled()->willReturn(1);
$this->builder2->build()->shouldBeCalled();

$exitCode = $this->execute(array('target' => 'Builder 1', '--keep-exit-code' => true), array());
$this->assertEquals(1, $exitCode);
}

public function testBuildNotTarget()
{
$this->buildRegistry->getBuilders(null)->willReturn(array(
Expand Down

0 comments on commit 7a9220f

Please sign in to comment.