Skip to content

Commit

Permalink
[#4] Added artefact report file + tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Oct 28, 2017
1 parent 6d1f7ba commit 4c92ac0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/ArtefactTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ trait ArtefactTrait
*/
protected $now;

/**
* Path to report file.
*
* @var string
*/
protected $report;

/**
* Artefact build result.
*
* @var bool
*/
protected $result;

/**
* Artefact constructor.
*/
Expand All @@ -82,6 +96,7 @@ public function __construct()
* @option $message Commit message with optional tokens.
* @option $gitignore Path to gitignore file to replace current .gitignore.
* @option $push Push artefact to the remote repository. Defaults to FALSE.
* @option $report Path to the report file.
*/
public function artefact($remote, array $opts = [
'root' => InputOption::VALUE_REQUIRED,
Expand All @@ -91,6 +106,7 @@ public function artefact($remote, array $opts = [
'gitignore' => InputOption::VALUE_REQUIRED,
'push' => false,
'now' => InputOption::VALUE_REQUIRED,
'report' => InputOption::VALUE_REQUIRED,
])
{
$this->checkRequirements();
Expand Down Expand Up @@ -135,11 +151,16 @@ protected function doPush()
$this->say(sprintf('Added changes: %s', $result->getMessage()));

$result = $this->gitPush($this->gitGetSrcRepo(), 'dst', $this->branch);
if ($result->wasSuccessful()) {
$this->result = $result->wasSuccessful();
if ($this->result) {
$this->sayOkay(sprintf('Pushed branch "%s" with commit message "%s"', $this->branch, $this->message));
} else {
$this->say(sprintf('Error occurred while pushing branch "%s" with commit message "%s"', $this->branch, $this->message));
}

if ($this->report) {
$this->dumpReport();
}
}

/**
Expand Down Expand Up @@ -171,6 +192,8 @@ protected function resolveOptions(array $options)
}

$this->needsPush = !empty($options['push']);

$this->report = !empty($options['report']) ? $options['report'] : null;
}

/**
Expand All @@ -181,6 +204,7 @@ protected function showInfo()
$this->writeln('----------------------------------------------------------------------');
$this->writeln(' Artefact information');
$this->writeln('----------------------------------------------------------------------');
$this->writeln(' Build timestamp: '.date('Y/m/d H:i:s', $this->now));
$this->writeln(' Source repository: '.$this->gitGetSrcRepo());
$this->writeln(' Remote repository: '.$this->gitGetRemoteRepo());
$this->writeln(' Remote branch: '.$this->branch);
Expand All @@ -189,6 +213,26 @@ protected function showInfo()
$this->writeln('----------------------------------------------------------------------');
}

/**
* Dump artefact report to a file.
*/
protected function dumpReport()
{
$lines[] = '----------------------------------------------------------------------';
$lines[] = ' Artefact report';
$lines[] = '----------------------------------------------------------------------';
$lines[] = ' Build timestamp: '.date('Y/m/d H:i:s', $this->now);
$lines[] = ' Source repository: '.$this->gitGetSrcRepo();
$lines[] = ' Remote repository: '.$this->gitGetRemoteRepo();
$lines[] = ' Remote branch: '.$this->branch;
$lines[] = ' Gitignore file: '.($this->gitignoreFile ? $this->gitignoreFile : 'No');
$lines[] = ' Commit message: '.$this->message;
$lines[] = ' Push result: '.($this->result ? 'Success' : 'Failure');
$lines[] = '----------------------------------------------------------------------';

$this->fsFileSystem->dumpFile($this->report, implode(PHP_EOL, $lines));
}

/**
* Set the branch of the remote repository.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/ArtefactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,22 @@ public function testPushMultipleTags()
'1.txt',
], $remoteBranch);
}

public function testReport()
{
$report = $this->getFixtureSrcDir().DIRECTORY_SEPARATOR.'report.txt';

$this->gitCreateFixtureCommits(1, $this->getFixtureSrcDir());
$this->runRoboCommand(sprintf('artefact --src=%s --push %s --report=%s', $this->getFixtureSrcDir(), $this->getFixtureRemoteDir(), $report));

$this->assertFileExists($report);
$output = file_get_contents($report);

$this->assertContains('Artefact report', $output);
$this->assertContains(sprintf('Source repository: %s', $this->getFixtureSrcDir()), $output);
$this->assertContains(sprintf('Remote repository: %s', $this->getFixtureRemoteDir()), $output);
$this->assertContains(sprintf('Remote branch: %s', $this->defaultCurrentBranch), $output);
$this->assertContains('Gitignore file: No', $output);
$this->assertContains('Push result: Success', $output);
}
}

0 comments on commit 4c92ac0

Please sign in to comment.