-
Notifications
You must be signed in to change notification settings - Fork 903
Implement ToolProvider SPI
#656
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
core/src/main/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
core/src/main/resources/META-INF/services/java.util.spi.ToolProvider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| com.google.googlejavaformat.java.GoogleJavaFormatToolProvider |
55 changes: 55 additions & 0 deletions
55
core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProviderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| 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"); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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️⃣ ? 🙂
There was a problem hiding this comment.
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 :(