Skip to content

Commit

Permalink
Ignore external git config when running unit tests (#2021 fixes #1990)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Jan 23, 2024
2 parents 7770835 + 647f7e8 commit b3c4893
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Fixed
* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990))

## [2.45.0] - 2024-01-23
### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,9 +27,11 @@
import org.junit.jupiter.api.Test;

import com.diffplug.common.base.StringPrinter;
import com.diffplug.spotless.ClearGitConfig;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.ResourceHarness;

@ClearGitConfig
class GitAttributesTest extends ResourceHarness {
private List<File> testFiles(String prefix) {
List<File> result = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 DiffPlug
* Copyright 2020-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,8 +27,10 @@
import org.eclipse.jgit.lib.RefDatabase;
import org.junit.jupiter.api.Test;

import com.diffplug.spotless.ClearGitConfig;
import com.diffplug.spotless.ResourceHarness;

@ClearGitConfig
class GitRachetMergeBaseTest extends ResourceHarness {
@Test
void test() throws IllegalStateException, GitAPIException, IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 DiffPlug
* Copyright 2022-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,9 +27,11 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import com.diffplug.spotless.ClearGitConfig;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.extra.GitWorkarounds.RepositorySpecificResolver;

@ClearGitConfig
class GitWorkaroundsTest extends ResourceHarness {
@Test
void inline() throws IOException, GitAPIException {
Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Fixed
* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990))

## [6.25.0] - 2024-01-23
### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,6 +36,9 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import com.diffplug.spotless.ClearGitConfig;

@ClearGitConfig
class GitRatchetGradleTest extends GradleIntegrationHarness {
private static final String TEST_PATH = "src/markdown/test.md";

Expand Down Expand Up @@ -268,10 +271,9 @@ public BuildResultAssertion outcome(String taskPath, TaskOutcome expected) {
private RevCommit addAndCommit(Git git) throws NoFilepatternException, GitAPIException {
PersonIdent emptyPerson = new PersonIdent("jane doe", "[email protected]", new Date(0), TimeZone.getTimeZone("UTC"));
git.add().addFilepattern(".").call();
RevCommit commit = git.commit().setMessage("baseline")
return git.commit().setMessage("baseline")
.setCommitter(emptyPerson)
.setAuthor(emptyPerson)
.call();
return commit;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,9 @@
import org.eclipse.jgit.api.Git;
import org.junit.jupiter.api.Test;

import com.diffplug.spotless.ClearGitConfig;

@ClearGitConfig
class LicenseHeaderTest extends GradleIntegrationHarness {
private static final String NOW = String.valueOf(YearMonth.now().getYear());

Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Fixed
* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990))

## [2.43.0] - 2024-01-23
### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 DiffPlug
* Copyright 2020-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,7 +25,9 @@
import org.junit.jupiter.api.Test;

import com.diffplug.common.base.StringPrinter;
import com.diffplug.spotless.ClearGitConfig;

@ClearGitConfig
class GitRatchetMavenTest extends MavenIntegrationHarness {
private static final String TEST_PATH = "src/markdown/test.md";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 DiffPlug
* Copyright 2020-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,8 +21,10 @@
import org.eclipse.jgit.api.Git;
import org.junit.jupiter.api.Test;

import com.diffplug.spotless.ClearGitConfig;
import com.diffplug.spotless.maven.MavenIntegrationHarness;

@ClearGitConfig
class LicenseHeaderRatchetTest extends MavenIntegrationHarness {
private static final String NOW = String.valueOf(YearMonth.now().getYear());

Expand Down
1 change: 1 addition & 0 deletions testlib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {

implementation "com.diffplug.durian:durian-io:${VER_DURIAN}"
implementation "com.diffplug.durian:durian-collect:${VER_DURIAN}"
implementation "org.eclipse.jgit:org.eclipse.jgit:${VER_JGIT}"
implementation gradleTestKit()
}

Expand Down
60 changes: 60 additions & 0 deletions testlib/src/main/java/com/diffplug/spotless/ClearGitConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.List;

import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.util.SystemReader;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.parallel.ResourceAccessMode;
import org.junit.jupiter.api.parallel.ResourceLock;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@ExtendWith(ClearGitConfig.GitConfigExtension.class)
@ResourceLock(value = "GIT", mode = ResourceAccessMode.READ_WRITE)
public @interface ClearGitConfig {

class GitConfigExtension implements BeforeEachCallback, AfterEachCallback {

@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {
for (var config : getConfigs()) {
config.clear();
}
}

@Override
public void afterEach(ExtensionContext extensionContext) throws Exception {
for (var config : getConfigs()) {
config.load();
}
}

private static List<StoredConfig> getConfigs() throws Exception {
var reader = SystemReader.getInstance();
return List.of(reader.getUserConfig(), reader.getSystemConfig());
}
}
}

0 comments on commit b3c4893

Please sign in to comment.