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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.google.googlejavaformat.java;

import java.io.PrintWriter;
import java.util.spi.ToolProvider;

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

public int run(PrintWriter out, PrintWriter err, String... args) {
try {
return Main.main(out, err, args);
} catch (Exception 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
@@ -0,0 +1 @@
com.google.googlejavaformat.java.GoogleJavaFormatToolProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 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)
Copy link
Contributor Author

@sormuras sormuras Sep 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw. this hurts a bit. When will Google upgrade to Jupiter aka JUnit 5️⃣ ? 🙂

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have any news on that front :(

public class GoogleJavaFormatToolProviderTest {

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

ToolProvider format =
ServiceLoader.load(ToolProvider.class).stream()
.map(ServiceLoader.Provider::get)
.filter(provider -> provider.name().equals(name))
.findAny()
.orElseThrow(() -> new AssertionError("Tool not found by name: " + name));

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).contains("https://github.com/google/google-java-format");
assertThat(usage).contains("Usage: google-java-format");
}
}