com.google.devtools.build.lib.syntax.Mutability Java Examples

The following examples show how to use com.google.devtools.build.lib.syntax.Mutability. 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: TargetUtilsTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilteredExecutionInfo_fromUncheckedExecRequirements() throws Exception {
  scratch.file("tests/BUILD", "sh_binary(name = 'no-tag', srcs=['sh.sh'])");

  Rule noTag = (Rule) getTarget("//tests:no-tag");

  Map<String, String> execInfo =
      TargetUtils.getFilteredExecutionInfo(
          Dict.of((Mutability) null, "supports-worker", "1"),
          noTag, /* allowTagsPropagation */
          true);
  assertThat(execInfo).containsExactly("supports-worker", "1");

  execInfo =
      TargetUtils.getFilteredExecutionInfo(
          Dict.of((Mutability) null, "some-custom-tag", "1", "no-cache", "1"),
          noTag,
          /* allowTagsPropagation */ true);
  assertThat(execInfo).containsExactly("no-cache", "1");
}
 
Example #2
Source File: AndroidStarlarkData.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public Dict<Provider, NativeInfo> mergeResources(
    AndroidDataContext ctx,
    AndroidManifestInfo manifest,
    Sequence<?> resources, // <ConfiguredTarget>
    Sequence<?> deps, // <AndroidResourcesInfo>
    boolean neverlink,
    boolean enableDataBinding)
    throws EvalException, InterruptedException {
  ValidatedAndroidResources validated =
      mergeRes(ctx, manifest, resources, deps, neverlink, enableDataBinding);
  JavaInfo javaInfo =
      getJavaInfoForRClassJar(validated.getClassJar(), validated.getJavaSourceJar());
  return Dict.of(
      (Mutability) null,
      AndroidResourcesInfo.PROVIDER,
      validated.toProvider(),
      JavaInfo.PROVIDER,
      javaInfo);
}
 
Example #3
Source File: AndroidStarlarkData.java    From bazel with Apache License 2.0 6 votes vote down vote up
public static Dict<Provider, NativeInfo> getNativeInfosFrom(
    ResourceApk resourceApk, Label label) {
  ImmutableMap.Builder<Provider, NativeInfo> builder = ImmutableMap.builder();

  builder
      .put(AndroidResourcesInfo.PROVIDER, resourceApk.toResourceInfo(label))
      .put(AndroidAssetsInfo.PROVIDER, resourceApk.toAssetsInfo(label));

  resourceApk.toManifestInfo().ifPresent(info -> builder.put(AndroidManifestInfo.PROVIDER, info));

  builder.put(
      JavaInfo.PROVIDER,
      getJavaInfoForRClassJar(
          resourceApk.getResourceJavaClassJar(), resourceApk.getResourceJavaSrcJar()));

  return Dict.copyOf((Mutability) null, builder.build());
}
 
Example #4
Source File: StarlarkNativeModule.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public Dict<String, Dict<String, Object>> existingRules(StarlarkThread thread)
    throws EvalException, InterruptedException {
  BazelStarlarkContext.from(thread).checkLoadingOrWorkspacePhase("native.existing_rules");
  PackageContext context = getContext(thread);
  Collection<Target> targets = context.pkgBuilder.getTargets();
  Mutability mu = thread.mutability();
  Dict<String, Dict<String, Object>> rules = Dict.of(mu);
  for (Target t : targets) {
    if (t instanceof Rule) {
      Dict<String, Object> rule = targetDict(t, mu);
      Preconditions.checkNotNull(rule);
      rules.put(t.getName(), rule, (Location) null);
    }
  }

  return rules;
}
 
