diff options
author | 2020-08-03 21:16:36 +0800 | |
---|---|---|
committer | 2020-08-03 22:42:52 +0800 | |
commit | 6052789c154452e2198ddf57983d626ba870c321 (patch) | |
tree | 34829991b8c39f60cae9ff12776725affef34faa | |
parent | make MavenEbuilder not print unnecessary lines (diff) | |
download | java-ebuilder-6052789c154452e2198ddf57983d626ba870c321.tar.gz java-ebuilder-6052789c154452e2198ddf57983d626ba870c321.tar.bz2 java-ebuilder-6052789c154452e2198ddf57983d626ba870c321.zip |
initial effort to support multiple testing frameworks
Signed-off-by: zongyu <zzy2529420793@gmail.com>
6 files changed, 161 insertions, 39 deletions
diff --git a/src/main/java/org/gentoo/java/ebuilder/Config.java b/src/main/java/org/gentoo/java/ebuilder/Config.java index 27dbc7c..dbfa30b 100644 --- a/src/main/java/org/gentoo/java/ebuilder/Config.java +++ b/src/main/java/org/gentoo/java/ebuilder/Config.java @@ -99,6 +99,14 @@ public class Config { */ private final PrintWriter stdoutWriter; /** + * URI that goes to *-test-sources.jar distributed by maven central + */ + private URI testSrcUri; + /** + * whether testSrcUri is set and whether the pkg is from maven central. + */ + private boolean testSrcUriExists; + /** * Path to workdir. */ private Path workdir; @@ -378,6 +386,35 @@ public class Config { } /** + * Getter for {@link #testSrcUri}. + * + * @return {@link #testSrcUri} + */ + public URI getTestSrcUri() { + return testSrcUri; + } + + /** + * Getter for {@link #testSrcUriExists}. + * + * @return {@link #testSrcUriExists} + */ + public boolean hasTestSrcUri() { + return testSrcUriExists; + } + + /** + * Setter for {@link #testSrcUri}. + * + * @param testSrcUri {@link #testSrcUri} + */ + public void setTestSrcUri(final URI testSrcUri) { + this.testSrcUri = testSrcUri; + if (isFromMavenCentral()) { + this.testSrcUriExists = true; + } + } + /** * Getter for {@link #workdir}. * * @return {@link #workdir} @@ -429,6 +466,9 @@ public class Config { */ public void setFromMavenCentral(final boolean fromMavenCentral) { this.fromMavenCentral = fromMavenCentral; + if (getTestSrcUri() != null) { + this.testSrcUriExists = true; + } } /** diff --git a/src/main/java/org/gentoo/java/ebuilder/Main.java b/src/main/java/org/gentoo/java/ebuilder/Main.java index 72e0a04..49f6a49 100644 --- a/src/main/java/org/gentoo/java/ebuilder/Main.java +++ b/src/main/java/org/gentoo/java/ebuilder/Main.java @@ -219,7 +219,9 @@ public class Main { try { config.setBinjarUri(new URI(args[i])); } catch (final URISyntaxException ex) { - config.getErrorWriter().println("ERROR: BINJAR_URI " + args[i] + config.getErrorWriter().println( + "ERROR: URI goes to binary jar " + + args[i] + " is not valid."); } @@ -292,6 +294,19 @@ public class Main { i++; config.setSlot(args[i]); break; + case "--test-src-uri": + i++; + + try { + config.setTestSrcUri(new URI(args[i])); + } catch (final URISyntaxException ex) { + config.getErrorWriter().println( + "ERROR: URI that goes to src code for testing" + + args[i] + + " is not valid."); + } + + break; case "--workdir": case "-w": i++; diff --git a/src/main/java/org/gentoo/java/ebuilder/maven/MavenEbuilder.java b/src/main/java/org/gentoo/java/ebuilder/maven/MavenEbuilder.java index 857d6c1..b9778ef 100644 --- a/src/main/java/org/gentoo/java/ebuilder/maven/MavenEbuilder.java +++ b/src/main/java/org/gentoo/java/ebuilder/maven/MavenEbuilder.java @@ -6,9 +6,11 @@ import java.io.PrintWriter; import java.nio.file.Path; import java.time.LocalDate; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.Set; import org.gentoo.java.ebuilder.Config; /** @@ -30,6 +32,14 @@ public class MavenEbuilder { = "https://wiki.gentoo.org/wiki/No_homepage"; /** + * The extra dependency for framework "pkgdiff" + */ + private static final String dependencyForPkgdiff + = "\n\t\tamd64? (\n\t\t\tdev-util/pkgdiff" + + "\n\t\t\tdev-util/japi-compliance-checker" + + "\n\t\t)"; + + /** * EAPI version. */ private static final String EAPI = "7"; @@ -42,7 +52,13 @@ public class MavenEbuilder { * Pattern for checking whether download tarball name matches expected name. */ private static final Pattern PATTERN_TARBALL_NAME - = Pattern.compile("^.*/\\$\\{P\\}-sources.((?:\\.tar)\\.\\S+|(?:\\.jar))$"); + = Pattern.compile("^.*/\\$\\{P\\}-sources((?:\\.tar)\\.\\S+|(?:\\.jar))$"); + + /** + * Pattern for checking whether download tarball for testing name matches expected name. + */ + private static final Pattern PATTERN_TEST_TARBALL_NAME + = Pattern.compile("^.*/\\$\\{P\\}-test-sources\\.jar$"); /** * Pattern for checking whether the dependency is specifying versions. @@ -139,9 +155,10 @@ public class MavenEbuilder { * @return testing framework name or null */ private String determineTestingFramework( - final List<MavenProject> mavenProjects) { + final List<MavenProject> mavenProjects, final Config config) { for (final MavenProject mavenProject : mavenProjects) { - final String result = determineTestingFramework(mavenProject); + final String result + = determineTestingFramework(mavenProject, config); if (result != null) { return result; @@ -158,24 +175,61 @@ public class MavenEbuilder { * * @return testing framework name or null */ - private String determineTestingFramework(final MavenProject mavenProject) { + private String determineTestingFramework( + final MavenProject mavenProject, final Config config) { + Set<String> frameworks = new HashSet<>(10); + for (final MavenDependency dependency : mavenProject. getTestDependencies()) { - if ("junit".equals(dependency.getGroupId()) - && "junit".equals(dependency.getArtifactId())) { - return "junit"; - } + frameworks.add(determineTestingFrameworkByDependency(dependency)); } for (final MavenDependency dependency : mavenProject. getCommonDependencies()) { - if ("junit".equals(dependency.getGroupId()) - && "junit".equals(dependency.getArtifactId())) { - return "junit"; - } + frameworks.add(determineTestingFrameworkByDependency(dependency)); } - return null; + if (config.hasBinjarUri()) { + frameworks.add("pkgdiff"); + } + + frameworks.remove(null); + + if (frameworks.size() == 0) { + return null; + } else { + return String.join(" ", frameworks); + } + } + + /** + * Determines the testing framework based on project dependencies. + * + * @param mavenProject maven project + * + * @return testing framework name or null + */ + private String determineTestingFrameworkByDependency( + final MavenDependency dependency) { + /** TODO: missing support for + * "org.junit.vintage:junit-vintage-engine" tests + * "POJO" tests + * "spock" tests + * "cucumber" tests + */ + if ("junit".equals(dependency.getGroupId()) + && "junit".equals(dependency.getArtifactId())) { + return "junit"; + } else if ("org.testng".equals(dependency.getGroupId()) + && "testng".equals(dependency.getArtifactId())) { + return "testng"; + } else if ("org.junit.jupiter".equals(dependency.getGroupId()) + && "junit-jupiter-engine". + equals(dependency.getArtifactId())) { + return "junit-5"; + } else { + return null; + } } /** @@ -275,30 +329,22 @@ public class MavenEbuilder { } /** - * If the tarball name does not match pattern ${P}-test.ext then we will update - * it to store the tarball as ${P}-test.ext. + * If the tarball name does not match pattern ${P}-test-sources.jar + * we will update it to store the tarball as ${P}-test-sources.jar. + * Note that we only need it for Maven central artifacts, so it is + * safe to assume that the extension should be ".jar". * * @param TestSrcUri source test URI * * @return either original source test URI or updated source test URI + */ private String improveTestSrcUri(final String TestSrcUri) { if (PATTERN_TEST_TARBALL_NAME.matcher(TestSrcUri).matches()) { return TestSrcUri; } - final Matcher matcher = PATTERN_TARBALL_EXTENSION.matcher(TestSrcUri); - - /** - * We do not know how to get the extension so we will leave the tarball - * name as it is. - / - if (!matcher.matches()) { - return TestSrcUri; - } - - return TestSrcUri + " -> " + "${P}-test" + matcher.group(1); + return TestSrcUri + " -> " + "${P}-test-sources.jar"; } - */ /** * Merges maven project system dependencies of specified type and removed @@ -309,15 +355,6 @@ public class MavenEbuilder { * and "test") * * @return list of merged dependencies - /** - * Merges maven project system dependencies of specified type and removed - * duplicates. - * - * @param mavenProjects list of maven projects - * @param type type of dependencies ("common", "compile", "runtime" - * and "test") - * - * @return list of merged dependencies */ private List<String> mergeSystemDependencies( final List<MavenProject> mavenProjects, final String type) { @@ -401,6 +438,11 @@ public class MavenEbuilder { writer.print(config.getBinjarUri()); } + if (config.hasTestSrcUri()) { + writer.print(" --test-src-uri "); + writer.print(config.getTestSrcUri()); + } + if (config.getLicense() != null) { writer.print(" --license "); writer.print(config.getLicense()); @@ -526,7 +568,7 @@ public class MavenEbuilder { writer.println("\t)"); } - if (!testDependencies.isEmpty()) { + if (!testDependencies.isEmpty() || config.hasBinjarUri()) { writer.println("\ttest? ("); testDependencies.stream().forEach((dependency) -> { @@ -534,6 +576,11 @@ public class MavenEbuilder { writer.println(dependency); }); + // TODO: check whether amd64 is inside KEYWORDS + if (config.hasBinjarUri()) { + writer.println(dependencyForPkgdiff); + } + writer.println("\t)"); } @@ -734,6 +781,10 @@ public class MavenEbuilder { writer.print("\n\t" + improveBinjarUri( replaceWithVars(config.getBinjarUri().toString(), config))); } + if (config.hasTestSrcUri()) { + writer.print("\n\t" + improveTestSrcUri( + replaceWithVars(config.getTestSrcUri().toString(), config))); + } writer.println('"'); writer.print("LICENSE=\""); @@ -838,7 +889,8 @@ public class MavenEbuilder { writer.println("JAVA_BINJAR_FILENAME=\"${P}-bin.jar\""); } - final String testingFramework = determineTestingFramework(mavenProject); + final String testingFramework + = determineTestingFramework(mavenProject, config); boolean firstTestVar = true; if (testingFramework != null) { diff --git a/src/main/java/org/gentoo/java/ebuilder/maven/MavenParser.java b/src/main/java/org/gentoo/java/ebuilder/maven/MavenParser.java index d487024..73acd6e 100644 --- a/src/main/java/org/gentoo/java/ebuilder/maven/MavenParser.java +++ b/src/main/java/org/gentoo/java/ebuilder/maven/MavenParser.java @@ -44,6 +44,7 @@ public class MavenParser { final MavenProject mavenProject = parsePom(config, mavenCache, pomFile, effectivePom); + // TODO: I suppose they should go to "POJO" tests if (mavenProject.hasTests() && mavenProject.getTestDependencies().isEmpty()) { mavenProject.addDependency(new MavenDependency( @@ -51,6 +52,10 @@ public class MavenParser { mavenCache.getDependency("junit", "junit", "4.11"))); } + if (config.hasTestSrcUri()) { + mavenProject.setHasTests(true); + } + result.add(mavenProject); }); diff --git a/src/main/java/org/gentoo/java/ebuilder/maven/MavenProject.java b/src/main/java/org/gentoo/java/ebuilder/maven/MavenProject.java index 2fd88de..4ce11b8 100644 --- a/src/main/java/org/gentoo/java/ebuilder/maven/MavenProject.java +++ b/src/main/java/org/gentoo/java/ebuilder/maven/MavenProject.java @@ -561,6 +561,15 @@ public class MavenProject { } /** + * Setter for {@link #hasTests} + * + * @param hasTests {@link #hasTests} + */ + public void setHasTests(boolean hasTests) { + this.hasTests = hasTests; + } + + /** * Returns dependencies based on the specified scopes. * * @param scopes array of scopes diff --git a/src/main/resources/usage.txt b/src/main/resources/usage.txt index 78ab745..1ceceeb 100644 --- a/src/main/resources/usage.txt +++ b/src/main/resources/usage.txt @@ -3,6 +3,7 @@ Usage: java-ebuilder <args> Generating ebuild: --download-uri|-u <uri> uri that goes to SRC_URI --binjar-uri <uri> uri that goes to binary jar +--test-src-uri <uri> uri that goes to test-sources from Maven Central --dump-projects|-d outputs information about parsed projects (useful for debugging) --ebuild|-e <file> path to ebuild that should be created |