From 3f2f480f0bcdd26d3efbe982836cf8f3597341dd Mon Sep 17 00:00:00 2001 From: cparsons Date: Thu, 2 Nov 2017 19:21:55 -0400 Subject: Open-source the remainder of non-legacy Apple/Objc java tests RELNOTES: None. PiperOrigin-RevId: 174397203 --- .../com/google/devtools/build/lib/rules/cpp/BUILD | 72 ++++++++++++++ .../lib/rules/cpp/LinkBuildVariablesTest.java | 73 +------------- .../lib/rules/cpp/LinkBuildVariablesTestCase.java | 110 +++++++++++++++++++++ 3 files changed, 183 insertions(+), 72 deletions(-) create mode 100644 src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD create mode 100644 src/test/java/com/google/devtools/build/lib/rules/cpp/LinkBuildVariablesTestCase.java (limited to 'src/test/java/com/google/devtools/build/lib/rules/cpp') diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD b/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD new file mode 100644 index 0000000000..891afc7ee7 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD @@ -0,0 +1,72 @@ +package( + default_testonly = 1, + default_visibility = ["//src:__subpackages__"], +) + +filegroup( + name = "srcs", + testonly = 0, + srcs = glob(["**"]), + visibility = ["//src/test/java/com/google/devtools/build/lib:__pkg__"], +) + +java_test( + name = "cpp-rules-tests", + srcs = glob(["*.java"]) + ["proto/CcProtoLibraryTest.java"], + tags = ["rules"], + test_class = "com.google.devtools.build.lib.AllTests", + deps = [ + "//src/main/java/com/google/devtools/build/lib:bazel-main", + "//src/main/java/com/google/devtools/build/lib:bazel-rules", + "//src/main/java/com/google/devtools/build/lib:build-base", + "//src/main/java/com/google/devtools/build/lib:core-rules", + "//src/main/java/com/google/devtools/build/lib:io", + "//src/main/java/com/google/devtools/build/lib:packages-internal", + "//src/main/java/com/google/devtools/build/lib:util", + "//src/main/java/com/google/devtools/build/lib/actions", + "//src/main/java/com/google/devtools/build/lib/analysis/platform", + "//src/main/java/com/google/devtools/build/lib/cmdline", + "//src/main/java/com/google/devtools/build/lib/collect/nestedset", + "//src/main/java/com/google/devtools/build/lib/concurrent", + "//src/main/java/com/google/devtools/build/lib/rules/cpp", + "//src/main/java/com/google/devtools/build/lib/vfs", + "//src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs", + "//src/main/java/com/google/devtools/common/options", + "//src/main/java/com/google/devtools/common/options:invocation_policy", + "//src/main/protobuf:crosstool_config_java_proto", + "//src/main/protobuf:extra_actions_base_java_proto", + "//src/test/java/com/google/devtools/build/lib:actions_testutil", + "//src/test/java/com/google/devtools/build/lib:analysis_testutil", + "//src/test/java/com/google/devtools/build/lib:packages_testutil", + "//src/test/java/com/google/devtools/build/lib:testutil", + "//third_party:guava", + "//third_party:guava-testlib", + "//third_party:jsr305", + "//third_party:junit4", + "//third_party:truth", + "//third_party/protobuf:protobuf_java", + ], +) + +java_library( + name = "util", + srcs = [ + "LinkBuildVariablesTestCase.java", + ], + deps = [ + "//src/main/java/com/google/devtools/build/lib:build-base", + "//src/main/java/com/google/devtools/build/lib:util", + "//src/main/java/com/google/devtools/build/lib/actions", + "//src/main/java/com/google/devtools/build/lib/rules/cpp", + "//src/main/java/com/google/devtools/build/lib/vfs", + "//src/main/protobuf:crosstool_config_java_proto", + "//src/test/java/com/google/devtools/build/lib:analysis_testutil", + "//third_party:guava", + "//third_party:guava-testlib", + "//third_party:jsr305", + "//third_party:junit4", + "//third_party:truth", + "//third_party/java/dd_plist", + "//third_party/protobuf/3.4.0:protobuf_java", + ], +) diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/LinkBuildVariablesTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/LinkBuildVariablesTest.java index b772ea74ac..fea5b3d85b 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/LinkBuildVariablesTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/LinkBuildVariablesTest.java @@ -16,13 +16,9 @@ package com.google.devtools.build.lib.rules.cpp; import static com.google.common.truth.Truth.assertThat; -import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; -import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.ConfiguredTarget; import com.google.devtools.build.lib.analysis.util.AnalysisMock; -import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; -import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.LibraryToLinkValue; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.VariableValue; @@ -35,74 +31,7 @@ import org.junit.runners.JUnit4; /** Tests that {@code CppLinkAction} is populated with the correct build variables. */ @RunWith(JUnit4.class) -public class LinkBuildVariablesTest extends BuildViewTestCase { - - private CppLinkAction getCppLinkAction(ConfiguredTarget target, Link.LinkTargetType type) { - Artifact linkerOutput = null; - switch (type) { - case STATIC_LIBRARY: - case ALWAYS_LINK_STATIC_LIBRARY: - linkerOutput = getBinArtifact("lib" + target.getLabel().getName() + ".a", target); - break; - case PIC_STATIC_LIBRARY: - case ALWAYS_LINK_PIC_STATIC_LIBRARY: - linkerOutput = getBinArtifact("lib" + target.getLabel().getName() + "pic.a", target); - break; - case DYNAMIC_LIBRARY: - linkerOutput = getBinArtifact("lib" + target.getLabel().getName() + ".so", target); - break; - case EXECUTABLE: - linkerOutput = getExecutable(target); - break; - default: - throw new IllegalArgumentException( - String.format("Cannot get CppLinkAction for link type %s", type)); - } - return (CppLinkAction) getGeneratingAction(linkerOutput); - } - - /** Returns active build variables for a link action of given type for given target. */ - protected Variables getLinkBuildVariables(ConfiguredTarget target, Link.LinkTargetType type) { - return getCppLinkAction(target, type).getLinkCommandLine().getBuildVariables(); - } - - /** Returns the value of a given sequence variable in context of the given Variables instance. */ - protected List getSequenceVariableValue(Variables variables, String variable) - throws Exception { - FeatureConfiguration mockFeatureConfiguration = - CcToolchainFeaturesTest.buildFeatures( - "feature {", - " name: 'a'", - " flag_set {", - " action: 'foo'", - " flag_group {", - " iterate_over: '" + variable + "'", - " flag: '%{" + variable + "}'", - " }", - " }", - "}") - .getFeatureConfiguration( - FeatureSpecification.create(ImmutableSet.of("a"), ImmutableSet.of())); - return mockFeatureConfiguration.getCommandLine("foo", variables); - } - - /** Returns the value of a given string variable in context of the given Variables instance. */ - protected String getVariableValue(Variables variables, String variable) throws Exception { - FeatureConfiguration mockFeatureConfiguration = - CcToolchainFeaturesTest.buildFeatures( - "feature {", - " name: 'a'", - " flag_set {", - " action: 'foo'", - " flag_group {", - " flag: '%{" + variable + "}'", - " }", - " }", - "}") - .getFeatureConfiguration( - FeatureSpecification.create(ImmutableSet.of("a"), ImmutableSet.of())); - return Iterables.getOnlyElement(mockFeatureConfiguration.getCommandLine("foo", variables)); - } +public class LinkBuildVariablesTest extends LinkBuildVariablesTestCase { @Test public void testLinkstampBuildVariable() throws Exception { diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/LinkBuildVariablesTestCase.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/LinkBuildVariablesTestCase.java new file mode 100644 index 0000000000..1cbb47921c --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/LinkBuildVariablesTestCase.java @@ -0,0 +1,110 @@ +// 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.rules.cpp; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.devtools.build.lib.actions.Artifact; +import com.google.devtools.build.lib.analysis.ConfiguredTarget; +import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; +import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration; +import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables; +import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain; +import com.google.protobuf.TextFormat; +import java.util.List; + +/** + * Common test code to test that {@code CppLinkAction} is populated with the correct build + * variables. + **/ +public class LinkBuildVariablesTestCase extends BuildViewTestCase { + + private CppLinkAction getCppLinkAction(ConfiguredTarget target, Link.LinkTargetType type) { + Artifact linkerOutput = null; + switch (type) { + case STATIC_LIBRARY: + case ALWAYS_LINK_STATIC_LIBRARY: + linkerOutput = getBinArtifact("lib" + target.getLabel().getName() + ".a", target); + break; + case PIC_STATIC_LIBRARY: + case ALWAYS_LINK_PIC_STATIC_LIBRARY: + linkerOutput = getBinArtifact("lib" + target.getLabel().getName() + "pic.a", target); + break; + case DYNAMIC_LIBRARY: + linkerOutput = getBinArtifact("lib" + target.getLabel().getName() + ".so", target); + break; + case EXECUTABLE: + linkerOutput = getExecutable(target); + break; + default: + throw new IllegalArgumentException( + String.format("Cannot get CppLinkAction for link type %s", type)); + } + return (CppLinkAction) getGeneratingAction(linkerOutput); + } + + /** Returns active build variables for a link action of given type for given target. */ + protected Variables getLinkBuildVariables(ConfiguredTarget target, Link.LinkTargetType type) { + return getCppLinkAction(target, type).getLinkCommandLine().getBuildVariables(); + } + + /** + * Creates a CcToolchainFeatures from features described in the given toolchain fragment. + */ + public static CcToolchainFeatures buildFeatures(String... toolchain) throws Exception { + CToolchain.Builder toolchainBuilder = CToolchain.newBuilder(); + TextFormat.merge(Joiner.on("").join(toolchain), toolchainBuilder); + return new CcToolchainFeatures(toolchainBuilder.buildPartial()); + } + + /** Returns the value of a given sequence variable in context of the given Variables instance. */ + protected List getSequenceVariableValue(Variables variables, String variable) + throws Exception { + FeatureConfiguration mockFeatureConfiguration = + buildFeatures( + "feature {", + " name: 'a'", + " flag_set {", + " action: 'foo'", + " flag_group {", + " iterate_over: '" + variable + "'", + " flag: '%{" + variable + "}'", + " }", + " }", + "}") + .getFeatureConfiguration( + FeatureSpecification.create(ImmutableSet.of("a"), ImmutableSet.of())); + return mockFeatureConfiguration.getCommandLine("foo", variables); + } + + /** Returns the value of a given string variable in context of the given Variables instance. */ + protected String getVariableValue(Variables variables, String variable) throws Exception { + FeatureConfiguration mockFeatureConfiguration = + buildFeatures( + "feature {", + " name: 'a'", + " flag_set {", + " action: 'foo'", + " flag_group {", + " flag: '%{" + variable + "}'", + " }", + " }", + "}") + .getFeatureConfiguration( + FeatureSpecification.create(ImmutableSet.of("a"), ImmutableSet.of())); + return Iterables.getOnlyElement(mockFeatureConfiguration.getCommandLine("foo", variables)); + } +} -- cgit v1.2.3