Example #5
Source File: SkylarkUserDefinedRuleTest.java    From buck with Apache License 2.0 6 votes vote down vote up
private Environment newEnvironment(Mutability mutability) throws LabelSyntaxException {
  PrintingEventHandler eventHandler = new PrintingEventHandler(EventKind.ALL_EVENTS);
  ParseContext parseContext =
      new ParseContext(
          PackageContext.of(
              (include, exclude, excludeDirectories) -> {
                throw new UnsupportedOperationException();
              },
              ImmutableMap.of(),
              PackageIdentifier.create(
                  "@repo", PathFragment.create("some_package").getChild("subdir")),
              eventHandler,
              ImmutableMap.of()));

  Environment env =
      Environment.builder(mutability)
          .setGlobals(BazelLibrary.GLOBALS)
          .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS)
          .build();
  parseContext.setup(env);
  return env;
}
 
Example #6
Source File: WorkspaceFactory.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * @param builder a builder for the Workspace
 * @param ruleClassProvider a provider for known rule classes
 * @param environmentExtensions the Starlark environment extensions
 * @param mutability the Mutability for the current evaluation context
 * @param installDir the install directory
 * @param workspaceDir the workspace directory
 * @param defaultSystemJavabaseDir the local JDK directory
 */
public WorkspaceFactory(
    Package.Builder builder,
    RuleClassProvider ruleClassProvider,
    ImmutableList<EnvironmentExtension> environmentExtensions,
    Mutability mutability,
    boolean allowOverride,
    @Nullable Path installDir,
    @Nullable Path workspaceDir,
    @Nullable Path defaultSystemJavabaseDir,
    StarlarkSemantics starlarkSemantics) {
  this.builder = builder;
  this.mutability = mutability;
  this.installDir = installDir;
  this.workspaceDir = workspaceDir;
  this.defaultSystemJavabaseDir = defaultSystemJavabaseDir;
  this.environmentExtensions = environmentExtensions;
  this.ruleFactory = new RuleFactory(ruleClassProvider);
  this.workspaceGlobals = new WorkspaceGlobals(allowOverride, ruleFactory);
  this.starlarkSemantics = starlarkSemantics;
  this.workspaceFunctions =
      createWorkspaceFunctions(
          allowOverride, ruleFactory, this.workspaceGlobals, starlarkSemantics);
}
 
Example #7
Source File: TargetUtilsTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilteredExecutionInfo_fromUncheckedExecRequirements_withWorkerKeyMnemonic()
    throws Exception {
  scratch.file("tests/BUILD", "sh_binary(name = 'no-tag', srcs=['sh.sh'])");

  Rule noTag = (Rule) getTarget("//tests:no-tag");

  Map<String, String> execInfo =
      TargetUtils.getFilteredExecutionInfo(
          Dict.of(
              (Mutability) null, "supports-workers", "1", "worker-key-mnemonic", "MyMnemonic"),
          noTag, /* allowTagsPropagation */
          true);
  assertThat(execInfo)
      .containsExactly("supports-workers", "1", "worker-key-mnemonic", "MyMnemonic");
}
 
Example #8
Source File: TargetUtilsTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilteredExecutionInfo_whenIncompatibleFlagDisabled() throws Exception {
  // when --incompatible_allow_tags_propagation=false
  scratch.file(
      "tests/BUILD",
      "sh_binary(name = 'tag1', srcs=['sh.sh'], tags=['supports-workers', 'no-cache'])");
  Rule tag1 = (Rule) getTarget("//tests:tag1");
  Dict<String, String> executionRequirementsUnchecked =
      Dict.of((Mutability) null, "no-remote", "1");

  Map<String, String> execInfo =
      TargetUtils.getFilteredExecutionInfo(
          executionRequirementsUnchecked, tag1, /* allowTagsPropagation */ false);

  assertThat(execInfo).containsExactly("no-remote", "1");
}
 
