Skip to content

Commit 019b1df

Browse files
author
TheSnoozer
committed
stop using com.github.stefanbirkner.system-rules to mock the system environment variables, instead just allow the callback to set them - as a result no illegal access is needed anymore (YAY)
1 parent 04e829c commit 019b1df

File tree

5 files changed

+21
-71
lines changed

5 files changed

+21
-71
lines changed

.github/workflows/default-tests.yml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,7 @@ jobs:
3131
needs: checkstyle
3232
strategy:
3333
matrix:
34-
java_allow_illegal_access: [false]
35-
java_version: ['11', '12', '13', '14', '15']
36-
include:
37-
- java_version: '16'
38-
java_allow_illegal_access: true
39-
- java_version: '17'
40-
java_allow_illegal_access: true
41-
- java_version: '18'
42-
java_allow_illegal_access: true
43-
- java_version: '19'
44-
java_allow_illegal_access: true
34+
java_version: ['11', '12', '13', '14', '15', '16', '17', '18', '19']
4535

4636
steps:
4737
- uses: actions/checkout@v3
@@ -59,12 +49,8 @@ jobs:
5949
path: ~/.m2
6050
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
6151
restore-keys: ${{ runner.os }}-m2
62-
- name: Build with Maven (no illegal access allowed)
63-
if: ${{ ! matrix.java_allow_illegal_access }}
52+
- name: Build with Maven
6453
run: mvn clean verify javadoc:javadoc -B
65-
- name: Build with Maven (illegal access allowed)
66-
if: ${{ matrix.java_allow_illegal_access }}
67-
run: mvn clean verify javadoc:javadoc -Pjava-allow-illegal-access -B
6854

6955
integration-test:
7056
name: Run integration test with Java ${{ matrix.java_version }} and Maven ${{ matrix.maven_version }}

pom.xml

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,6 @@
308308
<scope>test</scope>
309309
</dependency>
310310

311-
<dependency>
312-
<groupId>com.github.stefanbirkner</groupId>
313-
<artifactId>system-rules</artifactId>
314-
<version>1.19.0</version>
315-
<scope>test</scope>
316-
</dependency>
317-
318311
<!--to avoid complaints during tests -->
319312
<dependency>
320313
<groupId>org.slf4j</groupId>
@@ -336,22 +329,6 @@
336329
</distributionManagement>
337330

