aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/analysis/util/ActionTester.java
blob: eca845975cdd82e798091a71ad95816ab92dbdcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2015 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.analysis.util;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import com.google.devtools.build.lib.actions.Action;
import com.google.devtools.build.lib.actions.Actions;

/**
 * Test helper for testing {@link Action} implementations.
 */
public class ActionTester {

  /**
   * A generator for action instances.
   */
  public interface ActionCombinationFactory {

    /**
     * Returns a new action instance. The parameter {@code i} is used to vary the parameters used to
     * create the action. Implementations should do something like this:
     * <code><pre>
     * return new MyAction(owner, inputs, outputs, configuration,
     *     (i & 1) == 0 ? a1 : a2,
     *     (i & 2) == 0 ? b1 : b2,
     *     (i & 4) == 0 ? c1 : c2);
     *     (i & 16) == 0 ? d1 : d2);
     * </pre></code>
     *
     * <p>The wrap-around (in this case at 32) is intentional and is checked for by the testing
     * method.
     *
     * <p>To reduce the combinatorial complexity of testing an action class, all elements that are
     * only used to change the executed command line should go into a single parameter, and the key
     * computation should take the generated command line into account.
     *
     * <p>Furthermore, when called with identical parameters, this method should return different
     * instances (i.e. according to {@code ==}), but they should have the same key.
     */
    Action generate(int i);
  }

  /**
   * Tests that different actions have different keys. The count should specify how many different
   * permutations the {@link ActionCombinationFactory} can generate.
   */
  public static void runTest(int count, ActionCombinationFactory factory) throws Exception {
    Action[] actions = new Action[count];
    for (int i = 0; i < actions.length; i++) {
      actions[i] = factory.generate(i);
    }
    // Sanity check that the count is correct.
    assertThat(Actions.canBeShared(actions[0], factory.generate(count))).isTrue();

    for (int i = 0; i < actions.length; i++) {
      assertThat(Actions.canBeShared(actions[i], factory.generate(i))).isTrue();
      for (int j = i + 1; j < actions.length; j++) {
        assertWithMessage(i + " and " + j).that(Actions.canBeShared(actions[i], actions[j]))
            .isFalse();
      }
    }
  }
}