use of com.google.devtools.common.options.OptionsParsingException in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testSetValueWithNoValueThrows.
@Test
public void testSetValueWithNoValueThrows() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getSetValueBuilder();
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_string=user value");
TestOptions testOptions = getTestOptions();
assertEquals("user value", testOptions.testString);
try {
enforcer.enforce(parser, "build");
fail();
} catch (OptionsParsingException e) {
// expected.
}
}
use of com.google.devtools.common.options.OptionsParsingException in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testDisallowValuesDisallowsMultipleValues.
@Test
public void testDisallowValuesDisallowsMultipleValues() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_multiple_string").getDisallowValuesBuilder().addDisallowedValues("foo").addDisallowedValues("bar");
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_multiple_string=baz", "--test_multiple_string=bar");
// Option should be "baz" and "bar" as specified by the user.
TestOptions testOptions = getTestOptions();
assertThat(testOptions.testMultipleString).containsExactly("baz", "bar").inOrder();
try {
enforcer.enforce(parser, "build");
fail();
} catch (OptionsParsingException e) {
// expected, since bar is disallowed.
}
}
use of com.google.devtools.common.options.OptionsParsingException in project bazel by bazelbuild.
the class CommandEnvironment method beforeCommand.
/**
* Hook method called by the BlazeCommandDispatcher prior to the dispatch of
* each command.
*
* @param options The CommonCommandOptions used by every command.
* @throws AbruptExitException if this command is unsuitable to be run as specified
*/
void beforeCommand(Command command, OptionsParser optionsParser, CommonCommandOptions options, long execStartTimeNanos, long waitTimeInMs) throws AbruptExitException {
commandStartTime -= options.startupTime;
if (runtime.getStartupOptionsProvider().getOptions(BlazeServerStartupOptions.class).watchFS) {
try {
// TODO(ulfjack): Get rid of the startup option and drop this code.
optionsParser.parse("--watchfs");
} catch (OptionsParsingException e) {
// This should never happen.
throw new IllegalStateException(e);
}
}
this.commandName = command.name();
this.options = optionsParser;
eventBus.post(new GotOptionsEvent(runtime.getStartupOptionsProvider(), optionsParser));
throwPendingException();
outputService = null;
BlazeModule outputModule = null;
if (command.builds()) {
for (BlazeModule module : runtime.getBlazeModules()) {
OutputService moduleService = module.getOutputService();
if (moduleService != null) {
if (outputService != null) {
throw new IllegalStateException(String.format("More than one module (%s and %s) returns an output service", module.getClass(), outputModule.getClass()));
}
outputService = moduleService;
outputModule = module;
}
}
}
SkyframeExecutor skyframeExecutor = getSkyframeExecutor();
skyframeExecutor.setOutputService(outputService);
// Ensure that the working directory will be under the workspace directory.
Path workspace = getWorkspace();
Path workingDirectory;
if (inWorkspace()) {
workingDirectory = workspace.getRelative(options.clientCwd);
} else {
workspace = FileSystemUtils.getWorkingDirectory(getDirectories().getFileSystem());
workingDirectory = workspace;
}
this.relativeWorkingDirectory = workingDirectory.relativeTo(workspace);
this.workingDirectory = workingDirectory;
updateClientEnv(options.clientEnv);
// Fail fast in the case where a Blaze command forgets to install the package path correctly.
skyframeExecutor.setActive(false);
// Let skyframe figure out if it needs to store graph edges for this build.
skyframeExecutor.decideKeepIncrementalState(runtime.getStartupOptionsProvider().getOptions(BlazeServerStartupOptions.class).batch, optionsParser.getOptions(BuildView.Options.class));
// Start the performance and memory profilers.
runtime.beforeCommand(this, options, execStartTimeNanos);
// actionClientEnv contains the environment where values from actionEnvironment are
// overridden.
actionClientEnv.clear();
actionClientEnv.putAll(clientEnv);
if (command.builds()) {
Map<String, String> testEnv = new TreeMap<>();
for (Map.Entry<String, String> entry : optionsParser.getOptions(BuildConfiguration.Options.class).testEnvironment) {
testEnv.put(entry.getKey(), entry.getValue());
}
// for inheritence.
for (Map.Entry<String, String> entry : optionsParser.getOptions(BuildConfiguration.Options.class).actionEnvironment) {
if (entry.getValue() == null) {
visibleClientEnv.add(entry.getKey());
} else {
visibleClientEnv.remove(entry.getKey());
actionClientEnv.put(entry.getKey(), entry.getValue());
}
}
try {
for (Map.Entry<String, String> entry : testEnv.entrySet()) {
if (entry.getValue() == null) {
String clientValue = clientEnv.get(entry.getKey());
if (clientValue != null) {
optionsParser.parse(OptionPriority.SOFTWARE_REQUIREMENT, "test environment variable from client environment", ImmutableList.of("--test_env=" + entry.getKey() + "=" + clientEnv.get(entry.getKey())));
}
}
}
} catch (OptionsParsingException e) {
throw new IllegalStateException(e);
}
}
eventBus.post(new CommandStartEvent(command.name(), getCommandId(), getClientEnv(), workingDirectory, getDirectories(), waitTimeInMs + options.waitTime));
}
use of com.google.devtools.common.options.OptionsParsingException in project bazel by bazelbuild.
the class CanonicalizeCommand method exec.
@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
BlazeRuntime runtime = env.getRuntime();
Options canonicalizeOptions = options.getOptions(Options.class);
String commandName = canonicalizeOptions.forCommand;
BlazeCommand command = runtime.getCommandMap().get(commandName);
if (command == null) {
env.getReporter().handle(Event.error("Not a valid command: '" + commandName + "' (should be one of " + Joiner.on(", ").join(runtime.getCommandMap().keySet()) + ")"));
return ExitCode.COMMAND_LINE_ERROR;
}
Collection<Class<? extends OptionsBase>> optionsClasses = BlazeCommandUtils.getOptions(command.getClass(), runtime.getBlazeModules(), runtime.getRuleClassProvider());
try {
OptionsParser parser = OptionsParser.newOptionsParser(optionsClasses);
parser.setAllowResidue(false);
parser.parse(options.getResidue());
InvocationPolicyEnforcer invocationPolicyEnforcer = InvocationPolicyEnforcer.create(canonicalizeOptions.invocationPolicy);
invocationPolicyEnforcer.enforce(parser, commandName);
List<String> result = parser.canonicalize();
for (String piece : result) {
env.getReporter().getOutErr().printOutLn(piece);
}
} catch (OptionsParsingException e) {
env.getReporter().handle(Event.error(e.getMessage()));
return ExitCode.COMMAND_LINE_ERROR;
}
return ExitCode.SUCCESS;
}
use of com.google.devtools.common.options.OptionsParsingException in project bazel by bazelbuild.
the class ConfigSetting method matchesConfig.
/**
* Given a list of [flagName, flagValue] pairs, returns true if flagName == flagValue for
* every item in the list under this configuration, false otherwise.
*/
private boolean matchesConfig(Map<String, String> expectedSettings, BuildConfiguration config) throws OptionsParsingException {
// Rather than returning fast when we find a mismatch, continue looking at the other flags
// to check that they're indeed valid flag specifications.
boolean foundMismatch = false;
// Since OptionsParser instantiation involves reflection, let's try to minimize that happening.
Map<Class<? extends OptionsBase>, OptionsParser> parserCache = new HashMap<>();
for (Map.Entry<String, String> setting : expectedSettings.entrySet()) {
String optionName = setting.getKey();
String expectedRawValue = setting.getValue();
Class<? extends OptionsBase> optionClass = config.getOptionClass(optionName);
if (optionClass == null) {
throw new OptionsParsingException("unknown option: '" + optionName + "'");
}
OptionsParser parser = parserCache.get(optionClass);
if (parser == null) {
parser = OptionsParser.newOptionsParser(optionClass);
parserCache.put(optionClass, parser);
}
parser.parse("--" + optionName + "=" + expectedRawValue);
Object expectedParsedValue = parser.getOptions(optionClass).asMap().get(optionName);
if (!optionMatches(config, optionName, expectedParsedValue)) {
foundMismatch = true;
}
}
return !foundMismatch;
}
Aggregations