Example #9
Source File: StarlarkDebugServerTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #10
Source File: StarlarkCcToolchainConfigureTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testSplitEscaped() throws Exception {
  Mutability mu = null;
  newTest()
      .testExpression("split_escaped('a:b:c', ':')", StarlarkList.of(mu, "a", "b", "c"))
      .testExpression("split_escaped('a%:b', ':')", StarlarkList.of(mu, "a:b"))
      .testExpression("split_escaped('a%%b', ':')", StarlarkList.of(mu, "a%b"))
      .testExpression("split_escaped('a:::b', ':')", StarlarkList.of(mu, "a", "", "", "b"))
      .testExpression("split_escaped('a:b%:c', ':')", StarlarkList.of(mu, "a", "b:c"))
      .testExpression("split_escaped('a%%:b:c', ':')", StarlarkList.of(mu, "a%", "b", "c"))
      .testExpression("split_escaped(':a', ':')", StarlarkList.of(mu, "", "a"))
      .testExpression("split_escaped('a:', ':')", StarlarkList.of(mu, "a", ""))
      .testExpression("split_escaped('::a::', ':')", StarlarkList.of(mu, "", "", "a", "", ""))
      .testExpression("split_escaped('%%%:a%%%%:b', ':')", StarlarkList.of(mu, "%:a%%", "b"))
      .testExpression("split_escaped('', ':')", StarlarkList.of(mu))
      .testExpression("split_escaped('%', ':')", StarlarkList.of(mu, "%"))
      .testExpression("split_escaped('%%', ':')", StarlarkList.of(mu, "%"))
      .testExpression("split_escaped('%:', ':')", StarlarkList.of(mu, ":"))
      .testExpression("split_escaped(':', ':')", StarlarkList.of(mu, "", ""))
      .testExpression("split_escaped('a%%b', ':')", StarlarkList.of(mu, "a%b"))
      .testExpression("split_escaped('a%:', ':')", StarlarkList.of(mu, "a:"));
}
 
Example #11
Source File: RuleAnalysisLegacyBuildRuleViewTest.java    From buck with Apache License 2.0 6 votes vote down vote up
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 #12
Source File: BuiltInProviderInfoTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@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 #13
Source File: GlobTest.java    From buck with Apache License 2.0 6 votes vote down vote up
private Pair<Boolean, Environment> evaluate(
    Path buildFile, Mutability mutability, EventHandler eventHandler)
    throws IOException, InterruptedException {
  byte[] buildFileContent =
      FileSystemUtils.readWithKnownFileSize(buildFile, buildFile.getFileSize());
  BuildFileAST buildFileAst =
      BuildFileAST.parseBuildFile(
          ParserInputSource.create(buildFileContent, buildFile.asFragment()), eventHandler);
  Environment env =
      Environment.builder(mutability)
          .setGlobals(BazelLibrary.GLOBALS)
          .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS)
          .build();
  new ParseContext(
          PackageContext.of(
              NativeGlobber.create(root),
              ImmutableMap.of(),
              PackageIdentifier.create(RepositoryName.DEFAULT, PathFragment.create("pkg")),
              eventHandler,
              ImmutableMap.of()))
      .setup(env);
  env.setup(
      "glob", FuncallExpression.getBuiltinCallable(SkylarkBuildModule.BUILD_MODULE, "glob"));
  return new Pair<>(buildFileAst.exec(env, eventHandler), env);
}
 
Example #14
Source File: BuiltInProviderInfoTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void infoWithNoDefaultValueOnAnnotationWorks() 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(
                        InfoWithNoDefaultValOnAnnotation.PROVIDER.getName(),
                        InfoWithNoDefaultValOnAnnotation.PROVIDER)))
            .build();

    FuncallExpression ast =
        new FuncallExpression(
            new Identifier(ImmutableInfoWithNoDefaultValOnAnnotation.PROVIDER.getName()),
            ImmutableList.of(new Argument.Keyword(new Identifier("val"), new IntegerLiteral(2))));

    assertEquals(new ImmutableInfoWithNoDefaultValOnAnnotation(2), ast.eval(env));
  }
}
 