338331
<profiles>
339-
<profile>
340-
<id>java-allow-illegal-access</id>
341-
<build>
342-
<plugins>
343-
<plugin>
344-
<groupId>org.apache.maven.plugins</groupId>
345-
<artifactId>maven-surefire-plugin</artifactId>
346-
<configuration>
347-
<trimStackTrace>false</trimStackTrace>
348-
<!-- Workaround for https://github.com/stefanbirkner/system-lambda/issues/23 -->
349-
<argLine>--illegal-access=permit --add-opens=java.base/java.util=ALL-UNNAMED</argLine>
350-
</configuration>
351-
</plugin>
352-
</plugins>
353-
</build>
354-
</profile>
355332
<profile>
356333
<id>gpg</id>
357334
<build>

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,15 @@ public class GitCommitIdMojo extends AbstractMojo {
402402
*/
403403
private Charset sourceCharset = StandardCharsets.UTF_8;
404404

405+
/**
406+
* This method is used to mock the system environment in testing.
407+
*
408+
* @return unmodifiable string map view of the current system environment {@link System#getenv}.
409+
*/
410+
protected Map<String, String> getCustomSystemEnv() {
411+
return System.getenv();
412+
}
413+
405414
@Override
406415
public void execute() throws MojoExecutionException {
407416
LogInterface log = new LogInterface() {
@@ -531,6 +540,11 @@ public void error(String msg, Throwable t) {
531540
}
532541

533542
final GitCommitIdPlugin.Callback cb = new GitCommitIdPlugin.Callback() {
543+
@Override
544+
public Map<String, String> getSystemEnv() {
545+
return getCustomSystemEnv();
546+
}
547+
534548
@Override
535549
public Supplier<String> supplyProjectVersion() {
536550
return () -> project.getVersion();

src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424
import org.codehaus.plexus.util.FileUtils;
2525
import org.eclipse.jgit.api.Git;
2626
import org.eclipse.jgit.api.ResetCommand;
27-
import org.junit.Assert;
28-
import org.junit.Rule;
2927
import org.junit.Test;
30-
import org.junit.contrib.java.lang.system.EnvironmentVariables;
3128
import org.junit.runner.RunWith;
3229
import pl.project13.core.git.GitDescribeConfig;
3330
import pl.project13.core.util.JsonManager;
@@ -40,13 +37,10 @@
4037
import static java.util.Arrays.*;
4138
import static org.assertj.core.api.Assertions.assertThat;
4239
import static org.assertj.core.api.Assertions.entry;
40+
import static org.mockito.Mockito.when;
4341

4442
@RunWith(JUnitParamsRunner.class)
4543
public class GitCommitIdMojoIntegrationTest extends GitIntegrationTest {
46-
47-
@Rule
48-
public final EnvironmentVariables environmentVariablesMock = new EnvironmentVariables();
49-
5044
private static final boolean UseJGit = false;
5145
private static final boolean UseNativeGit = true;
5246

@@ -271,16 +265,7 @@ public void shouldNotUseBuildEnvironmentBranchInfoWhenParameterSet(boolean useNa
271265
env.put("JENKINS_URL", "http://myciserver.com");
272266
env.put("GIT_BRANCH", "mybranch");
273267
env.put("GIT_LOCAL_BRANCH", "localbranch");
274-
275-
// remove all keys from System.getenv()
276-
List<String> keySet = new ArrayList<>(System.getenv().keySet());
277-
keySet.stream().forEach(key -> environmentVariablesMock.set(key, null));
278-
279-
// set System.getenv() to be equal to given parameter env
280-
env.entrySet().stream().forEach(entry -> environmentVariablesMock.set(entry.getKey(), entry.getValue()));
281-
282-
// verify that System.getenv() is actually equal
283-
Assert.assertEquals(env, System.getenv());
268+
when(mojo.getCustomSystemEnv()).thenReturn(env);
284269

285270
// reset repo and force detached HEAD
286271
try (final Git git = git("my-jar-project")) {
@@ -357,18 +342,7 @@ private void shouldUseJenkinsBranchInfoWhenAvailableHelperAndAssertBranch(boolea
357342
mojo.useNativeGit = useNativeGit;
358343
mojo.useBranchNameFromBuildEnvironment = true;
359344

360-
// remove all keys from System.getenv()
361-
List<String> keySet = new ArrayList<>(System.getenv().keySet());
362-
for (String key: keySet) {
363-
environmentVariablesMock.set(key, null);
364-
}
365-
// set System.getenv() to be equal to given parameter env
366-
for (Map.Entry<String, String> entry: env.entrySet()) {
367-
environmentVariablesMock.set(entry.getKey(), entry.getValue());
368-
}
369-
370-
// verify that System.getenv() is actually equal
371-
Assert.assertEquals(env, System.getenv());
345+
when(mojo.getCustomSystemEnv()).thenReturn(env);
372346

373347
// reset repo and force detached HEAD
374348
try (final Git git = git("my-jar-project")) {

src/test/java/pl/project13/maven/git/GitIntegrationTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
import java.util.Properties;
3535
import java.util.concurrent.ThreadLocalRandom;
3636

37-
import static org.mockito.Mockito.mock;
38-
import static org.mockito.Mockito.when;
37+
import static org.mockito.Mockito.*;
3938

4039
public abstract class GitIntegrationTest {
4140

@@ -60,7 +59,7 @@ public void setUp() throws Exception {
6059
} while (sandbox.exists());
6160

6261
mavenSandbox = new FileSystemMavenSandbox(currSandbox);
63-
mojo = new GitCommitIdMojo();
62+
mojo = spy(GitCommitIdMojo.class);
6463
initializeMojoWithDefaults(mojo);
6564
}
6665

0 commit comments

Comments
 (0)