Skip to content

Commit de41295

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 2e1b85f commit de41295

File tree

6 files changed

+31
-57
lines changed

6 files changed

+31
-57
lines changed

.github/workflows/default-tests.yml

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +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
45-
34+
java_version: [ '11', '12', '13', '14', '15', '16', '17', '18', '19' ]
4635
steps:
4736
- uses: actions/checkout@v3
4837
with:
@@ -59,12 +48,8 @@ jobs:
5948
path: ~/.m2
6049
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
6150
restore-keys: ${{ runner.os }}-m2
62-
- name: Build with Maven (no illegal access allowed)
63-
if: ${{ ! matrix.java_allow_illegal_access }}
51+
- name: Build with Maven
6452
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
6853

6954
coveralls:
7055
name: Run coveralls

pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,6 @@
238238
<version>1.1.1</version>
239239
<scope>test</scope>
240240
</dependency>
241-
<dependency>
242-
<groupId>com.github.stefanbirkner</groupId>
243-
<artifactId>system-rules</artifactId>
244-
<version>1.19.0</version>
245-
<scope>test</scope>
246-
</dependency>
247241
<!-- slf4j is a dependency of jgit, this suppresses slf4j's warning during testing when we mock jgit stuff -->
248242
<dependency>
249243
<groupId>org.slf4j</groupId>

