Skip to content

Commit

Permalink
Adds header to generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelstolt committed Sep 26, 2023
1 parent ef14f96 commit 964dc1e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 28 deletions.
17 changes: 14 additions & 3 deletions bin/release-version
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ foreach ($autoloads as $autoload) {
}
}

use Composer\Semver\Comparator;
use Stolt\LeanPackage\Helpers\Str as OsHelper;
use vierbergenlars\SemVer\version as SemVer;
use PHLAK\SemVer\Version;

$binApplicationName = 'lean-package-validator';
$binFile = __DIR__ . DIRECTORY_SEPARATOR . $binApplicationName;
Expand Down Expand Up @@ -130,7 +131,7 @@ if ($version === null) {
}

if ($version) {
if (SemVer::lte($version, $currentVersion)) {
if (Comparator::lessThanOrEqualTo($version, $currentVersion)) {
echo "Earlier version '{$version}' provided." . PHP_EOL;
exit(1);
}
Expand All @@ -156,7 +157,17 @@ if ($increment) {
exit(1);
}

$version = (new SemVer($currentVersion))->inc($increment)->getVersion();
switch ($increment) {
case 'major':
$version = (new Version($currentVersion))->incrementMajor()->__toString();
break;
case 'minor':
$version = (new Version($currentVersion))->incrementMinor()->__toString();
break;
case 'patch':
$version = (new Version($currentVersion))->incrementPatch()->__toString();
break;
}
}

if (!isset($version)) {
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"mockery/mockery": "^1.0",
"phlak/semver": "^4.1",
"php-mock/php-mock-phpunit": "^2.4||^1.1",
"phpstan/phpstan": "^1.6.2",
"phpunit/phpunit": "8.*",
"vierbergenlars/php-semver": "^3.0"
"phpunit/phpunit": "^10.3"
}
}
31 changes: 12 additions & 19 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true">
<testsuites>
<testsuite name="Lean Package Validator">
<directory>tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage/>
<testsuites>
<testsuite name="Lean Package Validator">
<directory>tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
18 changes: 18 additions & 0 deletions src/Commands/ValidateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ protected function configure()
$overwriteDescription = 'Overwrite existing .gitattributes file '
. 'with missing export-ignores';
$validateArchiveDescription = 'Validate Git archive against current HEAD';
$omitHeaderDescription = 'Omit adding a header to created or modified .gitattributes file';

$exampleGlobPattern = '{.*,*.md}';
$globPatternDescription = 'Use this glob pattern e.g. <comment>'
Expand Down Expand Up @@ -139,6 +140,12 @@ protected function configure()
InputOption::VALUE_NONE,
$alignExportIgnoresDescription
);
$this->addOption(
'omit-header',
null,
InputOption::VALUE_NONE,
$omitHeaderDescription
);
}

/**
Expand Down Expand Up @@ -171,6 +178,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$validateArchive = $input->getOption('validate-git-archive');
$globPattern = $input->getOption('glob-pattern');
$globPatternFile = (string) $input->getOption('glob-pattern-file');
$omitHeader = $input->getOption('omit-header');

$enforceStrictOrderComparison = $input->getOption('enforce-strict-order');

Expand Down Expand Up @@ -238,6 +246,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($expectedGitattributesFileContent !== '') {
if ($createGitattributesFile || $overwriteGitattributesFile) {
try {
if ($omitHeader === false) {
$headerContent = 'This file was partly modified by the lean package validator (http://git.io/lean-package-validator).' . PHP_EOL;

if ($createGitattributesFile) {
$headerContent = 'This file was generated by the lean package validator (http://git.io/lean-package-validator).' . PHP_EOL;
}
$expectedGitattributesFileContent = $headerContent . PHP_EOL . $expectedGitattributesFileContent;
}

$expectedGitattributesFileContent = $expectedGitattributesFileContent;
$outputContent .= $this->createGitattributesFile(
$expectedGitattributesFileContent
);
Expand Down
57 changes: 53 additions & 4 deletions tests/Commands/ValidateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,47 @@ function () {
$mock->disable();
}

/**
* @test
*/
public function validateOnNonExistentGitattributesFilesWithCreationOptionCreatesOneWithoutHeader()
{
$artifactFilenames = ['CONDUCT.md'];

$this->createTemporaryFiles(
$artifactFilenames,
['specs']
);

$command = $this->application->find('validate');
$commandTester = new CommandTester($command);
$commandTester->execute([
'command' => $command->getName(),
'directory' => WORKING_DIRECTORY,
'--create' => true,
'--omit-header' => true
]);

$expectedDisplay = <<<CONTENT
Warning: There is no .gitattributes file present in /tmp/lpv.
Created a .gitattributes file with the shown content:
* text=auto eol=lf
.gitattributes export-ignore
CONDUCT.md export-ignore
specs/ export-ignore
CONTENT;

$this->assertEquals($expectedDisplay, $commandTester->getDisplay());
$this->assertTrue($commandTester->getStatusCode() == 0);
$this->assertFileExists(
WORKING_DIRECTORY . DIRECTORY_SEPARATOR . '.gitattributes'
);
}

