Java Code Examples for com.google.devtools.build.lib.syntax.Mutability#create()
The following examples show how to use
com.google.devtools.build.lib.syntax.Mutability#create() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RuleAnalysisLegacyBuildRuleViewTest.java From buck with Apache License 2.0 | 6 votes |
private static ProviderInfoCollection createProviderInfoCollection( ImmutableMap<String, ImmutableSet<Artifact>> namedOutputs, ImmutableSet<Artifact> defaultOutputs) throws EvalException { SkylarkDict<String, Set<Artifact>> dict; try (Mutability mutability = Mutability.create("test")) { Environment env = Environment.builder(mutability) .setGlobals(BazelLibrary.GLOBALS) .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS) .build(); dict = SkylarkDict.of(env); for (Map.Entry<String, ImmutableSet<Artifact>> entry : namedOutputs.entrySet()) { dict.put(entry.getKey(), entry.getValue(), Location.BUILTIN, mutability); } } return TestProviderInfoCollectionImpl.builder() .put(new FakeInfo(new FakeBuiltInProvider("foo"))) .build(new ImmutableDefaultInfo(dict, defaultOutputs)); }
Example 2
Source File: BuiltInProviderInfoTest.java From buck with Apache License 2.0 | 6 votes |
@Test public void defaultValuesWorkInStarlarkContext() throws InterruptedException, EvalException { try (Mutability mutability = Mutability.create("test")) { Environment env = Environment.builder(mutability) .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS) .setGlobals( Environment.GlobalFrame.createForBuiltins( ImmutableMap.of( ImmutableSomeInfo.PROVIDER.getName(), ImmutableSomeInfo.PROVIDER))) .build(); FuncallExpression ast = new FuncallExpression( new Identifier(ImmutableSomeInfo.PROVIDER.getName()), ImmutableList.of( new Argument.Keyword(new Identifier("my_info"), new IntegerLiteral(2)))); assertEquals(new ImmutableSomeInfo("default value", 2), ast.eval(env)); } }
Example 3
Source File: BuiltInProviderInfoTest.java From buck with Apache License 2.0 | 6 votes |
@Test public void validatesTypesWhenInstantiatingFromStaticMethod() throws InterruptedException, EvalException { try (Mutability mutability = Mutability.create("providertest")) { Environment env = Environment.builder(mutability) .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS) .setGlobals( Environment.GlobalFrame.createForBuiltins( ImmutableMap.of( SomeInfoWithInstantiate.PROVIDER.getName(), SomeInfoWithInstantiate.PROVIDER))) .build(); FuncallExpression ast = new FuncallExpression( new Identifier("SomeInfoWithInstantiate"), ImmutableList.of( new Argument.Keyword( new Identifier("my_info"), new StringLiteral("not a number")))); thrown.expect(EvalException.class); thrown.expectMessage("expected value of type 'int'"); ast.eval(env); } }
Example 4
Source File: StarlarkDebugServerTest.java From bazel with Apache License 2.0 | 6 votes |
/** * Creates and starts a worker thread parsing, resolving, and executing the given Starlark file to * populate the specified module, or if none is given, in a fresh module with a default * environment. */ private static Thread execInWorkerThread(ParserInput input, @Nullable Module module) { Thread javaThread = new Thread( () -> { try (Mutability mu = Mutability.create("test")) { StarlarkThread thread = new StarlarkThread(mu, StarlarkSemantics.DEFAULT); EvalUtils.exec( input, FileOptions.DEFAULT, module != null ? module : Module.create(), thread); } catch (SyntaxError.Exception | EvalException | InterruptedException ex) { throw new AssertionError(ex); } }); javaThread.start(); return javaThread; }
Example 5
Source File: BuiltInProviderInfoTest.java From buck with Apache License 2.0 | 5 votes |
@Test public void passesLocationWhenInstantiatingFromStaticMethod() throws InterruptedException, EvalException { Location location = Location.fromPathAndStartColumn( PathFragment.create("foo/bar.bzl"), 0, 0, new Location.LineAndColumn(1, 1)); Object o; try (Mutability mutability = Mutability.create("providertest")) { Environment env = Environment.builder(mutability) .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS) .setGlobals( Environment.GlobalFrame.createForBuiltins( ImmutableMap.of( SomeInfoWithInstantiateAndLocation.PROVIDER.getName(), SomeInfoWithInstantiateAndLocation.PROVIDER))) .build(); o = BuildFileAST.parseSkylarkFileWithoutImports( ParserInputSource.create( "SomeInfoWithInstantiateAndLocation(my_info=1)", PathFragment.create("foo/bar.bzl")), env.getEventHandler()) .eval(env); } assertThat(o, Matchers.instanceOf(SomeInfoWithInstantiateAndLocation.class)); SomeInfoWithInstantiateAndLocation someInfo = (SomeInfoWithInstantiateAndLocation) o; assertEquals(ImmutableList.of("foo"), someInfo.str_list()); assertEquals("1", someInfo.myInfo()); assertEquals(location.getPath(), someInfo.location().getPath()); assertEquals(location.getStartLineAndColumn(), someInfo.location().getStartLineAndColumn()); }
Example 6
Source File: BuckStarlarkFunctionTest.java From buck with Apache License 2.0 | 5 votes |
@Test public void allowsNoneable() throws Throwable { BuckStarlarkFunction function = new BuckStarlarkFunction( "withNone", ImmutableList.of("non_noneable", "noneable"), ImmutableList.of("None"), ImmutableSet.of("noneable")) { public Object withNone(Object nonNoneable, Object noneable) { return SkylarkList.createImmutable(ImmutableList.of(nonNoneable, noneable)); } }; try (Mutability mutability = Mutability.create("test")) { Environment env = Environment.builder(mutability) .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS) .setGlobals( Environment.GlobalFrame.createForBuiltins( ImmutableMap.of( function.getMethodDescriptor().getName(), function, "None", Runtime.NONE))) .build(); Object none = BuildFileAST.eval(env, "withNone(noneable=None, non_noneable=1)[1]"); Object defaultNone = BuildFileAST.eval(env, "withNone(non_noneable=1)[1]"); Object nonNull = BuildFileAST.eval(env, "withNone(noneable=2, non_noneable=1)[1]"); assertEquals(Runtime.NONE, none); assertEquals(Runtime.NONE, defaultNone); assertEquals(2, nonNull); expectedException.expect(EvalException.class); expectedException.expectMessage("cannot be None"); BuildFileAST.eval(env, "withNone(noneable=2, non_noneable=None)[1]"); } }
Example 7
Source File: BasicRuleRuleDescription.java From buck with Apache License 2.0 | 5 votes |
private SkylarkDict<String, Set<Artifact>> getNamedOutputs( ActionRegistry actionRegistry, BasicRuleDescriptionArg args) { if (!args.getNamedOuts().isPresent()) { return SkylarkDict.empty(); } ImmutableMap<String, ImmutableSet<String>> namedOuts = args.getNamedOuts().get(); SkylarkDict<String, Set<Artifact>> dict; try (Mutability mutability = Mutability.create("test")) { Environment env = Environment.builder(mutability) .setGlobals(BazelLibrary.GLOBALS) .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS) .build(); dict = SkylarkDict.of(env); for (Map.Entry<String, ImmutableSet<String>> labelsToNamedOutnames : namedOuts.entrySet()) { try { dict.put( labelsToNamedOutnames.getKey(), declareArtifacts(actionRegistry, labelsToNamedOutnames.getValue()), Location.BUILTIN, mutability); } catch (EvalException e) { throw new HumanReadableException("Invalid name %s", labelsToNamedOutnames.getKey()); } } } return dict; }
Example 8
Source File: TestMutableEnv.java From buck with Apache License 2.0 | 5 votes |
public TestMutableEnv(ImmutableMap<String, Object> globals) { ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder(); builder.putAll(globals); Runtime.addConstantsToBuilder(builder); MethodLibrary.addBindingsToBuilder(builder); mutability = Mutability.create("testing"); env = Environment.builder(mutability) .setGlobals(Environment.GlobalFrame.createForBuiltins(builder.build())) .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS) .build(); }
Example 9
Source File: StarlarkDefinedConfigTransition.java From bazel with Apache License 2.0 | 5 votes |
/** Evaluate the input function with the given argument, and return the return value. */ private Object evalFunction( StarlarkCallable function, ImmutableList<Object> args, EventHandler eventHandler) throws InterruptedException, EvalException { try (Mutability mu = Mutability.create("eval_transition_function")) { StarlarkThread thread = new StarlarkThread(mu, semantics); thread.setPrintHandler(Event.makeDebugPrintHandler(eventHandler)); starlarkContext.storeInThread(thread); return Starlark.call(thread, function, args, /*kwargs=*/ ImmutableMap.of()); } }
Example 10
Source File: SkylarkUserDefinedRuleTest.java From buck with Apache License 2.0 | 5 votes |
@Test public void acceptsAutomaticallyAddedParameters() throws EvalException, LabelSyntaxException, InterruptedException { // TODO: Add visibility when that's added to implicit params ImmutableMap<String, AttributeHolder> params = ImmutableMap.of(); ImmutableMap<String, Object> expected = ImmutableMap.of( "buck.base_path", "some_package/subdir", "buck.type", "@foo//bar:extension.bzl:baz_rule", "name", "some_rule_name"); SkylarkUserDefinedRule rule = SkylarkUserDefinedRule.of( location, SimpleFunction.of(1), TEST_IMPLICIT_ATTRIBUTES, HIDDEN_IMPLICIT_ATTRIBUTES, params, false, false); rule.export(Label.parseAbsolute("@foo//bar:extension.bzl", ImmutableMap.of()), "baz_rule"); try (Mutability mutability = Mutability.create("argtest")) { Environment env = newEnvironment(mutability); Object res = rule.call( ImmutableList.of(), ImmutableMap.of("name", "some_rule_name"), getJunkAst(), env); ImmutableMap<String, ImmutableMap<String, Object>> rules = ParseContext.getParseContext(env, null).getRecordedRules(); assertEquals(Runtime.NONE, res); assertEquals(1, rules.size()); assertEquals(expected, rules.get("some_rule_name")); } }
Example 11
Source File: BuckStarlarkFunctionTest.java From buck with Apache License 2.0 | 5 votes |
@Test public void noDefaultValues() throws Throwable { BuckStarlarkFunction function = new BuckStarlarkFunction( "toStr", ImmutableList.of("num"), ImmutableList.of(), ImmutableSet.of()) { public String toStr(Integer num) { return num.toString(); } }; try (Mutability mutability = Mutability.create("test")) { Environment env = Environment.builder(mutability) .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS) .setGlobals( Environment.GlobalFrame.createForBuiltins( ImmutableMap.of(function.getMethodDescriptor().getName(), function))) .build(); FuncallExpression ast = new FuncallExpression( new Identifier("toStr"), ImmutableList.of(new Argument.Keyword(new Identifier("num"), new IntegerLiteral(1)))); assertEquals("1", ast.eval(env)); } }
Example 12
Source File: BzlLoadFunction.java From bazel with Apache License 2.0 | 5 votes |
/** Executes the .bzl file defining the module to be loaded. */ private void executeBzlFile( StarlarkFile file, Label label, Module module, Map<String, Module> loadedModules, StarlarkSemantics starlarkSemantics, ExtendedEventHandler skyframeEventHandler, ImmutableMap<RepositoryName, RepositoryName> repositoryMapping) throws BzlLoadFailedException, InterruptedException { try (Mutability mu = Mutability.create("loading", label)) { StarlarkThread thread = new StarlarkThread(mu, starlarkSemantics); thread.setLoader(loadedModules::get); StoredEventHandler starlarkEventHandler = new StoredEventHandler(); thread.setPrintHandler(Event.makeDebugPrintHandler(starlarkEventHandler)); ruleClassProvider.setStarlarkThreadContext(thread, label, repositoryMapping); execAndExport(file, label, starlarkEventHandler, module, thread); Event.replayEventsOn(skyframeEventHandler, starlarkEventHandler.getEvents()); for (Postable post : starlarkEventHandler.getPosts()) { skyframeEventHandler.post(post); } if (starlarkEventHandler.hasErrors()) { throw BzlLoadFailedException.errors(label.toPathFragment()); } } }
Example 13
Source File: StarlarkCallbackHelper.java From bazel with Apache License 2.0 | 5 votes |
public Object call(EventHandler eventHandler, ClassObject ctx, Object... arguments) throws EvalException, InterruptedException { try (Mutability mu = Mutability.create("callback", callback)) { StarlarkThread thread = new StarlarkThread(mu, starlarkSemantics); thread.setPrintHandler(Event.makeDebugPrintHandler(eventHandler)); context.storeInThread(thread); return Starlark.call( thread, callback, buildArgumentList(ctx, arguments), /*kwargs=*/ ImmutableMap.of()); } catch (ClassCastException | IllegalArgumentException e) { // TODO(adonovan): investigate throw new EvalException(null, e.getMessage()); } }
Example 14
Source File: SkylarkUserDefinedRuleTest.java From buck with Apache License 2.0 | 4 votes |
@Test public void usesDefaultValuesIfMissingParameter() throws LabelSyntaxException, InterruptedException, EvalException { ImmutableMap<String, AttributeHolder> params = ImmutableMap.of( "arg1", StringAttribute.of("some string", "", false, ImmutableList.of()), "arg2", StringAttribute.of("some string", "", true, ImmutableList.of()), "arg3", IntAttribute.of(5, "", false, ImmutableList.of()), "arg4", IntAttribute.of(5, "", true, ImmutableList.of())); ImmutableMap<String, Object> expected = ImmutableMap.<String, Object>builder() .put("buck.base_path", "some_package/subdir") .put("buck.type", "@foo//bar:extension.bzl:baz_rule") .put("name", "some_rule_name") .put("arg1", "some string") .put("arg2", "arg2_val") .put("arg3", 5) .put("arg4", 2) .build(); SkylarkUserDefinedRule rule = SkylarkUserDefinedRule.of( location, SimpleFunction.of(1), TEST_IMPLICIT_ATTRIBUTES, HIDDEN_IMPLICIT_ATTRIBUTES, params, false, false); rule.export(Label.parseAbsolute("@foo//bar:extension.bzl", ImmutableMap.of()), "baz_rule"); try (Mutability mutability = Mutability.create("argtest")) { Environment env = newEnvironment(mutability); Object res = rule.call( ImmutableList.of(), ImmutableMap.of("name", "some_rule_name", "arg2", "arg2_val", "arg4", 2), getJunkAst(), env); ImmutableMap<String, ImmutableMap<String, Object>> rules = ParseContext.getParseContext(env, null).getRecordedRules(); assertEquals(Runtime.NONE, res); assertEquals(1, rules.size()); assertEquals(expected, rules.get("some_rule_name")); } }
Example 15
Source File: TestInfoTest.java From buck with Apache License 2.0 | 4 votes |
@Test public void coercesTimeout() throws InterruptedException, EvalException { Object raw1; Object raw2; try (Mutability mutability = Mutability.create("providertest")) { Environment env = Environment.builder(mutability) .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS) .setGlobals( Environment.GlobalFrame.createForBuiltins( ImmutableMap.of(TestInfo.PROVIDER.getName(), TestInfo.PROVIDER))) .build(); raw1 = BuildFileAST.eval( env, String.format( "TestInfo(" + "\ntest_name=\"%s\"," + "\ntest_case_name=\"%s\"," + "\ntimeout_ms=TestInfo(test_name=\"%s\", test_case_name=\"%s\").timeout_ms" + "\n)", TEST_NAME, TEST_CASE_NAME, TEST_NAME, TEST_CASE_NAME)); raw2 = BuildFileAST.eval( env, String.format( "TestInfo(" + "\ntest_name=\"%s\"," + "\ntest_case_name=\"%s\"," + "\ntimeout_ms=TestInfo(test_name=\"%s\", test_case_name=\"%s\", timeout_ms=5).timeout_ms" + "\n)", TEST_NAME, TEST_CASE_NAME, TEST_NAME, TEST_CASE_NAME)); } assertThat(raw1, Matchers.instanceOf(TestInfo.class)); TestInfo val1 = (TestInfo) raw1; assertEquals(Runtime.NONE, val1.timeoutMs()); assertEquals(Optional.empty(), val1.typedTimeoutMs()); assertThat(raw2, Matchers.instanceOf(TestInfo.class)); TestInfo val2 = (TestInfo) raw2; assertEquals(5, val2.timeoutMs()); assertEquals(Optional.of(5L), val2.typedTimeoutMs()); }
Example 16
Source File: SkylarkUserDefinedRuleTest.java From buck with Apache License 2.0 | 4 votes |
@Test public void createsCorrectCallable() throws EvalException, IOException, InterruptedException, LabelSyntaxException { ImmutableMap<String, AttributeHolder> params = ImmutableMap.of( "arg1", StringAttribute.of("some string", "", false, ImmutableList.of()), "arg2", StringAttribute.of("some string", "", true, ImmutableList.of()), "arg3", IntAttribute.of(5, "", false, ImmutableList.of()), "arg4", IntAttribute.of(5, "", true, ImmutableList.of())); ImmutableMap<String, Object> expected = ImmutableMap.<String, Object>builder() .put("buck.base_path", "some_package/subdir") .put("buck.type", "@foo//bar:extension.bzl:baz_rule") .put("name", "some_rule_name") .put("arg1", "arg1_val") .put("arg2", "arg2_val") .put("arg3", 1) .put("arg4", 2) .build(); SkylarkUserDefinedRule rule = SkylarkUserDefinedRule.of( location, SimpleFunction.of(1), TEST_IMPLICIT_ATTRIBUTES, HIDDEN_IMPLICIT_ATTRIBUTES, params, false, false); rule.export(Label.parseAbsolute("@foo//bar:extension.bzl", ImmutableMap.of()), "baz_rule"); try (Mutability mutability = Mutability.create("argtest")) { Environment env = newEnvironment(mutability); Object res = rule.call( ImmutableList.of(), ImmutableMap.of( "name", "some_rule_name", "arg1", "arg1_val", "arg2", "arg2_val", "arg3", 1, "arg4", 2), getJunkAst(), env); ImmutableMap<String, ImmutableMap<String, Object>> rules = ParseContext.getParseContext(env, null).getRecordedRules(); assertEquals(Runtime.NONE, res); assertEquals(1, rules.size()); assertEquals(expected, rules.get("some_rule_name")); } }
Example 17
Source File: AbstractSkylarkFileParser.java From buck with Apache License 2.0 | 4 votes |
/** @return The parsed result defined in {@code parseFile}. */ protected ParseResult parse(Path parseFile) throws IOException, BuildFileParseException, InterruptedException { com.google.devtools.build.lib.vfs.Path buildFilePath = fileSystem.getPath(parseFile.toString()); String basePath = getBasePath(parseFile); Label containingLabel = createContainingLabel(basePath); ImplicitlyLoadedExtension implicitLoad = loadImplicitExtension(parseFile.getFileSystem().getPath(basePath), containingLabel); BuildFileAST buildFileAst = parseFile(buildFilePath, containingLabel); Globber globber = getGlobber(parseFile); PackageContext packageContext = createPackageContext(basePath, globber, implicitLoad.getLoadedSymbols()); ParseContext parseContext = new ParseContext(packageContext); try (Mutability mutability = Mutability.create("parsing " + parseFile)) { EnvironmentData envData = createBuildFileEvaluationEnvironment( buildFilePath, containingLabel, buildFileAst, mutability, parseContext, implicitLoad.getExtensionData()); if (!ValidationEnvironment.checkBuildSyntax( buildFileAst.getStatements(), eventHandler, envData.getEnvironment())) { throw BuildFileParseException.createForUnknownParseError("Cannot parse file " + parseFile); } boolean exec = buildFileAst.exec(envData.getEnvironment(), eventHandler); if (!exec) { // buildFileAst.exec reports extended error information to console with eventHandler // but this is not propagated to BuildFileParseException. So in case of resilient parsing // when exceptions are stored in BuildFileManifest they do not have detailed information. // TODO(sergeyb): propagate detailed error information from AST evaluation to exception throw BuildFileParseException.createForUnknownParseError( "Cannot evaluate file " + parseFile); } ImmutableList.Builder<String> loadedPaths = ImmutableList.builderWithExpectedSize(envData.getLoadedPaths().size() + 1); loadedPaths.add(buildFilePath.toString()); loadedPaths.addAll(envData.getLoadedPaths()); return getParseResult(parseFile, parseContext, globber, loadedPaths.build()); } }
Example 18
Source File: SkylarkPackageModuleTest.java From buck with Apache License 2.0 | 4 votes |
private ParseContext evaluate(Path buildFile, boolean expectSuccess) throws IOException, InterruptedException { try (Mutability mutability = Mutability.create("PACKAGE")) { return evaluate(buildFile, mutability, expectSuccess); } }
Example 19
Source File: GlobTest.java From buck with Apache License 2.0 | 4 votes |
private Environment assertEvaluate(Path buildFile) throws IOException, InterruptedException { try (Mutability mutability = Mutability.create("BUCK")) { return assertEvaluate(buildFile, mutability); } }
Example 20
Source File: BuckStarlarkFunctionTest.java From buck with Apache License 2.0 | 4 votes |
@Test public void withPartialNamedAndDefault() throws Throwable { BuckStarlarkFunction function = new BuckStarlarkFunction( "myFoo", ImmutableList.of("numNoDefault", "numWithDefault"), ImmutableList.of("1"), ImmutableSet.of()) { public String myFoo(Integer mand, Integer numNoDefault, Integer withDefault) { return String.valueOf(mand + numNoDefault + withDefault); } }; try (Mutability mutability = Mutability.create("test")) { Environment env = Environment.builder(mutability) .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS) .setGlobals( Environment.GlobalFrame.createForBuiltins( ImmutableMap.of(function.getMethodDescriptor().getName(), function))) .build(); FuncallExpression ast = new FuncallExpression( new Identifier("myFoo"), ImmutableList.of( new Argument.Positional(new IntegerLiteral(100)), new Argument.Keyword(new Identifier("numNoDefault"), new IntegerLiteral(10)))); assertEquals("111", ast.eval(env)); ast = new FuncallExpression( new Identifier("myFoo"), ImmutableList.of( new Argument.Positional(new IntegerLiteral(100)), new Argument.Keyword(new Identifier("numNoDefault"), new IntegerLiteral(10)), new Argument.Keyword(new Identifier("numWithDefault"), new IntegerLiteral(5)))); assertEquals("115", ast.eval(env)); } }