Example #15
Source File: BuiltInProviderInfoTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@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 #16
Source File: RunInfoTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void usesDefaultSkylarkValues() 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(RunInfo.PROVIDER.getName(), RunInfo.PROVIDER)))
            .build();

    FuncallExpression ast = new FuncallExpression(new Identifier("RunInfo"), ImmutableList.of());
    ast.setLocation(Location.BUILTIN);

    Object raw = ast.eval(env);
    assertTrue(raw instanceof RunInfo);
    RunInfo runInfo = (RunInfo) raw;

    CommandLine cli =
        new ExecCompatibleCommandLineBuilder(new ArtifactFilesystem(new FakeProjectFilesystem()))
            .build(runInfo.args());

    assertEquals(ImmutableList.of(), cli.getCommandLineArgs());
    assertEquals(ImmutableMap.of(), cli.getEnvironmentVariables());
  }
}
 
Example #17
Source File: StarlarkCallbackHelper.java    From bazel with Apache License 2.0 5 votes vote down vote up
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 #18
Source File: BuiltInProviderInfoTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void instantiatesFromStaticMethodIfPresent() throws InterruptedException, EvalException {
  Object o;
  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("str_list"),
                    new DictionaryLiteral(
                        ImmutableList.of(
                            new DictionaryLiteral.DictionaryEntryLiteral(
                                new StringLiteral("d"), new StringLiteral("d_value"))))),
                new Argument.Keyword(new Identifier("my_info"), new IntegerLiteral(4))));

    o = ast.eval(env);
  }

  assertThat(o, Matchers.instanceOf(SomeInfoWithInstantiate.class));
  SomeInfoWithInstantiate someInfo4 = (SomeInfoWithInstantiate) o;
  assertEquals(ImmutableList.of("d"), someInfo4.str_list());
  assertEquals("4", someInfo4.myInfo());
}
 
Example #19
Source File: SkylarkBuildModuleTest.java    From buck with Apache License 2.0 5 votes vote down vote up
private Environment evaluate(Path buildFile, Mutability mutability, boolean expectSuccess)
    throws IOException, InterruptedException {
  byte[] buildFileContent =
      FileSystemUtils.readWithKnownFileSize(buildFile, buildFile.getFileSize());
  BuildFileAST buildFileAst =
      BuildFileAST.parseBuildFile(
          ParserInputSource.create(buildFileContent, buildFile.asFragment()), eventHandler);
  Environment env =
      Environment.builder(mutability)
          .setGlobals(BazelLibrary.GLOBALS)
          .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS)
          .build();
  SkylarkUtils.setPhase(env, Phase.LOADING);
  new ParseContext(
          PackageContext.of(
              NativeGlobber.create(root),
              rawConfig,
              PackageIdentifier.create(RepositoryName.DEFAULT, PathFragment.create("my/package")),
              eventHandler,
              ImmutableMap.of()))
      .setup(env);
  env.setup(
      "package_name",
      FuncallExpression.getBuiltinCallable(SkylarkBuildModule.BUILD_MODULE, "package_name"));
  boolean exec = buildFileAst.exec(env, eventHandler);
  if (!exec && expectSuccess) {
    Assert.fail("Build file evaluation must have succeeded");
  }
  return env;
}
 
Example #20
Source File: RunInfoTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void errorOnInvalidEnvType() throws EvalException {

  try (Mutability mut = Mutability.create("test")) {
    Object env = SkylarkDict.of(getEnv(mut), "foo", 1, "bar", 2);
    thrown.expect(EvalException.class);
    // Broken cast, but this can apparently happen, so... verify :)
    RunInfo.instantiateFromSkylark((SkylarkDict<String, String>) env, ImmutableList.of("value"));
  }
}
 
Example #21
Source File: TestInfoTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void usesDefaultSkylarkValues() throws InterruptedException, EvalException {
  Object raw;
  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();

    raw =
        BuildFileAST.eval(
            env,
            String.format(
                "TestInfo(test_name=\"%s\", test_case_name=\"%s\")", TEST_NAME, TEST_CASE_NAME));
  }
  assertTrue(raw instanceof TestInfo);
  TestInfo testInfo = (TestInfo) raw;

  assertEquals(ImmutableSet.of(), testInfo.labels());
  assertEquals(ImmutableSet.of(), testInfo.contacts());
  assertEquals(Runtime.NONE, testInfo.timeoutMs());
  assertEquals("custom", testInfo.type());
  assertFalse(testInfo.runTestsSeparately());
  assertEquals(TEST_NAME, testInfo.testName());
  assertEquals(TEST_CASE_NAME, testInfo.testCaseName());
}
 
