Search in sources :

Example 1 with LazyString

use of com.google.devtools.build.lib.util.LazyString in project bazel by bazelbuild.

the class FileWriteActionTest method testFileWriteActionWithCompressionDoesNotForceLazyString.

@Test
public void testFileWriteActionWithCompressionDoesNotForceLazyString() throws Exception {
    Artifact outputArtifact = getBinArtifactWithNoOwner("destination.txt");
    final String backingContents = generateLongRandomString();
    class ForceCountingLazyString extends LazyString {

        public int forced = 0;

        @Override
        public String toString() {
            forced += 1;
            return backingContents;
        }
    }
    ForceCountingLazyString contents = new ForceCountingLazyString();
    FileWriteAction action = FileWriteAction.create(NULL_ACTION_OWNER, outputArtifact, contents, /*makeExecutable=*/
    false, FileWriteAction.Compression.ALLOW);
    // The string should only be forced once we actually read it, not when the action is
    // constructed.
    assertThat(contents.forced).isEqualTo(0);
    assertThat(action.getFileContents()).isEqualTo(backingContents);
    assertThat(contents.forced).isEqualTo(1);
}
Also used : LazyString(com.google.devtools.build.lib.util.LazyString) LazyString(com.google.devtools.build.lib.util.LazyString) Artifact(com.google.devtools.build.lib.actions.Artifact) Test(org.junit.Test)

Example 2 with LazyString

use of com.google.devtools.build.lib.util.LazyString in project bazel by bazelbuild.

the class ProtoCompileActionBuilderTest method outReplacementAreLazilyEvaluated.

@Test
public void outReplacementAreLazilyEvaluated() throws Exception {
    final boolean[] hasBeenCalled = new boolean[1];
    hasBeenCalled[0] = false;
    CharSequence outReplacement = new LazyString() {

        @Override
        public String toString() {
            hasBeenCalled[0] = true;
            return "mu";
        }
    };
    ProtoLangToolchainProvider toolchain = ProtoLangToolchainProvider.create("--java_out=param1,param2:$(OUT)", null, /* pluginExecutable */
    mock(TransitiveInfoCollection.class), /* runtime */
    NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER));
    SupportData supportData = SupportData.create(Predicates.<TransitiveInfoCollection>alwaysFalse(), ImmutableList.<Artifact>of(), NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER), /* protosInDirectDeps */
    NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER), true);
    CustomCommandLine cmdLine = createCommandLineFromToolchains(ImmutableList.of(new ToolchainInvocation("pluginName", toolchain, outReplacement)), supportData.getDirectProtoSources(), supportData.getTransitiveImports(), supportData.getProtosInDirectDeps(), "//foo:bar", true, /* allowServices */
    ImmutableList.<String>of());
    assertThat(hasBeenCalled[0]).isFalse();
    cmdLine.arguments();
    assertThat(hasBeenCalled[0]).isTrue();
}
Also used : ToolchainInvocation(com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder.ToolchainInvocation) CustomCommandLine(com.google.devtools.build.lib.analysis.actions.CustomCommandLine) LazyString(com.google.devtools.build.lib.util.LazyString) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Test(org.junit.Test)

Example 3 with LazyString

use of com.google.devtools.build.lib.util.LazyString in project bazel by bazelbuild.

the class FileWriteActionTest method testFileWriteActionWithLazyString.

@Test
public void testFileWriteActionWithLazyString() throws Exception {
    Artifact outputArtifact = getBinArtifactWithNoOwner("destination.txt");
    final String backingString = "Hello world";
    LazyString contents = new LazyString() {

        @Override
        public String toString() {
            return backingString;
        }
    };
    FileWriteAction action = FileWriteAction.create(NULL_ACTION_OWNER, outputArtifact, contents, /*makeExecutable=*/
    false, FileWriteAction.Compression.DISALLOW);
    assertThat(action.getFileContents()).isEqualTo(backingString);
}
Also used : LazyString(com.google.devtools.build.lib.util.LazyString) LazyString(com.google.devtools.build.lib.util.LazyString) Artifact(com.google.devtools.build.lib.actions.Artifact) Test(org.junit.Test)

Aggregations

LazyString (com.google.devtools.build.lib.util.LazyString)3 Test (org.junit.Test)3 Artifact (com.google.devtools.build.lib.actions.Artifact)2 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 CustomCommandLine (com.google.devtools.build.lib.analysis.actions.CustomCommandLine)1 ToolchainInvocation (com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder.ToolchainInvocation)1