src/main/java/pl/project13/core/GitCommitIdPlugin.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@
3333
public class GitCommitIdPlugin {
3434
public interface Callback {
3535

36+
/**
37+
* The system environment variables.
38+
* Implementations usually do not need to set or overwrite this,
39+
* this is mainly meant for testing purposes...
40+
* @return unmodifiable string map view of the current system environment {@link System#getenv}.
41+
*/
42+
default Map<String, String> getSystemEnv() {
43+
return System.getenv();
44+
}
45+
3646
/**
3747
* @return Supplier that provides the version of the project that is currently evaluated.
3848
* Used to determine {@link GitCommitPropertyConstant#BUILD_VERSION}.
@@ -312,7 +322,7 @@ protected static void loadBuildData(@Nonnull Callback cb, @Nonnull Properties pr
312322
Map<String, Supplier<String>> additionalProperties = Collections.singletonMap(
313323
GitCommitPropertyConstant.BUILD_VERSION, cb.supplyProjectVersion());
314324
BuildServerDataProvider buildServerDataProvider = BuildServerDataProvider.getBuildServerProvider(
315-
System.getenv(), cb.getLogInterface());
325+
cb.getSystemEnv(), cb.getLogInterface());
316326
buildServerDataProvider
317327
.setDateFormat(cb.getDateFormat())
318328
.setDateFormatTimeZone(cb.getDateFormatTimeZone())
@@ -345,7 +355,7 @@ private static void loadGitDataWithNativeGit(@Nonnull Callback cb, @Nonnull Prop
345355
.setIncludeOnlyProperties(cb.getIncludeOnlyProperties())
346356
.setOffline(cb.isOffline());
347357

348-
nativeGitProvider.loadGitData(cb.getEvaluateOnCommit(), properties);
358+
nativeGitProvider.loadGitData(cb.getEvaluateOnCommit(), cb.getSystemEnv(), properties);
349359
}
350360

351361
private static void loadGitDataWithJGit(@Nonnull Callback cb, @Nonnull Properties properties) throws GitCommitIdExecutionException {
@@ -362,6 +372,6 @@ private static void loadGitDataWithJGit(@Nonnull Callback cb, @Nonnull Propertie
362372
.setIncludeOnlyProperties(cb.getIncludeOnlyProperties())
363373
.setOffline(cb.isOffline());
364374

365-
jGitProvider.loadGitData(cb.getEvaluateOnCommit(), properties);
375+
jGitProvider.loadGitData(cb.getEvaluateOnCommit(), cb.getSystemEnv(), properties);
366376
}
367377
}

src/main/java/pl/project13/core/GitDataProvider.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,11 @@ public GitDataProvider setOffline(boolean offline) {
247247
*
248248
* @param evaluateOnCommit The commit that should be used as reference to generate the properties from.
249249
* Defaults to {@code HEAD}.
250+
* @param env unmodifiable string map view of the current system environment {@link System#getenv}.
250251
* @param properties The Properties-Set that should be enriched by the generated one.
251252
* @throws GitCommitIdExecutionException In case any problem occurred during loading of the properties from the git repository.
252253
*/
253-
public void loadGitData(@Nonnull String evaluateOnCommit, @Nonnull Properties properties) throws GitCommitIdExecutionException {
254+
protected void loadGitData(@Nonnull String evaluateOnCommit, @Nonnull Map<String,String> env, @Nonnull Properties properties) throws GitCommitIdExecutionException {
254255
this.evaluateOnCommit = evaluateOnCommit;
255256
init();
256257
// git.user.name
@@ -263,7 +264,7 @@ public void loadGitData(@Nonnull String evaluateOnCommit, @Nonnull Properties pr
263264
validateAbbrevLength(abbrevLength);
264265

265266
// git.branch
266-
maybePut(properties, GitCommitPropertyConstant.BRANCH, () -> determineBranchName(System.getenv()));
267+
maybePut(properties, GitCommitPropertyConstant.BRANCH, () -> determineBranchName(env));
267268
// git.commit.id.describe
268269
maybePutGitDescribe(properties);
269270
loadShortDescribe(properties);
@@ -359,7 +360,7 @@ void validateAbbrevLength(int abbrevLength) throws GitCommitIdExecutionException
359360
* If running within Jenkins/Hudson, honor the branch name passed via GIT_BRANCH env var.
360361
* This is necessary because Jenkins/Hudson always invoke build in a detached head state.
361362
*
362-
* @param env environment settings
363+
* @param env unmodifiable string map view of the current system environment {@link System#getenv}.
363364
* @return results of getBranchName() or, if in Jenkins/Hudson, value of GIT_BRANCH
364365
* @throws GitCommitIdExecutionException the branch name could not be determined
365366
*/

src/test/java/pl/project13/core/GitCommitIdPluginIntegrationTest.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.eclipse.jgit.api.Git;
2424
import org.eclipse.jgit.api.ResetCommand;
2525
import org.junit.*;
26-
import org.junit.contrib.java.lang.system.EnvironmentVariables;
2726
import org.junit.runner.RunWith;
2827
import pl.project13.core.git.GitDescribeConfig;
2928
import pl.project13.core.util.JsonManager;
@@ -43,10 +42,6 @@
4342

4443
@RunWith(JUnitParamsRunner.class)
4544
public class GitCommitIdPluginIntegrationTest {
46-
47-
@Rule
48-
public final EnvironmentVariables environmentVariablesMock = new EnvironmentVariables();
49-
5045
private static final boolean UseJGit = false;
5146
private static final boolean UseNativeGit = true;
5247

@@ -282,16 +277,6 @@ public void shouldNotUseBuildEnvironmentBranchInfoWhenParameterSet(boolean useNa
282277
env.put("JENKINS_URL", "http://myciserver.com");
283278
env.put("GIT_BRANCH", "mybranch");
284279
env.put("GIT_LOCAL_BRANCH", "localbranch");
285-
286-
// remove all keys from System.getenv()
287-
List<String> keySet = new ArrayList<>(System.getenv().keySet());
288-
keySet.stream().forEach(key -> environmentVariablesMock.set(key, null));
289-
290-
// set System.getenv() to be equal to given parameter env
291-
env.entrySet().stream().forEach(entry -> environmentVariablesMock.set(entry.getKey(), entry.getValue()));
292-
293-
// verify that System.getenv() is actually equal
294-
Assert.assertEquals(env, System.getenv());
295280

296281
// reset repo and force detached HEAD
297282
try (final Git git = git(dotGitDirectory)) {
@@ -364,25 +349,13 @@ private void shouldUseJenkinsBranchInfoWhenAvailableHelperAndAssertBranch(boolea
364349
new GitCommitIdTestCallback()
365350
.setDotGitDirectory(dotGitDirectory)
366351
.setUseNativeGit(useNativeGit)
352+
.setSystemEnv(env)
367353
.setUseBranchNameFromBuildEnvironment(true)
368354
.build();
369355
Properties properties = new Properties();
370356

371357
// given
372358

373-
// remove all keys from System.getenv()
374-
List<String> keySet = new ArrayList<>(System.getenv().keySet());
375-
for (String key: keySet) {
376-
environmentVariablesMock.set(key, null);
377-
}
378-
// set System.getenv() to be equal to given parameter env
379-
for (Map.Entry<String, String> entry: env.entrySet()) {
380-
environmentVariablesMock.set(entry.getKey(), entry.getValue());
381-
}
382-
383-
// verify that System.getenv() is actually equal
384-
Assert.assertEquals(env, System.getenv());
385-
386359
// reset repo and force detached HEAD
387360
try (final Git git = git(dotGitDirectory)) {
388361
git.reset().setMode(ResetCommand.ResetType.HARD).setRef("b6a73ed").call();

src/test/java/pl/project13/core/GitCommitIdTestCallback.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.function.Supplier;
3333

3434
public class GitCommitIdTestCallback {
35+
private Map<String, String> systemEnv = System.getenv();
3536
private String projectVersion = "dummy-version";
3637
private LogInterface logInterface = createDummyLogInterface();
3738
private String dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ";
@@ -65,6 +66,11 @@ public GitCommitIdTestCallback() {
6566
}
6667
}
6768

69+
public GitCommitIdTestCallback setSystemEnv(Map<String, String> systemEnv) {
70+
this.systemEnv = systemEnv;
71+
return this;
72+
}
73+
6874
public GitCommitIdTestCallback setProjectVersion(String projectVersion) {
6975
this.projectVersion = projectVersion;
7076
return this;
@@ -187,6 +193,11 @@ public GitCommitIdTestCallback setShouldPropertiesEscapeUnicode(boolean shouldPr
187193

188194
public GitCommitIdPlugin.Callback build() {
189195
return new GitCommitIdPlugin.Callback() {
196+
@Override
197+
public Map<String, String> getSystemEnv() {
198+
return systemEnv;
199+
}
200+
190201
@Override
191202
public Supplier<String> supplyProjectVersion() {
192203
return () -> projectVersion;

0 commit comments

Comments
 (0)