Skip to content

Commit

Permalink
Issue-1527: Add more tests detailing the dependency between jgiven an…
Browse files Browse the repository at this point in the history
…d test tasks.

A lot of those are technically testing the gradle API, but since handling that API is fiddly, and it is easy to do it wrong, I opt to rather have an additional assertion on how the Jgiven task operates. Especially for the case that we lay out in our example projects

Signed-off-by: l-1squared <[email protected]>
  • Loading branch information
l-1squared committed Feb 20, 2024
1 parent d540fa1 commit 1c351d2
Showing 1 changed file with 88 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

@ExtendWith(JGivenExtension.class)
public class JGivenPluginTest extends
ScenarioTest<JGivenPluginTest.Given, JGivenPluginTest.When, JGivenPluginTest.Then> {
ScenarioTest<JGivenPluginTest.Given, JGivenPluginTest.When, JGivenPluginTest.Then> {

@ScenarioState
public final FileWrapper testProjectDir = new FileWrapper();
Expand Down Expand Up @@ -64,115 +64,141 @@ public void plugin_can_build_with_an_empty_project() throws Exception {
@Test
public void jgiven_test_results_are_written_to_the_buildDir() throws IOException {
given()
.the_plugin_is_applied()
.and().there_are_JGiven_tests();
.the_plugin_is_applied()
.and().there_are_JGiven_tests();

when()
.a_build().with().the_task("test").is_successful();
.a_build().with().the_task("test").is_successful();

then()
.the_JGiven_results_are_written_to("build/jgiven-results/test");
.the_JGiven_results_are_written_to("build/jgiven-results/test");

}

@Test
public void jgiven_test_results_can_be_written_to_custom_directory() throws IOException {
String customResultsDir = "build/jgiven-jsons";
given()
.the_plugin_is_applied()
.and().the_results_dir_is_set_to(customResultsDir)
.and().there_are_JGiven_tests();
.the_plugin_is_applied()
.and().the_results_dir_is_set_to(customResultsDir)
.and().there_are_JGiven_tests();

when()
.a_build().with().the_task("test").is_successful();
.a_build().with().the_task("test").is_successful();

then()
.the_JGiven_results_are_written_to(customResultsDir);
.the_JGiven_results_are_written_to(customResultsDir);

}

@Test
public void jgiven_html_report_is_generated() throws IOException {
given()
.the_plugin_is_applied()
.and().there_are_JGiven_tests();
.the_plugin_is_applied()
.and().there_are_JGiven_tests();

when()
.a_build().with()
.the_task("test").and()
.the_task("jgivenTestReport").is_successful();
.a_build().with()
.the_task("test").and()
.the_task("jgivenTestReport").is_successful();

then()
.the_JGiven_html_reports_are_written_to("build/reports/jgiven/test/html");
.the_JGiven_html_reports_are_written_to("build/reports/jgiven/test/html");

}

@Test
public void configure_reports() throws IOException {
given()
.the_plugin_is_applied()
.and().there_are_JGiven_tests()
.and().the_jgiven_report_is_configured_by("reports {\n"
+ " html {\n"
+ " required = false\n"
+ " }\n"
+ " text {\n"
+ " required = true\n"
+ " }\n"
+ "}\n")
.the_plugin_is_applied()
.and().there_are_JGiven_tests()
.and().the_jgiven_report_is_configured_by("reports {\n"
+ " html {\n"
+ " required = false\n"
+ " }\n"
+ " text {\n"
+ " required = true\n"
+ " }\n"
+ "}\n")
;

when()
.a_build().with()
.the_task("test").and()
.the_task("jgivenTestReport").is_successful();
.a_build().with()
.the_task("test").and()
.the_task("jgivenTestReport").is_successful();

then()
.the_JGiven_reports_are_not_written_to("build/reports/jgiven/test/html")
.and().the_JGiven_text_reports_are_written_to("build/reports/jgiven/test/text");
.the_JGiven_reports_are_not_written_to("build/reports/jgiven/test/html")
.and().the_JGiven_text_reports_are_written_to("build/reports/jgiven/test/text");

}

@Test
public void configure_html_report() throws IOException {
given()
.the_plugin_is_applied()
.and().there_are_JGiven_tests()
.and().the_jgiven_report_is_configured_by("reports {\n"
+ " html {\n"
+ " required = true\n"
+ " title = 'JGiven Gradle Plugin'\n"
+ " }\n"
+ "}\n")
.the_plugin_is_applied()
.and().there_are_JGiven_tests()
.and().the_jgiven_report_is_configured_by("reports {\n"
+ " html {\n"
+ " required = true\n"
+ " title = 'JGiven Gradle Plugin'\n"
+ " }\n"
+ "}\n")
;

when()
.a_build().with()
.the_task("test").and()
.the_task("jgivenTestReport").is_successful();
.a_build().with()
.the_task("test").and()
.the_task("jgivenTestReport").is_successful();

then()
.the_JGiven_html_reports_are_written_to("build/reports/jgiven/test/html");
.the_JGiven_html_reports_are_written_to("build/reports/jgiven/test/html");

}

@Test
void no_jgiven_results_no_report() throws IOException {

given()
.the_plugin_is_applied()
.and().there_are_JGiven_tests();
.the_plugin_is_applied()
.and().there_are_JGiven_tests();

when()
.a_build().with()
.the_task("-x").the_task("test").and()
.the_task("jgivenTestReport").is_successful();
.a_build().with()
.the_task("-x").the_task("test").and()
.the_task("jgivenTestReport").is_successful();

then().the_JGiven_reports_are_not_written_to("build/reports/jgiven/test/html");

}

@Test
void no_report_task_no_report() throws IOException {

given()
.the_plugin_is_applied()
.and().there_are_JGiven_tests();

when()
.a_build().with()
.the_task("test").is_successful();

then()
.the_JGiven_reports_are_not_written_to("build/reports/jgiven/test/html");
.the_JGiven_reports_are_not_written_to("build/reports/jgiven/test/html");

}

@Test
void jgiven_reports_can_be_attached_to_test_tasks() throws IOException {
given().the_plugin_is_applied()
.and().there_are_JGiven_tests()
.and().test_tasks_are_finalized_by_JGiven();

when().the_task("test").is_successful();

then().the_JGiven_html_reports_are_written_to("build/reports/jgiven/test/html");
}

@SuppressWarnings("UnusedReturnValue")
static class Given extends Stage<Given> {
@ExpectedScenarioState
Expand Down Expand Up @@ -212,6 +238,11 @@ Given the_jgiven_report_is_configured_by(String configuration) throws IOExceptio
buildFile.write("jgivenTestReport { " + configuration + " } ");
return self();
}

Given test_tasks_are_finalized_by_JGiven() throws IOException{
buildFile.write("test.finalizedBy jgivenTestReport\n");
return self();
}
}

@SuppressWarnings("UnusedReturnValue")
Expand All @@ -234,10 +265,10 @@ When a_build() {

When is_successful() {
result = GradleRunner.create()
.withProjectDir(testProjectDir.get())
.withArguments(tasks)
.withPluginClasspath()
.build();
.withProjectDir(testProjectDir.get())
.withArguments(tasks)
.withPluginClasspath()
.build();
return self();
}
}
Expand All @@ -259,8 +290,8 @@ Then the_build_directory_has_not_been_created() {
Then all_tasks_have_been_executed() {
for (String task : tasks) {
TaskOutcome outcome = Optional.ofNullable(result.task(":" + task))
.map(BuildTask::getOutcome)
.orElse(TaskOutcome.FAILED);
.map(BuildTask::getOutcome)
.orElse(TaskOutcome.FAILED);
assertThat(outcome).isEqualTo(TaskOutcome.SUCCESS);
}
return self();
Expand All @@ -280,8 +311,8 @@ private void assertDirectoryContainsFilesOfType(String destination, final String
File destinationDir = new File(testProjectDir.get(), destination);
assertThat(destinationDir).isDirectory();
assertThat(destinationDir
.listFiles((dir, name) -> name.endsWith("." + extension)))
.isNotEmpty();
.listFiles((dir, name) -> name.endsWith("." + extension)))
.isNotEmpty();
}

Then the_JGiven_reports_are_not_written_to(String destination) {
Expand Down

0 comments on commit 1c351d2

Please sign in to comment.