Skip to content

Commit

Permalink
Simplified files assertions.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Dec 9, 2024
1 parent 133a5d6 commit 1b5a2c7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 73 deletions.
32 changes: 32 additions & 0 deletions tests/phpunit/Functional/AbstractFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,36 @@ protected function runArtifactCommand(?array $args = [], bool $expect_fail = FAL
return $this->consoleApplicationRun($input, [], $expect_fail);
}

/**
* Assert that files exist.
*
* @param string $path
* Repository location.
* @param array<string>|string $files
* File or array of files.
*/
protected function assertFilesExist(string $path, array|string $files): void {
$files = is_array($files) ? $files : [$files];

foreach ($files as $file) {
$this->assertFileExists($path . DIRECTORY_SEPARATOR . $file);
}
}

/**
* Assert that files do not exist.
*
* @param string $path
* Repository location.
* @param array<string>|string $files
* File or array of files.
*/
protected function assertFilesNotExist(string $path, array|string $files): void {
$files = is_array($files) ? $files : [$files];

foreach ($files as $file) {
$this->assertFileDoesNotExist($path . DIRECTORY_SEPARATOR . $file);
}
}

}
46 changes: 23 additions & 23 deletions tests/phpunit/Functional/ForcePushModeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,26 @@ public function testSubRepos(): void {
$this->gitInitRepo($this->src . DIRECTORY_SEPARATOR . 'r3/r31/r311');
$this->fixtureCreateFile($this->src, 'r3/r31/r311/c');

$this->gitAssertFilesExist($this->src, ['r1/c']);
$this->gitAssertFilesNotExist($this->src, ['r1/.git/index']);
$this->gitAssertFilesNotExist($this->src, ['r2/r21.git/index']);
$this->gitAssertFilesNotExist($this->src, ['r3/r31/r311/.git/index']);
$this->assertFilesExist($this->src, ['r1/c']);
$this->assertFilesNotExist($this->src, ['r1/.git/index']);
$this->assertFilesNotExist($this->src, ['r2/r21.git/index']);
$this->assertFilesNotExist($this->src, ['r3/r31/r311/.git/index']);

$output = $this->assertArtifactCommandSuccess(['-vvv' => TRUE]);
$this->assertStringContainsString(sprintf('Removing sub-repository "%s"', $this->fsGetAbsolutePath($this->src . DIRECTORY_SEPARATOR . 'r1/.git')), $output);
$this->assertStringContainsString(sprintf('Removing sub-repository "%s"', $this->fsGetAbsolutePath($this->src . DIRECTORY_SEPARATOR . 'r2/r21/.git')), $output);
$this->assertStringContainsString(sprintf('Removing sub-repository "%s"', $this->fsGetAbsolutePath($this->src . DIRECTORY_SEPARATOR . 'r3/r31/r311/.git')), $output);
$this->gitAssertFixtureCommits(2, $this->dst, 'testbranch', ['Commit number 3', 'Deployment commit']);

$this->gitAssertFilesExist($this->dst, ['r1/c']);
$this->gitAssertFilesExist($this->dst, ['r2/r21/c']);
$this->gitAssertFilesExist($this->dst, ['r3/r31/r311/c']);
$this->gitAssertFilesNotExist($this->dst, ['r1/.git/index']);
$this->gitAssertFilesNotExist($this->dst, ['r1/.git']);
$this->gitAssertFilesNotExist($this->dst, ['r2/r21/.git/index']);
$this->gitAssertFilesNotExist($this->dst, ['r2/r21/.git']);
$this->gitAssertFilesNotExist($this->dst, ['r3/r31/311/.git/index']);
$this->gitAssertFilesNotExist($this->dst, ['r3/r31/311/.git']);
$this->assertFilesExist($this->dst, ['r1/c']);
$this->assertFilesExist($this->dst, ['r2/r21/c']);
$this->assertFilesExist($this->dst, ['r3/r31/r311/c']);
$this->assertFilesNotExist($this->dst, ['r1/.git/index']);
$this->assertFilesNotExist($this->dst, ['r1/.git']);
$this->assertFilesNotExist($this->dst, ['r2/r21/.git/index']);
$this->assertFilesNotExist($this->dst, ['r2/r21/.git']);
$this->assertFilesNotExist($this->dst, ['r3/r31/311/.git/index']);
$this->assertFilesNotExist($this->dst, ['r3/r31/311/.git']);
}

