-
Notifications
You must be signed in to change notification settings - Fork 134
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
Test helper for refaster checks #697
Merged
+413
−1
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
45e6fc4
Error-prone testing library
gatesn 1c242b6
refactory
gatesn d0c6e56
refactory
gatesn c2de832
Create an error-prone refaster testing helper
gatesn e0cd741
Create an error-prone refaster testing helper
gatesn d4b1415
Add generated changelog entries
gatesn 9998b82
Remove static imports
gatesn 44036fa
Merge branch 'ngates/error-prone-testing-lib' of github.com:palantir/…
gatesn d54ad61
Remove static imports
gatesn c9030cd
My checkstyle was broken
gatesn a9263a7
Address comments
gatesn 9c44fc2
Remove test
gatesn 618587b
Remove plugins
gatesn 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
This file contains 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 |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
.project | ||
.settings | ||
build | ||
/out/ | ||
out/ | ||
gradle.properties | ||
docs/node_modules/ | ||
generated_src |
This file contains 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
40 changes: 40 additions & 0 deletions
40
...e-refaster-rules/src/test/java/com/palantir/baseline/refaster/CollectionsIsEmptyTest.java
This file contains 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,40 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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.palantir.baseline.refaster; | ||
|
||
import org.junit.Test; | ||
|
||
public class CollectionsIsEmptyTest { | ||
|
||
@Test | ||
public void test() { | ||
RefasterTestHelper | ||
.forRefactoring(CollectionsIsEmpty.class) | ||
.withInputLines( | ||
"Test", | ||
"import java.util.ArrayList;", | ||
"public class Test {", | ||
" boolean empty = new ArrayList<>().size() == 0;", | ||
"}") | ||
.hasOutputLines( | ||
"import java.util.ArrayList;", | ||
"public class Test {", | ||
" boolean empty = new ArrayList<>().isEmpty();", | ||
"}"); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...faster-rules/src/test/java/com/palantir/baseline/refaster/OptionalOrElseSupplierTest.java
This file contains 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,44 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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.palantir.baseline.refaster; | ||
|
||
import org.junit.Test; | ||
|
||
public class OptionalOrElseSupplierTest { | ||
|
||
@Test | ||
public void test() { | ||
RefasterTestHelper | ||
.forRefactoring(OptionalOrElseSupplier.class) | ||
.withInputLines( | ||
"Test", | ||
"import java.util.*;", | ||
"import java.util.function.Supplier;", | ||
"public class Test {", | ||
" Supplier<String> supplier = () -> \"hello\";", | ||
" String s = Optional.ofNullable(\"world\").orElse(supplier.get());", | ||
"}") | ||
.hasOutputLines( | ||
"import java.util.*;", | ||
"import java.util.function.Supplier;", | ||
"public class Test {", | ||
" Supplier<String> supplier = () -> \"hello\";", | ||
" String s = Optional.ofNullable(\"world\").orElseGet(supplier);", | ||
"}"); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/SortedFirstTest.java
This file contains 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,45 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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.palantir.baseline.refaster; | ||
|
||
import org.junit.Test; | ||
|
||
public class SortedFirstTest { | ||
|
||
@Test | ||
public void test() { | ||
RefasterTestHelper | ||
.forRefactoring(SortedFirst.class) | ||
.withInputLines( | ||
"Test", | ||
"import java.util.*;", | ||
"import java.util.stream.Stream;", | ||
"public class Test {", | ||
" Optional<Integer> i = Arrays.asList(5, -10, 7, -18, 23).stream()", | ||
" .sorted(Comparator.reverseOrder())", | ||
" .findFirst();", | ||
"}") | ||
.hasOutputLines( | ||
"import java.util.*;", | ||
"import java.util.stream.Stream;", | ||
"public class Test {", | ||
" Optional<Integer> i = Arrays.asList(5, -10, 7, -18, 23).stream()" | ||
+ ".min(Comparator.reverseOrder());", | ||
"}"); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/Utf8LengthTest.java
This file contains 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,41 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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.palantir.baseline.refaster; | ||
|
||
import org.junit.Test; | ||
|
||
public class Utf8LengthTest { | ||
|
||
@Test | ||
public void test() { | ||
RefasterTestHelper | ||
.forRefactoring(Utf8Length.class) | ||
.withInputLines( | ||
"Test", | ||
"import java.nio.charset.StandardCharsets;", | ||
"public class Test {", | ||
" int i = \"hello world\".getBytes(StandardCharsets.UTF_8).length;", | ||
"}") | ||
.hasOutputLines( | ||
"import com.google.common.base.Utf8;", | ||
"import java.nio.charset.StandardCharsets;", | ||
"public class Test {", | ||
" int i = Utf8.encodedLength(\"hello world\");", | ||
"}"); | ||
} | ||
|
||
} |
This file contains 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,29 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
apply plugin: 'nebula.maven-publish' | ||
apply plugin: 'nebula.source-jar' | ||
apply plugin: 'java-library' | ||
apply from: "${rootDir}/gradle/publish-jar.gradle" | ||
|
||
dependencies { | ||
compile 'com.google.errorprone:error_prone_refaster' | ||
compile 'com.google.errorprone:error_prone_test_helpers' | ||
|
||
compile 'com.google.guava:guava' | ||
compile 'junit:junit' | ||
compile 'org.assertj:assertj-core' | ||
} |
88 changes: 88 additions & 0 deletions
88
baseline-refaster-testing/src/main/java/com/palantir/baseline/refaster/CompilerUtility.java
This file contains 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,88 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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.palantir.baseline.refaster; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.io.CharStreams; | ||
import com.sun.source.tree.CompilationUnitTree; | ||
import com.sun.tools.javac.api.JavacTaskImpl; | ||
import com.sun.tools.javac.api.JavacTool; | ||
import com.sun.tools.javac.util.Context; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import javax.tools.Diagnostic; | ||
import javax.tools.DiagnosticCollector; | ||
import javax.tools.JavaCompiler; | ||
import javax.tools.JavaFileObject; | ||
import javax.tools.StandardJavaFileManager; | ||
|
||
final class CompilerUtility { | ||
|
||
private CompilerUtility() {} | ||
|
||
static CompilerResult compile(JavaFileObject javaFileObject) { | ||
JavaCompiler compiler = JavacTool.create(); | ||
DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<>(); | ||
StandardJavaFileManager fileManager = | ||
compiler.getStandardFileManager(diagnosticsCollector, Locale.ENGLISH, StandardCharsets.UTF_8); | ||
|
||
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask( | ||
CharStreams.nullWriter(), | ||
fileManager, | ||
diagnosticsCollector, | ||
ImmutableList.of(), | ||
null, | ||
ImmutableList.of(javaFileObject)); | ||
|
||
Iterable<? extends CompilationUnitTree> trees; | ||
try { | ||
trees = task.parse(); | ||
task.analyze(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return new CompilerResult() { | ||
@Override | ||
public Context context() { | ||
return task.getContext(); | ||
} | ||
|
||
@Override | ||
public List<CompilationUnitTree> compilationUnits() { | ||
return ImmutableList.copyOf(trees); | ||
} | ||
|
||
@Override | ||
public List<Diagnostic<? extends JavaFileObject>> diagnostics() { | ||
return diagnosticsCollector.getDiagnostics(); | ||
} | ||
}; | ||
} | ||
|
||
interface CompilerResult { | ||
|
||
Context context(); | ||
|
||
List<CompilationUnitTree> compilationUnits(); | ||
|
||
List<Diagnostic<? extends JavaFileObject>> diagnostics(); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.
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.
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.
ah also don't apply this, publish-jar does the right thing here
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.
because of
gradle-baseline/gradle/publish-jar.gradle
Lines 3 to 4 in 9efd8cc