/**
* @test
*/
Expand All @@ -598,9 +639,11 @@ public function validateOnNonExistentGitattributesFilesWithCreationOptionCreates
]);

$expectedDisplay = <<<CONTENT
Warning: There is no .gitattributes file present in {$this->temporaryDirectory}.
Warning: There is no .gitattributes file present in /tmp/lpv.
Created a .gitattributes file with the shown content:
This file was generated by the lean package validator (http://git.io/lean-package-validator).
* text=auto eol=lf
.gitattributes export-ignore
Expand All @@ -610,7 +653,7 @@ public function validateOnNonExistentGitattributesFilesWithCreationOptionCreates
CONTENT;

$this->assertSame($expectedDisplay, $commandTester->getDisplay());
$this->assertEquals($expectedDisplay, $commandTester->getDisplay());
$this->assertTrue($commandTester->getStatusCode() == 0);
$this->assertFileExists(
WORKING_DIRECTORY . DIRECTORY_SEPARATOR . '.gitattributes'
Expand Down Expand Up @@ -642,6 +685,8 @@ public function validateOnNonExistentGitattributesFilesWithCreationOptionCreates
Warning: There is no .gitattributes file present in {$this->temporaryDirectory}.
Created a .gitattributes file with the shown content:
This file was generated by the lean package validator (http://git.io/lean-package-validator).
* text=auto eol=lf
.gitattributes export-ignore
Expand All @@ -652,6 +697,8 @@ public function validateOnNonExistentGitattributesFilesWithCreationOptionCreates
CONTENT;

$expectedGitattributesContent = <<<CONTENT
This file was generated by the lean package validator (http://git.io/lean-package-validator).
* text=auto eol=lf
.gitattributes export-ignore
Expand All @@ -660,7 +707,7 @@ public function validateOnNonExistentGitattributesFilesWithCreationOptionCreates
CONTENT;

$this->assertSame($expectedDisplay, $commandTester->getDisplay());
$this->assertEquals($expectedDisplay, $commandTester->getDisplay());
$this->assertTrue($commandTester->getStatusCode() == 0);
$this->assertStringEqualsFile(
WORKING_DIRECTORY . DIRECTORY_SEPARATOR . '.gitattributes',
Expand Down Expand Up @@ -854,6 +901,8 @@ public function overwriteOptionOnNonExistentGitattributesFileImplicatesCreate()
Warning: There is no .gitattributes file present in {$this->temporaryDirectory}.
Created a .gitattributes file with the shown content:
This file was partly modified by the lean package validator (http://git.io/lean-package-validator).
* text=auto eol=lf
.gitattributes export-ignore
Expand Down Expand Up @@ -1819,7 +1868,7 @@ public function gitignoredFilesAreExcludedFromValidation()
/**
* @return array
*/
public function optionProvider()
public static function optionProvider()
{
return [
['--overwrite'],
Expand Down

0 comments on commit 964dc1e

Please sign in to comment.