Skip to content

Commit 07ff70c

Browse files
committed
WIP Adds new commands integration tests
1 parent ad06441 commit 07ff70c

File tree

7 files changed

+209
-7
lines changed

7 files changed

+209
-7
lines changed

src/Commands/CreateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6969
return self::FAILURE;
7070
}
7171

72-
$output->writeln(".gitattributes file has been created at {$directory}");
72+
$output->writeln("A .gitattributes file has been created at {$directory}.");
7373

7474
return self::SUCCESS;
7575
}

src/Commands/UpdateCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4848
$gitattributesPath = $this->analyser->getGitattributesFilePath();
4949

5050
if (!\file_exists($gitattributesPath)) {
51-
$output->writeln('No .gitattributes file found. Use the create command to create one first.');
51+
$output->writeln('No .gitattributes file found. Use the <info>create</info> command to create one first.');
5252

5353
return self::FAILURE;
5454
}
@@ -69,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6969
return self::FAILURE;
7070
}
7171

72-
$output->writeln(".gitattributes file has been updated at {$directory}");
72+
$output->writeln("The .gitattributes file at {$directory} has been updated.");
7373

7474
return self::SUCCESS;
7575
}

src/Commands/ValidateCommand.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
286286

287287
$createGitattributesFile = $input->getOption('create');
288288
$overwriteGitattributesFile = $input->getOption('overwrite');
289+
290+
if ($input->hasOption('create') && (bool) $input->getOption('create')) {
291+
$output->writeln('<comment>The --create option is deprecated. Please use the dedicated <info>create</info> command.</comment>');
292+
}
293+
if ($input->hasOption('overwrite') && (bool) $input->getOption('overwrite')) {
294+
$output->writeln('<comment>The --overwrite option is deprecated. Please use the dedicated <info>update</info> command.</comment>');
295+
}
296+
289297
$validateArchive = $input->getOption('validate-git-archive');
290298
$globPattern = $input->getOption('glob-pattern');
291299
$globPatternFile = (string) $input->getOption('glob-pattern-file');
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stolt\LeanPackage\Tests\Commands;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
use Stolt\LeanPackage\Analyser;
9+
use Stolt\LeanPackage\Commands\CreateCommand;
10+
use Stolt\LeanPackage\GitattributesFileRepository;
11+
use Stolt\LeanPackage\Presets\Finder;
12+
use Stolt\LeanPackage\Presets\PhpPreset;
13+
use Stolt\LeanPackage\Tests\TestCase;
14+
use Zenstruck\Console\Test\TestCommand;
15+
16+
final class CreateCommandTest extends TestCase
17+
{
18+
protected function setUp(): void
19+
{
20+
$this->setUpTemporaryDirectory();
21+
}
22+
23+
protected function tearDown(): void
24+
{
25+
$this->removeDirectory($this->temporaryDirectory);
26+
}
27+
28+
#[Test]
29+
public function createsNewGitattributesFileWithHeaderAndExpectedContent(): void
30+
{
31+
$analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory);
32+
$repository = new GitattributesFileRepository($analyser);
33+
$command = new CreateCommand($analyser, $repository);
34+
35+
$artifactFilenames = ['README.md', '.gitignore'];
36+
37+
$this->createTemporaryFiles(
38+
$artifactFilenames,
39+
['tests']
40+
);
41+
42+
TestCommand::for($command)
43+
->addArgument($this->temporaryDirectory)
44+
->execute()
45+
->assertSuccessful()
46+
->assertOutputContains("A .gitattributes file has been created at {$this->temporaryDirectory}.");
47+
48+
$gitattributesPath = $this->temporaryDirectory . DIRECTORY_SEPARATOR . '.gitattributes';
49+
$this->assertFileExists($gitattributesPath);
50+
51+
$content = (string) \file_get_contents($gitattributesPath);
52+
53+
// Header should be present (created files use "generated" header)
54+
$this->assertStringContainsString(
55+
'# This file was generated by the lean package validator (http://git.io/lean-package-validator).',
56+
$content
57+
);
58+
59+
// Sanity check: expected export-ignores likely include .gitattributes itself and tests/
60+
$this->assertStringContainsString('.gitattributes', $content);
61+
$this->assertStringContainsString('tests/', $content);
62+
$this->assertStringContainsString('export-ignore', $content);
63+
}
64+
65+
#[Test]
66+
public function failsIfGitattributesAlreadyExists(): void
67+
{
68+
$gitattributesContent = <<<CONTENT
69+
* text=auto eol=lf
70+
71+
phpspec.yml.dist export-ignore
72+
specs/ export-ignore
73+
CONTENT;
74+
75+
$this->createTemporaryGitattributesFile($gitattributesContent);
76+
77+
$analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory);
78+
$repository = new GitattributesFileRepository($analyser);
79+
$command = new CreateCommand($analyser, $repository);
80+
81+
TestCommand::for($command)
82+
->addArgument($this->temporaryDirectory)
83+
->execute()
84+
->assertFaulty()
85+
->assertOutputContains('A .gitattributes file already exists. Use the update command to modify it.');
86+
}
87+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stolt\LeanPackage\Tests\Commands;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
use Stolt\LeanPackage\Analyser;
9+
use Stolt\LeanPackage\Commands\UpdateCommand;
10+
use Stolt\LeanPackage\GitattributesFileRepository;
11+
use Stolt\LeanPackage\Presets\Finder;
12+
use Stolt\LeanPackage\Presets\PhpPreset;
13+
use Stolt\LeanPackage\Tests\TestCase;
14+
use Zenstruck\Console\Test\TestCommand;
15+
16+
final class UpdateCommandTest extends TestCase
17+
{
18+
protected function setUp(): void
19+
{
20+
$this->setUpTemporaryDirectory();
21+
}
22+
23+
protected function tearDown(): void
24+
{
25+
$this->removeDirectory($this->temporaryDirectory);
26+
}
27+
28+
#[Test]
29+
public function updatesExistingGitattributesAndReplacesHeader(): void
30+
{
31+
$analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory);
32+
$repository = new GitattributesFileRepository($analyser);
33+
$command = new UpdateCommand($analyser, $repository);
34+
35+
$artifactFilenames = ['.gitignore'];
36+
37+
$this->createTemporaryFiles(
38+
$artifactFilenames,
39+
['tests', '.github']
40+
);
41+
42+
$gitattributesContent = <<<CONTENT
43+
# This file was generated by the lean package validator (http://git.io/lean-package-validator).
44+
45+
* text=auto eol=lf
46+
47+
.gitattributes export-ignore
48+
.github/ export-ignore
49+
tests/ export-ignore
50+
CONTENT;
51+
52+
$this->createTemporaryGitattributesFile($gitattributesContent);
53+
54+
TestCommand::for($command)
55+
->addArgument($this->temporaryDirectory)
56+
->execute()
57+
->assertSuccessful()
58+
->assertOutputContains("The .gitattributes file at {$this->temporaryDirectory} has been updated.");
59+
60+
$gitattributesPath = $this->temporaryDirectory . DIRECTORY_SEPARATOR . '.gitattributes';
61+
$this->assertFileExists($gitattributesPath);
62+
63+
$content = (string) \file_get_contents($gitattributesPath);
64+
65+
// Header should be the "modified" one after update
66+
$this->assertStringContainsString(
67+
'# This file was partly modified by the lean package validator (http://git.io/lean-package-validator).',
68+
$content
69+
);
70+
71+
// Sanity: should still contain export-ignore entries and likely 'tests/' gets added
72+
$this->assertStringContainsString('.gitattributes', $content);
73+
$this->assertStringContainsString('.github/', $content);
74+
$this->assertStringContainsString('tests/', $content);
75+
$this->assertStringContainsString('export-ignore', $content);
76+
}
77+
78+
#[Test]
79+
public function failsWhenNoGitattributesFileIsPresent(): void
80+
{
81+
// Remove the file
82+
@\unlink($this->temporaryDirectory . DIRECTORY_SEPARATOR . '.gitattributes');
83+
84+
$analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory);
85+
$repository = new GitattributesFileRepository($analyser);
86+
$command = new UpdateCommand($analyser, $repository);
87+
88+
TestCommand::for($command)
89+
->addArgument($this->temporaryDirectory)
90+
->execute()
91+
->assertFaulty()
92+
->assertOutputContains('No .gitattributes file found. Use the create command to create one first.');
93+
}
94+
}

