Search in sources :

Example 51 with Rule

use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.

the class MavenJarFunctionTest method testNoSha1WithCache.

@Test
public void testNoSha1WithCache() throws Exception {
    Rule rule = scratchRule("external", "foo", "maven_jar(", "    name = 'foo',", "    artifact = 'x:y:z:1.1',", ")");
    RepositoryCache cache = Mockito.mock(RepositoryCache.class);
    Mockito.when(cache.isEnabled()).thenReturn(true);
    MavenDownloader downloader = new MavenDownloader(cache);
    try {
        downloader.download("foo", WorkspaceAttributeMapper.of(rule), scratch.dir("/whatever"), TEST_SERVER);
        fail("Expected failure to fetch artifact because of nonexistent server.");
    } catch (IOException expected) {
        assertThat(expected.getMessage()).contains("Failed to fetch Maven dependency:");
    }
}
Also used : RepositoryCache(com.google.devtools.build.lib.bazel.repository.cache.RepositoryCache) Rule(com.google.devtools.build.lib.packages.Rule) IOException(java.io.IOException) Test(org.junit.Test)

Example 52 with Rule

use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.

the class BuildViewTestCase method scratchRule.

/**
   * Create and return a scratch rule.
   *
   * @param packageName the package name of the rule.
   * @param ruleName the name of the rule.
   * @param lines the text of the rule.
   * @return the rule instance for the created rule.
   * @throws IOException
   * @throws Exception
   */
protected Rule scratchRule(String packageName, String ruleName, String... lines) throws Exception {
    String buildFilePathString = packageName + "/BUILD";
    if (packageName.equals(Label.EXTERNAL_PACKAGE_NAME.getPathString())) {
        buildFilePathString = "WORKSPACE";
        scratch.overwriteFile(buildFilePathString, lines);
    } else {
        scratch.file(buildFilePathString, lines);
    }
    skyframeExecutor.invalidateFilesUnderPathForTesting(reporter, new ModifiedFileSet.Builder().modify(new PathFragment(buildFilePathString)).build(), rootDirectory);
    return (Rule) getTarget("//" + packageName + ":" + ruleName);
}
Also used : ModifiedFileSet(com.google.devtools.build.lib.vfs.ModifiedFileSet) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Rule(com.google.devtools.build.lib.packages.Rule)

Example 53 with Rule

use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.

the class SdkMavenRepositoryTest method testGeneratedAarImport.

@Test
public void testGeneratedAarImport() throws Exception {
    sdkMavenRepository.writeBuildFiles(workspaceDir);
    Rule aarImport = getConfiguredTarget("//com.google.android:bar-1.0.0").getTarget().getAssociatedRule();
    assertThat(aarImport.getRuleClass()).isEqualTo("aar_import");
    AttributeMap attributes = RawAttributeMapper.of(aarImport);
    assertThat(attributes.get("aar", BuildType.LABEL)).isEqualTo(Label.parseAbsoluteUnchecked("//:repo/com/google/android/bar/1.0.0/bar.aar"));
    assertThat(attributes.get("exports", BuildType.LABEL_LIST)).containsExactly(Label.parseAbsoluteUnchecked("//com.google.android:foo-1.0.0"));
}
Also used : AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) Rule(com.google.devtools.build.lib.packages.Rule) Test(org.junit.Test)

Example 54 with Rule

use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.

the class SdkMavenRepositoryTest method testGeneratedJavaImport.

@Test
public void testGeneratedJavaImport() throws Exception {
    sdkMavenRepository.writeBuildFiles(workspaceDir);
    Rule javaImport = getConfiguredTarget("//com.google.android:foo-1.0.0").getTarget().getAssociatedRule();
    assertThat(javaImport.getRuleClass()).isEqualTo("java_import");
    AttributeMap attributes = RawAttributeMapper.of(javaImport);
    assertThat(attributes.get("jars", BuildType.LABEL_LIST)).containsExactly(Label.parseAbsoluteUnchecked("//:repo/com/google/android/foo/1.0.0/foo.jar"));
    assertThat(attributes.get("exports", BuildType.LABEL_LIST)).isEmpty();
}
Also used : AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) Rule(com.google.devtools.build.lib.packages.Rule) Test(org.junit.Test)

Example 55 with Rule

use of com.google.devtools.build.lib.packages.Rule in project bazel by bazelbuild.

the class SkylarkFileContentHashTests method getHash.

/**
   * Returns the hash code of the rule target defined by the pkg and the target name parameters.
   * Asserts that the targets and it's Skylark dependencies were loaded properly.
   */
private String getHash(String pkg, String name) throws Exception {
    PackageCacheOptions packageCacheOptions = Options.getDefaults(PackageCacheOptions.class);
    packageCacheOptions.defaultVisibility = ConstantRuleVisibility.PUBLIC;
    packageCacheOptions.showLoadingProgress = true;
    packageCacheOptions.globbingThreads = 7;
    getSkyframeExecutor().preparePackageLoading(new PathPackageLocator(outputBase, ImmutableList.of(rootDirectory)), packageCacheOptions, "", UUID.randomUUID(), ImmutableMap.<String, String>of(), ImmutableMap.<String, String>of(), new TimestampGranularityMonitor(BlazeClock.instance()));
    SkyKey pkgLookupKey = PackageValue.key(PackageIdentifier.parse("@//" + pkg));
    EvaluationResult<PackageValue> result = SkyframeExecutorTestUtils.evaluate(getSkyframeExecutor(), pkgLookupKey, /*keepGoing=*/
    false, reporter);
    assertFalse(result.hasError());
    Collection<Target> targets = result.get(pkgLookupKey).getPackage().getTargets();
    for (Target target : targets) {
        if (target.getName().equals(name)) {
            return ((Rule) target).getRuleClassObject().getRuleDefinitionEnvironment().getTransitiveContentHashCode();
        }
    }
    throw new IllegalStateException("target not found: " + name);
}
Also used : PathPackageLocator(com.google.devtools.build.lib.pkgcache.PathPackageLocator) SkyKey(com.google.devtools.build.skyframe.SkyKey) Target(com.google.devtools.build.lib.packages.Target) Rule(com.google.devtools.build.lib.packages.Rule) PackageCacheOptions(com.google.devtools.build.lib.pkgcache.PackageCacheOptions) TimestampGranularityMonitor(com.google.devtools.build.lib.util.io.TimestampGranularityMonitor)

Aggregations

Rule (com.google.devtools.build.lib.packages.Rule)79 Test (org.junit.Test)27 Label (com.google.devtools.build.lib.cmdline.Label)26 Attribute (com.google.devtools.build.lib.packages.Attribute)20 Target (com.google.devtools.build.lib.packages.Target)19 Nullable (javax.annotation.Nullable)10 RawAttributeMapper (com.google.devtools.build.lib.packages.RawAttributeMapper)9 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)9 OutputFile (com.google.devtools.build.lib.packages.OutputFile)8 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)7 NoSuchThingException (com.google.devtools.build.lib.packages.NoSuchThingException)7 SkyKey (com.google.devtools.build.skyframe.SkyKey)7 ImmutableList (com.google.common.collect.ImmutableList)6 InputFile (com.google.devtools.build.lib.packages.InputFile)6 IOException (java.io.IOException)6 LinkedHashSet (java.util.LinkedHashSet)6 AggregatingAttributeMapper (com.google.devtools.build.lib.packages.AggregatingAttributeMapper)5 Package (com.google.devtools.build.lib.packages.Package)5 Artifact (com.google.devtools.build.lib.actions.Artifact)4 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)4