public function testCleanupAfterSuccess(): void {
Expand Down Expand Up @@ -120,7 +120,7 @@ public function testGitignore(): void {
$this->assertArtifactCommandSuccess();

$this->gitAssertFixtureCommits(2, $this->dst, 'testbranch', ['Deployment commit']);
$this->gitAssertFilesNotExist($this->dst, 'f3');
$this->assertFilesNotExist($this->dst, 'f3');

// Now, remove the .gitignore and push again.
$this->fixtureRemoveFile($this->src, '.gitignore');
Expand All @@ -139,8 +139,8 @@ public function testGitignoreCustom(): void {
$this->assertArtifactCommandSuccess(['--gitignore' => $this->src . DIRECTORY_SEPARATOR . 'mygitignore']);

$this->gitAssertFixtureCommits(2, $this->dst, 'testbranch', ['Deployment commit']);
$this->gitAssertFilesNotExist($this->dst, 'uic');
$this->gitAssertFilesExist($this->dst, 'uc');
$this->assertFilesNotExist($this->dst, 'uic');
$this->assertFilesExist($this->dst, 'uc');

// Now, remove the .gitignore and push again.
// We have to create 'uic' file since it was rightfully
Expand All @@ -154,7 +154,7 @@ public function testGitignoreCustom(): void {

$this->gitAssertFixtureCommits(3, $this->dst, 'testbranch', ['Deployment commit'], FALSE);
$this->gitAssertFilesCommitted($this->dst, ['f1', 'f2', 'uic'], 'testbranch');
$this->gitAssertFilesExist($this->dst, ['f1', 'f2', 'uic'], 'testbranch');
$this->assertFilesExist($this->dst, ['f1', 'f2', 'uic']);
$this->gitAssertFilesNotCommitted($this->dst, ['uc'], 'testbranch');
}

Expand All @@ -179,8 +179,8 @@ public function testGitignoreCustomRemoveCommittedFiles(): void {
$this->gitAssertFixtureCommits(2, $this->dst, 'testbranch', ['Custom third commit', 'Deployment commit'], FALSE);
$this->gitAssertFilesCommitted($this->dst, ['.gitignore', 'f2', 'ic', 'd/cc', 'uc'], 'testbranch');
$this->gitAssertFilesNotCommitted($this->dst, ['f1', 'ii', 'd/ci', 'ui'], 'testbranch');
$this->gitAssertFilesExist($this->dst, ['f2', 'ic', 'd/cc', 'uc'], 'testbranch');
$this->gitAssertFilesNotExist($this->dst, ['f1', 'ii', 'd/ci', 'ui'], 'testbranch');
$this->assertFilesExist($this->dst, ['f2', 'ic', 'd/cc', 'uc']);
$this->assertFilesNotExist($this->dst, ['f1', 'ii', 'd/ci', 'ui']);
}

public function testGitignoreCustomAllowlisting(): void {
Expand Down Expand Up @@ -254,17 +254,17 @@ public function testGitignoreCustomAllowlisting(): void {
'vendor_cc', 'dir_other/vendor/ve_cc', 'vendor_com with space com.txt',
], 'testbranch');

$this->gitAssertFilesExist($this->dst, [
$this->assertFilesExist($this->dst, [
'f2', 'ic', 'cc', 'uc',
'd_cc/sub_cc', 'd_ic/sub_ic', 'd_uc/sub_uc',
'vendor/ve_ii',
], 'testbranch');
$this->gitAssertFilesNotExist($this->dst, [
]);
$this->assertFilesNotExist($this->dst, [
'f1', 'ii', 'ci', 'ui', 'ud',
'd_ci/sub_ci',
'd_ii/sub_ii', 'd_ui/sub_ui', 'd_ud/sub_ud',
'vendor_cc', 'dir_other/vendor/ve_cc', 'vendor_com with space com.txt',
], 'testbranch');
]);
}

public function testBuildTag(): void {
Expand Down
14 changes: 9 additions & 5 deletions tests/phpunit/Functional/GeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public function testInfo(): void {

$this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output);

$this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch);
$this->gitCheckout($this->dst, $this->currentBranch);
$this->assertFilesNotExist($this->dst, 'f1');
}

public function testShowChanges(): void {
Expand All @@ -56,7 +57,8 @@ public function testShowChanges(): void {
$this->assertStringContainsString('Added changes:', $output);

$this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output);
$this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch);
$this->gitCheckout($this->dst, $this->currentBranch);
$this->assertFilesNotExist($this->dst, 'f1');
}

public function testNoCleanup(): void {
Expand All @@ -69,7 +71,7 @@ public function testNoCleanup(): void {

$this->gitAssertCurrentBranch($this->src, $this->artifactBranch);
$this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output);
$this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch);
$this->assertFilesNotExist($this->dst, 'f1');
}

public function testDebug(): void {
Expand Down Expand Up @@ -97,7 +99,8 @@ public function testDebug(): void {
$this->assertStringContainsString('Push result: Success', $output);

$this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output);
$this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch);
$this->gitCheckout($this->dst, $this->currentBranch);
$this->assertFilesNotExist($this->dst, 'f1');
}

public function testDebugLogFile(): void {
Expand Down Expand Up @@ -154,7 +157,8 @@ public function testDebugDisabled(): void {
$this->assertStringNotContainsString('Debug messages enabled', $output);

$this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output);
$this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch);
$this->gitCheckout($this->dst, $this->currentBranch);
$this->assertFilesNotExist($this->dst, 'f1');
}

}
46 changes: 1 addition & 45 deletions tests/phpunit/Traits/GitTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,51 +275,7 @@ protected function gitAssertFixtureCommits(int $count, string $path, string $bra
$this->assertEquals($expected_commits, $commits, 'All fixture commits are present');

if ($should_assert_files) {
$this->gitAssertFilesExist($this->dst, $expected_files, $branch);
}
}

/**
* Assert that files exist in repository in specified branch.
*
* @param string $path
* Repository location.
* @param array<string>|string $files
* File or array of files.
* @param string|null $branch
* Optional branch. If set, will be checked out before assertion.
*/
protected function gitAssertFilesExist(string $path, array|string $files, ?string $branch = NULL): void {
$files = is_array($files) ? $files : [$files];

if ($branch) {
$this->gitCheckout($path, $branch);
}

foreach ($files as $file) {
$this->assertFileExists($path . DIRECTORY_SEPARATOR . $file);
}
}

/**
* Assert that files do not exist in repository in specified branch.
*
* @param string $path
* Repository location.
* @param array<string>|string $files
* File or array of files.
* @param string|null $branch
* Optional branch. If set, will be checked out before assertion.
*/
protected function gitAssertFilesNotExist(string $path, array|string $files, ?string $branch = NULL): void {
$files = is_array($files) ? $files : [$files];

if ($branch) {
$this->gitCheckout($path, $branch);
}

foreach ($files as $file) {
$this->assertFileDoesNotExist($path . DIRECTORY_SEPARATOR . $file);
$this->assertFilesExist($this->dst, $expected_files);
}
}

Expand Down

0 comments on commit 1b5a2c7

Please sign in to comment.