// 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.exec.util;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.actions.ActionExecutionMetadata;
import com.google.devtools.build.lib.actions.ActionInput;
import com.google.devtools.build.lib.actions.ActionInputHelper;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.EmptyRunfilesSupplier;
import com.google.devtools.build.lib.actions.ResourceSet;
import com.google.devtools.build.lib.actions.SimpleSpawn;
import com.google.devtools.build.lib.actions.Spawn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;

/**
 * Builder class to create {@link Spawn} instances for testing.
 */
public final class SpawnBuilder {
  private String mnemonic = "Mnemonic";
  private String progressMessage = "progress message";
  @Nullable private String ownerLabel;
  private final List<String> args;
  private final Map<String, String> environment = new HashMap<>();
  private final Map<String, String> executionInfo = new HashMap<>();
  private final List<ActionInput> inputs = new ArrayList<>();
  private final List<ActionInput> outputs = new ArrayList<>();

  public SpawnBuilder(String... args) {
    this.args = ImmutableList.copyOf(args);
  }

  public Spawn build() {
    ActionExecutionMetadata owner = new FakeOwner(mnemonic, progressMessage, ownerLabel);
    return new SimpleSpawn(
        owner,
        ImmutableList.copyOf(args),
        ImmutableMap.copyOf(environment),
        ImmutableMap.copyOf(executionInfo),
        /*runfilesSupplier=*/ EmptyRunfilesSupplier.INSTANCE,
        ImmutableMap.of(),
        ImmutableList.copyOf(inputs),
        /*tools=*/ ImmutableList.<Artifact>of(),
        ImmutableList.copyOf(outputs),
        ResourceSet.ZERO);
  }

  public SpawnBuilder withMnemonic(String mnemonic) {
    this.mnemonic = Preconditions.checkNotNull(mnemonic);
    return this;
  }

  public SpawnBuilder withProgressMessage(String progressMessage) {
    this.progressMessage = progressMessage;
    return this;
  }

  public SpawnBuilder withOwnerLabel(String ownerLabel) {
    this.ownerLabel = ownerLabel;
    return this;
  }

  public SpawnBuilder withEnvironment(String key, String value) {
    this.environment.put(key, value);
    return this;
  }

  public SpawnBuilder withExecutionInfo(String key, String value) {
    this.executionInfo.put(key, value);
    return this;
  }

  public SpawnBuilder withInput(ActionInput input) {
    this.inputs.add(input);
    return this;
  }

  public SpawnBuilder withInput(String name) {
    this.inputs.add(ActionInputHelper.fromPath(name));
    return this;
  }

  public SpawnBuilder withInputs(String... names) {
    for (String name : names) {
      this.inputs.add(ActionInputHelper.fromPath(name));
    }
    return this;
  }

  public SpawnBuilder withOutput(String name) {
    this.outputs.add(ActionInputHelper.fromPath(name));
    return this;
  }

  public SpawnBuilder withOutputs(String... names) {
    for (String name : names) {
      this.outputs.add(ActionInputHelper.fromPath(name));
    }
    return this;
  }
}

@@ -1109,24 +1109,91 @@
 esSupplier;%0A
+import com.google.devtools.build.lib.actions.FilesetOutputSymlink;%0A
 import com.g
@@ -1238,16 +1238,16 @@
 rceSet;%0A
-
 import c
@@ -2017,16 +2017,124 @@
 ist%3C%3E();
+%0A  private final Map%3CArtifact, ImmutableList%3CFilesetOutputSymlink%3E%3E filesetMappings =%0A      new HashMap%3C%3E();
 %0A%0A  publ
@@ -2579,19 +2579,38 @@
 ableMap.
-of(
+copyOf(filesetMappings
 ),%0A     
@@ -4057,32 +4057,32 @@
 romPath(name));%0A
-
     %7D%0A    return
@@ -4091,11 +4091,284 @@
 his;%0A  %7D
+%0A%0A  public SpawnBuilder withFilesetMapping(%0A      Artifact fileset, ImmutableList%3CFilesetOutputSymlink%3E mappings) %7B%0A    Preconditions.checkArgument(fileset.isFileset(), %22Artifact %25s is not fileset%22, fileset);%0A    filesetMappings.put(fileset, mappings);%0A    return this;%0A  %7D
 %0A%7D%0A

Allow tests to specify fileset mappings via SpawnBuilder