forked from TechnologyBrewery/habushu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HAB-TechnologyBrewery#124 Add support for Python linting to check for…
… issues before packaging
- Loading branch information
1 parent
5d66918
commit caf8b3b
Showing
11 changed files
with
234 additions
and
8 deletions.
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
89 changes: 89 additions & 0 deletions
89
...hu-maven-plugin/src/main/java/org/technologybrewery/habushu/ValidatePythonSourceMojo.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,89 @@ | ||
package org.technologybrewery.habushu; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugins.annotations.LifecyclePhase; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
import org.apache.maven.plugins.annotations.ResolutionScope; | ||
import org.technologybrewery.habushu.exec.PoetryCommandHelper; | ||
|
||
/** | ||
* Leverages the lint package to validate both source and test Python | ||
* directories using Poetry's run command. | ||
*/ | ||
@Mojo(name = "validate-python-source", defaultPhase = LifecyclePhase.PROCESS_CLASSES, requiresDependencyResolution = ResolutionScope.COMPILE) | ||
public class ValidatePythonSourceMojo extends AbstractHabushuMojo { | ||
|
||
protected static final String LINT_PACKAGE = "pylint"; | ||
|
||
/** | ||
* By default, linting will be enabled on the source module. Can be configured to false so that linting is | ||
* not triggered during build. | ||
*/ | ||
@Parameter(property = "habushu.lintSource", required = false, defaultValue = "true") | ||
private boolean lintSource; | ||
|
||
/** | ||
* Specifies disabled checkers for lint on source module. | ||
*/ | ||
@Parameter(property = "habushu.sourceLintDisabledChecker", required = false, defaultValue = "C,R,W") | ||
private String sourceLintDisabledChecker; | ||
|
||
/** | ||
* Specifies enabled checkers for lint on source module. | ||
*/ | ||
@Parameter(property = "habushu.sourceLintEnabledChecker", required = false) | ||
private String sourceLintEnabledChecker; | ||
|
||
/** | ||
* By default, build will stop if lint errors are found in source module. Can be configured to true so that | ||
* build will continue. | ||
*/ | ||
@Parameter(property = "habushu.sourceFailOnLintErrors", required = false, defaultValue = "true") | ||
private boolean sourceFailOnLintErrors; | ||
|
||
@Override | ||
public void doExecute() throws MojoExecutionException { | ||
if (lintSource) { | ||
lintSource(); | ||
} | ||
} | ||
|
||
public void lintSource() throws MojoExecutionException { | ||
PoetryCommandHelper poetryHelper = createPoetryCommandHelper(); | ||
List<String> executeLintArgsSource = new ArrayList<>(); | ||
if (this.sourceDirectory.exists()) { | ||
executeLintArgsSource.addAll(Arrays.asList("run", LINT_PACKAGE)); | ||
executeLintArgsSource.add(getCanonicalPathForFile(sourceDirectory)); | ||
|
||
if (!poetryHelper.isDependencyInstalled(LINT_PACKAGE)) { | ||
getLog().info( | ||
String.format("%s dependency not specified in pyproject.toml - installing now...", LINT_PACKAGE)); | ||
poetryHelper.installDevelopmentDependency(LINT_PACKAGE); | ||
} | ||
|
||
if (StringUtils.isNotEmpty(sourceLintDisabledChecker)) { | ||
executeLintArgsSource.addAll(Arrays.asList("--disable", sourceLintDisabledChecker)); | ||
} | ||
|
||
if (StringUtils.isNotEmpty(sourceLintEnabledChecker)) { | ||
executeLintArgsSource.addAll(Arrays.asList("--enable", sourceLintEnabledChecker)); | ||
} | ||
|
||
if (!sourceFailOnLintErrors) { | ||
executeLintArgsSource.add("--exit-zero"); | ||
} | ||
|
||
getLog().info("Validating code in source directory using Pylint..."); | ||
poetryHelper.executeAndLogOutput(executeLintArgsSource); | ||
} else { | ||
getLog().warn(String.format("Configured source (%s) directory does not exist - skipping...", | ||
sourceDirectory)); | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
habushu-maven-plugin/src/main/java/org/technologybrewery/habushu/ValidatePythonTestMojo.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,86 @@ | ||
package org.technologybrewery.habushu; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugins.annotations.LifecyclePhase; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
import org.apache.maven.plugins.annotations.ResolutionScope; | ||
import org.technologybrewery.habushu.exec.PoetryCommandHelper; | ||
|
||
@Mojo(name = "validate-python-test", defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES, requiresDependencyResolution = ResolutionScope.COMPILE) | ||
public class ValidatePythonTestMojo extends AbstractHabushuMojo { | ||
|
||
protected static final String LINT_PACKAGE = "pylint"; | ||
|
||
/** | ||
* By default, linting will be enabled on the test module. Can be configured to false so that linting is | ||
* not triggered during build. | ||
*/ | ||
@Parameter(property = "habushu.lintTest", required = false, defaultValue = "true") | ||
private boolean lintTest; | ||
|
||
/** | ||
* Specifies disabled checkers for lint on test module. | ||
*/ | ||
@Parameter(property = "habushu.testLintDisabledChecker", required = false, defaultValue = "C,R,W,E0102") | ||
private String testLintDisabledChecker; | ||
|
||
/** | ||
* Specifies enabled checkers for lint on test module. | ||
*/ | ||
@Parameter(property = "habushu.testLintEnabledChecker", required = false) | ||
private String testLintEnabledChecker; | ||
|
||
/** | ||
* By default, build will stop if lint errors are found in test module. Can be configured to true so that | ||
* pylint does interrupt build. | ||
*/ | ||
@Parameter(property = "habushu.testFailOnLintErrors", required = false, defaultValue = "true") | ||
private boolean testFailOnLintErrors; | ||
|
||
@Override | ||
public void doExecute() throws MojoExecutionException { | ||
if (lintTest) { | ||
lintTest(); | ||
} | ||
} | ||
|
||
public void lintTest() throws MojoExecutionException { | ||
PoetryCommandHelper poetryHelper = createPoetryCommandHelper(); | ||
List<String> executeLintArgsTest = new ArrayList<>(); | ||
if (this.testDirectory.exists()) { | ||
executeLintArgsTest.addAll(Arrays.asList("run", LINT_PACKAGE)); | ||
executeLintArgsTest.add(getCanonicalPathForFile(testDirectory)); | ||
if (!poetryHelper.isDependencyInstalled(LINT_PACKAGE)) { | ||
getLog().info( | ||
String.format("%s dependency not specified in pyproject.toml - installing now...", LINT_PACKAGE)); | ||
poetryHelper.installDevelopmentDependency(LINT_PACKAGE); | ||
} | ||
|
||
if (StringUtils.isNotEmpty(testLintDisabledChecker)) { | ||
executeLintArgsTest.addAll(Arrays.asList("--disable", testLintDisabledChecker)); | ||
} | ||
|
||
if (StringUtils.isNotEmpty(testLintEnabledChecker)) { | ||
executeLintArgsTest.addAll(Arrays.asList("--enable", testLintEnabledChecker)); | ||
} | ||
|
||
if (!testFailOnLintErrors) { | ||
executeLintArgsTest.add("--exit-zero"); | ||
} | ||
|
||
executeLintArgsTest.add("--recursive=true"); | ||
|
||
getLog().info("Validating code in test directory using Pylint..."); | ||
poetryHelper.executeAndLogOutput(executeLintArgsTest); | ||
} else { | ||
getLog().warn(String.format("Configured test (%s) directory does not exist - skipping...", | ||
testDirectory)); | ||
} | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
habushu-mixology-consumer/tests/features/steps/generate-report-with-manual-tests.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from behave import * | ||
from behave import when, then # pylint: disable=no-name-in-module | ||
from habushu_mixology.reusable_module.worker import SubWorker | ||
from habushu_mixology.helloworld import generate_random_string | ||
from habushu_mixology.generated import person_pb2 | ||
|
@@ -9,7 +9,7 @@ | |
def step_impl(context): | ||
logging.info("Referencing a src file...") | ||
context.random = generate_random_string(5) | ||
person = person_pb2.Person() | ||
person = person_pb2.Person() # pylint: disable=no-member | ||
person.email = "[email protected]" | ||
context.person = person | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
habushu-mixology/tests/features/steps/reference_test_resources.py
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
2 changes: 1 addition & 1 deletion
2
habushu-mixology/tests/features/steps/use_environment_variables.py
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