Skip to content

Commit

Permalink
Polish "Support up-to-date checking of Format task"
Browse files Browse the repository at this point in the history
See gh-323
  • Loading branch information
wilkinsona committed May 24, 2022
1 parent fde7e85 commit ae64820
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-2022 the original author or authors.
*
* 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,11 +25,8 @@
import java.util.stream.Collectors;

import org.gradle.api.GradleException;
import org.gradle.api.file.FileTree;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;

import io.spring.javaformat.formatter.FileEdit;
Expand Down Expand Up @@ -72,12 +69,6 @@ public void checkFormatting() throws IOException, InterruptedException {
}
}

@Override
@PathSensitive(PathSensitivity.RELATIVE)
public FileTree getSource() {
return super.getSource();
}

@OutputFile
public File getReportLocation() {
return this.reportLocation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,10 +20,7 @@

import org.gradle.api.GradleException;
import org.gradle.api.file.FileTree;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.OutputFiles;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;

import io.spring.javaformat.formatter.FileEdit;
Expand Down Expand Up @@ -56,15 +53,9 @@ public void format() throws IOException, InterruptedException {
}
}

@Override
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
public FileTree getSource() {
return super.getSource();
}

@OutputFiles
public FileTree getOutputFiles() {
return super.getSource();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
import java.nio.charset.Charset;
import java.util.stream.Stream;

import org.gradle.api.file.FileTree;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SourceTask;

import io.spring.javaformat.config.IndentationStyle;
Expand Down Expand Up @@ -84,6 +88,13 @@ public Property<JavaBaseline> getJavaBaseline() {
return this.javaBaseline;
}

@Override
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
public FileTree getSource() {
return super.getSource();
}

/**
* Format the source files and provide a {@link Stream} of {@link FileEdit} instances.
* @return the file edits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
Expand Down Expand Up @@ -52,21 +53,42 @@ public void checkOk() throws IOException {
}

@Test
public void checkUpTpDate() throws IOException {
public void checkUpToDate() throws IOException {
GradleRunner runner = this.gradleBuild.source("src/test/resources/format").prepareRunner("format");
// Format that changes files
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
// Format of already formatted files
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
// Up-to-date
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
}

// 1) Actually format the sources.
BuildResult result1 = runner.build();
assertThat(result1.task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);

// 2) No-op reformat the sources
// Not up-to-date yet because 1) changed the sources.
BuildResult result2 = runner.build();
assertThat(result2.task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
@Test
public void notUpToDateWhenJavaBaselineChanges() throws IOException {
GradleRunner runner = this.gradleBuild.source("src/test/resources/format").prepareRunner("format");
// Format that changes files
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
// Format of already formatted files
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
// Up-to-date
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
Files.write(new File(this.gradleBuild.getProjectDir(), ".springjavaformatconfig").toPath(),
Arrays.asList("java-baseline=8"));
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}

// 3) Now we should be up-to-date since 2) was effectively a no-op
BuildResult result3 = runner.build();
assertThat(result3.task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
@Test
public void notUpToDateWhenIndentationStyleChanges() throws IOException {
GradleRunner runner = this.gradleBuild.source("src/test/resources/format").prepareRunner("format");
// Format that changes files
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
// Format of already formatted files
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
// Up-to-date
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
Files.write(new File(this.gradleBuild.getProjectDir(), ".springjavaformatconfig").toPath(),
Arrays.asList("indentation-style=spaces"));
assertThat(runner.build().task(":formatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}

@Test
Expand Down

0 comments on commit ae64820

Please sign in to comment.