diff options
author | jcater <jcater@google.com> | 2018-01-19 10:56:53 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-01-19 10:58:26 -0800 |
commit | 1708aad76b5223c233a5f96794345fe35c071da5 (patch) | |
tree | f9d8424ccdaa7699ff0a0301cf553c264352d4c8 /src/test/java/com/google/devtools/build | |
parent | 99f62ef22fd32277fde5fa1236f716a1e20b70c3 (diff) |
Simplify the toolchain test case setup.
Also add a new appendFile method on Scratch.
PiperOrigin-RevId: 182558199
Diffstat (limited to 'src/test/java/com/google/devtools/build')
6 files changed, 98 insertions, 39 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/rules/platform/ToolchainTestCase.java b/src/test/java/com/google/devtools/build/lib/rules/platform/ToolchainTestCase.java index 2f50ba7894..95431f0b61 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/platform/ToolchainTestCase.java +++ b/src/test/java/com/google/devtools/build/lib/rules/platform/ToolchainTestCase.java @@ -15,6 +15,7 @@ package com.google.devtools.build.lib.rules.platform; import static com.google.common.truth.Truth.assertThat; +import static java.util.stream.Collectors.joining; import com.google.common.collect.ImmutableList; import com.google.common.truth.IterableSubject; @@ -28,6 +29,7 @@ import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils; import com.google.devtools.build.lib.skylark.util.SkylarkTestCase; import com.google.devtools.build.skyframe.EvaluationResult; import com.google.devtools.build.skyframe.SkyKey; +import java.util.Collection; import java.util.List; import java.util.stream.Collectors; import org.junit.Before; @@ -60,6 +62,10 @@ public abstract class ToolchainTestCase extends SkylarkTestCase { .collect(Collectors.toList()); } + private static String formatConstraints(Collection<String> constraints) { + return constraints.stream().map(c -> String.format("'%s'", c)).collect(joining(", ")); + } + @Before public void createConstraints() throws Exception { scratch.file( @@ -93,33 +99,32 @@ public abstract class ToolchainTestCase extends SkylarkTestCase { .build(); } + public void addToolchain( + String packageName, + String toolchainName, + Collection<String> execConstraints, + Collection<String> targetConstraints, + String data) + throws Exception { + scratch.appendFile( + packageName + "/BUILD", + "load('//toolchain:toolchain_def.bzl', 'test_toolchain')", + "toolchain(", + " name = '" + toolchainName + "',", + " toolchain_type = '//toolchain:test_toolchain',", + " exec_compatible_with = [" + formatConstraints(execConstraints) + "],", + " target_compatible_with = [" + formatConstraints(targetConstraints) + "],", + " toolchain = ':" + toolchainName + "_impl')", + "test_toolchain(", + " name='" + toolchainName + "_impl',", + " data = '" + data + "')"); + } + @Before public void createToolchains() throws Exception { rewriteWorkspace("register_toolchains('//toolchain:toolchain_1', '//toolchain:toolchain_2')"); scratch.file( - "toolchain/BUILD", - "load(':toolchain_def.bzl', 'test_toolchain')", - "toolchain_type(name = 'test_toolchain')", - "toolchain(", - " name = 'toolchain_1',", - " toolchain_type = ':test_toolchain',", - " exec_compatible_with = ['//constraints:linux'],", - " target_compatible_with = ['//constraints:mac'],", - " toolchain = ':test_toolchain_1')", - "toolchain(", - " name = 'toolchain_2',", - " toolchain_type = ':test_toolchain',", - " exec_compatible_with = ['//constraints:mac'],", - " target_compatible_with = ['//constraints:linux'],", - " toolchain = ':test_toolchain_2')", - "test_toolchain(", - " name='test_toolchain_1',", - " data = 'foo')", - "test_toolchain(", - " name='test_toolchain_2',", - " data = 'bar')"); - scratch.file( "toolchain/toolchain_def.bzl", "def _impl(ctx):", " toolchain = platform_common.ToolchainInfo(", @@ -130,6 +135,20 @@ public abstract class ToolchainTestCase extends SkylarkTestCase { " attrs = {", " 'data': attr.string()})"); + scratch.file("toolchain/BUILD", "toolchain_type(name = 'test_toolchain')"); + addToolchain( + "toolchain", + "toolchain_1", + ImmutableList.of("//constraints:linux"), + ImmutableList.of("//constraints:mac"), + "foo"); + addToolchain( + "toolchain", + "toolchain_2", + ImmutableList.of("//constraints:mac"), + ImmutableList.of("//constraints:linux"), + "bar"); + testToolchainType = makeLabel("//toolchain:test_toolchain"); } diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/RegisteredToolchainsFunctionTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/RegisteredToolchainsFunctionTest.java index 8cd0e30147..01d216eddb 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/RegisteredToolchainsFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/RegisteredToolchainsFunctionTest.java @@ -44,17 +44,33 @@ public class RegisteredToolchainsFunctionTest extends ToolchainTestCase { // We have two registered toolchains, and two default for c++ assertThat(value.registeredToolchains()).hasSize(4); - assertThat(value.registeredToolchains().stream().anyMatch(toolchain -> - (toolchain.toolchainType().equals(testToolchainType)) - && toolchain.execConstraints().contains(linuxConstraint) - && toolchain.targetConstraints().contains(macConstraint) - && toolchain.toolchainLabel().equals(makeLabel("//toolchain:test_toolchain_1")))).isTrue(); - - assertThat(value.registeredToolchains().stream().anyMatch(toolchain -> - (toolchain.toolchainType().equals(testToolchainType)) - && toolchain.execConstraints().contains(macConstraint) - && toolchain.targetConstraints().contains(linuxConstraint) - && toolchain.toolchainLabel().equals(makeLabel("//toolchain:test_toolchain_2")))).isTrue(); + assertThat( + value + .registeredToolchains() + .stream() + .anyMatch( + toolchain -> + (toolchain.toolchainType().equals(testToolchainType)) + && toolchain.execConstraints().contains(linuxConstraint) + && toolchain.targetConstraints().contains(macConstraint) + && toolchain + .toolchainLabel() + .equals(makeLabel("//toolchain:toolchain_1_impl")))) + .isTrue(); + + assertThat( + value + .registeredToolchains() + .stream() + .anyMatch( + toolchain -> + (toolchain.toolchainType().equals(testToolchainType)) + && toolchain.execConstraints().contains(macConstraint) + && toolchain.targetConstraints().contains(linuxConstraint) + && toolchain + .toolchainLabel() + .equals(makeLabel("//toolchain:toolchain_2_impl")))) + .isTrue(); } @Test @@ -85,7 +101,7 @@ public class RegisteredToolchainsFunctionTest extends ToolchainTestCase { // Verify that the target registered with the extra_toolchains flag is first in the list. assertToolchainLabels(result.get(toolchainsKey)) .containsAllOf( - makeLabel("//extra:extra_toolchain_impl"), makeLabel("//toolchain:test_toolchain_1")) + makeLabel("//extra:extra_toolchain_impl"), makeLabel("//toolchain:toolchain_1_impl")) .inOrder(); } @@ -116,7 +132,7 @@ public class RegisteredToolchainsFunctionTest extends ToolchainTestCase { requestToolchainsFromSkyframe(toolchainsKey); assertThatEvaluationResult(result).hasNoError(); assertToolchainLabels(result.get(toolchainsKey)) - .contains(makeLabel("//toolchain:test_toolchain_1")); + .contains(makeLabel("//toolchain:toolchain_1_impl")); // Re-write the WORKSPACE. rewriteWorkspace("register_toolchains('//toolchain:toolchain_2')"); @@ -125,7 +141,7 @@ public class RegisteredToolchainsFunctionTest extends ToolchainTestCase { result = requestToolchainsFromSkyframe(toolchainsKey); assertThatEvaluationResult(result).hasNoError(); assertToolchainLabels(result.get(toolchainsKey)) - .contains(makeLabel("//toolchain:test_toolchain_2")); + .contains(makeLabel("//toolchain:toolchain_2_impl")); } @Test diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/RuleContextTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/RuleContextTest.java index 62ce808c39..8f33c0b0e6 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/RuleContextTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/RuleContextTest.java @@ -37,7 +37,7 @@ public class RuleContextTest extends ToolchainTestCase { "--platforms=//platforms:mac"); RuleContext ruleContext = getRuleContext(getConfiguredTarget("//x")); assertThat(ruleContext.getToolchainContext().getResolvedToolchainLabels()) - .contains(Label.parseAbsolute("//toolchain:test_toolchain_1")); + .contains(Label.parseAbsolute("//toolchain:toolchain_1_impl")); ResolvedToolchainProviders resolvedToolchainProviders = (ResolvedToolchainProviders) diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/ToolchainResolutionFunctionTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/ToolchainResolutionFunctionTest.java index d1b17bb5bd..128c59adc3 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/ToolchainResolutionFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/ToolchainResolutionFunctionTest.java @@ -60,7 +60,7 @@ public class ToolchainResolutionFunctionTest extends ToolchainTestCase { ToolchainResolutionValue toolchainResolutionValue = result.get(key); assertThat(toolchainResolutionValue.toolchainLabel()) - .isEqualTo(makeLabel("//toolchain:test_toolchain_2")); + .isEqualTo(makeLabel("//toolchain:toolchain_2_impl")); } @Test diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/ToolchainUtilTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/ToolchainUtilTest.java index cae6decfd8..44779427d3 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/ToolchainUtilTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/ToolchainUtilTest.java @@ -86,7 +86,7 @@ public class ToolchainUtilTest extends ToolchainTestCase { assertThat(toolchainContext.getRequiredToolchains()).containsExactly(testToolchainType); assertThat(toolchainContext.getResolvedToolchainLabels()) - .containsExactly(Label.parseAbsoluteUnchecked("//toolchain:test_toolchain_1")); + .containsExactly(Label.parseAbsoluteUnchecked("//toolchain:toolchain_1_impl")); assertThat(toolchainContext.getExecutionPlatform()).isNotNull(); assertThat(toolchainContext.getExecutionPlatform().label()) diff --git a/src/test/java/com/google/devtools/build/lib/testutil/Scratch.java b/src/test/java/com/google/devtools/build/lib/testutil/Scratch.java index a43af98166..689e50f24d 100644 --- a/src/test/java/com/google/devtools/build/lib/testutil/Scratch.java +++ b/src/test/java/com/google/devtools/build/lib/testutil/Scratch.java @@ -138,6 +138,30 @@ public final class Scratch { DEFAULT_CHARSET); } + /** Like {@code scratch.file}, but the lines are added to the end if the file already exists. */ + public Path appendFile(String pathName, Collection<String> lines) throws IOException { + return appendFile(pathName, lines.toArray(new String[lines.size()])); + } + + /** Like {@code scratch.file}, but the lines are added to the end if the file already exists. */ + public Path appendFile(String pathName, String... lines) throws IOException { + return appendFile(pathName, DEFAULT_CHARSET, lines); + } + + /** Like {@code scratch.file}, but the lines are added to the end if the file already exists. */ + public Path appendFile(String pathName, Charset charset, String... lines) throws IOException { + Path path = resolve(pathName); + + StringBuilder content = new StringBuilder(); + if (path.exists()) { + content.append(readFile(pathName)); + content.append("\n"); + } + content.append(linesAsString(lines)); + + return overwriteFile(pathName, content.toString()); + } + /** * Like {@code scratch.file}, but the file is first deleted if it already * exists. |