Skip to content

Commit 4114c95

Browse files
committed
Increased coverage.
Signed-off-by: Alex Skrypnyk <alex@drevops.com>
1 parent de9c46c commit 4114c95

File tree

9 files changed

+146
-89
lines changed

9 files changed

+146
-89
lines changed

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd"
33
bootstrap="vendor/autoload.php"
44
cacheDirectory=".logs/.phpunit.cache"
55
executionOrder="depends,defects"

src/Commands/ArtifactCommand.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
185185
$this->loggerInit((string) $this->getName(), $input, $output);
186186

187187
$remote = $input->getArgument('remote');
188-
if (empty($remote) || !is_string($remote)) {
188+
if (!is_string($remote) || empty(trim($remote))) {
189189
throw new \RuntimeException('Remote argument must be a non-empty string');
190190
}
191191

@@ -259,7 +259,9 @@ protected function doExecute(): void {
259259
catch (GitException $exception) {
260260
$result = $exception->getRunnerResult();
261261
if (!$result) {
262+
// @codeCoverageIgnoreStart
262263
throw new \Exception('Unknown error occurred', $exception->getCode(), $exception);
264+
// @codeCoverageIgnoreEnd
263265
}
264266

265267
$error = $result->getOutputAsString();
@@ -268,13 +270,13 @@ protected function doExecute(): void {
268270
}
269271
}
270272
catch (\Exception $exception) {
271-
// Capture message and allow to rollback.
273+
// Capture message to allow showing a report.
272274
$error = $exception->getMessage();
273275
}
274276

275277
$this->showReport(is_null($error));
276278

277-
if ($this->needCleanup) {
279+
if ($this->needCleanup && is_null($error)) {
278280
$this->logger->notice('Cleaning up');
279281
$this->repo->resetToPreviousCommit();
280282
$this->repo->restoreGitignoreToCustom();
@@ -310,8 +312,8 @@ protected function resolveOptions(string $url, array $options): void {
310312
}
311313

312314
$this->remoteUrl = $url;
313-
$this->remoteName = self::GIT_REMOTE_NAME;
314315
$this->now = empty($options['now']) ? time() : (int) $options['now'];
316+
$this->remoteName = sprintf('%s-%s', self::GIT_REMOTE_NAME, $this->now);
315317
$this->showChanges = !empty($options['show-changes']);
316318
$this->needCleanup = empty($options['no-cleanup']);
317319
$this->isDryRun = !empty($options['dry-run']);
@@ -343,7 +345,9 @@ protected function resolveOptions(string $url, array $options): void {
343345

344346
$contents = file_get_contents($gitignore);
345347
if (!$contents) {
348+
// @codeCoverageIgnoreStart
346349
throw new \Exception('Unable to load contents of ' . $gitignore);
350+
// @codeCoverageIgnoreEnd
347351
}
348352

349353
$this->logger->debug('-----Custom .gitignore---------');
@@ -436,11 +440,12 @@ protected function setMode(string $mode, array $options): void {
436440
* Check that there all requirements are met in order to to run this command.
437441
*/
438442
protected function checkRequirements(): void {
439-
// @todo Refactor this into more generic implementation.
440443
$this->logger->notice('Checking requirements');
441444

442445
if (!$this->fsIsCommandAvailable('git')) {
446+
// @codeCoverageIgnoreStart
443447
throw new \RuntimeException('Git command is not available');
448+
// @codeCoverageIgnoreEnd
444449
}
445450

446451
$this->logger->notice('All requirements were met');
@@ -472,7 +477,9 @@ protected function getTokenSafebranch(): string {
472477
$replacement = preg_replace('/[^a-z0-9-]/i', '-', strtolower($name));
473478

474479
if (empty($replacement)) {
480+
// @codeCoverageIgnoreStart
475481
throw new \Exception('Safe branch name is empty');
482+
// @codeCoverageIgnoreEnd
476483
}
477484

478485
return $replacement;

src/Traits/FilesystemTrait.php

Lines changed: 21 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -58,56 +58,16 @@ protected function fsSetRootDir(?string $path = NULL): static {
5858
* script was started from or current working directory.
5959
*/
6060
protected function fsGetRootDir(): string {
61-
if (isset($this->fsRootDir)) {
62-
return $this->fsRootDir;
63-
}
64-
65-
if (isset($_SERVER['PWD']) && is_string($_SERVER['PWD']) && !empty($_SERVER['PWD'])) {
66-
return $_SERVER['PWD'];
67-
}
68-
69-
return (string) getcwd();
70-
}
71-
72-
/**
73-
* Set current working directory.
74-
*
75-
* It is important to note that this should be called in pair with
76-
* cwdRestore().
77-
*
78-
* @param string $dir
79-
* Path to the current directory.
80-
*
81-
* @return static
82-
* The called object.
83-
*/
84-
protected function fsSetCwd(string $dir): static {
85-
chdir($dir);
86-
$this->fsOriginalCwdStack[] = $dir;
87-
88-
return $this;
89-
}
90-
91-
/**
92-
* Set current working directory to a previously saved path.
93-
*
94-
* It is important to note that this should be called in pair with cwdSet().
95-
*/
96-
protected function fsCwdRestore(): void {
97-
$dir = array_shift($this->fsOriginalCwdStack);
98-
if ($dir) {
99-
chdir($dir);
61+
if (!isset($this->fsRootDir)) {
62+
if (isset($_SERVER['PWD']) && is_string($_SERVER['PWD']) && !empty($_SERVER['PWD'])) {
63+
$this->fsRootDir = $_SERVER['PWD'];
64+
}
65+
else {
66+
$this->fsRootDir = (string) getcwd();
67+
}
10068
}
101-
}
10269

103-
/**
104-
* Get current working directory.
105-
*
106-
* @return string
107-
* Full path of current working directory.
108-
*/
109-
protected function fsCwdGet(): string {
110-
return (string) getcwd();
70+
return $this->fsRootDir;
11171
}
11272

11373
/**
@@ -139,14 +99,14 @@ protected function fsIsCommandAvailable(string $command): bool {
13999
*/
140100
protected function fsGetAbsolutePath(string $file, ?string $root = NULL): string {
141101
if ($this->fs->isAbsolutePath($file)) {
142-
return $this->fsRealpath($file);
102+
return static::fsRealpath($file);
143103
}
144104

145105
$root = $root ? $root : $this->fsGetRootDir();
146-
$root = $this->fsRealpath($root);
106+
$root = static::fsRealpath($root);
147107
$file = $root . DIRECTORY_SEPARATOR . $file;
148108

149-
return $this->fsRealpath($file);
109+
return static::fsRealpath($file);
150110
}
151111

152112
/**
@@ -192,16 +152,16 @@ protected function fsAssertPathsExist($paths, bool $strict = TRUE): bool {
192152
*
193153
* @see https://stackoverflow.com/a/29372360/712666
194154
*/
195-
protected function fsRealpath(string $path): string {
155+
protected static function fsRealpath(string $path): string {
196156
// Whether $path is unix or not.
197-
$unipath = $path === '' || $path[0] !== '/';
157+
$is_unix_path = $path === '' || $path[0] !== '/';
198158
$unc = str_starts_with($path, '\\\\');
199159

200160
// Attempt to detect if path is relative in which case, add cwd.
201-
if (!str_contains($path, ':') && $unipath && !$unc) {
161+
if (!str_contains($path, ':') && $is_unix_path && !$unc) {
202162
$path = getcwd() . DIRECTORY_SEPARATOR . $path;
203163
if ($path[0] === '/') {
204-
$unipath = FALSE;
164+
$is_unix_path = FALSE;
205165
}
206166
}
207167

@@ -225,21 +185,21 @@ protected function fsRealpath(string $path): string {
225185
}
226186

227187
$path = implode(DIRECTORY_SEPARATOR, $absolutes);
188+
// Put initial separator that could have been lost.
189+
$path = $is_unix_path ? $path : '/' . $path;
190+
$path = $unc ? '\\\\' . $path : $path;
228191

229192
// Resolve any symlinks.
230-
if (function_exists('readlink') && file_exists($path) && linkinfo($path) > 0) {
193+
if (function_exists('readlink') && file_exists($path) && is_link($path) > 0) {
231194
$path = readlink($path);
232195

233196
if (!$path) {
197+
// @codeCoverageIgnoreStart
234198
throw new \Exception(sprintf('Could not resolve symlink for path: %s', $path));
199+
// @codeCoverageIgnoreEnd
235200
}
236201
}
237202

238-
// Put initial separator that could have been lost.
239-
$path = $unipath ? $path : '/' . $path;
240-
241-
$path = $unc ? '\\\\' . $path : $path;
242-
243203
if (str_starts_with($path, sys_get_temp_dir())) {
244204
$tmp_realpath = realpath(sys_get_temp_dir());
245205
if ($tmp_realpath) {

tests/Functional/BranchModeTest.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,15 @@ public function testCleanupAfterSuccess(): void {
7979
}
8080

8181
public function testCleanupAfterFailure(): void {
82-
$this->gitCreateFixtureCommits(2);
83-
84-
$this->assertArtifactCommandSuccess();
85-
$this->gitAssertFixtureCommits(2, $this->dst, 'testbranch', ['Deployment commit']);
82+
$this->gitCreateFixtureCommits(1);
8683

87-
$this->gitCreateFixtureCommits(3, 2);
88-
// Trigger erroneous build by pushing to the same branch.
89-
$this->assertArtifactCommandFailure();
84+
$output = $this->assertArtifactCommandFailure([
85+
'--mode' => ArtifactCommand::MODE_BRANCH,
86+
'--branch' => '*invalid',
87+
]);
9088

91-
$this->gitAssertCurrentBranch($this->src, $this->currentBranch);
92-
$this->gitAssertRemoteNotExists($this->src, $this->remoteName);
89+
$this->assertStringContainsString('Incorrect value "*invalid" specified for git remote branch', $output);
90+
$this->gitAssertCurrentBranch($this->src, $this->gitGetGlobalDefaultBranch());
9391
}
9492

9593
public function testGitignore(): void {

tests/Functional/ForcePushModeTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ public function testCleanupAfterFailure(): void {
106106
$output = $this->assertArtifactCommandFailure(['--branch' => '*invalid']);
107107

108108
$this->assertStringContainsString('Incorrect value "*invalid" specified for git remote branch', $output);
109-
$this->gitAssertCurrentBranch($this->src, $this->currentBranch);
110-
$this->gitAssertRemoteNotExists($this->src, $this->remoteName);
109+
$this->gitAssertCurrentBranch($this->src, $this->gitGetGlobalDefaultBranch());
111110
}
112111

113112
public function testGitignore(): void {

tests/Functional/FunctionalTestBase.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,13 @@ abstract class FunctionalTestBase extends UnitTestBase {
2121

2222
/**
2323
* Fixture source repository directory.
24-
*
25-
* @var string
2624
*/
27-
protected $src;
25+
protected string $src;
2826

2927
/**
3028
* Fixture remote repository directory.
31-
*
32-
* @var string
3329
*/
34-
protected $dst;
30+
protected string $dst;
3531

3632
/**
3733
* Remote name.
@@ -92,7 +88,7 @@ protected function setUp(): void {
9288
* {@inheritdoc}
9389
*/
9490
protected function tearDown(): void {
95-
if ($this->fs->exists($this->fixtureDir)) {
91+
if ($this->fixtureDir && $this->fs->exists($this->fixtureDir)) {
9692
$this->fs->remove($this->fixtureDir);
9793
}
9894
}
@@ -168,15 +164,18 @@ protected function runArtifactCommand(?array $args = [], bool $expect_fail = FAL
168164
$input = [
169165
'--root' => $this->fixtureDir,
170166
'--now' => $this->now,
171-
'--src' => $this->src,
172167
'remote' => $this->dst,
173168
];
174169

175-
if ($this->mode) {
170+
if (!empty($this->src)) {
171+
$input['--src'] = $this->src;
172+
}
173+
174+
if (!empty($this->mode)) {
176175
$input['--mode'] = $this->mode;
177176
}
178177

179-
$input += $args;
178+
$input = $args + $input;
180179
}
181180

182181
return $this->consoleApplicationRun($input, [], $expect_fail);

tests/Functional/GeneralTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,39 @@ public function testShowChanges(): void {
6161
$this->assertFilesNotExist($this->dst, 'f1');
6262
}
6363

64+
public function testSrcDefault(): void {
65+
$this->gitCreateFixtureCommits(1);
66+
67+
$old_fixture_dir = $this->fixtureDir;
68+
$this->fixtureDir = $this->src;
69+
70+
$this->src = '';
71+
72+
$output = $this->runArtifactCommand(['--dry-run' => TRUE]);
73+
74+
$this->assertStringContainsString('Source repository: ' . $this->fixtureDir, $output);
75+
76+
$this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output);
77+
78+
$this->fixtureDir = $old_fixture_dir;
79+
}
80+
81+
public function testModeInvalid(): void {
82+
$this->gitCreateFixtureCommits(1);
83+
84+
$output = $this->assertArtifactCommandFailure(['--mode' => 'invalid']);
85+
86+
$this->assertStringContainsString('Invalid mode provided. Allowed modes are: force-push, branch', $output);
87+
}
88+
89+
public function testRemoteInvalid(): void {
90+
$this->gitCreateFixtureCommits(1);
91+
92+
$output = $this->assertArtifactCommandFailure(['remote' => 'git://user/repo']);
93+
94+
$this->assertStringContainsString('Invalid remote URL provided: git://user/repo', $output);
95+
}
96+
6497
public function testNoCleanup(): void {
6598
$this->gitCreateFixtureCommits(1);
6699

0 commit comments

Comments
 (0)