Skip to content

Commit

Permalink
JENKINS-69384 - SerenityJsonSummaryFile: Add support for missing work…
Browse files Browse the repository at this point in the history
…space (#171)

* SerenityJsonSummaryFile: Check for missing workspace (#163)

* add tests for SerenityJsonSummaryFile

* when called outside of a workspace, workspace is null -> added check
  for that edge case to avoid passing in null to Paths.get(), which
  avoids NPE on Java 17

* Remove null check for serenityjsonfile (#163)

serenityjsonfile is never null, so reduce unnecessary clutter.
  • Loading branch information
fknittel authored Dec 12, 2023
1 parent 49a8a54 commit d6ef358
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ public SerenityJsonSummaryFile(String workspace) {
}

public boolean exists() {
return Files.exists(getPath());
try {
return Files.exists(getPath());
} catch (IllegalArgumentException e) {
return false;
}
}

public Path getPath() {
if (workspace == null) {
throw new IllegalArgumentException("no workspace");
}
return java.nio.file.Paths.get(workspace, SERENITY_OUTPUT_DIRECTORY, SERENITY_JSON_SUMMARY_FILE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public SerenityPointGenerator(Run<?, ?> build, TaskListener listener,
}

public boolean hasReport() {
return (serenityJsonSummaryFile != null && serenityJsonSummaryFile.exists());
return serenityJsonSummaryFile.exists();
}

public Point[] generate() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package jenkinsci.plugins.influxdb.generators.serenity;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class SerenityJsonSummaryFileTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void testAvailableSummary() throws Exception {
writeToTemporaryPath("target/site/serenity/serenity-summary.json", "expected summary content");

SerenityJsonSummaryFile serenityJsonSummaryFile = new SerenityJsonSummaryFile(pathOfTemporaryFolder());

assertTrue(serenityJsonSummaryFile.exists());
assertEquals("expected summary content", serenityJsonSummaryFile.getContents());
}

@Test
public void testUnavailableSummary() throws Exception {
SerenityJsonSummaryFile serenityJsonSummaryFile = new SerenityJsonSummaryFile(pathOfTemporaryFolder());

assertFalse(serenityJsonSummaryFile.exists());
}

@Test
public void testUnavailableWorkspace() throws Exception {
SerenityJsonSummaryFile serenityJsonSummaryFile = new SerenityJsonSummaryFile(null);

assertFalse(serenityJsonSummaryFile.exists());
}

private void writeToTemporaryPath(String path, String content) throws IOException {
Path temporaryPath = temporaryFolder.getRoot().toPath();
Path summaryJson = temporaryPath.resolve(path);
Files.createDirectories(summaryJson.getParent());
Files.writeString(summaryJson, content, StandardCharsets.UTF_8);
}

private String pathOfTemporaryFolder() {
return temporaryFolder.getRoot().getAbsolutePath();
}
}

0 comments on commit d6ef358

Please sign in to comment.