Skip to content

Commit b041601

Browse files
Beautify the Release Manager
- Use only function names that "tell a story" in main delegate(). - Add test for failed update to README.md.
1 parent 1f437d1 commit b041601

File tree

2 files changed

+79
-18
lines changed

2 files changed

+79
-18
lines changed

build/classes/ReleaseManager.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ public function delegate()
109109
try {
110110
$this->getContext();
111111
$this->checkPrerequisites();
112-
$this->info(sprintf('Create release %s.', $this->releaseTag));
112+
$this->getReleaseTag();
113113
$this->updateReadme();
114-
$this->checkPostConditions();
115114
$this->createCommit();
116115
} catch (Exception $exception) {
117116
$this->error($exception->getMessage());
@@ -132,7 +131,6 @@ protected function getContext()
132131
{
133132
$this->branch = $this->git->getBranch();
134133
$this->tag = $this->git->getTag();
135-
$this->releaseTag = $this->semanticVersioning->getNextReleaseTag($this->tag, $this->releaseType);
136134
}
137135

138136
/**
@@ -157,6 +155,12 @@ protected function checkPrerequisites()
157155
}
158156
}
159157

158+
protected function getReleaseTag()
159+
{
160+
$this->releaseTag = $this->semanticVersioning->getNextReleaseTag($this->tag, $this->releaseType);
161+
$this->info(sprintf('Create release %s.', $this->releaseTag));
162+
}
163+
160164
/**
161165
* @throws Exception
162166
*
@@ -173,29 +177,33 @@ protected function updateReadme()
173177

174178
$readme = file($this->readmeFile);
175179
$readmeContent = '';
180+
$readmeState = 0;
176181
foreach ($readme as $line) {
177182
if (strpos($line, "Version $this->tag") === 0) {
178183
$readmeContent .= str_replace($this->tag, $this->releaseTag, $line);
184+
$readmeState += 1;
179185
} elseif (strpos($line, $this->tag) === 0) {
180186
$readmeContent .= str_replace($this->tag, "$changelog\n$this->tag", $line);
187+
$readmeState += 2;
181188
} else {
182189
$readmeContent .= $line;
183190
}
184191
}
185192
file_put_contents($this->readmeFile, $readmeContent);
186-
}
187193

188-
/**
189-
* @throws Exception
190-
*
191-
* @return void
192-
*/
193-
protected function checkPostConditions()
194-
{
195-
if ($this->git->hasCleanWorkingTree()) {
196-
throw new Exception(
197-
'Could not update README.md. The format has probably changed.'
198-
);
194+
if ($readmeState < 3) {
195+
if (!($readmeState & 1)) {
196+
$errors[] = sprintf("Missing note 'Version %s' of the last release below the title.", $this->tag);
197+
}
198+
if (!($readmeState & 2)) {
199+
$errors[] = sprintf("Missing changelog entry '%s: ..' from the last release.", $this->tag);
200+
}
201+
if (isset($errors)) {
202+
throw new Exception(sprintf(
203+
"Could not update README.md. The format has probably changed:\n%s",
204+
json_encode($errors, JSON_PRETTY_PRINT)
205+
));
206+
}
199207
}
200208
}
201209

tests/build/ReleaseManagerTest.php

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public function delegateSucceeds()
5656
- Fixed PHP 7.4 compatibility (#37) by @Dargmuesli.
5757
');
5858

59-
// Stub only the minimum required. We rely here for the most part on an existing Git
60-
// API that is available in most test environments. We only specify that the latest
61-
// version is the real existing 1.5.3 to be able to control the test flow.
59+
// Replace only the minimum required with a test double. We rely here mainly on an
60+
// existing Git API - which is available in most test environments. We only pretend
61+
// that the latest release is 1.5.3, and we expect 1.5.4 to be released now.
6262
$gitStub = $this->getMockBuilder(Git::class)
6363
->setMethods(['getTag'])
6464
->getMock();
@@ -102,4 +102,57 @@ public function delegateSucceeds()
102102
$this->expectOutputRegex($expectedOutputRegex);
103103
$this->assertStringMatchesFormat($expectedReadme, file_get_contents($readmeFile));
104104
}
105+
106+
/**
107+
* @test
108+
*/
109+
public function delegateFailsIfReadmeFormatChanges()
110+
{
111+
$readme = trim('
112+
Syllable
113+
========
114+
Version v1.5.3
115+
116+
..
117+
118+
Changes
119+
-------
120+
v1.5.3
121+
- Fixed PHP 7.4 compatibility (#37) by @Dargmuesli.
122+
');
123+
124+
// Replace only the minimum required with a test double. We rely here mainly on an
125+
// existing Git API - which is available in most test environments. We only pretend
126+
// that the latest release is 1.5.3, and we expect 1.5.4 to be released now.
127+
$gitStub = $this->getMockBuilder(Git::class)
128+
->setMethods(['getTag'])
129+
->getMock();
130+
$gitStub->expects($this->any())->method('getTag')->willReturn('1.5.3');
131+
132+
$expectedOutput = trim('
133+
Create release 1.5.4.
134+
Could not update README.md. The format has probably changed:
135+
[
136+
"Missing note \'Version 1.5.3\' of the last release below the title.",
137+
"Missing changelog entry \'1.5.3: ..\' from the last release."
138+
]
139+
Aborting.
140+
')."\n";
141+
$expectedReadme = $readme;
142+
143+
$this->addFileToTestDirectory('README.md', $readme);
144+
145+
$releaseType = SemanticVersioning::PATCH_RELEASE;
146+
$readmeFile = $this->getPathOfTestDirectoryFile('README.md');
147+
148+
$releaseManager = new ReleaseManager();
149+
$releaseManager->setReleaseType($releaseType);
150+
$releaseManager->setReadmeFile($readmeFile);
151+
$releaseManager->setGit($gitStub);
152+
$result = $releaseManager->delegate();
153+
154+
$this->assertFalse($result);
155+
$this->expectOutputString($expectedOutput);
156+
$this->assertEquals($expectedReadme, file_get_contents($readmeFile));
157+
}
105158
}

0 commit comments

Comments
 (0)