Skip to content

Commit

Permalink
Use junit-pioneer to simplify polluted tests
Browse files Browse the repository at this point in the history
* Add junit-pioneer to inject environment variables into tests. This allows
removing custom Gradle task pollutedTest and running them normally.
* Refactor WhenAnAsciidoctorClassIsInstantiatedInAnEnvironmentWithGemPath
as a parametrized tests.
* Add missing cases for Asciidoctor Factory in
WhenAnAsciidoctorClassIsInstantiatedInAnEnvironmentWithGemPath.

Fixes asciidoctor#1193
  • Loading branch information
abelsromero committed May 1, 2023
1 parent e131d22 commit fbc6ff9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 65 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Build Improvement::
* Fix upstream tests forcing SNAPSHOT on Asciidoctor gem installation (#1123) (@abelsromero)
* Fix upstream build removing the explicit plugin repository (#1131)
* Set JUnit5 as default test engine (#1186) (@abelsromero)
* Removed pollutedTest Gradle task using junit-pioneer (#1193) (@abelsromero)

== 2.5.4 (2022-06-30)

Expand Down
30 changes: 2 additions & 28 deletions asciidoctorj-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ jar {
}

test {
useJUnitPlatform() {
excludeTags("polluted")
}
useJUnitPlatform()
}

task createVersionFile {
Expand All @@ -112,29 +110,5 @@ version.asciidoctor: $asciidoctorGemVersion
"""
}
}
jar.dependsOn createVersionFile

task pollutedTest(type: Test) {
useJUnitPlatform() {
includeTags 'polluted'
}
forkEvery = 10
minHeapSize = '128m'
maxHeapSize = '1024m'
jvmArgs '-XX:-UseGCOverheadLimit'

environment 'GEM_PATH', '/some/path'
environment 'GEM_HOME', '/some/other/path'

testLogging {
// events 'passed', 'failed', 'skipped', 'standard_out', 'standard_error'
// events 'standard_out', 'standard_error'
afterSuite { desc, result ->
if (!desc.parent && logger.infoEnabled) {
logger.info "Test results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} passed, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped)"
}
}
}
}

test.dependsOn pollutedTest
jar.dependsOn createVersionFile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
import org.asciidoctor.jruby.AsciidoctorJRuby;
import org.asciidoctor.jruby.internal.JRubyRuntimeContext;
import org.jruby.Ruby;
import org.jruby.RubyString;
import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;

/**
* This test checks if the Ruby instance for an Asciidoctor instance is
Expand All @@ -20,51 +25,77 @@
@Polluted
public class WhenAnAsciidoctorClassIsInstantiatedInAnEnvironmentWithGemPath {

@Test
public void should_not_have_gempath_in_ruby_env_when_created_with_null_gempath() {
@ParameterizedTest(name = "should_not_have_gempath_in_ruby_env_when_created_with_{1}")
@MethodSource("emptyGempathAsciidoctorProvider")
public void should_not_have_gempath_in_ruby_env_when_created_with_(Asciidoctor asciidoctor, String testDescription) {
// Given: Our environment is polluted (Cannot set these env vars here, so just check that gradle has set them correctly)
assertThat(System.getenv("GEM_PATH"), notNullValue());
assertThat(System.getenv("GEM_HOME"), notNullValue());

// When: A new Asciidoctor instance is created passing in a null GEM_PATH
Asciidoctor asciidoctor = AsciidoctorJRuby.Factory.create((String) null);
assertThat(System.getenv("GEM_PATH")).isNotEmpty();
assertThat(System.getenv("GEM_HOME")).isNotEmpty();

// Then: The org.jruby.JRuby instance does not see this variable
Ruby rubyRuntime = JRubyRuntimeContext.get(asciidoctor);
assertThat(rubyRuntime.evalScriptlet("ENV['GEM_PATH']"), is(rubyRuntime.getNil()));
assertThat(rubyRuntime.evalScriptlet("ENV['GEM_HOME']"), is(rubyRuntime.getNil()));
assertThat(rubyRuntime.evalScriptlet("ENV['GEM_PATH']")).isEqualTo(rubyRuntime.getNil());
assertThat(rubyRuntime.evalScriptlet("ENV['GEM_HOME']")).isEqualTo(rubyRuntime.getNil());
}

@Test
public void should_have_gempath_in_ruby_env_when_created_with_default_create() {
// Given: Our environment is polluted (Cannot set these env vars here, so just check that gradle has set them correctly)
assertThat(System.getenv("GEM_PATH"), notNullValue());
assertThat(System.getenv("GEM_HOME"), notNullValue());
private static Stream<Arguments> emptyGempathAsciidoctorProvider() {
return Stream.of(
arguments(AsciidoctorJRuby.Factory.create(), "default_create"),
arguments(AsciidoctorJRuby.Factory.create((String) null), "null_gempath")
);
}

// When: A new Asciidoctor instance is created passing in no GEM_PATH
Asciidoctor asciidoctor = AsciidoctorJRuby.Factory.create();
@ParameterizedTest(name = "should_have_gempath_in_ruby_env_when_created_with_{3}")
@MethodSource("modifiedGempathAsciidoctorProvider")
public void should_have_gempath_in_ruby_env_when_created_with_(Asciidoctor asciidoctor,
String expectedGemPath,
String expectedGemHome,
String testDescription) {
// Given: Our environment is polluted (Cannot set these env vars here, so just check that gradle has set them correctly)
assertThat(System.getenv("GEM_PATH")).isNotEmpty();
assertThat(System.getenv("GEM_HOME")).isNotEmpty();

// Then: The org.jruby.JRuby instance sees this variable
// Then: The org.jruby.JRuby instance does not see this variable
Ruby rubyRuntime = JRubyRuntimeContext.get(asciidoctor);
assertThat(rubyRuntime.evalScriptlet("ENV['GEM_PATH']"), is(rubyRuntime.getNil()));
assertThat(rubyRuntime.evalScriptlet("ENV['GEM_HOME']"), is(rubyRuntime.getNil()));
assertThat(rubyRuntime.evalScriptlet("ENV['GEM_PATH']").asJavaString()).isEqualTo(expectedGemPath);
assertThat(rubyRuntime.evalScriptlet("ENV['GEM_HOME']").asJavaString()).isEqualTo(expectedGemHome);
}

private static Stream<Arguments> modifiedGempathAsciidoctorProvider() {
final ClassLoader customClassloader = new String().getClass().getClassLoader();
final String customGemPath = "/another/gempath";
return Stream.of(
arguments(AsciidoctorJRuby.Factory.create(customGemPath),
customGemPath, customGemPath, "gempath"),
arguments(AsciidoctorJRuby.Factory.create(customClassloader),
Polluted.GEM_PATH, Polluted.GEM_HOME, "classloader"),
arguments(AsciidoctorJRuby.Factory.create(customClassloader, customGemPath),
customGemPath, customGemPath, "classloader_and_custom_gempath")
);
}

@Test
public void should_have_gempath_in_ruby_env_when_created_with_gempath() {
public void should_have_gempath_in_ruby_env_when_created_with_custom_paths() {
final String[] loadPaths = {"/load/path1", "/load/path2"};
final String customGemPath = "/another/custom/gempath";
// Given: Our environment is polluted (Cannot set these env vars here, so just check that gradle has set them correctly)
final String gemPath = "/another/path";
assertThat(System.getenv("GEM_PATH"), notNullValue());
assertThat(System.getenv("GEM_HOME"), notNullValue());
assertThat(System.getenv("GEM_PATH")).isNotEmpty();
assertThat(System.getenv("GEM_HOME")).isNotEmpty();

// When: A new Asciidoctor instance is created passing in a null GEM_PATH
Asciidoctor asciidoctor = AsciidoctorJRuby.Factory.create(gemPath);
Asciidoctor asciidoctor = AsciidoctorJRuby.Factory.create(List.of(loadPaths), customGemPath);

// Then: The org.jruby.JRuby instance does not see this variable
Ruby rubyRuntime = JRubyRuntimeContext.get(asciidoctor);
RubyString rubyGemPath = rubyRuntime.newString(gemPath);
assertThat(rubyRuntime.evalScriptlet("ENV['GEM_PATH']"), is((Object) rubyGemPath));
assertThat(rubyRuntime.evalScriptlet("ENV['GEM_HOME']"), is((Object) rubyGemPath));
assertThat(rubyRuntime.evalScriptlet("ENV['GEM_PATH']").asJavaString()).isEqualTo(customGemPath);
assertThat(rubyRuntime.evalScriptlet("ENV['GEM_HOME']").asJavaString()).isEqualTo(customGemPath);
assertThat(getLoadPaths(rubyRuntime)).contains(loadPaths);
}

private static List<String> getLoadPaths(Ruby rubyRuntime) {
return (List<String>) rubyRuntime.getLoadService().getLoadPath()
.convertToArray()
.stream()
.map(v -> v.toString())
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.asciidoctor.categories;

import org.junit.jupiter.api.Tag;
import org.junitpioneer.jupiter.SetEnvironmentVariable;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -13,6 +13,10 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Tag("polluted")
@SetEnvironmentVariable(key = "GEM_PATH", value = Polluted.GEM_PATH)
@SetEnvironmentVariable(key = "GEM_HOME", value = Polluted.GEM_HOME)
public @interface Polluted {

String GEM_PATH = "/some/path";
String GEM_HOME = "/some/other/path";
}
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ ext {
jsoupVersion = '1.14.3'
junit4Version = '4.13.2'
junit5Version = '5.9.2'
junitPioneerVersion = '2.0.1'
assertjVersion = '3.24.2'
nettyVersion = '4.1.58.Final'
saxonVersion = '9.9.0-2'
Expand Down Expand Up @@ -139,8 +140,10 @@ subprojects {
if (usesJUnit5(it.project)) {
dependencies {
testImplementation(platform("org.junit:junit-bom:$junit5Version"))
testImplementation("org.junit.jupiter:junit-jupiter-api")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testImplementation "org.junit.jupiter:junit-jupiter-api"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"
testImplementation "org.junit.jupiter:junit-jupiter-params"
testImplementation "org.junit-pioneer:junit-pioneer:$junitPioneerVersion"
}
}

Expand Down

0 comments on commit fbc6ff9

Please sign in to comment.