Skip to content

Commit

Permalink
Merge pull request #90 from ADI10HERO/write-version-test
Browse files Browse the repository at this point in the history
fix: add missing write version test
  • Loading branch information
garethjevans authored Aug 13, 2021
2 parents 75c4678 + 2c8e6ee commit 9af94d6
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,15 @@ public void setIncrementPreRelease(boolean incrementPreRelease) {

@Override
public StepExecution start(StepContext stepContext) throws Exception {
return new Execution(outputFormat, startTag, buildMetadata, writeVersion, preRelease,
preservePreRelease, incrementPreRelease, stepContext);
return new Execution(
outputFormat,
startTag,
buildMetadata,
writeVersion,
preRelease,
preservePreRelease,
incrementPreRelease,
stepContext);
}

/** This class extends Step Execution class, contains the run method. */
Expand Down Expand Up @@ -175,18 +182,24 @@ public static class Execution extends SynchronousStepExecution<String> {
/**
* Constructor with fields initialisation.
*
* @param outputFormat Output format for the next version
* @param startTag Git tag
* @param buildMetadata Add meta date to the version.
* @param writeVersion Should write the new version in the file.
* @param preRelease Pre release information to add
* @param preservePreRelease Keep existing prerelease information or not
* @param outputFormat Output format for the next version
* @param startTag Git tag
* @param buildMetadata Add meta date to the version.
* @param writeVersion Should write the new version in the file.
* @param preRelease Pre release information to add
* @param preservePreRelease Keep existing prerelease information or not
* @param incrementPreRelease Increment prerelease information or not
* @param context Jenkins context
* @param context Jenkins context
*/
protected Execution(String outputFormat, String startTag, String buildMetadata,
boolean writeVersion, String preRelease, boolean preservePreRelease,
boolean incrementPreRelease, @Nonnull StepContext context) {
protected Execution(
String outputFormat,
String startTag,
String buildMetadata,
boolean writeVersion,
String preRelease,
boolean preservePreRelease,
boolean incrementPreRelease,
@Nonnull StepContext context) {
super(context);
this.outputFormat = outputFormat;
this.startTag = startTag;
Expand Down Expand Up @@ -237,8 +250,7 @@ protected String run() throws Exception {
Version nextVersion;
if (!incrementPreRelease || StringUtils.isEmpty(currentVersion.getPreReleaseVersion())) {
// based on the commit list, determine how to bump the version
nextVersion =
new ConventionalCommits().nextVersion(currentVersion, commitHistory);
nextVersion = new ConventionalCommits().nextVersion(currentVersion, commitHistory);
} else {
nextVersion = currentVersion.incrementPreReleaseVersion();
}
Expand All @@ -263,13 +275,14 @@ protected String run() throws Exception {
nextVersion = nextVersion.setPreReleaseVersion(preRelease);
}

getContext().get(TaskListener.class).getLogger().println(nextVersion);

if (writeVersion) {
WriteVersion writer = new WriteVersion();
writer.write(nextVersion, dir);
String writeLog = writer.write(nextVersion, dir);
getContext().get(TaskListener.class).getLogger().println(writeLog);
}

getContext().get(TaskListener.class).getLogger().println(nextVersion);

return nextVersion.toString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@ public void setProcessHelper(ProcessHelper processHelper) {
* @throws IOException If an error occurs while reading/writing files.
* @throws InterruptedException If an error occurs while executing command using processHelper.
*/
public void write(Version nextVersion, File directory) throws IOException, InterruptedException {
public String write(Version nextVersion, File directory)
throws IOException, InterruptedException {

ProjectType projectType = ProjectTypeFactory.getProjectType(directory);
String message = "The next version was written to the configuration file.";

if (projectType != null) {
if (processHelper == null) {
processHelper = new DefaultProcessHelper();
}
projectType.writeVersion(directory, nextVersion, processHelper);
} else {
message = "Could not write the next version to the configuration file.";
LogUtils logger = new LogUtils();
logger.log(Level.INFO, Level.INFO, Level.FINE, Level.FINE, true, "Could not write to file");
logger.log(Level.INFO, Level.INFO, Level.FINE, Level.FINE, true, message);
}
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public void shouldBumpCurrentVersionWhenRemovePreRelease() throws Exception {
assertThat(JenkinsRule.getLog(b), containsString("Started"));
assertThat(JenkinsRule.getLog(b), containsString("nextVersion"));
assertThat(JenkinsRule.getLog(b), containsString("Current Tag is: 0.2.0-alpha"));
assertThat(JenkinsRule.getLog(b), containsString("0.2.0\n"));
assertThat(JenkinsRule.getLog(b), containsString("0.2.0"));
assertThat(JenkinsRule.getLog(b), containsString("Finished: SUCCESS"));
}

Expand Down Expand Up @@ -394,4 +394,33 @@ public void shouldNotIncrementPreReleaseInformation() throws Exception {
assertThat(JenkinsRule.getLog(b), containsString("0.1.0"));
assertThat(JenkinsRule.getLog(b), containsString("Finished: SUCCESS"));
}

@Test
public void shouldFailWriteVersion() throws Exception {
WorkflowJob p = rule.jenkins.createProject(WorkflowJob.class, "p");
URL zipFile = getClass().getResource("simple-project-with-notags.zip");
assertThat(zipFile, is(notNullValue()));

p.setDefinition(
new CpsFlowDefinition(
"node {\n"
+ " unzip '"
+ zipFile.getPath()
+ "'\n"
+ " nextVersion(writeVersion: true)\n"
+ "}\n",
true));

WorkflowRun b = rule.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0).get());

System.out.println(JenkinsRule.getLog(b));
assertThat(JenkinsRule.getLog(b), containsString("Started"));
assertThat(JenkinsRule.getLog(b), containsString("nextVersion"));
assertThat(JenkinsRule.getLog(b), containsString("No tags found"));
assertThat(JenkinsRule.getLog(b), containsString("0.1.0"));
assertThat(
JenkinsRule.getLog(b),
containsString("Could not write the next version to the configuration file."));
assertThat(JenkinsRule.getLog(b), containsString("Finished: SUCCESS"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package io.jenkins.plugins.conventionalcommits.utils;

import com.github.zafarkhaja.semver.Version;
import io.jenkins.plugins.conventionalcommits.ConventionalCommits;
import io.jenkins.plugins.conventionalcommits.process.ProcessHelper;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.*;

@RunWith(MockitoJUnitRunner.class)
public class WriteVersionTest {

@Rule public TemporaryFolder rootFolder = new TemporaryFolder();

@Mock private ProcessHelper processHelper;

@Mock private Handler mockedHandler;

@Captor private ArgumentCaptor<LogRecord> logRecordCaptor;
@Captor private ArgumentCaptor<ArrayList<String>> captor;

@Before
public void setup() {
final Logger logger = Logger.getLogger(ConventionalCommits.class.getName());
logger.addHandler(mockedHandler);
logger.setLevel(Level.FINE);
}

@Test
public void testWriteMavenProjectVersion() throws IOException, InterruptedException {

String os = System.getProperty("os.name");
String commandName = "mvn";

if (os.contains("Windows")) {
commandName += ".cmd";
}

List<String> command = Arrays.asList(commandName, "versions:set", "-DnewVersion=2.0.0");

File mavenDir = rootFolder.newFolder("SampleMavenProject");
File pom = rootFolder.newFile(mavenDir.getName() + File.separator + "pom.xml");

String pomContent =
"<project>\n"
+ " <modelVersion>4.0.0</modelVersion>\n"
+ " <groupId>com.test.app</groupId>\n"
+ " <artifactId>test-app</artifactId>\n"
+ " <version>1.0.0</version>\n"
+ "</project>\n";

FileWriter pomWriter = new FileWriter(pom);
pomWriter.write(pomContent);
pomWriter.close();

assertThat(processHelper, is(notNullValue()));
when(processHelper.runProcessBuilder(mavenDir, command)).thenReturn("2.0.0");

WriteVersion writer = new WriteVersion();
writer.setProcessHelper(processHelper);
writer.write(Version.valueOf("2.0.0"), mavenDir);

verify(processHelper).runProcessBuilder(any(), captor.capture());
List<String> capturedCommand = captor.getValue();
assertThat(capturedCommand, is(command));
}

@Test
public void testWriteVersionFailed() throws IOException, InterruptedException {

File dir = rootFolder.newFolder("SampleProject");
String message = "Could not write the next version to the configuration file.";

WriteVersion writer = new WriteVersion();
writer.write(Version.valueOf("1.0.0"), dir);

verify(mockedHandler).publish(logRecordCaptor.capture());
String logMessage = logRecordCaptor.getValue().getMessage();

assertThat(logMessage, is(message));
}
}

0 comments on commit 9af94d6

Please sign in to comment.