Skip to content

Commit ed3bcf8

Browse files
committed
Run the in-process formatter in FormatDiffTest and FormatterServicesTest
Both tested the bootstrapping and native services only, never the one formatDiff loads on the JVM. FormatDiffTest runs it next to them, and both reflow a long string on a changed line with each of the three.
1 parent 7f21231 commit ed3bcf8

2 files changed

Lines changed: 73 additions & 5 deletions

File tree

‎gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/FormatDiffTest.java‎

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ void parsing_git_diff_output_works() throws IOException {
6969
@MethodSource("getFormatters")
7070
void reformat_a_subpath_of_a_git_directory_for_only_changed_lines(FormatterService formatterService)
7171
throws IOException, InterruptedException {
72-
runCommandInRepo("git", "init");
73-
runCommandInRepo("git", "config", "user.name", "Test User");
74-
runCommandInRepo("git", "config", "user.email", "test-user@palantir.com");
75-
runCommandInRepo("git", "config", "commit.gpgsign", "false");
76-
runCommandInRepo("git", "commit", "--allow-empty", "-m", "Init");
72+
initRepo();
7773

7874
Path subdir = repo.resolve("subdir");
7975
Files.createDirectories(subdir);
@@ -92,6 +88,44 @@ void reformat_a_subpath_of_a_git_directory_for_only_changed_lines(FormatterServi
9288
assertThat(dontTouchMe).hasContent(" class DontTouchMe {}");
9389
}
9490

91+
@ParameterizedTest
92+
@MethodSource("getFormatters")
93+
void reflows_the_long_string_of_a_changed_line(FormatterService formatterService)
94+
throws IOException, InterruptedException {
95+
initRepo();
96+
Path longString = repo.resolve("LongString.java");
97+
Files.writeString(longString, """
98+
class LongString {
99+
}
100+
""");
101+
runCommandInRepo("git", "add", ".");
102+
runCommandInRepo("git", "commit", "-m", "A class");
103+
104+
Files.writeString(longString, """
105+
class LongString {
106+
String changed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.";
107+
}
108+
""");
109+
110+
FormatDiff.formatDiff(repo, formatterService);
111+
112+
assertThat(longString).hasContent("""
113+
class LongString {
114+
String changed =
115+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et"
116+
+ " dolore magna.";
117+
}
118+
""");
119+
}
120+
121+
private void initRepo() throws IOException, InterruptedException {
122+
runCommandInRepo("git", "init");
123+
runCommandInRepo("git", "config", "user.name", "Test User");
124+
runCommandInRepo("git", "config", "user.email", "test-user@palantir.com");
125+
runCommandInRepo("git", "config", "commit.gpgsign", "false");
126+
runCommandInRepo("git", "commit", "--allow-empty", "-m", "Init");
127+
}
128+
95129
private void runCommandInRepo(String... args) throws IOException, InterruptedException {
96130
Process process =
97131
new ProcessBuilder().command(args).directory(repo.toFile()).start();
@@ -107,6 +141,9 @@ private void runCommandInRepo(String... args) throws IOException, InterruptedExc
107141

108142
private static Stream<FormatterService> getFormatters() throws IOException {
109143
return Stream.of(
144+
// In this JVM, as formatDiff runs it without the native binary.
145+
FormatterServiceLoader.load(
146+
getClasspath().stream().map(Path::toFile).toList()),
110147
new BootstrappingFormatterService(
111148
javaBinPath(), Runtime.version().feature(), getClasspath()),
112149
new NativeImageFormatterService(

‎open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.palantir.javaformat.Utils;
2525
import com.palantir.javaformat.java.FormatterException;
2626
import com.palantir.javaformat.java.FormatterService;
27+
import com.palantir.javaformat.java.FormatterServiceImpl;
2728
import com.palantir.javaformat.java.Replacement;
2829
import java.io.File;
2930
import java.net.URI;
@@ -96,13 +97,43 @@ public void main( String aaaa ) {
9697
.isEmpty();
9798
}
9899

100+
@ParameterizedTest
101+
@MethodSource("getFormattersAndTheInProcessOne")
102+
void reflows_the_long_string_of_the_range(FormatterService formatterService) throws FormatterException {
103+
String input = """
104+
class S {
105+
String changed = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.";
106+
}
107+
""";
108+
String expectedOutput = """
109+
class S {
110+
String changed =
111+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et"
112+
+ " dolore magna.";
113+
}
114+
""";
115+
int start = input.indexOf(" String changed");
116+
int end = input.indexOf('\n', start);
117+
118+
List<Replacement> replacements =
119+
formatterService.getFormatReplacements(input, List.of(Range.closedOpen(start, end)));
120+
121+
assertThat(Utils.applyReplacements(input, replacements)).isEqualTo(expectedOutput);
122+
}
123+
99124
private static Stream<FormatterService> getFormatters() {
100125
return Stream.of(
101126
new BootstrappingFormatterService(
102127
javaBinPath(), Runtime.version().feature(), getClasspath()),
103128
new NativeImageFormatterService(Path.of(System.getenv("NATIVE_IMAGE_CLASSPATH"))));
104129
}
105130

131+
// With the one that formats in this JVM, as formatDiff does without the native binary. On input that does not parse
132+
// it throws, where the other two return no replacements, so it is not among getFormatters.
133+
private static Stream<FormatterService> getFormattersAndTheInProcessOne() {
134+
return Stream.concat(Stream.of(new FormatterServiceImpl()), getFormatters());
135+
}
136+
106137
private String getTestResourceContent(String resourceName) {
107138
try {
108139
URI resource = getClass().getClassLoader().getResource(resourceName).toURI();

0 commit comments

Comments
 (0)