Skip to content

Commit

Permalink
Pass in a set of SkyFunctions in AnalysisMock.
Browse files Browse the repository at this point in the history
This is required because Bazel will get tools from an external repository, and thus tests will need to support external repositories.

This code is currently unused and was lopped off of a git branch I maintain locally that contains a Bazel version that gets tools from an external repository.

--
MOS_MIGRATED_REVID=104851767
  • Loading branch information
lberki authored and hanwen committed Oct 8, 2015
1 parent 76c0e49 commit fdc46c9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@
import com.google.common.base.Functions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams;
import com.google.devtools.build.lib.analysis.BlazeDirectories;
import com.google.devtools.build.lib.analysis.ConfigurationCollectionFactory;
import com.google.devtools.build.lib.analysis.config.ConfigurationFactory;
import com.google.devtools.build.lib.analysis.config.FragmentOptions;
import com.google.devtools.build.lib.analysis.util.AnalysisMock;
import com.google.devtools.build.lib.bazel.repository.LocalRepositoryFunction;
import com.google.devtools.build.lib.bazel.repository.RepositoryDelegatorFunction;
import com.google.devtools.build.lib.bazel.repository.RepositoryFunction;
import com.google.devtools.build.lib.bazel.rules.BazelConfiguration;
import com.google.devtools.build.lib.bazel.rules.BazelConfigurationCollection;
import com.google.devtools.build.lib.bazel.rules.BazelRuleClassProvider;
import com.google.devtools.build.lib.bazel.rules.android.AndroidRepositoryRules;
import com.google.devtools.build.lib.bazel.rules.python.BazelPythonConfiguration;
import com.google.devtools.build.lib.bazel.rules.workspace.LocalRepositoryRule;
import com.google.devtools.build.lib.packages.Attribute;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.util.MockToolsConfig;
Expand All @@ -38,16 +44,20 @@
import com.google.devtools.build.lib.rules.java.JvmConfigurationLoader;
import com.google.devtools.build.lib.rules.objc.ObjcConfigurationLoader;
import com.google.devtools.build.lib.rules.python.PythonConfigurationLoader;
import com.google.devtools.build.lib.skyframe.SkyFunctions;
import com.google.devtools.build.lib.testutil.TestRuleClassProvider;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.skyframe.SkyFunction;
import com.google.devtools.build.skyframe.SkyFunctionName;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class BazelAnalysisMock extends AnalysisMock {
public static final AnalysisMock INSTANCE = new BazelAnalysisMock();
Expand Down Expand Up @@ -238,4 +248,20 @@ public Collection<String> getOptionOverrides() {
public ImmutableList<Class<? extends FragmentOptions>> getBuildOptions() {
return BazelRuleClassProvider.BUILD_OPTIONS;
}

@Override
public ImmutableMap<SkyFunctionName, SkyFunction> getSkyFunctions(BlazeDirectories directories) {
// Bazel will access tools using a local_repository rule, thus, we need to support those rules
// in test cases, too. So we need the appropriate SkyFunctions.
RepositoryFunction localRepositoryFunction = new LocalRepositoryFunction();
localRepositoryFunction.setDirectories(directories);
ImmutableMap<String, RepositoryFunction> repositoryHandlers = ImmutableMap.of(
LocalRepositoryRule.NAME, localRepositoryFunction);

return ImmutableMap.of(
SkyFunctions.REPOSITORY,
new RepositoryDelegatorFunction(directories, repositoryHandlers, new AtomicBoolean(true)),
localRepositoryFunction.getSkyFunctionName(),
localRepositoryFunction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
package com.google.devtools.build.lib.analysis.util;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.BlazeDirectories;
import com.google.devtools.build.lib.analysis.ConfigurationCollectionFactory;
import com.google.devtools.build.lib.analysis.config.ConfigurationFactory;
import com.google.devtools.build.lib.analysis.config.FragmentOptions;
import com.google.devtools.build.lib.packages.util.MockToolsConfig;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.skyframe.SkyFunction;
import com.google.devtools.build.skyframe.SkyFunctionName;

import java.io.IOException;
import java.util.Collection;
Expand Down Expand Up @@ -48,6 +52,9 @@ public abstract class AnalysisMock {

public abstract ImmutableList<Class<? extends FragmentOptions>> getBuildOptions();

public abstract ImmutableMap<SkyFunctionName, SkyFunction> getSkyFunctions(
BlazeDirectories directories);

public static class Delegate extends AnalysisMock {
private final AnalysisMock delegate;

Expand Down Expand Up @@ -84,5 +91,11 @@ public Collection<String> getOptionOverrides() {
public ImmutableList<Class<? extends FragmentOptions>> getBuildOptions() {
return delegate.getBuildOptions();
}

@Override
public ImmutableMap<SkyFunctionName, SkyFunction> getSkyFunctions(
BlazeDirectories directories) {
return delegate.getSkyFunctions(directories);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2011-2015 Google Inc. All Rights Reserved.
// Copyright 2015 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.
Expand All @@ -16,7 +16,6 @@
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -63,8 +62,6 @@
import com.google.devtools.build.lib.vfs.ModifiedFileSet;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.skyframe.SkyFunction;
import com.google.devtools.build.skyframe.SkyFunctionName;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.common.options.Options;
import com.google.devtools.common.options.OptionsParser;
Expand Down Expand Up @@ -127,21 +124,22 @@ public FlagBuilder without(Flag flag) {

protected AnalysisTestUtil.DummyWorkspaceStatusActionFactory workspaceStatusActionFactory;
private PathPackageLocator pkgLocator;
private AnalysisMock analysisMock;
protected boolean enableLoading = true;

@Override
protected void setUp() throws Exception {
super.setUp();
AnalysisMock mock = getAnalysisMock();
analysisMock = getAnalysisMock();
pkgLocator = new PathPackageLocator(rootDirectory);
directories = new BlazeDirectories(outputBase, outputBase, rootDirectory);
workspaceStatusActionFactory =
new AnalysisTestUtil.DummyWorkspaceStatusActionFactory(directories);

mockToolsConfig = new MockToolsConfig(rootDirectory);
mock.setupMockClient(mockToolsConfig);
mock.setupMockWorkspaceFiles(directories.getEmbeddedBinariesRoot());
configurationFactory = mock.createConfigurationFactory();
analysisMock.setupMockClient(mockToolsConfig);
analysisMock.setupMockWorkspaceFiles(directories.getEmbeddedBinariesRoot());
configurationFactory = analysisMock.createConfigurationFactory();

useRuleClassProvider(TestRuleClassProvider.getRuleClassProvider());
}
Expand Down Expand Up @@ -176,7 +174,7 @@ protected void useRuleClassProvider(ConfiguredRuleClassProvider ruleClassProvide
ImmutableList.<DiffAwareness.Factory>of(),
Predicates.<PathFragment>alwaysFalse(),
Preprocessor.Factory.Supplier.NullSupplier.INSTANCE,
ImmutableMap.<SkyFunctionName, SkyFunction>of(),
analysisMock.getSkyFunctions(directories),
getPrecomputedValues(),
ImmutableList.<SkyValueDirtinessChecker>of());
skyframeExecutor.preparePackageLoading(pkgLocator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.skyframe.SkyFunction;
import com.google.devtools.build.skyframe.SkyFunctionName;
import com.google.devtools.common.options.Options;
import com.google.devtools.common.options.OptionsParser;
import com.google.devtools.common.options.OptionsParsingException;
Expand Down Expand Up @@ -205,7 +204,7 @@ protected void setUp() throws Exception {
ImmutableList.<DiffAwareness.Factory>of(),
Predicates.<PathFragment>alwaysFalse(),
getPreprocessorFactorySupplier(),
ImmutableMap.<SkyFunctionName, SkyFunction>of(),
mock.getSkyFunctions(directories),
getPrecomputedValues(),
ImmutableList.<SkyValueDirtinessChecker>of());
skyframeExecutor.preparePackageLoading(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -46,8 +45,6 @@
import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.skyframe.SkyFunction;
import com.google.devtools.build.skyframe.SkyFunctionName;
import com.google.devtools.common.options.Converters;
import com.google.devtools.common.options.Option;
import com.google.devtools.common.options.Options;
Expand Down Expand Up @@ -93,6 +90,8 @@ protected void setUp() throws Exception {
pkgFactory = new PackageFactory(ruleClassProvider);
AnalysisTestUtil.DummyWorkspaceStatusActionFactory workspaceStatusActionFactory =
new AnalysisTestUtil.DummyWorkspaceStatusActionFactory(directories);
AnalysisMock analysisMock = getAnalysisMock();

skyframeExecutor =
SequencedSkyframeExecutor.create(
pkgFactory,
Expand All @@ -105,15 +104,14 @@ protected void setUp() throws Exception {
ImmutableList.<DiffAwareness.Factory>of(),
Predicates.<PathFragment>alwaysFalse(),
Preprocessor.Factory.Supplier.NullSupplier.INSTANCE,
ImmutableMap.<SkyFunctionName, SkyFunction>of(),
analysisMock.getSkyFunctions(directories),
ImmutableList.<PrecomputedValue.Injected>of(),
ImmutableList.<SkyValueDirtinessChecker>of());

skyframeExecutor.preparePackageLoading(pkgLocator,
Options.getDefaults(PackageCacheOptions.class).defaultVisibility, true,
7, ruleClassProvider.getDefaultsPackageContent(), UUID.randomUUID());

AnalysisMock analysisMock = getAnalysisMock();
analysisMock.setupMockClient(new MockToolsConfig(rootDirectory));
analysisMock.setupMockWorkspaceFiles(directories.getEmbeddedBinariesRoot());
configurationFactory = analysisMock.createConfigurationFactory();
Expand Down

0 comments on commit fdc46c9

Please sign in to comment.