Skip to content

Commit

Permalink
MockToolsConfig now enforces relative path args.
Browse files Browse the repository at this point in the history
In MockToolsConfig, all rootDirectory.getRelative
calls except the one in the ctor (and in getPath)
are replaced with a call to getPath, because this
standardizes all path lookups.
The ctor's call is legitimate and remains, because
it converts an absolute path string to a Path
object and there's no other way to do that.

Also, MockToolsConfig.getPath now validates that
its argument is a relative path, because that's
what the function expects (according to the
variable name) and because that's how we can
support real (not in-memory) filesystems.

Finally, fix call sites that passed absolute paths
(e.g. create("/local_config_xcode/WORKSPACE")) to
pass a relative path instead, again because that's
how we can use a real filesystem. (All paths will
be made relative to the FileSystem's root
directory.)

RELNOTES: none
PiperOrigin-RevId: 294155732
  • Loading branch information
laszlocsomor authored and Copybara-Service committed Feb 10, 2020
1 parent 9ec7181 commit e1d2e2a
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 89 deletions.
Expand Up @@ -67,18 +67,20 @@ private BazelAnalysisMock() {

@Override
public List<String> getWorkspaceContents(MockToolsConfig config) {
String bazelToolWorkspace = config.getPath("/bazel_tools_workspace").getPathString();
String bazelPlatformsWorkspace = config.getPath("/platforms").getPathString();
String rulesJavaWorkspace = config.getPath("/rules_java_workspace").getPathString();
String xcodeWorkspace = config.getPath("local_config_xcode_workspace").getPathString();
String protobufWorkspace = config.getPath("protobuf_workspace").getPathString();
String bazelToolWorkspace = config.getPath("bazel_tools_workspace").getPathString();
String bazelPlatformsWorkspace = config.getPath("platforms_workspace").getPathString();
String rulesJavaWorkspace = config.getPath("rules_java_workspace").getPathString();
String localConfigPlatformWorkspace =
config.getPath("/local_config_platform_workspace").getPathString();
config.getPath("local_config_platform_workspace").getPathString();

return new ArrayList<>(
ImmutableList.of(
"local_repository(name = 'bazel_tools', path = '" + bazelToolWorkspace + "')",
"local_repository(name = 'platforms', path = '" + bazelPlatformsWorkspace + "')",
"local_repository(name = 'local_config_xcode', path = '/local_config_xcode')",
"local_repository(name = 'com_google_protobuf', path = '/protobuf')",
"local_repository(name = 'local_config_xcode', path = '" + xcodeWorkspace + "')",
"local_repository(name = 'com_google_protobuf', path = '" + protobufWorkspace + "')",
"local_repository(name = 'rules_java', path = '" + rulesJavaWorkspace + "')",
"register_toolchains('@rules_java//java/toolchains/runtime:all')",
"register_toolchains('@rules_java//java/toolchains/javac:all')",
Expand All @@ -100,26 +102,28 @@ public void setupMockClient(MockToolsConfig config) throws IOException {
@Override
public void setupMockClient(MockToolsConfig config, List<String> workspaceContents)
throws IOException {
config.create("/local_config_xcode/BUILD", "xcode_config(name = 'host_xcodes')");
config.create("local_config_xcode_workspace/BUILD", "xcode_config(name = 'host_xcodes')");
config.create(
"/protobuf/BUILD", "licenses(['notice'])", "exports_files(['protoc', 'cc_toolchain'])");
config.create("/local_config_xcode/WORKSPACE");
config.create("/protobuf/WORKSPACE");
config.overwrite("WORKSPACE", workspaceContents.toArray(new String[workspaceContents.size()]));
"protobuf_workspace/BUILD",
"licenses(['notice'])",
"exports_files(['protoc', 'cc_toolchain'])");
config.create("local_config_xcode_workspace/WORKSPACE");
config.create("protobuf_workspace/WORKSPACE");
config.overwrite("WORKSPACE", workspaceContents.toArray(new String[0]));
/** The rest of platforms is initialized in {@link MockPlatformSupport}. */
config.create("/platforms/WORKSPACE", "workspace(name = 'platforms')");
config.create("/bazel_tools_workspace/WORKSPACE", "workspace(name = 'bazel_tools')");
config.create("platforms_workspace/WORKSPACE", "workspace(name = 'platforms')");
config.create("bazel_tools_workspace/WORKSPACE", "workspace(name = 'bazel_tools')");
Runfiles runfiles = Runfiles.create();
for (String filename : Arrays.asList("tools/jdk/java_toolchain_alias.bzl")) {
java.nio.file.Path path = Paths.get(runfiles.rlocation("io_bazel/" + filename));
if (!Files.exists(path)) {
continue; // the io_bazel workspace root only exists for Bazel
}
config.create(
"/bazel_tools_workspace/" + filename, MoreFiles.asCharSource(path, UTF_8).read());
"bazel_tools_workspace/" + filename, MoreFiles.asCharSource(path, UTF_8).read());
}
config.create(
"/bazel_tools_workspace/tools/jdk/BUILD",
"bazel_tools_workspace/tools/jdk/BUILD",
"load(",
" ':java_toolchain_alias.bzl',",
" 'java_toolchain_alias',",
Expand Down Expand Up @@ -208,17 +212,16 @@ public void setupMockClient(MockToolsConfig config, List<String> workspaceConten

ImmutableList<String> androidBuildContents = createAndroidBuildContents();
config.create(
"/bazel_tools_workspace/tools/android/BUILD",
androidBuildContents.toArray(new String[androidBuildContents.size()]));
"bazel_tools_workspace/tools/android/BUILD", androidBuildContents.toArray(new String[0]));
config.create(
"/bazel_tools_workspace/tools/android/emulator/BUILD",
"bazel_tools_workspace/tools/android/emulator/BUILD",
Iterables.toArray(createToolsAndroidEmulatorContents(), String.class));

config.create(
"/bazel_tools_workspace/tools/genrule/BUILD", "exports_files(['genrule-setup.sh'])");
"bazel_tools_workspace/tools/genrule/BUILD", "exports_files(['genrule-setup.sh'])");

config.create(
"/bazel_tools_workspace/tools/test/BUILD",
"bazel_tools_workspace/tools/test/BUILD",
"filegroup(name = 'runtime', srcs = ['test-setup.sh', 'test-xml-generator.sh'])",
"filegroup(name = 'test_wrapper', srcs = ['test_wrapper_bin'])",
"filegroup(name = 'xml_writer', srcs = ['xml_writer_bin'])",
Expand All @@ -232,7 +235,7 @@ public void setupMockClient(MockToolsConfig config, List<String> workspaceConten

// Use an alias package group to allow for modification at the simpler path
config.create(
"/bazel_tools_workspace/tools/whitelists/config_feature_flag/BUILD",
"bazel_tools_workspace/tools/whitelists/config_feature_flag/BUILD",
"package_group(",
" name='config_feature_flag',",
" includes=['@//tools/whitelists/config_feature_flag'],",
Expand All @@ -247,24 +250,24 @@ public void setupMockClient(MockToolsConfig config, List<String> workspaceConten
"package_group(name='config_feature_flag', packages=['//...'])");

config.create(
"/bazel_tools_workspace/tools/zip/BUILD",
"bazel_tools_workspace/tools/zip/BUILD",
"package(default_visibility=['//visibility:public'])",
"exports_files(['precompile.py'])",
"cc_binary(name='zipper', srcs=['zip_main.cc'])");

config.create(
"/bazel_tools_workspace/tools/launcher/BUILD",
"bazel_tools_workspace/tools/launcher/BUILD",
"package(default_visibility=['//visibility:public'])",
"load('@bazel_tools//third_party/cc_rules/macros:defs.bzl', 'cc_binary')",
"cc_binary(name='launcher', srcs=['launcher_main.cc'])");

config.create(
"/bazel_tools_workspace/tools/def_parser/BUILD",
"bazel_tools_workspace/tools/def_parser/BUILD",
"package(default_visibility=['//visibility:public'])",
"filegroup(name='def_parser', srcs=['def_parser.exe'])");

config.create(
"/bazel_tools_workspace/objcproto/BUILD",
"bazel_tools_workspace/objcproto/BUILD",
"package(default_visibility=['//visibility:public'])",
"objc_library(",
" name = 'protobuf_lib',",
Expand All @@ -277,37 +280,37 @@ public void setupMockClient(MockToolsConfig config, List<String> workspaceConten
" name = 'well_known_type_proto',",
" srcs = ['well_known_type.proto'],",
")");
config.create("/bazel_tools_workspace/objcproto/empty.m");
config.create("/bazel_tools_workspace/objcproto/empty.cc");
config.create("/bazel_tools_workspace/objcproto/well_known_type.proto");
config.create("bazel_tools_workspace/objcproto/empty.m");
config.create("bazel_tools_workspace/objcproto/empty.cc");
config.create("bazel_tools_workspace/objcproto/well_known_type.proto");

config.create("/rules_java_workspace/WORKSPACE", "workspace(name = 'rules_java')");
config.create("/rules_java_workspace/java/BUILD");
config.create("rules_java_workspace/WORKSPACE", "workspace(name = 'rules_java')");
config.create("rules_java_workspace/java/BUILD");
config.create(
"/rules_java_workspace/java/defs.bzl",
"rules_java_workspace/java/defs.bzl",
"def java_binary(**attrs):",
" native.java_binary(**attrs)",
"def java_library(**attrs):",
" native.java_library(**attrs)",
"def java_import(**attrs):",
" native.java_import(**attrs)");
config.create(
"/rules_java_workspace/java/repositories.bzl",
"rules_java_workspace/java/repositories.bzl",
"def rules_java_dependencies():",
" pass",
"def rules_java_toolchains():",
" pass");

config.create(
"/rules_java_workspace/java/toolchains/runtime/BUILD",
"rules_java_workspace/java/toolchains/runtime/BUILD",
"toolchain_type(name = 'toolchain_type')",
"toolchain(",
" name = 'local_jdk',",
" toolchain = '@bazel_tools//tools/jdk:jdk',",
" toolchain_type = '@rules_java//java/toolchains/runtime:toolchain_type',",
" )");
config.create(
"/rules_java_workspace/java/toolchains/javac/BUILD",
"rules_java_workspace/java/toolchains/javac/BUILD",
"toolchain_type(name = 'toolchain_type')",
"toolchain(",
" name = 'javac_toolchain',",
Expand Down Expand Up @@ -419,15 +422,15 @@ public void setupMockWorkspaceFiles(Path embeddedBinariesRoot) throws IOExceptio

@Override
public void setupMockToolsRepository(MockToolsConfig config) throws IOException {
config.create("/bazel_tools_workspace/WORKSPACE", "workspace(name = 'bazel_tools')");
config.create("/bazel_tools_workspace/tools/build_defs/repo/BUILD");
config.create("bazel_tools_workspace/WORKSPACE", "workspace(name = 'bazel_tools')");
config.create("bazel_tools_workspace/tools/build_defs/repo/BUILD");
config.create(
"/bazel_tools_workspace/tools/build_defs/repo/utils.bzl",
"bazel_tools_workspace/tools/build_defs/repo/utils.bzl",
"def maybe(repo_rule, name, **kwargs):",
" if name not in native.existing_rules():",
" repo_rule(name = name, **kwargs)");
config.create(
"/bazel_tools_workspace/tools/build_defs/repo/http.bzl",
"bazel_tools_workspace/tools/build_defs/repo/http.bzl",
"def http_archive(**kwargs):",
" pass",
"",
Expand Down
Expand Up @@ -234,15 +234,15 @@ public void initializeSkyframeExecutor(boolean doPackageLoadingChecks) throws Ex

actionKeyContext = new ActionKeyContext();
mockToolsConfig = new MockToolsConfig(rootDirectory, false);
mockToolsConfig.create("/bazel_tools_workspace/WORKSPACE", "workspace(name = 'bazel_tools')");
mockToolsConfig.create("/bazel_tools_workspace/tools/build_defs/repo/BUILD");
mockToolsConfig.create("bazel_tools_workspace/WORKSPACE", "workspace(name = 'bazel_tools')");
mockToolsConfig.create("bazel_tools_workspace/tools/build_defs/repo/BUILD");
mockToolsConfig.create(
"/bazel_tools_workspace/tools/build_defs/repo/utils.bzl",
"bazel_tools_workspace/tools/build_defs/repo/utils.bzl",
"def maybe(repo_rule, name, **kwargs):",
" if name not in native.existing_rules():",
" repo_rule(name = name, **kwargs)");
mockToolsConfig.create(
"/bazel_tools_workspace/tools/build_defs/repo/http.bzl",
"bazel_tools_workspace/tools/build_defs/repo/http.bzl",
"def http_archive(**kwargs):",
" pass",
"",
Expand Down
Expand Up @@ -111,15 +111,15 @@ public final void initializeSkyframeExecutor() throws Exception {
analysisMock.getProductName());

mockToolsConfig = new MockToolsConfig(rootDirectory);
mockToolsConfig.create("/bazel_tools_workspace/WORKSPACE", "workspace(name = 'bazel_tools')");
mockToolsConfig.create("/bazel_tools_workspace/tools/build_defs/repo/BUILD");
mockToolsConfig.create("bazel_tools_workspace/WORKSPACE", "workspace(name = 'bazel_tools')");
mockToolsConfig.create("bazel_tools_workspace/tools/build_defs/repo/BUILD");
mockToolsConfig.create(
"/bazel_tools_workspace/tools/build_defs/repo/utils.bzl",
"bazel_tools_workspace/tools/build_defs/repo/utils.bzl",
"def maybe(repo_rule, name, **kwargs):",
" if name not in native.existing_rules():",
" repo_rule(name = name, **kwargs)");
mockToolsConfig.create(
"/bazel_tools_workspace/tools/build_defs/repo/http.bzl",
"bazel_tools_workspace/tools/build_defs/repo/http.bzl",
"def http_archive(**kwargs):",
" pass",
"",
Expand Down
Expand Up @@ -48,14 +48,14 @@ protected ConfiguredRuleClassProvider getRuleClassProvider() {
@Before
public void setup() throws Exception {
BazelMockCcSupport.INSTANCE.setup(mockToolsConfig);
scratch.overwriteFile("/bazel_tools_workspace/tools/build_defs/cc/BUILD");
scratch.overwriteFile("bazel_tools_workspace/tools/build_defs/cc/BUILD");
scratch.overwriteFile(
"/bazel_tools_workspace/tools/build_defs/cc/action_names.bzl",
"bazel_tools_workspace/tools/build_defs/cc/action_names.bzl",
ResourceLoader.readFromResources(
TestConstants.RULES_CC_REPOSITORY_EXECROOT + "cc/action_names.bzl"));

scratch.overwriteFile(
"/bazel_tools_workspace/tools/cpp/cc_toolchain_config_lib.bzl",
"bazel_tools_workspace/tools/cpp/cc_toolchain_config_lib.bzl",
ResourceLoader.readFromResources(
TestConstants.RULES_CC_REPOSITORY_EXECROOT + "cc/cc_toolchain_config_lib.bzl"));
scratch.file("/ndk/source.properties", "Pkg.Desc = Android NDK", "Pkg.Revision = 13.1.3345770");
Expand All @@ -72,9 +72,10 @@ private void scratchPlatformsDirectories(String arch, int... apiLevels) throws E
@Test
public void testApiLevelHighestVersionDetection() throws Exception {
scratchPlatformsDirectories("arch-x86", 19, 20, 22, 24);
String bazelToolsWorkspace = scratch.dir("bazel_tools_workspace").getPathString();
FileSystemUtils.appendIsoLatin1(
scratch.resolve("WORKSPACE"),
"local_repository(name = 'bazel_tools', path = '/bazel_tools_workspace')",
"local_repository(name = 'bazel_tools', path = '" + bazelToolsWorkspace + "')",
"android_ndk_repository(",
" name = 'androidndk',",
" path = '/ndk',",
Expand All @@ -96,9 +97,10 @@ public void testApiLevelHighestVersionDetection() throws Exception {
@Test
public void testInvalidNdkReleaseTxt() throws Exception {
scratchPlatformsDirectories("arch-x86", 24);
String bazelToolsWorkspace = scratch.dir("bazel_tools_workspace").getPathString();
FileSystemUtils.appendIsoLatin1(
scratch.resolve("WORKSPACE"),
"local_repository(name = 'bazel_tools', path = '/bazel_tools_workspace')",
"local_repository(name = 'bazel_tools', path = '" + bazelToolsWorkspace + "')",
"android_ndk_repository(",
" name = 'androidndk',",
" path = '/ndk',",
Expand All @@ -121,9 +123,10 @@ public void testInvalidNdkReleaseTxt() throws Exception {
@Test
public void testInvalidNdkSourceProperties() throws Exception {
scratchPlatformsDirectories("arch-x86", 24);
String bazelToolsWorkspace = scratch.dir("bazel_tools_workspace").getPathString();
FileSystemUtils.appendIsoLatin1(
scratch.resolve("WORKSPACE"),
"local_repository(name = 'bazel_tools', path = '/bazel_tools_workspace')",
"local_repository(name = 'bazel_tools', path = '" + bazelToolsWorkspace + "')",
"android_ndk_repository(",
" name = 'androidndk',",
" path = '/ndk',",
Expand All @@ -148,9 +151,10 @@ public void testInvalidNdkSourceProperties() throws Exception {
@Test
public void testUnsupportedNdkVersion() throws Exception {
scratchPlatformsDirectories("arch-x86", 24);
String bazelToolsWorkspace = scratch.dir("bazel_tools_workspace").getPathString();
FileSystemUtils.appendIsoLatin1(
scratch.resolve("WORKSPACE"),
"local_repository(name = 'bazel_tools', path = '/bazel_tools_workspace')",
"local_repository(name = 'bazel_tools', path = '" + bazelToolsWorkspace + "')",
"android_ndk_repository(",
" name = 'androidndk',",
" path = '/ndk',",
Expand All @@ -175,9 +179,10 @@ public void testMiscLibraries() throws Exception {
scratchPlatformsDirectories("arch-x86", 19, 20, 22, 24);
scratch.file(String.format("/ndk/sources/android/cpufeatures/cpu-features.c"));
scratch.file(String.format("/ndk/sources/android/cpufeatures/cpu-features.h"));
String bazelToolsWorkspace = scratch.dir("bazel_tools_workspace").getPathString();
FileSystemUtils.appendIsoLatin1(
scratch.resolve("WORKSPACE"),
"local_repository(name = 'bazel_tools', path = '/bazel_tools_workspace')",
"local_repository(name = 'bazel_tools', path = '" + bazelToolsWorkspace + "')",
"android_ndk_repository(",
" name = 'androidndk',",
" path = '/ndk',",
Expand All @@ -198,9 +203,10 @@ public void testMiscLibraries() throws Exception {

@Test
public void testMissingPlatformsDirectory() throws Exception {
String bazelToolsWorkspace = scratch.dir("bazel_tools_workspace").getPathString();
FileSystemUtils.appendIsoLatin1(
scratch.resolve("WORKSPACE"),
"local_repository(name = 'bazel_tools', path = '/bazel_tools_workspace')",
"local_repository(name = 'bazel_tools', path = '" + bazelToolsWorkspace + "')",
"android_ndk_repository(",
" name = 'androidndk',",
" path = '/ndk',",
Expand Down

0 comments on commit e1d2e2a

Please sign in to comment.