Example #22
Source File: StarlarkNativeModule.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Nullable
private static Dict<String, Object> targetDict(Target target, Mutability mu)
    throws EvalException {
  if (!(target instanceof Rule)) {
    return null;
  }
  Dict<String, Object> values = Dict.of(mu);

  Rule rule = (Rule) target;
  AttributeContainer cont = rule.getAttributeContainer();
  for (Attribute attr : rule.getAttributes()) {
    if (!Character.isAlphabetic(attr.getName().charAt(0))) {
      continue;
    }

    if (attr.getName().equals("distribs")) {
      // attribute distribs: cannot represent type class java.util.Collections$SingletonSet
      // in Starlark: [INTERNAL].
      continue;
    }

    try {
      Object val = starlarkifyValue(mu, cont.getAttr(attr.getName()), target.getPackage());
      if (val == null) {
        continue;
      }
      values.put(attr.getName(), val, (Location) null);
    } catch (NotRepresentableException e) {
      throw new NotRepresentableException(
          String.format(
              "target %s, attribute %s: %s", target.getName(), attr.getName(), e.getMessage()));
    }
  }

  values.put("name", rule.getName(), (Location) null);
  values.put("kind", rule.getRuleClass(), (Location) null);
  return values;
}
 
Example #23
Source File: BuiltInProviderInfoTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void instantiatesFromStaticMethodWithDefaultValuesIfPresent()
    throws InterruptedException, EvalException {
  Object o;
  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 IntegerLiteral(4))));

    o = ast.eval(env);
  }

  assertThat(o, Matchers.instanceOf(SomeInfoWithInstantiate.class));
  SomeInfoWithInstantiate someInfo4 = (SomeInfoWithInstantiate) o;
  assertEquals(ImmutableList.of("foo"), someInfo4.str_list());
  assertEquals("4", someInfo4.myInfo());
}
 
Example #24
Source File: SkylarkPackageModuleTest.java    From buck with Apache License 2.0 5 votes vote down vote up
private ParseContext evaluate(Path buildFile, Mutability mutability, boolean expectSuccess)
    throws IOException, InterruptedException {
  byte[] buildFileContent =
      FileSystemUtils.readWithKnownFileSize(buildFile, buildFile.getFileSize());
  BuildFileAST buildFileAst =
      BuildFileAST.parseBuildFile(
          ParserInputSource.create(buildFileContent, buildFile.asFragment()), eventHandler);
  Environment env =
      Environment.builder(mutability)
          .setGlobals(BazelLibrary.GLOBALS)
          .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS)
          .build();
  SkylarkUtils.setPhase(env, SkylarkUtils.Phase.LOADING);
  ParseContext parseContext =
      new ParseContext(
          PackageContext.of(
              NativeGlobber.create(root),
              ImmutableMap.of(),
              PackageIdentifier.create(RepositoryName.DEFAULT, PathFragment.create("my/package")),
              eventHandler,
              ImmutableMap.of()));
  parseContext.setup(env);
  env.setup(
      "package",
      FuncallExpression.getBuiltinCallable(SkylarkPackageModule.PACKAGE_MODULE, "package"));
  boolean exec = buildFileAst.exec(env, eventHandler);
  if (!exec && expectSuccess) {
    Assert.fail("Package file evaluation must have succeeded");
  }
  return parseContext;
}
 
