Skip to content
Closed
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
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
<artifactId>auto-value-annotations</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service-annotations</artifactId>
<optional>true</optional>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2021 Google Inc.
*
* 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.google.googlejavaformat.java;

import com.google.auto.service.AutoService;
import java.io.PrintWriter;
import java.util.spi.ToolProvider;

/** Provide a way to be invoked without necessarily starting a new VM. */
@AutoService(ToolProvider.class)
public class GoogleJavaFormatToolProvider implements ToolProvider {
@Override
public String name() {
return "google-java-format";
}

@Override
public int run(PrintWriter out, PrintWriter err, String... args) {
try {
return Main.main(out, err, args);
} catch (RuntimeException e) {
err.print(e.getMessage());
return -1; // pass non-zero value back indicating an error has happened
}
}
}
15 changes: 11 additions & 4 deletions core/src/main/java/com/google/googlejavaformat/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,27 @@ public Main(PrintWriter outWriter, PrintWriter errWriter, InputStream inStream)
* @param args the command-line arguments
*/
public static void main(String[] args) {
int result;
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out, UTF_8));
PrintWriter err = new PrintWriter(new OutputStreamWriter(System.err, UTF_8));
int result = main(out, err, args);
System.exit(result);
}

/**
* Package-private main entry point used this CLI program and the java.util.spi.ToolProvider
* implementation in the same package as this Main class.
*/
static int main(PrintWriter out, PrintWriter err, String... args) {
try {
Main formatter = new Main(out, err, System.in);
result = formatter.format(args);
return formatter.format(args);
} catch (UsageException e) {
err.print(e.getMessage());
result = 0;
return 0;
} finally {
err.flush();
out.flush();
}
System.exit(result);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static com.google.googlejavaformat.java.javadoc.Token.Type.LIST_ITEM_OPEN_TAG;
import static com.google.googlejavaformat.java.javadoc.Token.Type.PARAGRAPH_OPEN_TAG;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.googlejavaformat.java.javadoc.Token.Type;

Expand Down Expand Up @@ -395,7 +394,7 @@ private int innerIndent() {

// If this is a hotspot, keep a String of many spaces around, and call append(string, start, end).
private void appendSpaces(int count) {
output.append(Strings.repeat(" ", count));
output.append(" ".repeat(count));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2021 Google Inc.
*
* 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.google.googlejavaformat.java;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ServiceLoader;
import java.util.spi.ToolProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for {@link GoogleJavaFormatToolProvider}. */
@RunWith(JUnit4.class)
public class GoogleJavaFormatToolProviderTest {

@Test
public void testUsageOutputAfterLoadingViaToolName() {
String name = "google-java-format";

assertThat(
ServiceLoader.load(ToolProvider.class).stream()
.map(ServiceLoader.Provider::get)
.map(ToolProvider::name))
.contains(name);

ToolProvider format = ToolProvider.findFirst(name).get();

StringWriter out = new StringWriter();
StringWriter err = new StringWriter();

int result = format.run(new PrintWriter(out, true), new PrintWriter(err, true), "--help");

assertThat(result).isEqualTo(0);

String usage = err.toString();

// Check that doc links are included.
assertThat(usage).containsMatch("http.*/google-java-format");
assertThat(usage).contains("Usage: google-java-format");
}
}
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<checker.version>3.6.1</checker.version>
<errorprone.version>2.7.1</errorprone.version>
<auto-value.version>1.8.2</auto-value.version>
<auto-service.version>1.0</auto-service.version>
<maven-javadoc-plugin.version>3.1.0</maven-javadoc-plugin.version>
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
</properties>
Expand Down Expand Up @@ -124,6 +125,11 @@
<artifactId>auto-value-annotations</artifactId>
<version>${auto-value.version}</version>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service-annotations</artifactId>
<version>${auto-service.version}</version>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down Expand Up @@ -220,6 +226,11 @@
<artifactId>auto-value</artifactId>
<version>${auto-value.version}</version>
</path>
<path>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>${auto-service.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
Expand Down