Log error messages and throw exit status exceptions to stop processing

This commit is contained in:
Erik C. Thauvin 2024-06-27 20:33:04 -07:00
parent 4fa74c3efd
commit 8db60b473c
Signed by: erik
GPG key ID: 776702A6A2DA330E

View file

@ -18,6 +18,7 @@ package rife.bld.extension;
import rife.bld.BaseProject; import rife.bld.BaseProject;
import rife.bld.operations.TestOperation; import rife.bld.operations.TestOperation;
import rife.bld.operations.exceptions.ExitStatusException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -137,6 +138,23 @@ public class TestNgOperation extends TestOperation<TestNgOperation, List<String>
return this; return this;
} }
@Override
public void execute() throws IOException, InterruptedException, ExitStatusException {
if (project_ == null) {
if (LOGGER.isLoggable(Level.SEVERE) && !silent()) {
LOGGER.severe("A project must be specified.");
}
throw new ExitStatusException(ExitStatusException.EXIT_FAILURE);
} else if (packages_.isEmpty() && suites_.isEmpty() && methods_.isEmpty()) {
if (LOGGER.isLoggable(Level.SEVERE) && !silent()) {
LOGGER.severe("At least an XML suite, package or method is required.");
}
throw new ExitStatusException(ExitStatusException.EXIT_FAILURE);
} else {
super.execute();
}
}
/** /**
* Part of the {@link #execute execute} operation, constructs the command list to use for building the process. * Part of the {@link #execute execute} operation, constructs the command list to use for building the process.
* *
@ -144,24 +162,20 @@ public class TestNgOperation extends TestOperation<TestNgOperation, List<String>
*/ */
@Override @Override
protected List<String> executeConstructProcessCommandList() { protected List<String> executeConstructProcessCommandList() {
if (project_ == null) {
LOGGER.severe("A project must be specified.");
} else if (packages_.isEmpty() && suites_.isEmpty()) {
LOGGER.severe("At least one package or XML suite is required.");
}
if (!options_.containsKey("-d")) {
options_.put("-d", Path.of(project_.buildDirectory().getPath(), "test-output").toString());
}
final List<String> args = new ArrayList<>(); final List<String> args = new ArrayList<>();
if (project_ != null) {
if (!options_.containsKey("-d")) {
options_.put("-d", new File(project_.buildDirectory(), "test-output").getAbsolutePath());
}
args.add(javaTool()); args.add(javaTool());
args.addAll(javaOptions()); args.addAll(javaOptions());
args.add("-cp"); args.add("-cp");
if (testClasspath_.isEmpty()) { if (testClasspath_.isEmpty()) {
args.add(String.format("%s:%s:%s:%s", Path.of(project_.libTestDirectory().getPath(), "*"), args.add(String.format("%s:%s:%s:%s", new File(project_.libTestDirectory(), "*"),
Path.of(project_.libCompileDirectory().getPath(), "*"), project_.buildMainDirectory(), new File(project_.libCompileDirectory(), "*"), project_.buildMainDirectory(),
project_.buildTestDirectory())); project_.buildTestDirectory()));
} else { } else {
args.add(String.join(":", testClasspath_)); args.add(String.join(":", testClasspath_));
@ -178,17 +192,21 @@ public class TestNgOperation extends TestOperation<TestNgOperation, List<String>
args.addAll(suites_); args.addAll(suites_);
} else if (!options_.containsKey("-testclass")) { } else if (!options_.containsKey("-testclass")) {
try { try {
args.add(writeDefaultSuite().getPath()); args.add(writeDefaultSuite().getAbsolutePath());
} catch (IOException ioe) { } catch (IOException ioe) {
LOGGER.log(Level.SEVERE, "An IO error occurred while accessing the default testng.xml file", ioe); if (LOGGER.isLoggable(Level.SEVERE) && !silent()) {
LOGGER.severe("An IO error occurred while accessing the default testng.xml file: "
+ ioe.getMessage());
}
throw new RuntimeException(ioe);
} }
} }
if (LOGGER.isLoggable(Level.FINE)) {
if (LOGGER.isLoggable(Level.FINE) && !silent()) {
LOGGER.fine(String.join(" ", args)); LOGGER.fine(String.join(" ", args));
} }
if (LOGGER.isLoggable(Level.INFO) && !silent()) {
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info(String.format("Report will be saved in file://%s", LOGGER.info(String.format("Report will be saved in file://%s",
new File(options_.get("-d")).toURI().getPath())); new File(options_.get("-d")).toURI().getPath()));
} }
@ -233,7 +251,7 @@ public class TestNgOperation extends TestOperation<TestNgOperation, List<String>
} }
/** /**
* Should TestNG generate results on a per suite basis by creating a sub directory for each suite and dumping * Should TestNG generate results on a per-suite basis by creating a subdirectory for each suite and dumping
* results into it. * results into it.
* *
* <p>Default is {@code false}</p>. * <p>Default is {@code false}</p>.
@ -628,7 +646,7 @@ public class TestNgOperation extends TestOperation<TestNgOperation, List<String>
*/ */
public TestNgOperation shareThreadPoolForDataProviders(boolean shareThreadPoolForDataProviders) { public TestNgOperation shareThreadPoolForDataProviders(boolean shareThreadPoolForDataProviders) {
if (shareThreadPoolForDataProviders) { if (shareThreadPoolForDataProviders) {
options_.put("-shareThreadPoolForDataProviders", String.valueOf(shareThreadPoolForDataProviders)); options_.put("-shareThreadPoolForDataProviders", "true");
} }
return this; return this;
} }
@ -943,7 +961,7 @@ public class TestNgOperation extends TestOperation<TestNgOperation, List<String>
*/ */
public TestNgOperation useGlobalThreadPool(boolean useGlobalThreadPool) { public TestNgOperation useGlobalThreadPool(boolean useGlobalThreadPool) {
if (useGlobalThreadPool) { if (useGlobalThreadPool) {
options_.put("-useGlobalThreadPool", String.valueOf(useGlobalThreadPool)); options_.put("-useGlobalThreadPool", "true");
} }
return this; return this;
} }