-
Notifications
You must be signed in to change notification settings - Fork 455
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
Feature/pass filename to prettier #620
Merged
simschla
merged 12 commits into
diffplug:main
from
simschla:feature/pass-filename-to-prettier
Jun 23, 2020
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
322a292
add support for prettier to auto-detect parser
simschla b148f24
also test the filename-detection using a integrationTest
simschla fcbc706
simplify integration test in gradle and add one in maven
simschla cac68d3
add documentation for autodetection of parser
simschla 08f164d
add change to "CHANGES.md" files
simschla 36a66af
drive-by: make sure npm's correlated node is always first on path
simschla fbfe86c
revert opening StepHarness API to support filenames
simschla 4ac63f9
PR feedback: forwarding to only implementation
simschla 1953f8f
prevent illegal states by asserting non-null precondition
simschla 40048d0
StepHarnessWIthFile had too many degrees of freedom. Testing a step …
nedtwigg 41214ab
Most of the prettier steps don't depend on the filename, so there's n…
nedtwigg fb0390d
Respect "parser" the same as we respect "filepath"
nedtwigg 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
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
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
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
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
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
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
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
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
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
111 changes: 111 additions & 0 deletions
111
testlib/src/main/java/com/diffplug/spotless/StepHarnessWithFile.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,111 @@ | ||
/* | ||
* Copyright 2016-2020 DiffPlug | ||
* | ||
* 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.diffplug.spotless; | ||
|
||
import java.io.File; | ||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
import org.assertj.core.api.AbstractThrowableAssert; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.Assert; | ||
|
||
/** An api for testing a `FormatterStep` that depends on the File path. */ | ||
public class StepHarnessWithFile implements AutoCloseable { | ||
private final FormatterFunc formatter; | ||
|
||
private StepHarnessWithFile(FormatterFunc formatter) { | ||
this.formatter = Objects.requireNonNull(formatter); | ||
} | ||
|
||
/** Creates a harness for testing steps which do depend on the file. */ | ||
public static StepHarnessWithFile forStep(FormatterStep step) { | ||
// We don't care if an individual FormatterStep is misbehaving on line-endings, because | ||
// Formatter fixes that. No reason to care in tests either. It's likely to pop up when | ||
// running tests on Windows from time-to-time | ||
return new StepHarnessWithFile(FormatterFunc.Closeable.of( | ||
() -> { | ||
if (step instanceof FormatterStepImpl.Standard) { | ||
((FormatterStepImpl.Standard<?>) step).cleanupFormatterFunc(); | ||
} | ||
}, | ||
new FormatterFunc() { | ||
@Override | ||
public String apply(String input) throws Exception { | ||
return apply(input, new File("")); | ||
} | ||
|
||
@Override | ||
public String apply(String input, File source) throws Exception { | ||
return LineEnding.toUnix(step.format(input, source)); | ||
} | ||
})); | ||
} | ||
|
||
/** Creates a harness for testing a formatter whose steps do depend on the file. */ | ||
public static StepHarnessWithFile forFormatter(Formatter formatter) { | ||
return new StepHarnessWithFile(FormatterFunc.Closeable.of( | ||
formatter::close, | ||
input -> formatter.compute(input, new File("")))); | ||
} | ||
|
||
/** Asserts that the given element is transformed as expected, and that the result is idempotent. */ | ||
public StepHarnessWithFile test(File file, String before, String after) throws Exception { | ||
String actual = formatter.apply(before, file); | ||
Assert.assertEquals("Step application failed", after, actual); | ||
return testUnaffected(file, after); | ||
} | ||
|
||
/** Asserts that the given element is idempotent w.r.t the step under test. */ | ||
public StepHarnessWithFile testUnaffected(File file, String idempotentElement) throws Exception { | ||
String actual = formatter.apply(idempotentElement, file); | ||
Assert.assertEquals("Step is not idempotent", idempotentElement, actual); | ||
return this; | ||
} | ||
|
||
/** Asserts that the given elements in the resources directory are transformed as expected. */ | ||
public StepHarnessWithFile testResource(File file, String resourceBefore, String resourceAfter) throws Exception { | ||
String before = ResourceHarness.getTestResource(resourceBefore); | ||
String after = ResourceHarness.getTestResource(resourceAfter); | ||
return test(file, before, after); | ||
} | ||
|
||
/** Asserts that the given elements in the resources directory are transformed as expected. */ | ||
public StepHarnessWithFile testResourceUnaffected(File file, String resourceIdempotent) throws Exception { | ||
String idempotentElement = ResourceHarness.getTestResource(resourceIdempotent); | ||
return testUnaffected(file, idempotentElement); | ||
} | ||
|
||
/** Asserts that the given elements in the resources directory are transformed as expected. */ | ||
public StepHarnessWithFile testException(File file, String resourceBefore, Consumer<AbstractThrowableAssert<?, ? extends Throwable>> exceptionAssertion) throws Exception { | ||
String before = ResourceHarness.getTestResource(resourceBefore); | ||
try { | ||
formatter.apply(before, file); | ||
Assert.fail(); | ||
} catch (Throwable t) { | ||
AbstractThrowableAssert<?, ? extends Throwable> abstractAssert = Assertions.assertThat(t); | ||
exceptionAssertion.accept(abstractAssert); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (formatter instanceof FormatterFunc.Closeable) { | ||
((FormatterFunc.Closeable) formatter).close(); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
testlib/src/main/resources/npm/prettier/filename/clean.json
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,11 @@ | ||
{ | ||
"hello": "world", | ||
"hello int": 123, | ||
"hello array": ["1", "2", "3", "4"], | ||
"hello object": { | ||
"property1": "111111", | ||
"property2": "2222222", | ||
"property3": "333333333", | ||
"property4": "44444444" | ||
} | ||
} |
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 @@ | ||
{ "hello": "world", "hello int": 123, "hello array":[ "1", "2", "3", "4"], "hello object":{ "property1":"111111","property2":"2222222","property3":"333333333","property4":"44444444"} } |
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
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.
As a reaction to the discussion in #619 - make sure we always use the node binary associated with the npm binary we use.