tests/Commands/ValidateCommandTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ public function archiveWithLicenseFileIsConsideredValid(): void
783783
}
784784

785785
#[Test]
786+
#[RunInSeparateProcess]
786787
public function failingGitattributesFilesCreationReturnsExpectedStatusCode(): void
787788
{
788789
$artifactFilenames = ['CONDUCT.md'];
@@ -809,6 +810,7 @@ public function failingGitattributesFilesCreationReturnsExpectedStatusCode(): vo
809810
]);
810811

811812
$expectedDisplay = <<<CONTENT
813+
The --create option is deprecated. Please use the dedicated create command.
812814
Warning: There is no .gitattributes file present in {$this->temporaryDirectory}.
813815
814816
Creation of .gitattributes file failed.
@@ -841,6 +843,7 @@ public function validateOnNonExistentGitattributesFilesWithCreationOptionCreates
841843
]);
842844

843845
$expectedDisplay = <<<CONTENT
846+
The --create option is deprecated. Please use the dedicated create command.
844847
Warning: There is no .gitattributes file present in {$this->temporaryDirectory}.
845848
846849
Created a .gitattributes file with the shown content:
@@ -879,6 +882,7 @@ public function validateOnNonExistentGitattributesFilesWithCreationOptionCreates
879882
]);
880883