Example #25
Source File: SkydocMain.java    From bazel with Apache License 2.0 5 votes vote down vote up
/** Evaluates the AST from a single Starlark file, given the already-resolved imports. */
private static Module evalStarlarkBody(
    StarlarkSemantics semantics,
    StarlarkFile file,
    Map<String, Module> imports,
    List<RuleInfoWrapper> ruleInfoList,
    List<ProviderInfoWrapper> providerInfoList,
    List<AspectInfoWrapper> aspectInfoList)
    throws InterruptedException, StarlarkEvaluationException {

  Module module =
      Module.withPredeclared(
          semantics, getPredeclaredEnvironment(ruleInfoList, providerInfoList, aspectInfoList));

  Resolver.resolveFile(file, module);
  if (!file.ok()) {
    throw new StarlarkEvaluationException(file.errors().get(0).toString());
  }

  // execute
  try (Mutability mu = Mutability.create("Skydoc")) {
    StarlarkThread thread = new StarlarkThread(mu, semantics);
    // We use the default print handler, which writes to stderr.
    thread.setLoader(imports::get);

    EvalUtils.exec(file, module, thread);
  } catch (EvalException | InterruptedException ex) {
    // This exception class seems a bit unnecessary. Replace with EvalException?
    throw new StarlarkEvaluationException("Starlark evaluation error", ex);
  }
  return module;
}
 
Example #26
Source File: BuiltInProviderInfoTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@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 #27
Source File: AbstractSkylarkFileParser.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * @return The environment that can be used for evaluating build files. It includes built-in
 *     functions like {@code glob} and native rules like {@code java_library}.
 */
private EnvironmentData createBuildFileEvaluationEnvironment(
    com.google.devtools.build.lib.vfs.Path buildFilePath,
    Label containingLabel,
    BuildFileAST buildFileAst,
    Mutability mutability,
    ParseContext parseContext,
    @Nullable ExtensionData implicitLoadExtensionData)
    throws IOException, InterruptedException, BuildFileParseException {
  ImmutableList<ExtensionData> dependencies =
      loadExtensions(containingLabel, buildFileAst.getImports());
  ImmutableMap<String, Environment.Extension> importMap =
      toImportMap(dependencies, implicitLoadExtensionData);
  Environment env =
      Environment.builder(mutability)
          .setImportedExtensions(importMap)
          .setGlobals(buckGlobals.getBuckBuildFileContextGlobals())
          .setSemantics(BuckStarlark.BUCK_STARLARK_SEMANTICS)
          .setEventHandler(eventHandler)
          .build();
  SkylarkUtils.setPhase(env, Phase.LOADING);

  parseContext.setup(env);

  return ImmutableEnvironmentData.of(
      env, toLoadedPaths(buildFilePath, dependencies, implicitLoadExtensionData));
}
 
Example #28
Source File: BuiltInProviderInfoTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void allowsNoneAsAParamToStaticMethod() 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(
                        SomeInfoWithNoneable.PROVIDER.getName(),
                        SomeInfoWithNoneable.PROVIDER,
                        "None",
                        Runtime.NONE)))
            .build();

    Object none = BuildFileAST.eval(env, "SomeInfoWithNoneable(noneable_val=None, val=1)");
    Object strValue = BuildFileAST.eval(env, "SomeInfoWithNoneable(noneable_val=\"foo\", val=1)");

    assertThat(none, Matchers.instanceOf(SomeInfoWithNoneable.class));
    assertThat(strValue, Matchers.instanceOf(SomeInfoWithNoneable.class));

    assertEquals(Runtime.NONE, ((SomeInfoWithNoneable) none).noneableVal());
    assertEquals("foo", ((SomeInfoWithNoneable) strValue).noneableVal());

    thrown.expect(EvalException.class);
    thrown.expectMessage("cannot be None");
    BuildFileAST.eval(env, "SomeInfoWithNoneable(noneable_val=None, val=None)");
  }
}
 
Example #29
Source File: BuckStarlarkFunctionTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@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 #30
Source File: BasicRuleRuleDescription.java    From buck with Apache License 2.0 5 votes vote down vote up
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;
}