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

google-java-format Windows bug workaround now only runs for affected version (1.1). #73

Merged
merged 2 commits into from
Jan 20, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class GoogleJavaFormatStep {
// prevent direct instantiation
private GoogleJavaFormatStep() {}

private static final String DEFAULT_VERSION = "1.1";
private static final String DEFAULT_VERSION = "1.2";
static final String NAME = "google-java-format";
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format:";
static final String FORMATTER_CLASS = "com.google.googlejavaformat.java.Formatter";
Expand Down Expand Up @@ -67,10 +67,12 @@ static final class State implements Serializable {
/** The jar that contains the eclipse formatter. */
final JarState jarState;
final String stepName;
final String version;

State(String stepName, String version, Provisioner provisioner) throws IOException {
this.stepName = stepName;
this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner);
this.stepName = stepName;
this.version = version;
}

@SuppressWarnings({"unchecked", "rawtypes"})
Expand All @@ -94,7 +96,7 @@ FormatterFunc createFormat() throws Exception {
String formatted = (String) formatterMethod.invoke(formatter, input);
String removedUnused = (String) removeUnusedMethod.invoke(null, formatted, removeJavadocConstant);
String sortedImports = (String) importOrdererMethod.invoke(null, removedUnused);
return fixWindowsBug(sortedImports);
return fixWindowsBug(sortedImports, version);
};
}

Expand All @@ -107,18 +109,21 @@ FormatterFunc createRemoveUnusedImportsOnly() throws Exception {
Object removeJavadocConstant = Enum.valueOf((Class<Enum>) removeJavadocOnlyClass, REMOVE_UNUSED_IMPORT_JavadocOnlyImports_Keep);
Method removeUnusedMethod = removeUnusedClass.getMethod(REMOVE_UNUSED_METHOD, String.class, removeJavadocOnlyClass);

return input -> fixWindowsBug((String) removeUnusedMethod.invoke(null, input, removeJavadocConstant));
return input -> {
String removeUnused = (String) removeUnusedMethod.invoke(null, input, removeJavadocConstant);
return fixWindowsBug(removeUnused, version);
};
}
}

private static final boolean IS_WINDOWS = LineEnding.PLATFORM_NATIVE.str().equals("\r\n");

/**
* google-java-format's removeUnusedImports does *wacky* stuff on Windows.
* google-java-format-1.1's removeUnusedImports does *wacky* stuff on Windows.
* The beauty of normalizing all line endings to unix!
*/
static String fixWindowsBug(String input) {
if (IS_WINDOWS) {
static String fixWindowsBug(String input, String version) {
if (IS_WINDOWS && version.equals("1.1")) {
int firstImport = input.indexOf("\nimport ");
if (firstImport == 0) {
return input;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class GoogleJavaFormatStepTest extends ResourceHarness {
@Test
public void behavior() throws Exception {
FormatterStep step = GoogleJavaFormatStep.create("1.1", TestProvisioner.mavenCentral());
FormatterStep step = GoogleJavaFormatStep.create("1.2", TestProvisioner.mavenCentral());
StepHarness.forStep(step)
.testResource("java/googlejavaformat/JavaCodeUnformatted.test", "java/googlejavaformat/JavaCodeFormatted.test")
.testResource("java/googlejavaformat/JavaCodeWithLicenseUnformatted.test", "java/googlejavaformat/JavaCodeWithLicenseFormatted.test")
Expand All @@ -39,15 +39,15 @@ public void behavior() throws Exception {
@Test
public void equality() throws Exception {
new StepEqualityTester() {
String version = "1.1";
String version = "1.2";

@Override
protected void setupTest(API api) {
// same version == same
api.assertThisEqualToThis();
api.areDifferentThan();
// change the version, and it's different
version = "1.0";
version = "1.1";
api.assertThisEqualToThis();
api.areDifferentThan();
}
Expand All @@ -61,7 +61,7 @@ protected FormatterStep create() {
}

@Test
public void fixWindowsBug() {
public void fixWindowsBugForGfj1Point1() {
fixWindowsBugTestcase("");
fixWindowsBugTestcase(
"",
Expand Down Expand Up @@ -96,6 +96,6 @@ public void fixWindowsBug() {

private void fixWindowsBugTestcase(String... lines) {
String input = StringPrinter.buildStringFromLines(lines);
Assert.assertEquals(input, GoogleJavaFormatStep.fixWindowsBug(input));
Assert.assertEquals(input, GoogleJavaFormatStep.fixWindowsBug(input, "1.1"));
}
}