// Copyright 2017 The Bazel Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.devtools.build.lib.testutil; import com.google.common.base.Function; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.cmdline.PackageIdentifier; import com.google.devtools.build.lib.packages.NoSuchPackageException; import com.google.devtools.build.lib.packages.Package; import com.google.devtools.build.lib.packages.RuleClassProvider; import com.google.devtools.build.lib.packages.Target; import com.google.devtools.build.lib.skyframe.PackageFunction; import com.google.devtools.build.lib.skyframe.packages.BazelPackageLoader; import com.google.devtools.build.lib.skyframe.packages.PackageLoader; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; /** * A Package.Builder.Helper for use in tests that a sanity check with {@link BazelPackageLoader} for * each loaded package, for the sake of getting pretty nice test coverage. */ public class BazelPackageBuilderHelperForTesting implements Package.Builder.Helper { private final RuleClassProvider ruleClassProvider; public BazelPackageBuilderHelperForTesting(RuleClassProvider ruleClassProvider) { this.ruleClassProvider = ruleClassProvider; } @Override public Package createFreshPackage(PackageIdentifier packageId, String runfilesPrefix) { return Package.Builder.DefaultHelper.INSTANCE.createFreshPackage(packageId, runfilesPrefix); } @Override public void onLoadingComplete(Package pkg) { sanityCheckBazelPackageLoader(pkg, ruleClassProvider); } private static final Function TARGET_TO_LABEL = new Function() { @Override public Label apply(Target input) { return input.getLabel(); } }; // This is synchronized because some Skylark internals aren't thread safe. private synchronized void sanityCheckBazelPackageLoader( Package pkg, RuleClassProvider ruleClassProvider) { PackageIdentifier pkgId = pkg.getPackageIdentifier(); if (pkgId.equals(Label.EXTERNAL_PACKAGE_IDENTIFIER) || !pkg.getPackageIdentifier().getRepository().isMain() || PackageFunction.isDefaultsPackage(pkg.getPackageIdentifier())) { // TODO(nharmata): Support these packages. return; } int numNameSegments = pkg.getNameFragment().segmentCount(); PathFragment fullFilenameFragment = pkg.getFilename().asFragment(); int numFullFilenameFragmentSegments = fullFilenameFragment.segmentCount(); Path workspaceRoot = pkg.getFilename().getFileSystem().getPath( fullFilenameFragment.subFragment( 0, numFullFilenameFragmentSegments - (numNameSegments + 1))); PackageLoader packageLoader = BazelPackageLoader.builder(workspaceRoot) .setRuleClassProvider(ruleClassProvider) .build(); Package newlyLoadedPkg; try { newlyLoadedPkg = packageLoader.loadPackage(pkg.getPackageIdentifier()); } catch (InterruptedException e) { return; } catch (NoSuchPackageException e) { throw new IllegalStateException(e); } ImmutableSet