forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateNewChangelogTest.php
More file actions
137 lines (111 loc) · 4.4 KB
/
CreateNewChangelogTest.php
File metadata and controls
137 lines (111 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\AutoReview;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
/**
* @internal
*/
#[CoversNothing]
#[Group('AutoReview')]
final class CreateNewChangelogTest extends TestCase
{
private string $currentVersion;
protected function setUp(): void
{
parent::setUp();
exec('git describe --tags --abbrev=0 2>&1', $output, $exitCode);
if ($exitCode !== 0) {
$this->markTestSkipped(sprintf(
"Unable to get the latest git tag.\nOutput: %s",
implode("\n", $output),
));
}
// Current tag should already have the next patch docs done, so for testing purposes,
// we will treat the next patch version as the current version.
$this->currentVersion = $this->incrementVersion(trim($output[0], 'v'));
}
#[DataProvider('provideCreateNewChangelog')]
public function testCreateNewChangelog(string $mode): void
{
$output = exec('git status --porcelain | wc -l');
if ($output !== '0') {
$this->markTestSkipped('You have uncommitted operations that will be erased by this test.');
}
$currentVersion = $this->currentVersion;
$newVersion = $this->incrementVersion($currentVersion, $mode);
exec(
sprintf('php ./admin/create-new-changelog.php %s %s --dry-run', $currentVersion, $newVersion),
$output,
$exitCode,
);
$this->assertSame(0, $exitCode, "Script exited with code {$exitCode}. Output: " . implode("\n", $output));
$this->assertStringContainsString(
"public const CI_VERSION = '{$newVersion}-dev';",
$this->getContents('./system/CodeIgniter.php'),
);
$this->assertFileExists("./user_guide_src/source/changelogs/v{$newVersion}.rst");
$this->assertStringContainsString(
"Version {$newVersion}",
$this->getContents("./user_guide_src/source/changelogs/v{$newVersion}.rst"),
);
$this->assertStringContainsString(
"**{$newVersion} release of CodeIgniter4**",
$this->getContents("./user_guide_src/source/changelogs/v{$newVersion}.rst"),
);
$this->assertStringContainsString(
$newVersion,
$this->getContents('./user_guide_src/source/changelogs/index.rst'),
);
$versionWithoutDots = str_replace('.', '', $newVersion);
$this->assertFileExists("./user_guide_src/source/installation/upgrade_{$versionWithoutDots}.rst");
$this->assertStringContainsString(
"Upgrading from {$currentVersion} to {$newVersion}",
$this->getContents("./user_guide_src/source/installation/upgrade_{$versionWithoutDots}.rst"),
);
$this->assertStringContainsString(
"upgrade_{$versionWithoutDots}",
$this->getContents('./user_guide_src/source/installation/upgrading.rst'),
);
// cleanup added and modified files
exec('git restore .');
exec('git clean -fd');
}
/**
* @return iterable<string, array{0: string}>
*/
public static function provideCreateNewChangelog(): iterable
{
yield 'patch update' => ['patch'];
yield 'minor update' => ['minor'];
yield 'major update' => ['major'];
}
private function incrementVersion(string $version, string $mode = 'patch'): string
{
$parts = explode('.', $version);
return match ($mode) {
'major' => sprintf('%d.0.0', ++$parts[0]),
'minor' => sprintf('%d.%d.0', $parts[0], ++$parts[1]),
'patch' => sprintf('%d.%d.%d', $parts[0], $parts[1], ++$parts[2]),
default => $this->fail('Invalid version increment mode. Use "major", "minor", or "patch".'),
};
}
private function getContents(string $path): string
{
$contents = @file_get_contents($path);
if ($contents === false) {
$this->fail("Failed to read file contents from {$path}.");
}
return $contents;
}
}