-
Notifications
You must be signed in to change notification settings - Fork 472
Feature/gherkin #1649
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
Merged
Merged
Feature/gherkin #1649
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2322641
Add a Gherkin formatter
jamietanna 184aa5e
Add Gradle bindings for Gherkin formatter
jamietanna 6f0a35c
Add Gherkin formatter changes to CHANGES
jamietanna 8d19346
Merge remote-tracking branch 'origin/main' into feature/gherkin
blacelle 39e4095
Rework based on Gherkin-utils
blacelle 1023d8c
Fix MD related files
blacelle 3a1652a
Fix tests
blacelle 7661516
Fix style
blacelle 9076bfb
Fix spotbugs
blacelle 551e4b8
Merge remote-tracking branch 'origin/main' into feature/gherkin
blacelle 86fc942
Rename simple to gherkinUtils
blacelle 9bc36a0
Add link to homepage and changelog
blacelle ac4884e
Rename all the GherkinSimple classes to GherkinUtils.
nedtwigg aefa8c8
Missing header update in plugin-maven/README.
nedtwigg 8a4cee9
Update the root README table.
nedtwigg 0514546
Rename the test to match the rest.
nedtwigg 5bf01af
Terrible trailing space is needed until cucumber/gherkin-utils#20 get…
nedtwigg 77190d4
Merge branch 'main' into feature/gherkin
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 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
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
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
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
60 changes: 60 additions & 0 deletions
60
lib/src/gherkin/java/com/diffplug/spotless/glue/gherkin/GherkinUtilsFormatterFunc.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,60 @@ | ||
/* | ||
* Copyright 2023 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.glue.gherkin; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.gherkin.GherkinUtilsConfig; | ||
|
||
import io.cucumber.gherkin.GherkinParser; | ||
import io.cucumber.gherkin.utils.pretty.Pretty; | ||
import io.cucumber.gherkin.utils.pretty.Syntax; | ||
import io.cucumber.messages.types.Envelope; | ||
import io.cucumber.messages.types.GherkinDocument; | ||
import io.cucumber.messages.types.Source; | ||
import io.cucumber.messages.types.SourceMediaType; | ||
|
||
public class GherkinUtilsFormatterFunc implements FormatterFunc { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(GherkinUtilsFormatterFunc.class); | ||
|
||
private final GherkinUtilsConfig gherkinSimpleConfig; | ||
|
||
public GherkinUtilsFormatterFunc(GherkinUtilsConfig gherkinSimpleConfig) { | ||
this.gherkinSimpleConfig = gherkinSimpleConfig; | ||
} | ||
|
||
// Follows https://github.com/cucumber/gherkin-utils/blob/main/java/src/test/java/io/cucumber/gherkin/utils/pretty/PrettyTest.java | ||
private GherkinDocument parse(String gherkin) { | ||
GherkinParser parser = GherkinParser | ||
.builder() | ||
.includeSource(false) | ||
.build(); | ||
return parser.parse(Envelope.of(new Source("test.feature", gherkin, SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN))) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("No envelope")) | ||
.getGherkinDocument() | ||
.orElseThrow(() -> new IllegalArgumentException("No gherkin document")); | ||
} | ||
|
||
@Override | ||
public String apply(String inputString) { | ||
GherkinDocument gherkinDocument = parse(inputString); | ||
|
||
return Pretty.prettyPrint(gherkinDocument, Syntax.gherkin); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
lib/src/main/java/com/diffplug/spotless/gherkin/GherkinUtilsConfig.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,38 @@ | ||
/* | ||
* Copyright 2023 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.gherkin; | ||
|
||
import java.io.Serializable; | ||
|
||
public class GherkinUtilsConfig implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public static int defaultIndentSpaces() { | ||
// https://cucumber.io/docs/gherkin/reference/ | ||
// Recommended indentation is 2 spaces | ||
return 2; | ||
} | ||
|
||
final int indentSpaces; | ||
|
||
public GherkinUtilsConfig(int indentSpaces) { | ||
this.indentSpaces = indentSpaces; | ||
} | ||
|
||
public int getIndentSpaces() { | ||
return indentSpaces; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
lib/src/main/java/com/diffplug/spotless/gherkin/GherkinUtilsStep.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,65 @@ | ||
/* | ||
* Copyright 2021-2023 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.gherkin; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Objects; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.JarState; | ||
import com.diffplug.spotless.Provisioner; | ||
|
||
public class GherkinUtilsStep { | ||
private static final String MAVEN_COORDINATE = "io.cucumber:gherkin-utils:"; | ||
private static final String DEFAULT_VERSION = "8.0.2"; | ||
|
||
public static String defaultVersion() { | ||
return DEFAULT_VERSION; | ||
} | ||
|
||
public static FormatterStep create(GherkinUtilsConfig gherkinSimpleConfig, | ||
String formatterVersion, Provisioner provisioner) { | ||
Objects.requireNonNull(provisioner, "provisioner cannot be null"); | ||
return FormatterStep.createLazy("gherkin", () -> new GherkinUtilsStep.State(gherkinSimpleConfig, formatterVersion, provisioner), GherkinUtilsStep.State::toFormatter); | ||
} | ||
|
||
private static final class State implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private final GherkinUtilsConfig gherkinSimpleConfig; | ||
private final JarState jarState; | ||
|
||
private State(GherkinUtilsConfig gherkinSimpleConfig, String formatterVersion, Provisioner provisioner) throws IOException { | ||
this.gherkinSimpleConfig = gherkinSimpleConfig; | ||
this.jarState = JarState.from(MAVEN_COORDINATE + formatterVersion, provisioner); | ||
} | ||
|
||
FormatterFunc toFormatter() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, | ||
InstantiationException, IllegalAccessException { | ||
Class<?> formatterFunc = jarState.getClassLoader().loadClass("com.diffplug.spotless.glue.gherkin.GherkinUtilsFormatterFunc"); | ||
Constructor<?> constructor = formatterFunc.getConstructor(GherkinUtilsConfig.class); | ||
return (FormatterFunc) constructor.newInstance(gherkinSimpleConfig); | ||
} | ||
} | ||
|
||
private GherkinUtilsStep() { | ||
// cannot be directly instantiated | ||
} | ||
} |
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
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
63 changes: 63 additions & 0 deletions
63
plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GherkinExtension.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,63 @@ | ||
/* | ||
* Copyright 2016-2023 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.gradle.spotless; | ||
|
||
import javax.inject.Inject; | ||
|
||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.gherkin.GherkinUtilsStep; | ||
|
||
public class GherkinExtension extends FormatExtension { | ||
static final String NAME = "gherkin"; | ||
|
||
@Inject | ||
public GherkinExtension(SpotlessExtension spotless) { | ||
super(spotless); | ||
} | ||
|
||
@Override | ||
protected void setupTask(SpotlessTask task) { | ||
if (target == null) { | ||
throw noDefaultTargetException(); | ||
} | ||
super.setupTask(task); | ||
} | ||
|
||
public GherkinUtilsConfig gherkinUtils() { | ||
return new GherkinUtilsConfig(); | ||
} | ||
|
||
public class GherkinUtilsConfig { | ||
private String version; | ||
private int indent; | ||
|
||
public GherkinUtilsConfig() { | ||
this.version = GherkinUtilsStep.defaultVersion(); | ||
this.indent = com.diffplug.spotless.gherkin.GherkinUtilsConfig.defaultIndentSpaces(); | ||
addStep(createStep()); | ||
} | ||
|
||
public void version(String version) { | ||
this.version = version; | ||
replaceStep(createStep()); | ||
} | ||
|
||
private FormatterStep createStep() { | ||
return GherkinUtilsStep.create(new com.diffplug.spotless.gherkin.GherkinUtilsConfig(indent), version, provisioner()); | ||
} | ||
} | ||
|
||
} |
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
44 changes: 44 additions & 0 deletions
44
plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GherkinExtensionTest.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,44 @@ | ||
/* | ||
* Copyright 2021-2023 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.gradle.spotless; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class GherkinExtensionTest extends GradleIntegrationHarness { | ||
@Test | ||
public void defaultFormatting() throws IOException { | ||
setFile("build.gradle").toLines( | ||
"plugins {", | ||
" id 'java'", | ||
" id 'com.diffplug.spotless'", | ||
"}", | ||
"repositories { mavenCentral() }", | ||
"spotless {", | ||
" gherkin {", | ||
" target 'examples/**/*.feature'", | ||
" gherkinUtils()", | ||
" }", | ||
"}"); | ||
setFile("src/main/resources/example.feature").toResource("gherkin/minimalBefore.feature"); | ||
setFile("examples/main/resources/example.feature").toResource("gherkin/minimalBefore.feature"); | ||
gradleRunner().withArguments("spotlessApply").build(); | ||
assertFile("src/main/resources/example.feature").sameAsResource("gherkin/minimalBefore.feature"); | ||
assertFile("examples/main/resources/example.feature").sameAsResource("gherkin/minimalAfter.feature"); | ||
} | ||
|
||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.