io.cucumber.core.cli.Main Maven / Gradle / Ivy
package io.cucumber.core.cli;
import io.cucumber.core.options.CommandlineOptionsParser;
import io.cucumber.core.options.Constants;
import io.cucumber.core.options.CucumberProperties;
import io.cucumber.core.options.CucumberPropertiesParser;
import io.cucumber.core.options.RuntimeOptions;
import io.cucumber.core.runtime.Runtime;
import org.apiguardian.api.API;
import java.util.Optional;
/**
* Cucumber Main. Runs Cucumber as a CLI.
*
* Options can be provided in by (order of precedence):
*
* - Command line arguments
* - Properties from {@link System#getProperties()}
* - Properties from in {@link System#getenv()}
* - Properties from {@value Constants#CUCUMBER_PROPERTIES_FILE_NAME}
*
* For available properties see {@link Constants}. For Command line options
* {@link CommandlineOptions}.
*/
@API(status = API.Status.STABLE)
public class Main {
public static void main(String... argv) {
byte exitStatus = run(argv, Thread.currentThread().getContextClassLoader());
System.exit(exitStatus);
}
/**
* Launches the Cucumber-JVM command line.
*
* @param argv runtime options. See details in the
* {@code cucumber.api.cli.Usage.txt} resource.
* @return 0 if execution was successful, 1 if it was not (test
* failures)
*/
public static byte run(String... argv) {
return run(argv, Thread.currentThread().getContextClassLoader());
}
/**
* Launches the Cucumber-JVM command line.
*
* @param argv runtime options. See details in the
* {@code cucumber.api.cli.Usage.txt} resource.
* @param classLoader classloader used to load the runtime
* @return 0 if execution was successful, 1 if it was not (test
* failures)
*/
public static byte run(String[] argv, ClassLoader classLoader) {
RuntimeOptions propertiesFileOptions = new CucumberPropertiesParser()
.parse(CucumberProperties.fromPropertiesFile())
.build();
RuntimeOptions environmentOptions = new CucumberPropertiesParser()
.parse(CucumberProperties.fromEnvironment())
.build(propertiesFileOptions);
RuntimeOptions systemOptions = new CucumberPropertiesParser()
.parse(CucumberProperties.fromSystemProperties())
.build(environmentOptions);
CommandlineOptionsParser commandlineOptionsParser = new CommandlineOptionsParser(System.out);
RuntimeOptions runtimeOptions = commandlineOptionsParser
.parse(argv)
.addDefaultGlueIfAbsent()
.addDefaultFeaturePathIfAbsent()
.addDefaultSummaryPrinterIfNotDisabled()
.enablePublishPlugin()
.build(systemOptions);
Optional exitStatus = commandlineOptionsParser.exitStatus();
if (exitStatus.isPresent()) {
return exitStatus.get();
}
final Runtime runtime = Runtime.builder()
.withRuntimeOptions(runtimeOptions)
.withClassLoader(() -> classLoader)
.build();
runtime.run();
return runtime.exitStatus();
}
}