From 6e0599f0543d8307bcf9257c94a334ea1937bde0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Kostrzewa?= Date: Sat, 7 Dec 2019 11:57:14 +0100 Subject: [PATCH] Added 'require.at.least.one.test' flag to fail build when no test classes exist for specified manifest and/or regex only if it's set to true --- README.md | 1 + .../com/sforce/cd/apexUnit/ApexUnitRunner.java | 2 +- .../apexUnit/arguments/CommandLineArguments.java | 9 ++++++++- .../client/codeCoverage/CodeCoverageComputer.java | 4 ++-- .../apexUnit/client/testEngine/TestExecutor.java | 15 +++++++-------- .../client/utils/ApexClassFetcherUtils.java | 3 ++- 6 files changed, 21 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 1258332..431c725 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ Optional Parameters: - -max.test.execution.time.threshold : Maximum execution time(in minutes) for a test before it gets aborted - -proxy.host : Proxy host for external access - -proxy.port : Proxy port for external access +- -require.at.least.one.test : Fail if no test classes exist for specified manifest and/or regex pattern (defaults to `true`) - -help : Displays options available for running this application Note: You must provide either of the (-regex.for.selecting.source.classes.for.code.coverage.computation OR -manifest.files.with.source.class.names.for.code.coverage.computation) AND either of -(regex.for.selecting.test.classes.to.execute OR -manifest.files.with.test.class.names.to.execute) diff --git a/src/main/java/com/sforce/cd/apexUnit/ApexUnitRunner.java b/src/main/java/com/sforce/cd/apexUnit/ApexUnitRunner.java index fba195b..51d3d37 100644 --- a/src/main/java/com/sforce/cd/apexUnit/ApexUnitRunner.java +++ b/src/main/java/com/sforce/cd/apexUnit/ApexUnitRunner.java @@ -105,7 +105,7 @@ public static void main(String[] args) { LOG.info("Total test methods executed: " + apexReportBeans.length); String reportFile = "ApexUnitReport.xml"; ApexUnitTestReportGenerator.generateTestReport(apexReportBeans, reportFile); - } else { + } else if (CommandLineArguments.isRequireAtLeastOneTest()) { ApexUnitUtils.shutDownWithErrMsg("Unable to generate test report. " + "Did not find any test results for the job id"); } diff --git a/src/main/java/com/sforce/cd/apexUnit/arguments/CommandLineArguments.java b/src/main/java/com/sforce/cd/apexUnit/arguments/CommandLineArguments.java index d16a0a4..efda1cb 100644 --- a/src/main/java/com/sforce/cd/apexUnit/arguments/CommandLineArguments.java +++ b/src/main/java/com/sforce/cd/apexUnit/arguments/CommandLineArguments.java @@ -36,7 +36,8 @@ public class CommandLineArguments { public static final String PROXY_HOST = "-proxy.host"; public static final String PROXY_PORT = "-proxy.port"; public static final String TEST_RELOAD = "-test.reload"; - + public static final String REQUIRE_AT_LEAST_ONE_TEST = "-require.at.least.one.test"; + public static final String HELP = "-help"; /* @@ -79,6 +80,8 @@ public class CommandLineArguments { static private boolean help; @Parameter(names = TEST_RELOAD, description = "Want to reload test if same class changes submitted again.", arity=1) static private boolean testReload; + @Parameter(names = REQUIRE_AT_LEAST_ONE_TEST, description = "Fail if no test classes exist for specified manifest and/or regex pattern (defaults to true)", arity=1) + static private boolean requireAtLeastOneTest = true; /* * Static getter methods for each of the CLI parameter @@ -152,5 +155,9 @@ public static boolean isHelp() { public static boolean isTestReload() { return testReload; } + + public static boolean isRequireAtLeastOneTest() { + return requireAtLeastOneTest; + } } diff --git a/src/main/java/com/sforce/cd/apexUnit/client/codeCoverage/CodeCoverageComputer.java b/src/main/java/com/sforce/cd/apexUnit/client/codeCoverage/CodeCoverageComputer.java index db3e5c6..66311d3 100644 --- a/src/main/java/com/sforce/cd/apexUnit/client/codeCoverage/CodeCoverageComputer.java +++ b/src/main/java/com/sforce/cd/apexUnit/client/codeCoverage/CodeCoverageComputer.java @@ -80,7 +80,7 @@ public CodeCoverageComputer() { public ApexClassCodeCoverageBean[] calculateAggregatedCodeCoverageUsingToolingAPI() { PartnerConnection connection = ConnectionHandler.getConnectionHandlerInstance().getConnection(); - ApexClassCodeCoverageBean[] apexClassCodeCoverageBeans = null; + ApexClassCodeCoverageBean[] apexClassCodeCoverageBeans = new ApexClassCodeCoverageBean[0]; String[] classesAsArray = null; /* @@ -184,7 +184,7 @@ public ApexClassCodeCoverageBean[] calculateAggregatedCodeCoverageUsingToolingAP "Code coverage metrics not computed. Null object returned while processing the JSON response from the Tooling API"); } } - } else { + } else if (CommandLineArguments.isRequireAtLeastOneTest()) { ApexUnitUtils.shutDownWithErrMsg("No/Invalid Apex source classes mentioned in manifest file and/or " + "regex pattern for ApexSourceClassPrefix didn't return any Apex source class names from the org"); } diff --git a/src/main/java/com/sforce/cd/apexUnit/client/testEngine/TestExecutor.java b/src/main/java/com/sforce/cd/apexUnit/client/testEngine/TestExecutor.java index d1ac481..367656f 100644 --- a/src/main/java/com/sforce/cd/apexUnit/client/testEngine/TestExecutor.java +++ b/src/main/java/com/sforce/cd/apexUnit/client/testEngine/TestExecutor.java @@ -56,6 +56,9 @@ public ApexReportBean[] testExecutionFlow() { if (LOG.isDebugEnabled()) { ApexClassFetcherUtils.logTheFetchedApexClasses(testClassesAsArray); } + if (testClassesAsArray == null || testClassesAsArray.length == 0) { + return null; + } String soql = QueryConstructor.getQueryForApexClassInfo(processClassArrayForQuery(testClassesAsArray)); QueryResult queryresult = null; try { @@ -93,12 +96,9 @@ public ApexReportBean[] testExecutionFlow() { } - if(!submitTest){ + if(!submitTest) { ApexUnitUtils.shutDownWithErrMsg("Test for these classes already running/enqueue at server..."); - } - else{ - - if (testClassesAsArray != null && testClassesAsArray.length > 0) { + } else { int numOfBatches = 0; int fromIndex = 0; @@ -122,7 +122,7 @@ public ApexReportBean[] testExecutionFlow() { testClassesInBatch = Arrays.copyOfRange(testClassesAsArray, fromIndex, toIndex); parentJobId = bulkApiHandler.handleBulkApiFlow(conn, bulkConnection, testClassesInBatch); - + LOG.info("#####Parent JOB ID #####"+parentJobId); if (parentJobId != null) { LOG.info("Parent job ID for the submission of the test classes to the Force.com platform is: " @@ -138,8 +138,7 @@ public ApexReportBean[] testExecutionFlow() { } } - - } + } return apexReportBean.toArray(new ApexReportBean[0]); diff --git a/src/main/java/com/sforce/cd/apexUnit/client/utils/ApexClassFetcherUtils.java b/src/main/java/com/sforce/cd/apexUnit/client/utils/ApexClassFetcherUtils.java index b058c67..fa128d7 100644 --- a/src/main/java/com/sforce/cd/apexUnit/client/utils/ApexClassFetcherUtils.java +++ b/src/main/java/com/sforce/cd/apexUnit/client/utils/ApexClassFetcherUtils.java @@ -76,7 +76,8 @@ public static String[] constructTestClassesArray(PartnerConnection connection) { consolidatedTestClassesAsArray = testClassesAsArray; } // if null, no apex test classes fetched to execute; throw warning - if (consolidatedTestClassesAsArray == null || consolidatedTestClassesAsArray.length == 0) { + if (CommandLineArguments.isRequireAtLeastOneTest() + && (consolidatedTestClassesAsArray == null || consolidatedTestClassesAsArray.length == 0)) { ApexUnitUtils.shutDownWithErrMsg("No/Invalid test classes mentioned in manifest file and/or " + "regex pattern for ApexTestPrefix didn't return any test class names from the org"); } else {