Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dogfood our ratchetFrom feature. #595

Merged
merged 7 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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]
* We are now dogfooding `ratchetFrom` and `licenseHeader` with a `$YEAR` token to ensure that Spotless copyright headers stay up-to-date without adding noise to file history. ([#595](https://github.com/diffplug/spotless/pull/595))

## [1.33.0] - 2020-06-03
### Added
Expand Down
3 changes: 1 addition & 2 deletions gradle/spotless-freshmark.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ if (tasks.names.contains('changelogCheck')) {
// that uses versionNext as versionLast
it.put('versionLast', spotlessChangelog.versionNext)
}
def changelogBumpFreshmark = freshmark.createIndependentTask('changelogBumpFreshmark')
changelogBumpFreshmark.setApply()
def changelogBumpFreshmark = freshmark.createIndependentApplyTask('changelogBumpFreshmark')
// freshmark should run after the changelog bump
changelogBumpFreshmark.dependsOn tasks.named('changelogBump')

Expand Down
4 changes: 2 additions & 2 deletions gradle/spotless.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ spotless {
* No good way to get around using this import:
* https://github.com/gradle/gradle/issues/3191
*/
String regex = "import org\\.gradle\\.api\\.internal\\.(?!plugins\\.DslObject)"
String regex = "import org\\.gradle\\.api\\.internal\\.(?!plugins\\.DslObject)(?!project\\.ProjectInternal)"
if ((text.contains('import org.gradle.internal.') || text.find(regex)) &&
!text.contains('def noInternalDepsClosure')) {
throw new AssertionError("Accidental internal import")
Expand All @@ -15,6 +15,7 @@ spotless {
if (project.name != 'ide' && project != rootProject) {
// the rootProject and ide projects don't have any java
java {
ratchetFrom 'origin/master'
custom 'noInternalDeps', noInternalDepsClosure
bumpThisNumberIfACustomStepChanges(1)
licenseHeaderFile rootProject.file('gradle/spotless.license')
Expand All @@ -26,7 +27,6 @@ spotless {
}
groovyGradle {
target '*.gradle'
paddedCell()
greclipse().configFile rootProject.files('gradle/spotless.eclipseformat.xml', 'gradle/spotless.groovyformat.prefs')
}
}
2 changes: 1 addition & 1 deletion gradle/spotless.license
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright $YEAR 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
105 changes: 28 additions & 77 deletions plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SelfTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2020 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 @@ -19,120 +19,71 @@
import java.util.function.Consumer;

import org.gradle.api.Project;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.tasks.incremental.IncrementalTaskInputs;
import org.gradle.testkit.runner.GradleRunner;
import org.junit.Ignore;
import org.junit.Test;

import com.diffplug.common.base.Errors;
import com.diffplug.common.base.StandardSystemProperty;
import com.diffplug.common.collect.MoreCollectors;
import com.diffplug.spotless.TestProvisioner;

/**
* If you'd like to step through the full spotless plugin,
* these tests make that easier. Uncomment ignore to do it.
*/
@Ignore
public class SelfTest {
enum Type {
CHECK {
@Override
public void runAllTasks(Project project) {
project.getTasks().stream()
.filter(task -> task instanceof SpotlessTask)
.map(task -> (SpotlessTask) task)
.forEach(task -> Errors.rethrow().run(() -> {
IncrementalTaskInputs inputs = Mocks.mockIncrementalTaskInputs(task.getTarget());
task.performAction(inputs);
}));
}

@Override
public <T> T checkApply(T check, T apply) {
return check;
}
},
APPLY {
@Override
public void runAllTasks(Project project) {
project.getTasks().stream()
.filter(task -> task instanceof SpotlessTask)
.map(task -> (SpotlessTask) task)
.forEach(task -> Errors.rethrow().run(() -> {
IncrementalTaskInputs inputs = Mocks.mockIncrementalTaskInputs(task.getTarget());
task.performAction(inputs);
}));
}

@Override
public <T> T checkApply(T check, T apply) {
return apply;
}
};

public abstract void runAllTasks(Project project);

public abstract <T> T checkApply(T check, T apply);
}

@Test
public void spotlessApply() throws Exception {
runTasksManually(Type.APPLY);
}

@Test
public void spotlessCheck() throws Exception {
runTasksManually(Type.CHECK);
public static void main(String[] args) throws Exception {
runTaskManually();
//runWithTestKit("spotlessApply");
}

/** Runs a full task manually, so you can step through all the logic. */
private static void runTasksManually(Type type) throws Exception {
private static void runTaskManually() throws Exception {
Project project = createProject(extension -> {
extension.ratchetFrom("origin/master");
extension.java(java -> {
java.target("**/*.java");
java.licenseHeaderFile("spotless.license.java");
java.importOrderFile("spotless.importorder");
java.eclipse().configFile("spotless.eclipseformat.xml");
java.target("src/*/java/**/*.java");
java.licenseHeaderFile("../gradle/spotless.license");
java.importOrderFile("../gradle/spotless.importorder");
java.eclipse().configFile("../gradle/spotless.eclipseformat.xml");
java.trimTrailingWhitespace();
java.customLazy("Lambda fix", () -> raw -> {
if (!raw.contains("public class SelfTest ")) {
// don't format this line away, lol
return raw.replace("} )", "})").replace("} ,", "},");
} else {
return raw;
}
});
});
extension.format("misc", misc -> {
misc.target("**/*.gradle", "**/*.md", "**/*.gitignore");
misc.indentWithTabs();
misc.trimTrailingWhitespace();
misc.endWithNewline();
java.removeUnusedImports();
});
});
type.runAllTasks(project);
project.getBuildscript().getRepositories().mavenCentral();
SpotlessTask onlyTask = project.getTasks().stream()
.filter(task -> task instanceof SpotlessTask)
.map(task -> (SpotlessTask) task)
.collect(MoreCollectors.singleOrEmpty()).get();

IncrementalTaskInputs inputs = Mocks.mockIncrementalTaskInputs(onlyTask.getTarget());
onlyTask.performAction(inputs);
// it will run forever with empty threads, so we have to kill it
System.exit(0);
}

/** Creates a Project which has had the SpotlessExtension setup. */
private static Project createProject(Consumer<SpotlessExtension> test) throws Exception {
//Project project = Mocks.mockProject(TestProvisioner.gradleProject(new File("").getAbsoluteFile()), afterEvaluate);
Project project = TestProvisioner.gradleProject(new File("").getAbsoluteFile());
// create the spotless plugin
SpotlessPlugin plugin = project.getPlugins().apply(SpotlessPlugin.class);
// setup the plugin
test.accept(plugin.getExtension());
// run the afterEvaluate section
((ProjectInternal) project).getProjectEvaluationBroadcaster().afterEvaluate(project, project.getState());
// return the configured plugin
return project;
}

/** Runs against the `spotlessSelfApply.gradle` file. */
static void runWithTestKit(Type type) throws Exception {
static void runWithTestKit(String taskType) throws Exception {
GradleRunner.create()
.withPluginClasspath()
.withProjectDir(new File(StandardSystemProperty.USER_DIR.value()).getParentFile())
.withArguments(
"--build-file", "spotlessSelf.gradle",
"--project-cache-dir", ".gradle-selfapply",
"spotless" + type.checkApply("Check", "Apply"),
taskType,
"--stacktrace")
.forwardOutput()
.build();
Expand Down

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pluginManagement {
plugins {
id 'com.diffplug.gradle.spotless' version '3.27.0'
id 'com.diffplug.gradle.spotless' version '4.2.0'
}
}
plugins {
Expand Down