Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.actions; |
| 15 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
jcater | ecd2abd | 2019-04-30 13:31:13 -0700 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
cpeyser | a666ffb | 2018-04-24 07:24:27 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.common.collect.Iterables; |
| 22 | import com.google.common.collect.Lists; |
janakr | bf4123d | 2018-07-24 11:38:19 -0700 | [diff] [blame] | 23 | import com.google.common.testing.EqualsTester; |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.ActionAnalysisMetadata.MiddlemanType; |
cpeyser | a666ffb | 2018-04-24 07:24:27 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.actions.Artifact.SourceArtifact; |
| 26 | import com.google.devtools.build.lib.actions.ArtifactResolver.ArtifactResolverSupplier; |
ulfjack | 60dac4d | 2019-12-06 07:13:00 -0800 | [diff] [blame^] | 27 | import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
| 29 | import com.google.devtools.build.lib.actions.util.LabelArtifactOwner; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.cmdline.Label; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | import com.google.devtools.build.lib.rules.cpp.CppFileTypes; |
| 32 | import com.google.devtools.build.lib.rules.java.JavaSemantics; |
cpeyser | a666ffb | 2018-04-24 07:24:27 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.skyframe.serialization.AutoRegistry; |
| 34 | import com.google.devtools.build.lib.skyframe.serialization.ObjectCodecs; |
shahan | 20f35b4 | 2018-02-28 15:57:33 -0800 | [diff] [blame] | 35 | import com.google.devtools.build.lib.skyframe.serialization.testutils.SerializationTester; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | import com.google.devtools.build.lib.testutil.Scratch; |
shahan | fae34b9 | 2018-02-13 10:08:47 -0800 | [diff] [blame] | 37 | import com.google.devtools.build.lib.vfs.FileSystem; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | import com.google.devtools.build.lib.vfs.Path; |
| 39 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 40 | import com.google.devtools.build.lib.vfs.Root; |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 41 | import com.google.devtools.build.skyframe.SkyFunctionName; |
Paul Roberts | 8c443ef | 2016-10-18 02:04:25 +0000 | [diff] [blame] | 42 | import java.io.IOException; |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
| 48 | import org.junit.runners.JUnit4; |
| 49 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 50 | @RunWith(JUnit4.class) |
| 51 | public class ArtifactTest { |
| 52 | private Scratch scratch; |
| 53 | private Path execDir; |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 54 | private ArtifactRoot rootDir; |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 55 | private final ActionKeyContext actionKeyContext = new ActionKeyContext(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | |
| 57 | @Before |
Florian Weikert | 0220dc7 | 2015-12-01 14:38:00 +0000 | [diff] [blame] | 58 | public final void setRootDir() throws Exception { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | scratch = new Scratch(); |
| 60 | execDir = scratch.dir("/exec"); |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 61 | rootDir = ArtifactRoot.asDerivedRoot(execDir, scratch.dir("/exec/root")); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Test |
Laurent Le Brun | f3cf98f | 2016-06-17 13:36:24 +0000 | [diff] [blame] | 65 | public void testConstruction_badRootDir() throws IOException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 66 | Path f1 = scratch.file("/exec/dir/file.ext"); |
| 67 | Path bogusDir = scratch.file("/exec/dir/bogus"); |
jcater | ecd2abd | 2019-04-30 13:31:13 -0700 | [diff] [blame] | 68 | assertThrows( |
| 69 | IllegalArgumentException.class, |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 70 | () -> |
| 71 | ActionsTestUtil.createArtifactWithExecPath( |
Googler | 19a3f55 | 2019-09-03 08:22:16 -0700 | [diff] [blame] | 72 | ArtifactRoot.asDerivedRoot(execDir, bogusDir), f1.relativeTo(execDir)) |
| 73 | .getRootRelativePath()); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | private static long getUsedMemory() { |
| 77 | System.gc(); |
| 78 | System.gc(); |
| 79 | System.runFinalization(); |
| 80 | System.gc(); |
| 81 | System.gc(); |
| 82 | return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testMemoryUsage() throws IOException { |
| 87 | ArtifactRoot root = ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/foo"))); |
| 88 | PathFragment aPath = PathFragment.create("src/a"); |
| 89 | int arrSize = 1 << 20; |
| 90 | Object[] arr = new Object[arrSize]; |
| 91 | long usedMemory = getUsedMemory(); |
| 92 | for (int i = 0; i < arrSize; i++) { |
| 93 | arr[i] = ActionsTestUtil.createArtifactWithExecPath(root, aPath); |
| 94 | } |
| 95 | assertThat((getUsedMemory() - usedMemory) / arrSize).isAtMost(34L); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testEquivalenceRelation() throws Exception { |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 100 | PathFragment aPath = PathFragment.create("src/a"); |
| 101 | PathFragment bPath = PathFragment.create("src/b"); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 102 | assertThat(ActionsTestUtil.createArtifactWithRootRelativePath(rootDir, aPath)) |
| 103 | .isEqualTo(ActionsTestUtil.createArtifactWithRootRelativePath(rootDir, aPath)); |
| 104 | assertThat(ActionsTestUtil.createArtifactWithRootRelativePath(rootDir, bPath)) |
| 105 | .isEqualTo(ActionsTestUtil.createArtifactWithRootRelativePath(rootDir, bPath)); |
| 106 | assertThat( |
| 107 | ActionsTestUtil.createArtifactWithRootRelativePath(rootDir, aPath) |
| 108 | .equals(ActionsTestUtil.createArtifactWithRootRelativePath(rootDir, bPath))) |
| 109 | .isFalse(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Test |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 113 | public void testComparison() { |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 114 | PathFragment aPath = PathFragment.create("src/a"); |
| 115 | PathFragment bPath = PathFragment.create("src/b"); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 116 | Artifact aArtifact = ActionsTestUtil.createArtifactWithRootRelativePath(rootDir, aPath); |
| 117 | Artifact bArtifact = ActionsTestUtil.createArtifactWithRootRelativePath(rootDir, bPath); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 118 | assertThat(Artifact.EXEC_PATH_COMPARATOR.compare(aArtifact, bArtifact)).isEqualTo(-1); |
| 119 | assertThat(Artifact.EXEC_PATH_COMPARATOR.compare(aArtifact, aArtifact)).isEqualTo(0); |
| 120 | assertThat(Artifact.EXEC_PATH_COMPARATOR.compare(bArtifact, bArtifact)).isEqualTo(0); |
| 121 | assertThat(Artifact.EXEC_PATH_COMPARATOR.compare(bArtifact, aArtifact)).isEqualTo(1); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Test |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 125 | public void testGetFilename() throws Exception { |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 126 | ArtifactRoot root = ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/foo"))); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 127 | Artifact javaFile = ActionsTestUtil.createArtifact(root, scratch.file("/foo/Bar.java")); |
| 128 | Artifact generatedHeader = |
| 129 | ActionsTestUtil.createArtifact(root, scratch.file("/foo/bar.proto.h")); |
| 130 | Artifact generatedCc = ActionsTestUtil.createArtifact(root, scratch.file("/foo/bar.proto.cc")); |
| 131 | Artifact aCPlusPlusFile = ActionsTestUtil.createArtifact(root, scratch.file("/foo/bar.cc")); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 132 | assertThat(JavaSemantics.JAVA_SOURCE.matches(javaFile.getFilename())).isTrue(); |
| 133 | assertThat(CppFileTypes.CPP_HEADER.matches(generatedHeader.getFilename())).isTrue(); |
| 134 | assertThat(CppFileTypes.CPP_SOURCE.matches(generatedCc.getFilename())).isTrue(); |
| 135 | assertThat(CppFileTypes.CPP_SOURCE.matches(aCPlusPlusFile.getFilename())).isTrue(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Test |
Paul Roberts | 8c443ef | 2016-10-18 02:04:25 +0000 | [diff] [blame] | 139 | public void testGetExtension() throws Exception { |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 140 | ArtifactRoot root = ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/foo"))); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 141 | Artifact javaFile = ActionsTestUtil.createArtifact(root, scratch.file("/foo/Bar.java")); |
Paul Roberts | 8c443ef | 2016-10-18 02:04:25 +0000 | [diff] [blame] | 142 | assertThat(javaFile.getExtension()).isEqualTo("java"); |
| 143 | } |
| 144 | |
| 145 | @Test |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 146 | public void testMangledPath() { |
| 147 | String path = "dir/sub_dir/name:end"; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 148 | assertThat(Actions.escapedPath(path)).isEqualTo("dir_Ssub_Udir_Sname_Cend"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | private List<Artifact> getFooBarArtifacts(MutableActionGraph actionGraph, boolean collapsedList) |
| 152 | throws Exception { |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 153 | ArtifactRoot root = ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/foo"))); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 154 | Artifact aHeader1 = ActionsTestUtil.createArtifact(root, scratch.file("/foo/bar1.h")); |
| 155 | Artifact aHeader2 = ActionsTestUtil.createArtifact(root, scratch.file("/foo/bar2.h")); |
| 156 | Artifact aHeader3 = ActionsTestUtil.createArtifact(root, scratch.file("/foo/bar3.h")); |
| 157 | ArtifactRoot middleRoot = |
| 158 | ArtifactRoot.middlemanRoot(scratch.dir("/foo"), scratch.dir("/foo/out")); |
| 159 | Artifact middleman = ActionsTestUtil.createArtifact(middleRoot, "middleman"); |
ulfjack | 60dac4d | 2019-12-06 07:13:00 -0800 | [diff] [blame^] | 160 | MiddlemanAction.create( |
| 161 | new ActionRegistry() { |
| 162 | @Override |
| 163 | public void registerAction(ActionAnalysisMetadata... actions) { |
| 164 | for (ActionAnalysisMetadata action : actions) { |
| 165 | try { |
| 166 | actionGraph.registerAction(action); |
| 167 | } catch (ActionConflictException e) { |
| 168 | throw new IllegalStateException(e); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public ActionLookupValue.ActionLookupKey getOwner() { |
| 175 | throw new UnsupportedOperationException(); |
| 176 | } |
| 177 | }, |
| 178 | ActionsTestUtil.NULL_ACTION_OWNER, |
| 179 | ImmutableList.of(aHeader1, aHeader2, aHeader3), |
| 180 | middleman, |
| 181 | "desc", |
| 182 | MiddlemanType.AGGREGATING_MIDDLEMAN); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 183 | return collapsedList ? Lists.newArrayList(aHeader1, middleman) : |
| 184 | Lists.newArrayList(aHeader1, aHeader2, middleman); |
| 185 | } |
| 186 | |
| 187 | @Test |
| 188 | public void testAddExecPaths() throws Exception { |
| 189 | List<String> paths = new ArrayList<>(); |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 190 | MutableActionGraph actionGraph = new MapBasedActionGraph(actionKeyContext); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 191 | Artifact.addExecPaths(getFooBarArtifacts(actionGraph, false), paths); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 192 | assertThat(paths).containsExactly("bar1.h", "bar2.h"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Test |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 196 | public void testAddExpandedArtifacts() throws Exception { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 197 | List<Artifact> expanded = new ArrayList<>(); |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 198 | MutableActionGraph actionGraph = new MapBasedActionGraph(actionKeyContext); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 199 | List<Artifact> original = getFooBarArtifacts(actionGraph, true); |
| 200 | Artifact.addExpandedArtifacts(original, expanded, |
Michael Thvedt | 434e68e | 2016-02-09 00:57:46 +0000 | [diff] [blame] | 201 | ActionInputHelper.actionGraphArtifactExpander(actionGraph)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 202 | |
| 203 | List<Artifact> manuallyExpanded = new ArrayList<>(); |
| 204 | for (Artifact artifact : original) { |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 205 | ActionAnalysisMetadata action = actionGraph.getGeneratingAction(artifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 206 | if (artifact.isMiddlemanArtifact()) { |
| 207 | Iterables.addAll(manuallyExpanded, action.getInputs()); |
| 208 | } else { |
| 209 | manuallyExpanded.add(artifact); |
| 210 | } |
| 211 | } |
Carmi Grushko | fd8acab | 2015-11-10 17:19:13 +0000 | [diff] [blame] | 212 | assertThat(expanded).containsExactlyElementsIn(manuallyExpanded); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Test |
| 216 | public void testAddExecPathsNewActionGraph() throws Exception { |
| 217 | List<String> paths = new ArrayList<>(); |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 218 | MutableActionGraph actionGraph = new MapBasedActionGraph(actionKeyContext); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 219 | Artifact.addExecPaths(getFooBarArtifacts(actionGraph, false), paths); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 220 | assertThat(paths).containsExactly("bar1.h", "bar2.h"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Test |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 224 | public void testRootRelativePathIsSameAsExecPath() throws Exception { |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 225 | ArtifactRoot root = ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/foo"))); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 226 | Artifact a = ActionsTestUtil.createArtifact(root, scratch.file("/foo/bar1.h")); |
cpovirk | a4d3da6 | 2019-05-02 14:27:33 -0700 | [diff] [blame] | 227 | assertThat(a.getRootRelativePath()).isSameInstanceAs(a.getExecPath()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Test |
| 231 | public void testToDetailString() throws Exception { |
shahan | e483df3 | 2018-04-05 08:03:04 -0700 | [diff] [blame] | 232 | Path execRoot = scratch.getFileSystem().getPath("/execroot/workspace"); |
tomlu | 89eaee5 | 2018-01-15 17:40:35 -0800 | [diff] [blame] | 233 | Artifact a = |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 234 | ActionsTestUtil.createArtifact( |
| 235 | ArtifactRoot.asDerivedRoot(execRoot, scratch.dir("/execroot/workspace/b")), "c"); |
shahan | e483df3 | 2018-04-05 08:03:04 -0700 | [diff] [blame] | 236 | assertThat(a.toDetailString()).isEqualTo("[[<execution_root>]b]c"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @Test |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 240 | public void testWeirdArtifact() { |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 241 | Path execRoot = scratch.getFileSystem().getPath("/"); |
jcater | b0c7ee5 | 2019-05-02 12:33:13 -0700 | [diff] [blame] | 242 | assertThrows( |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 243 | IllegalArgumentException.class, |
| 244 | () -> |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 245 | ActionsTestUtil.createArtifactWithExecPath( |
Googler | 19a3f55 | 2019-09-03 08:22:16 -0700 | [diff] [blame] | 246 | ArtifactRoot.asDerivedRoot(execRoot, scratch.dir("/a")), |
| 247 | PathFragment.create("c")) |
| 248 | .getRootRelativePath()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | @Test |
cpeyser | 875068a | 2018-02-01 08:40:58 -0800 | [diff] [blame] | 252 | public void testCodec() throws Exception { |
janakr | 658d47f | 2019-05-29 11:11:30 -0700 | [diff] [blame] | 253 | Artifact.DerivedArtifact artifact = |
| 254 | (Artifact.DerivedArtifact) ActionsTestUtil.createArtifact(rootDir, "src/a"); |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 255 | artifact.setGeneratingActionKey(ActionsTestUtil.NULL_ACTION_LOOKUP_DATA); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 256 | ArtifactRoot anotherRoot = |
| 257 | ArtifactRoot.asDerivedRoot(scratch.getFileSystem().getPath("/"), scratch.dir("/src")); |
janakr | 658d47f | 2019-05-29 11:11:30 -0700 | [diff] [blame] | 258 | Artifact.DerivedArtifact anotherArtifact = |
janakr | 3290e22 | 2019-05-29 16:34:22 -0700 | [diff] [blame] | 259 | new Artifact.DerivedArtifact( |
| 260 | anotherRoot, |
| 261 | anotherRoot.getExecPath().getRelative("src/c"), |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 262 | ActionsTestUtil.NULL_ARTIFACT_OWNER); |
| 263 | anotherArtifact.setGeneratingActionKey(ActionsTestUtil.NULL_ACTION_LOOKUP_DATA); |
janakr | 658d47f | 2019-05-29 11:11:30 -0700 | [diff] [blame] | 264 | new SerializationTester(artifact, anotherArtifact) |
shahan | fae34b9 | 2018-02-13 10:08:47 -0800 | [diff] [blame] | 265 | .addDependency(FileSystem.class, scratch.getFileSystem()) |
shahan | 20f35b4 | 2018-02-28 15:57:33 -0800 | [diff] [blame] | 266 | .runTests(); |
cpeyser | 875068a | 2018-02-01 08:40:58 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | @Test |
cpeyser | a666ffb | 2018-04-24 07:24:27 -0700 | [diff] [blame] | 270 | public void testCodecRecyclesSourceArtifactInstances() throws Exception { |
| 271 | Root root = Root.fromPath(scratch.dir("/")); |
| 272 | ArtifactRoot artifactRoot = ArtifactRoot.asSourceRoot(root); |
buchgr | ffad587 | 2019-06-18 08:06:32 -0700 | [diff] [blame] | 273 | ArtifactFactory artifactFactory = |
| 274 | new ArtifactFactory(execDir.getParentDirectory(), "blaze-out"); |
cpeyser | a666ffb | 2018-04-24 07:24:27 -0700 | [diff] [blame] | 275 | artifactFactory.setSourceArtifactRoots(ImmutableMap.of(root, artifactRoot)); |
| 276 | ArtifactResolverSupplier artifactResolverSupplierForTest = () -> artifactFactory; |
| 277 | |
cpeyser | a666ffb | 2018-04-24 07:24:27 -0700 | [diff] [blame] | 278 | ObjectCodecs objectCodecs = |
| 279 | new ObjectCodecs( |
| 280 | AutoRegistry.get() |
| 281 | .getBuilder() |
| 282 | .addReferenceConstant(scratch.getFileSystem()) |
| 283 | .setAllowDefaultCodec(true) |
| 284 | .build(), |
| 285 | ImmutableMap.of( |
| 286 | FileSystem.class, scratch.getFileSystem(), |
cpeyser | a666ffb | 2018-04-24 07:24:27 -0700 | [diff] [blame] | 287 | ArtifactResolverSupplier.class, artifactResolverSupplierForTest)); |
| 288 | |
| 289 | PathFragment pathFragment = PathFragment.create("src/foo.cc"); |
| 290 | ArtifactOwner owner = new LabelArtifactOwner(Label.parseAbsoluteUnchecked("//foo:bar")); |
| 291 | SourceArtifact sourceArtifact = new SourceArtifact(artifactRoot, pathFragment, owner); |
| 292 | SourceArtifact deserialized1 = |
| 293 | (SourceArtifact) objectCodecs.deserialize(objectCodecs.serialize(sourceArtifact)); |
| 294 | SourceArtifact deserialized2 = |
| 295 | (SourceArtifact) objectCodecs.deserialize(objectCodecs.serialize(sourceArtifact)); |
cpovirk | a4d3da6 | 2019-05-02 14:27:33 -0700 | [diff] [blame] | 296 | assertThat(deserialized1).isSameInstanceAs(deserialized2); |
cpeyser | a666ffb | 2018-04-24 07:24:27 -0700 | [diff] [blame] | 297 | |
| 298 | Artifact sourceArtifactFromFactory = |
| 299 | artifactFactory.getSourceArtifact(pathFragment, root, owner); |
| 300 | Artifact deserialized = |
| 301 | (Artifact) objectCodecs.deserialize(objectCodecs.serialize(sourceArtifactFromFactory)); |
cpovirk | a4d3da6 | 2019-05-02 14:27:33 -0700 | [diff] [blame] | 302 | assertThat(sourceArtifactFromFactory).isSameInstanceAs(deserialized); |
cpeyser | a666ffb | 2018-04-24 07:24:27 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | @Test |
Googler | 91cfbff | 2015-05-26 16:35:27 +0000 | [diff] [blame] | 306 | public void testLongDirname() throws Exception { |
| 307 | String dirName = createDirNameArtifact().getDirname(); |
Carmi Grushko | fd8acab | 2015-11-10 17:19:13 +0000 | [diff] [blame] | 308 | |
| 309 | assertThat(dirName).isEqualTo("aaa/bbb/ccc"); |
Googler | 91cfbff | 2015-05-26 16:35:27 +0000 | [diff] [blame] | 310 | } |
Carmi Grushko | fd8acab | 2015-11-10 17:19:13 +0000 | [diff] [blame] | 311 | |
Googler | 91cfbff | 2015-05-26 16:35:27 +0000 | [diff] [blame] | 312 | @Test |
| 313 | public void testDirnameInExecutionDir() throws Exception { |
tomlu | 89eaee5 | 2018-01-15 17:40:35 -0800 | [diff] [blame] | 314 | Artifact artifact = |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 315 | ActionsTestUtil.createArtifact( |
| 316 | ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/foo"))), |
| 317 | scratch.file("/foo/bar.txt")); |
Carmi Grushko | fd8acab | 2015-11-10 17:19:13 +0000 | [diff] [blame] | 318 | |
| 319 | assertThat(artifact.getDirname()).isEqualTo("."); |
Googler | 91cfbff | 2015-05-26 16:35:27 +0000 | [diff] [blame] | 320 | } |
Carmi Grushko | fd8acab | 2015-11-10 17:19:13 +0000 | [diff] [blame] | 321 | |
Googler | 91cfbff | 2015-05-26 16:35:27 +0000 | [diff] [blame] | 322 | @Test |
| 323 | public void testCanConstructPathFromDirAndFilename() throws Exception { |
| 324 | Artifact artifact = createDirNameArtifact(); |
| 325 | String constructed = |
| 326 | String.format("%s/%s", artifact.getDirname(), artifact.getFilename()); |
| 327 | |
| 328 | assertThat(constructed).isEqualTo("aaa/bbb/ccc/ddd"); |
| 329 | } |
Carmi Grushko | fd8acab | 2015-11-10 17:19:13 +0000 | [diff] [blame] | 330 | |
Dmitry Lomov | ddda06d | 2016-01-19 15:58:11 +0000 | [diff] [blame] | 331 | @Test |
| 332 | public void testIsSourceArtifact() throws Exception { |
| 333 | assertThat( |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 334 | new Artifact.SourceArtifact( |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 335 | ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/"))), |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 336 | PathFragment.create("src/foo.cc"), |
| 337 | ArtifactOwner.NullArtifactOwner.INSTANCE) |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 338 | .isSourceArtifact()) |
Dmitry Lomov | ddda06d | 2016-01-19 15:58:11 +0000 | [diff] [blame] | 339 | .isTrue(); |
| 340 | assertThat( |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 341 | ActionsTestUtil.createArtifact( |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 342 | ArtifactRoot.asDerivedRoot( |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 343 | scratch.dir("/genfiles"), scratch.dir("/genfiles/aaa")), |
| 344 | scratch.file("/genfiles/aaa/bar.out")) |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 345 | .isSourceArtifact()) |
Dmitry Lomov | ddda06d | 2016-01-19 15:58:11 +0000 | [diff] [blame] | 346 | .isFalse(); |
Dmitry Lomov | ddda06d | 2016-01-19 15:58:11 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | @Test |
| 350 | public void testGetRoot() throws Exception { |
tomlu | 89eaee5 | 2018-01-15 17:40:35 -0800 | [diff] [blame] | 351 | Path execRoot = scratch.getFileSystem().getPath("/"); |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 352 | ArtifactRoot root = ArtifactRoot.asDerivedRoot(execRoot, scratch.dir("/newRoot")); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 353 | assertThat(ActionsTestUtil.createArtifact(root, scratch.file("/newRoot/foo")).getRoot()) |
| 354 | .isEqualTo(root); |
Dmitry Lomov | ddda06d | 2016-01-19 15:58:11 +0000 | [diff] [blame] | 355 | } |
| 356 | |
janakr | bf4123d | 2018-07-24 11:38:19 -0700 | [diff] [blame] | 357 | @Test |
| 358 | public void hashCodeAndEquals() throws IOException { |
| 359 | Path execRoot = scratch.getFileSystem().getPath("/"); |
| 360 | ArtifactRoot root = ArtifactRoot.asDerivedRoot(execRoot, scratch.dir("/newRoot")); |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 361 | ActionLookupValue.ActionLookupKey firstOwner = |
| 362 | new ActionLookupValue.ActionLookupKey() { |
| 363 | @Override |
| 364 | public SkyFunctionName functionName() { |
| 365 | return null; |
| 366 | } |
| 367 | }; |
| 368 | ActionLookupValue.ActionLookupKey secondOwner = |
| 369 | new ActionLookupValue.ActionLookupKey() { |
| 370 | @Override |
| 371 | public SkyFunctionName functionName() { |
| 372 | return null; |
| 373 | } |
| 374 | }; |
| 375 | Artifact.DerivedArtifact derived1 = |
janakr | 3290e22 | 2019-05-29 16:34:22 -0700 | [diff] [blame] | 376 | new Artifact.DerivedArtifact(root, PathFragment.create("newRoot/shared"), firstOwner); |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 377 | derived1.setGeneratingActionKey(ActionLookupData.create(firstOwner, 0)); |
| 378 | Artifact.DerivedArtifact derived2 = |
janakr | 3290e22 | 2019-05-29 16:34:22 -0700 | [diff] [blame] | 379 | new Artifact.DerivedArtifact(root, PathFragment.create("newRoot/shared"), secondOwner); |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 380 | derived2.setGeneratingActionKey(ActionLookupData.create(secondOwner, 0)); |
janakr | bf4123d | 2018-07-24 11:38:19 -0700 | [diff] [blame] | 381 | ArtifactRoot sourceRoot = ArtifactRoot.asSourceRoot(Root.fromPath(root.getRoot().asPath())); |
| 382 | Artifact source1 = new SourceArtifact(sourceRoot, PathFragment.create("shared"), firstOwner); |
| 383 | Artifact source2 = new SourceArtifact(sourceRoot, PathFragment.create("shared"), secondOwner); |
| 384 | new EqualsTester() |
| 385 | .addEqualityGroup(derived1) |
| 386 | .addEqualityGroup(derived2) |
| 387 | .addEqualityGroup(source1, source2) |
| 388 | .testEquals(); |
| 389 | assertThat(derived1.hashCode()).isEqualTo(derived2.hashCode()); |
| 390 | assertThat(derived1.hashCode()).isNotEqualTo(source1.hashCode()); |
| 391 | assertThat(source1.hashCode()).isEqualTo(source2.hashCode()); |
| 392 | Artifact.OwnerlessArtifactWrapper wrapper1 = new Artifact.OwnerlessArtifactWrapper(derived1); |
| 393 | Artifact.OwnerlessArtifactWrapper wrapper2 = new Artifact.OwnerlessArtifactWrapper(derived2); |
| 394 | Artifact.OwnerlessArtifactWrapper wrapper3 = new Artifact.OwnerlessArtifactWrapper(source1); |
| 395 | Artifact.OwnerlessArtifactWrapper wrapper4 = new Artifact.OwnerlessArtifactWrapper(source2); |
| 396 | new EqualsTester() |
| 397 | .addEqualityGroup(wrapper1, wrapper2) |
| 398 | .addEqualityGroup(wrapper3, wrapper4) |
| 399 | .testEquals(); |
| 400 | Path path1 = derived1.getPath(); |
| 401 | Path path2 = derived2.getPath(); |
| 402 | Path path3 = source1.getPath(); |
| 403 | Path path4 = source2.getPath(); |
| 404 | new EqualsTester().addEqualityGroup(path1, path2, path3, path4).testEquals(); |
| 405 | } |
| 406 | |
Googler | 91cfbff | 2015-05-26 16:35:27 +0000 | [diff] [blame] | 407 | private Artifact createDirNameArtifact() throws Exception { |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 408 | return ActionsTestUtil.createArtifact( |
| 409 | ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/"))), |
| 410 | scratch.file("/aaa/bbb/ccc/ddd")); |
Googler | 91cfbff | 2015-05-26 16:35:27 +0000 | [diff] [blame] | 411 | } |
gregce | 11f3b0e | 2019-06-07 17:12:06 -0700 | [diff] [blame] | 412 | |
| 413 | @Test |
| 414 | public void canDeclareContentBasedOutput() throws Exception { |
| 415 | Path execRoot = scratch.getFileSystem().getPath("/"); |
| 416 | ArtifactRoot root = ArtifactRoot.asDerivedRoot(execRoot, scratch.dir("/newRoot")); |
| 417 | assertThat( |
| 418 | new Artifact.DerivedArtifact( |
| 419 | root, |
| 420 | PathFragment.create("newRoot/my.output"), |
| 421 | ActionsTestUtil.NULL_ARTIFACT_OWNER, |
| 422 | /*contentBasedPath=*/ true) |
| 423 | .contentBasedPath()) |
| 424 | .isTrue(); |
| 425 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 426 | } |