blob: 8861ed9a83df3d684ef1220c39e71b01bfb975b9 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.
14package com.google.devtools.build.lib.actions;
15
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010016import static com.google.common.truth.Truth.assertThat;
jcaterecd2abd2019-04-30 13:31:13 -070017import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018
19import com.google.common.collect.ImmutableList;
cpeysera666ffb2018-04-24 07:24:27 -070020import com.google.common.collect.ImmutableMap;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021import com.google.common.collect.Iterables;
22import com.google.common.collect.Lists;
janakrbf4123d2018-07-24 11:38:19 -070023import com.google.common.testing.EqualsTester;
Rumou Duan33bab462016-04-25 17:55:12 +000024import com.google.devtools.build.lib.actions.ActionAnalysisMetadata.MiddlemanType;
cpeysera666ffb2018-04-24 07:24:27 -070025import com.google.devtools.build.lib.actions.Artifact.SourceArtifact;
26import com.google.devtools.build.lib.actions.ArtifactResolver.ArtifactResolverSupplier;
ulfjack60dac4d2019-12-06 07:13:00 -080027import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
29import com.google.devtools.build.lib.actions.util.LabelArtifactOwner;
Lukacs Berki6e91eb92015-09-21 09:12:37 +000030import com.google.devtools.build.lib.cmdline.Label;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010031import com.google.devtools.build.lib.rules.cpp.CppFileTypes;
32import com.google.devtools.build.lib.rules.java.JavaSemantics;
cpeysera666ffb2018-04-24 07:24:27 -070033import com.google.devtools.build.lib.skyframe.serialization.AutoRegistry;
34import com.google.devtools.build.lib.skyframe.serialization.ObjectCodecs;
shahan20f35b42018-02-28 15:57:33 -080035import com.google.devtools.build.lib.skyframe.serialization.testutils.SerializationTester;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010036import com.google.devtools.build.lib.testutil.Scratch;
shahanfae34b92018-02-13 10:08:47 -080037import com.google.devtools.build.lib.vfs.FileSystem;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010038import com.google.devtools.build.lib.vfs.Path;
39import com.google.devtools.build.lib.vfs.PathFragment;
tomluee6a6862018-01-17 14:36:26 -080040import com.google.devtools.build.lib.vfs.Root;
janakrefb3f152019-06-05 17:42:34 -070041import com.google.devtools.build.skyframe.SkyFunctionName;
Paul Roberts8c443ef2016-10-18 02:04:25 +000042import java.io.IOException;
43import java.util.ArrayList;
44import java.util.List;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010045import org.junit.Before;
46import org.junit.Test;
47import org.junit.runner.RunWith;
48import org.junit.runners.JUnit4;
49
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010050@RunWith(JUnit4.class)
51public class ArtifactTest {
52 private Scratch scratch;
53 private Path execDir;
tomlu1cdcdf92018-01-16 11:07:51 -080054 private ArtifactRoot rootDir;
tomlu3d1a1942017-11-29 14:01:21 -080055 private final ActionKeyContext actionKeyContext = new ActionKeyContext();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010056
57 @Before
Florian Weikert0220dc72015-12-01 14:38:00 +000058 public final void setRootDir() throws Exception {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010059 scratch = new Scratch();
60 execDir = scratch.dir("/exec");
tomlu1cdcdf92018-01-16 11:07:51 -080061 rootDir = ArtifactRoot.asDerivedRoot(execDir, scratch.dir("/exec/root"));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010062 }
63
64 @Test
Laurent Le Brunf3cf98f2016-06-17 13:36:24 +000065 public void testConstruction_badRootDir() throws IOException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010066 Path f1 = scratch.file("/exec/dir/file.ext");
67 Path bogusDir = scratch.file("/exec/dir/bogus");
jcaterecd2abd2019-04-30 13:31:13 -070068 assertThrows(
69 IllegalArgumentException.class,
janakraea05602019-05-22 15:41:29 -070070 () ->
71 ActionsTestUtil.createArtifactWithExecPath(
Googler19a3f552019-09-03 08:22:16 -070072 ArtifactRoot.asDerivedRoot(execDir, bogusDir), f1.relativeTo(execDir))
73 .getRootRelativePath());
janakraea05602019-05-22 15:41:29 -070074 }
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 Nienhuysd08b27f2015-02-25 16:45:20 +010096 }
97
98 @Test
99 public void testEquivalenceRelation() throws Exception {
nharmatab4060b62017-04-04 17:11:39 +0000100 PathFragment aPath = PathFragment.create("src/a");
101 PathFragment bPath = PathFragment.create("src/b");
janakraea05602019-05-22 15:41:29 -0700102 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 Nienhuysd08b27f2015-02-25 16:45:20 +0100110 }
111
112 @Test
janakraea05602019-05-22 15:41:29 -0700113 public void testComparison() {
nharmatab4060b62017-04-04 17:11:39 +0000114 PathFragment aPath = PathFragment.create("src/a");
115 PathFragment bPath = PathFragment.create("src/b");
janakraea05602019-05-22 15:41:29 -0700116 Artifact aArtifact = ActionsTestUtil.createArtifactWithRootRelativePath(rootDir, aPath);
117 Artifact bArtifact = ActionsTestUtil.createArtifactWithRootRelativePath(rootDir, bPath);
lberkiaea56b32017-05-30 12:35:33 +0200118 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 Nienhuysd08b27f2015-02-25 16:45:20 +0100122 }
123
124 @Test
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100125 public void testGetFilename() throws Exception {
tomluee6a6862018-01-17 14:36:26 -0800126 ArtifactRoot root = ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/foo")));
janakraea05602019-05-22 15:41:29 -0700127 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"));
lberkiaea56b32017-05-30 12:35:33 +0200132 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 Nienhuysd08b27f2015-02-25 16:45:20 +0100136 }
137
138 @Test
Paul Roberts8c443ef2016-10-18 02:04:25 +0000139 public void testGetExtension() throws Exception {
tomluee6a6862018-01-17 14:36:26 -0800140 ArtifactRoot root = ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/foo")));
janakraea05602019-05-22 15:41:29 -0700141 Artifact javaFile = ActionsTestUtil.createArtifact(root, scratch.file("/foo/Bar.java"));
Paul Roberts8c443ef2016-10-18 02:04:25 +0000142 assertThat(javaFile.getExtension()).isEqualTo("java");
143 }
144
145 @Test
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100146 public void testMangledPath() {
147 String path = "dir/sub_dir/name:end";
lberkiaea56b32017-05-30 12:35:33 +0200148 assertThat(Actions.escapedPath(path)).isEqualTo("dir_Ssub_Udir_Sname_Cend");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100149 }
150
151 private List<Artifact> getFooBarArtifacts(MutableActionGraph actionGraph, boolean collapsedList)
152 throws Exception {
tomluee6a6862018-01-17 14:36:26 -0800153 ArtifactRoot root = ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/foo")));
janakraea05602019-05-22 15:41:29 -0700154 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");
ulfjack60dac4d2019-12-06 07:13:00 -0800160 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 Nienhuysd08b27f2015-02-25 16:45:20 +0100183 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<>();
tomlu3d1a1942017-11-29 14:01:21 -0800190 MutableActionGraph actionGraph = new MapBasedActionGraph(actionKeyContext);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100191 Artifact.addExecPaths(getFooBarArtifacts(actionGraph, false), paths);
lberkiaea56b32017-05-30 12:35:33 +0200192 assertThat(paths).containsExactly("bar1.h", "bar2.h");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100193 }
194
195 @Test
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100196 public void testAddExpandedArtifacts() throws Exception {
Rumou Duana77f32c2016-04-13 21:59:21 +0000197 List<Artifact> expanded = new ArrayList<>();
tomlu3d1a1942017-11-29 14:01:21 -0800198 MutableActionGraph actionGraph = new MapBasedActionGraph(actionKeyContext);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100199 List<Artifact> original = getFooBarArtifacts(actionGraph, true);
200 Artifact.addExpandedArtifacts(original, expanded,
Michael Thvedt434e68e2016-02-09 00:57:46 +0000201 ActionInputHelper.actionGraphArtifactExpander(actionGraph));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100202
203 List<Artifact> manuallyExpanded = new ArrayList<>();
204 for (Artifact artifact : original) {
Rumou Duan33bab462016-04-25 17:55:12 +0000205 ActionAnalysisMetadata action = actionGraph.getGeneratingAction(artifact);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100206 if (artifact.isMiddlemanArtifact()) {
207 Iterables.addAll(manuallyExpanded, action.getInputs());
208 } else {
209 manuallyExpanded.add(artifact);
210 }
211 }
Carmi Grushkofd8acab2015-11-10 17:19:13 +0000212 assertThat(expanded).containsExactlyElementsIn(manuallyExpanded);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100213 }
214
215 @Test
216 public void testAddExecPathsNewActionGraph() throws Exception {
217 List<String> paths = new ArrayList<>();
tomlu3d1a1942017-11-29 14:01:21 -0800218 MutableActionGraph actionGraph = new MapBasedActionGraph(actionKeyContext);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100219 Artifact.addExecPaths(getFooBarArtifacts(actionGraph, false), paths);
lberkiaea56b32017-05-30 12:35:33 +0200220 assertThat(paths).containsExactly("bar1.h", "bar2.h");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100221 }
222
223 @Test
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100224 public void testRootRelativePathIsSameAsExecPath() throws Exception {
tomluee6a6862018-01-17 14:36:26 -0800225 ArtifactRoot root = ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/foo")));
janakraea05602019-05-22 15:41:29 -0700226 Artifact a = ActionsTestUtil.createArtifact(root, scratch.file("/foo/bar1.h"));
cpovirka4d3da62019-05-02 14:27:33 -0700227 assertThat(a.getRootRelativePath()).isSameInstanceAs(a.getExecPath());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100228 }
229
230 @Test
231 public void testToDetailString() throws Exception {
shahane483df32018-04-05 08:03:04 -0700232 Path execRoot = scratch.getFileSystem().getPath("/execroot/workspace");
tomlu89eaee52018-01-15 17:40:35 -0800233 Artifact a =
janakraea05602019-05-22 15:41:29 -0700234 ActionsTestUtil.createArtifact(
235 ArtifactRoot.asDerivedRoot(execRoot, scratch.dir("/execroot/workspace/b")), "c");
shahane483df32018-04-05 08:03:04 -0700236 assertThat(a.toDetailString()).isEqualTo("[[<execution_root>]b]c");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100237 }
238
239 @Test
janakraea05602019-05-22 15:41:29 -0700240 public void testWeirdArtifact() {
tomlua729b9b2018-02-08 15:32:00 -0800241 Path execRoot = scratch.getFileSystem().getPath("/");
jcaterb0c7ee52019-05-02 12:33:13 -0700242 assertThrows(
tomlua729b9b2018-02-08 15:32:00 -0800243 IllegalArgumentException.class,
244 () ->
janakraea05602019-05-22 15:41:29 -0700245 ActionsTestUtil.createArtifactWithExecPath(
Googler19a3f552019-09-03 08:22:16 -0700246 ArtifactRoot.asDerivedRoot(execRoot, scratch.dir("/a")),
247 PathFragment.create("c"))
248 .getRootRelativePath());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100249 }
250
251 @Test
cpeyser875068a2018-02-01 08:40:58 -0800252 public void testCodec() throws Exception {
janakr658d47f2019-05-29 11:11:30 -0700253 Artifact.DerivedArtifact artifact =
254 (Artifact.DerivedArtifact) ActionsTestUtil.createArtifact(rootDir, "src/a");
janakrefb3f152019-06-05 17:42:34 -0700255 artifact.setGeneratingActionKey(ActionsTestUtil.NULL_ACTION_LOOKUP_DATA);
janakraea05602019-05-22 15:41:29 -0700256 ArtifactRoot anotherRoot =
257 ArtifactRoot.asDerivedRoot(scratch.getFileSystem().getPath("/"), scratch.dir("/src"));
janakr658d47f2019-05-29 11:11:30 -0700258 Artifact.DerivedArtifact anotherArtifact =
janakr3290e222019-05-29 16:34:22 -0700259 new Artifact.DerivedArtifact(
260 anotherRoot,
261 anotherRoot.getExecPath().getRelative("src/c"),
janakrefb3f152019-06-05 17:42:34 -0700262 ActionsTestUtil.NULL_ARTIFACT_OWNER);
263 anotherArtifact.setGeneratingActionKey(ActionsTestUtil.NULL_ACTION_LOOKUP_DATA);
janakr658d47f2019-05-29 11:11:30 -0700264 new SerializationTester(artifact, anotherArtifact)
shahanfae34b92018-02-13 10:08:47 -0800265 .addDependency(FileSystem.class, scratch.getFileSystem())
shahan20f35b42018-02-28 15:57:33 -0800266 .runTests();
cpeyser875068a2018-02-01 08:40:58 -0800267 }
268
269 @Test
cpeysera666ffb2018-04-24 07:24:27 -0700270 public void testCodecRecyclesSourceArtifactInstances() throws Exception {
271 Root root = Root.fromPath(scratch.dir("/"));
272 ArtifactRoot artifactRoot = ArtifactRoot.asSourceRoot(root);
buchgrffad5872019-06-18 08:06:32 -0700273 ArtifactFactory artifactFactory =
274 new ArtifactFactory(execDir.getParentDirectory(), "blaze-out");
cpeysera666ffb2018-04-24 07:24:27 -0700275 artifactFactory.setSourceArtifactRoots(ImmutableMap.of(root, artifactRoot));
276 ArtifactResolverSupplier artifactResolverSupplierForTest = () -> artifactFactory;
277
cpeysera666ffb2018-04-24 07:24:27 -0700278 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(),
cpeysera666ffb2018-04-24 07:24:27 -0700287 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));
cpovirka4d3da62019-05-02 14:27:33 -0700296 assertThat(deserialized1).isSameInstanceAs(deserialized2);
cpeysera666ffb2018-04-24 07:24:27 -0700297
298 Artifact sourceArtifactFromFactory =
299 artifactFactory.getSourceArtifact(pathFragment, root, owner);
300 Artifact deserialized =
301 (Artifact) objectCodecs.deserialize(objectCodecs.serialize(sourceArtifactFromFactory));
cpovirka4d3da62019-05-02 14:27:33 -0700302 assertThat(sourceArtifactFromFactory).isSameInstanceAs(deserialized);
cpeysera666ffb2018-04-24 07:24:27 -0700303 }
304
305 @Test
Googler91cfbff2015-05-26 16:35:27 +0000306 public void testLongDirname() throws Exception {
307 String dirName = createDirNameArtifact().getDirname();
Carmi Grushkofd8acab2015-11-10 17:19:13 +0000308
309 assertThat(dirName).isEqualTo("aaa/bbb/ccc");
Googler91cfbff2015-05-26 16:35:27 +0000310 }
Carmi Grushkofd8acab2015-11-10 17:19:13 +0000311
Googler91cfbff2015-05-26 16:35:27 +0000312 @Test
313 public void testDirnameInExecutionDir() throws Exception {
tomlu89eaee52018-01-15 17:40:35 -0800314 Artifact artifact =
janakraea05602019-05-22 15:41:29 -0700315 ActionsTestUtil.createArtifact(
316 ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/foo"))),
317 scratch.file("/foo/bar.txt"));
Carmi Grushkofd8acab2015-11-10 17:19:13 +0000318
319 assertThat(artifact.getDirname()).isEqualTo(".");
Googler91cfbff2015-05-26 16:35:27 +0000320 }
Carmi Grushkofd8acab2015-11-10 17:19:13 +0000321
Googler91cfbff2015-05-26 16:35:27 +0000322 @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 Grushkofd8acab2015-11-10 17:19:13 +0000330
Dmitry Lomovddda06d2016-01-19 15:58:11 +0000331 @Test
332 public void testIsSourceArtifact() throws Exception {
333 assertThat(
janakraea05602019-05-22 15:41:29 -0700334 new Artifact.SourceArtifact(
tomluee6a6862018-01-17 14:36:26 -0800335 ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/"))),
janakraea05602019-05-22 15:41:29 -0700336 PathFragment.create("src/foo.cc"),
337 ArtifactOwner.NullArtifactOwner.INSTANCE)
tomlu1cdcdf92018-01-16 11:07:51 -0800338 .isSourceArtifact())
Dmitry Lomovddda06d2016-01-19 15:58:11 +0000339 .isTrue();
340 assertThat(
janakraea05602019-05-22 15:41:29 -0700341 ActionsTestUtil.createArtifact(
tomlu1cdcdf92018-01-16 11:07:51 -0800342 ArtifactRoot.asDerivedRoot(
janakraea05602019-05-22 15:41:29 -0700343 scratch.dir("/genfiles"), scratch.dir("/genfiles/aaa")),
344 scratch.file("/genfiles/aaa/bar.out"))
tomlu1cdcdf92018-01-16 11:07:51 -0800345 .isSourceArtifact())
Dmitry Lomovddda06d2016-01-19 15:58:11 +0000346 .isFalse();
Dmitry Lomovddda06d2016-01-19 15:58:11 +0000347 }
348
349 @Test
350 public void testGetRoot() throws Exception {
tomlu89eaee52018-01-15 17:40:35 -0800351 Path execRoot = scratch.getFileSystem().getPath("/");
tomlu1cdcdf92018-01-16 11:07:51 -0800352 ArtifactRoot root = ArtifactRoot.asDerivedRoot(execRoot, scratch.dir("/newRoot"));
janakraea05602019-05-22 15:41:29 -0700353 assertThat(ActionsTestUtil.createArtifact(root, scratch.file("/newRoot/foo")).getRoot())
354 .isEqualTo(root);
Dmitry Lomovddda06d2016-01-19 15:58:11 +0000355 }
356
janakrbf4123d2018-07-24 11:38:19 -0700357 @Test
358 public void hashCodeAndEquals() throws IOException {
359 Path execRoot = scratch.getFileSystem().getPath("/");
360 ArtifactRoot root = ArtifactRoot.asDerivedRoot(execRoot, scratch.dir("/newRoot"));
janakrefb3f152019-06-05 17:42:34 -0700361 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 =
janakr3290e222019-05-29 16:34:22 -0700376 new Artifact.DerivedArtifact(root, PathFragment.create("newRoot/shared"), firstOwner);
janakrefb3f152019-06-05 17:42:34 -0700377 derived1.setGeneratingActionKey(ActionLookupData.create(firstOwner, 0));
378 Artifact.DerivedArtifact derived2 =
janakr3290e222019-05-29 16:34:22 -0700379 new Artifact.DerivedArtifact(root, PathFragment.create("newRoot/shared"), secondOwner);
janakrefb3f152019-06-05 17:42:34 -0700380 derived2.setGeneratingActionKey(ActionLookupData.create(secondOwner, 0));
janakrbf4123d2018-07-24 11:38:19 -0700381 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
Googler91cfbff2015-05-26 16:35:27 +0000407 private Artifact createDirNameArtifact() throws Exception {
janakraea05602019-05-22 15:41:29 -0700408 return ActionsTestUtil.createArtifact(
409 ArtifactRoot.asSourceRoot(Root.fromPath(scratch.dir("/"))),
410 scratch.file("/aaa/bbb/ccc/ddd"));
Googler91cfbff2015-05-26 16:35:27 +0000411 }
gregce11f3b0e2019-06-07 17:12:06 -0700412
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 Nienhuysd08b27f2015-02-25 16:45:20 +0100426}