881884
$expectedDisplay = <<<CONTENT
885+
The --create option is deprecated. Please use the dedicated create command.
882886
Warning: There is no .gitattributes file present in {$this->temporaryDirectory}.
883887
884888
Created a .gitattributes file with the shown content:
@@ -924,6 +928,7 @@ public function validateOnNonExistentGitattributesFilesWithCreationOptionCreates
924928
]);
925929

926930
$expectedDisplay = <<<CONTENT
931+
The --create option is deprecated. Please use the dedicated create command.
927932
Warning: There is no .gitattributes file present in {$this->temporaryDirectory}.
928933
929934
Created a .gitattributes file with the shown content:
@@ -1153,6 +1158,7 @@ public function overwriteOptionOnNonExistentGitattributesFileImplicatesCreate():
11531158
]);
11541159

11551160
$expectedDisplay = <<<CONTENT
1161+
The --overwrite option is deprecated. Please use the dedicated update command.
11561162
Warning: There is no .gitattributes file present in {$this->temporaryDirectory}.
11571163
11581164
Created a .gitattributes file with the shown content:
@@ -1414,6 +1420,7 @@ public function incompleteGitattributesFileIsOverwrittenWithAlignment(): void
14141420
]);
14151421

14161422
$expectedDisplay = <<<CONTENT
1423+
The --overwrite option is deprecated. Please use the dedicated update command.
14171424
The present .gitattributes file is considered invalid.
14181425
14191426
Overwrote it with the shown content:
@@ -1619,7 +1626,10 @@ public function incompleteGitattributesFileIsOverwritten(string $option): void
16191626
'--omit-header' => true,
16201627
]);
16211628

1629+
$dedicatedCommand = $option === '--create' ? 'create' : 'update';
1630+
16221631
$expectedDisplay = <<<CONTENT
1632+
The $option option is deprecated. Please use the dedicated $dedicatedCommand command.
16231633
The present .gitattributes file is considered invalid.
16241634
16251635
Overwrote it with the shown content:
@@ -1641,6 +1651,7 @@ public function incompleteGitattributesFileIsOverwritten(string $option): void
16411651

16421652
#[Test]
16431653
#[Ticket('https://github.com/raphaelstolt/lean-package-validator/issues/8')]
1654+
#[RunInSeparateProcess]
16441655
public function failingGitattributesFilesOverwriteReturnsExpectedStatusCode(): void
16451656
{
16461657
$gitattributesContent = <<<CONTENT
@@ -1676,6 +1687,7 @@ public function failingGitattributesFilesOverwriteReturnsExpectedStatusCode(): v
16761687
]);
16771688

16781689
$expectedDisplay = <<<CONTENT
1690+
The --create option is deprecated. Please use the dedicated create command.
16791691
The present .gitattributes file is considered invalid.
16801692
16811693
Overwrite of .gitattributes file failed.

tests/TestCase.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class TestCase extends PHPUnit
1919
* @var Application
2020
*/
2121
protected Application $application;
22+
2223
protected string $temporaryDirectory;
2324

2425
/**
@@ -28,13 +29,13 @@ class TestCase extends PHPUnit
2829
*/
2930
protected function setUpTemporaryDirectory(): void
3031
{
32+
$this->temporaryDirectory = \sys_get_temp_dir()
33+
. DIRECTORY_SEPARATOR
34+
. 'lpv';
35+
3136
if ((new OsHelper())->isWindows() === false) {
3237
\ini_set('sys_temp_dir', '/tmp/lpv');
3338
$this->temporaryDirectory = '/tmp/lpv';
34-
} else {
35-
$this->temporaryDirectory = \sys_get_temp_dir()
36-
. DIRECTORY_SEPARATOR
37-
. 'lpv';
3839
}
3940

4041
if (!\file_exists($this->temporaryDirectory)) {

0 commit comments

Comments
 (0)