Skip to content

Commit 73d82ee

Browse files
committed
Adds a dry-run option
1 parent f8df70c commit 73d82ee

File tree

5 files changed

+89
-5
lines changed

5 files changed

+89
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,11 @@ cat .gitattributes | lean-package-validator validate --stdin-input
201201
The `create` command will create a `.gitattributes` file in the given directory. This command replaces the `--create`
202202
option of the `validate` command. Please migrate to the dedicated commands.
203203

204-
205204
#### Update command
206205

207206
The `update` command will update a present `.gitattributes` file in the given directory. This command replaces the `--overwrite`
208-
option of the `validate` command. Please migrate to the dedicated commands.
207+
option of the `validate` command. Please migrate to the dedicated commands. Like the above-mentioned `create` command it
208+
provides a `--dry-run` option to see what the `.gitattributes` content would look like.
209209

210210
#### Init command
211211

src/Commands/CreateCommand.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ protected function configure(): void
4747
$this->addGenerationOptions(function (...$args) {
4848
$this->getDefinition()->addOption(new InputOption(...$args));
4949
});
50+
$this->getDefinition()->addOption(new InputOption(
51+
'dry-run',
52+
null,
53+
InputOption::VALUE_NONE,
54+
'Do not write any files. Output the expected .gitattributes content'
55+
));
5056
}
5157

5258
protected function execute(InputInterface $input, OutputInterface $output): int
@@ -61,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6167

6268
$gitattributesPath = $this->analyser->getGitattributesFilePath();
6369

64-
if (\file_exists($gitattributesPath)) {
70+
if (\file_exists($gitattributesPath) && $input->getOption('dry-run') !== true) {
6571
$output->writeln('A .gitattributes file already exists. Use the update command to modify it.');
6672

6773
return self::FAILURE;
@@ -75,6 +81,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7581
return self::FAILURE;
7682
}
7783

84+
// Support dry-run: print expected content and exit successfully without writing.
85+
if ($input->getOption('dry-run') === true) {
86+
$output->writeln($expected);
87+
88+
return self::SUCCESS;
89+
}
90+
7891
try {
7992
$this->repository->createGitattributesFile($expected);
8093
} catch (\Throwable $e) {

src/Commands/UpdateCommand.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ protected function configure(): void
4848
$this->getDefinition()->addOption(new InputOption(...$args));
4949
});
5050

51+
// Add dry-run option
52+
$this->getDefinition()->addOption(new InputOption(
53+
'dry-run',
54+
null,
55+
InputOption::VALUE_NONE,
56+
'Do not write any files. Output the expected .gitattributes content'
57+
));
5158
}
5259

5360
protected function execute(InputInterface $input, OutputInterface $output): int
@@ -62,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6269

6370
$gitattributesPath = $this->analyser->getGitattributesFilePath();
6471

65-
if (!\file_exists($gitattributesPath)) {
72+
if (!\file_exists($gitattributesPath) && $input->getOption('dry-run') !== true) {
6673
$output->writeln('No .gitattributes file found. Use the <info>create</info> command to create one first.');
6774

6875
return self::FAILURE;
@@ -76,6 +83,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7683
return self::FAILURE;
7784
}
7885

86+
// Support dry-run: print expected content and exit successfully without writing.
87+
if ($input->getOption('dry-run') === true) {
88+
$output->writeln($expected);
89+
90+
return self::SUCCESS;
91+
}
92+
7993
try {
8094
$this->repository->overwriteGitattributesFile($expected);
8195
} catch (\Throwable $e) {

tests/Commands/CreateCommandTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,34 @@ public function createsNewGitattributesFileWithHeaderAndExpectedContent(): void
6767
$this->assertStringContainsString('export-ignore', $content);
6868
}
6969

70+
#[Test]
71+
public function printsExpectedContentWithoutWritingAFile(): void
72+
{
73+
$analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory);
74+
$repository = new GitattributesFileRepository($analyser);
75+
$command = new CreateCommand($analyser, $repository);
76+
77+
$artifactFilenames = ['README.md', '.gitignore'];
78+
79+
$this->createTemporaryFiles(
80+
$artifactFilenames,
81+
['tests']
82+
);
83+
84+
$result = TestCommand::for($command)
85+
->addArgument($this->temporaryDirectory)
86+
->addOption('dry-run')
87+
->execute()
88+
->assertSuccessful();
89+
90+
$output = $result->output();
91+
92+
$this->assertStringContainsString('export-ignore', $output);
93+
$this->assertStringNotContainsString('has been created', $output);
94+
95+
$this->assertFileDoesNotExist($this->temporaryDirectory . DIRECTORY_SEPARATOR . '.gitattributes');
96+
}
97+
7098
#[Test]
7199
public function failsIfGitattributesAlreadyExists(): void
72100
{

tests/Commands/UpdateCommandTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\Attributes\Test;
88
use Stolt\LeanPackage\Analyser;
9+
use Stolt\LeanPackage\Commands\CreateCommand;
910
use Stolt\LeanPackage\Commands\UpdateCommand;
1011
use Stolt\LeanPackage\GitattributesFileRepository;
1112
use Stolt\LeanPackage\Helpers\Str as OsHelper;
@@ -79,7 +80,6 @@ public function updatesExistingGitattributesAndReplacesHeader(): void
7980
$this->assertStringContainsString('tests/', $content);
8081
$this->assertStringContainsString('export-ignore', $content);
8182
}
82-
8383
#[Test]
8484
public function failsWhenNoGitattributesFileIsPresent(): void
8585
{
@@ -100,4 +100,33 @@ public function failsWhenNoGitattributesFileIsPresent(): void
100100
->assertFaulty()
101101
->assertOutputContains('No .gitattributes file found. Use the create command to create one first.');
102102
}
103+
104+
#[Test]
105+
public function printsExpectedContentWithoutWritingAFile(): void
106+
{
107+
$analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory);
108+
$repository = new GitattributesFileRepository($analyser);
109+
$command = new UpdateCommand($analyser, $repository);
110+
111+
$artifactFilenames = ['README.md', '.gitignore', 'phpunit.xml.dist'];
112+
113+
$this->createTemporaryFiles(
114+
$artifactFilenames,
115+
['tests']
116+
);
117+
118+
$result = TestCommand::for($command)
119+
->addArgument($this->temporaryDirectory)
120+
->addOption('dry-run')
121+
->addOption('align-export-ignores')
122+
->execute()
123+
->assertSuccessful();
124+
125+
$output = $result->output();
126+
127+
$this->assertStringContainsString('export-ignore', $output);
128+
$this->assertStringNotContainsString('has been updated', $output);
129+
130+
$this->assertFileDoesNotExist($this->temporaryDirectory . DIRECTORY_SEPARATOR . '.gitattributes');
131+
}
103132
}

0 commit comments

Comments
 (0)