Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
| 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.skyframe; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertWithMessage; |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 18 | import static com.google.devtools.build.lib.actions.ActionInputHelper.treeFileArtifact; |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 19 | import static org.junit.Assert.fail; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 20 | |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.ImmutableMap; |
| 23 | import com.google.common.collect.ImmutableSet; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 24 | import com.google.common.util.concurrent.Runnables; |
| 25 | import com.google.devtools.build.lib.actions.Action; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.actions.ActionLookupValue.ActionLookupKey; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.actions.Artifact; |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact; |
| 29 | import com.google.devtools.build.lib.actions.Artifact.SpecialArtifactType; |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact; |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.actions.ArtifactOwner; |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 32 | import com.google.devtools.build.lib.actions.ArtifactRoot; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.util.TestAction; |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 35 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.events.NullEventHandler; |
| 38 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
| 39 | import com.google.devtools.build.lib.skyframe.DirtinessCheckerUtils.BasicFilesystemDirtinessChecker; |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.skyframe.ExternalFilesHelper.ExternalFileAction; |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.skyframe.PackageLookupFunction.CrossRepositoryLabelViolationStrategy; |
Luis Fernando Pino Duque | be10218 | 2016-05-23 14:03:55 +0000 | [diff] [blame] | 42 | import com.google.devtools.build.lib.testutil.TestConstants; |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 43 | import com.google.devtools.build.lib.testutil.TestRuleClassProvider; |
laszlocsomor | 29bdf63 | 2018-03-13 02:50:46 -0700 | [diff] [blame] | 44 | import com.google.devtools.build.lib.testutil.TimestampGranularityUtils; |
Laszlo Csomor | a278aec | 2018-03-09 04:07:31 -0800 | [diff] [blame] | 45 | import com.google.devtools.build.lib.util.io.OutErr; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 46 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
| 47 | import com.google.devtools.build.lib.vfs.BatchStat; |
| 48 | import com.google.devtools.build.lib.vfs.FileStatus; |
| 49 | import com.google.devtools.build.lib.vfs.FileStatusWithDigest; |
| 50 | import com.google.devtools.build.lib.vfs.FileStatusWithDigestAdapter; |
| 51 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 52 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 53 | import com.google.devtools.build.lib.vfs.Path; |
| 54 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 55 | import com.google.devtools.build.lib.vfs.Root; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 56 | import com.google.devtools.build.lib.vfs.RootedPath; |
| 57 | import com.google.devtools.build.lib.vfs.Symlinks; |
| 58 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 59 | import com.google.devtools.build.skyframe.Differencer.Diff; |
| 60 | import com.google.devtools.build.skyframe.EvaluationResult; |
| 61 | import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator; |
| 62 | import com.google.devtools.build.skyframe.MemoizingEvaluator; |
| 63 | import com.google.devtools.build.skyframe.RecordingDifferencer; |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 64 | import com.google.devtools.build.skyframe.SequencedRecordingDifferencer; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 65 | import com.google.devtools.build.skyframe.SequentialBuildDriver; |
| 66 | import com.google.devtools.build.skyframe.SkyFunction; |
| 67 | import com.google.devtools.build.skyframe.SkyFunctionName; |
| 68 | import com.google.devtools.build.skyframe.SkyKey; |
| 69 | import com.google.devtools.build.skyframe.SkyValue; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 70 | import java.io.IOException; |
| 71 | import java.util.ArrayList; |
| 72 | import java.util.Arrays; |
| 73 | import java.util.Collection; |
| 74 | import java.util.HashMap; |
| 75 | import java.util.List; |
| 76 | import java.util.Map; |
| 77 | import java.util.UUID; |
| 78 | import java.util.concurrent.atomic.AtomicReference; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 79 | import javax.annotation.Nullable; |
Janak Ramakrishnan | cba1645 | 2016-07-29 02:17:02 +0000 | [diff] [blame] | 80 | import org.junit.Before; |
| 81 | import org.junit.Test; |
| 82 | import org.junit.runner.RunWith; |
| 83 | import org.junit.runners.JUnit4; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Tests for {@link FilesystemValueChecker}. |
| 87 | */ |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 88 | @RunWith(JUnit4.class) |
| 89 | public class FilesystemValueCheckerTest { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 90 | |
| 91 | private RecordingDifferencer differencer; |
| 92 | private MemoizingEvaluator evaluator; |
| 93 | private SequentialBuildDriver driver; |
| 94 | private MockFileSystem fs; |
| 95 | private Path pkgRoot; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 96 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 97 | @Before |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 98 | public final void setUp() throws Exception { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 99 | ImmutableMap.Builder<SkyFunctionName, SkyFunction> skyFunctions = ImmutableMap.builder(); |
| 100 | |
| 101 | fs = new MockFileSystem(); |
| 102 | pkgRoot = fs.getPath("/testroot"); |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 103 | FileSystemUtils.createDirectoryAndParents(pkgRoot); |
| 104 | FileSystemUtils.createEmptyFile(pkgRoot.getRelative("WORKSPACE")); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 105 | |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 106 | AtomicReference<PathPackageLocator> pkgLocator = |
| 107 | new AtomicReference<>( |
| 108 | new PathPackageLocator( |
| 109 | fs.getPath("/output_base"), |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 110 | ImmutableList.of(Root.fromPath(pkgRoot)), |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 111 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY)); |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 112 | BlazeDirectories directories = |
| 113 | new BlazeDirectories( |
cushon | 849df36 | 2018-05-14 01:51:45 -0700 | [diff] [blame^] | 114 | new ServerDirectories(pkgRoot, pkgRoot, pkgRoot), |
| 115 | pkgRoot, |
| 116 | /* defaultSystemJavabase= */ null, |
| 117 | TestConstants.PRODUCT_NAME); |
nharmata | 3fb7d34 | 2018-02-23 11:37:51 -0800 | [diff] [blame] | 118 | ExternalFilesHelper externalFilesHelper = ExternalFilesHelper.createForTesting( |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 119 | pkgLocator, ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, directories); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 120 | skyFunctions.put(SkyFunctions.FILE_STATE, new FileStateFunction( |
| 121 | new AtomicReference<TimestampGranularityMonitor>(), externalFilesHelper)); |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 122 | skyFunctions.put(SkyFunctions.FILE, new FileFunction(pkgLocator)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 123 | skyFunctions.put( |
| 124 | SkyFunctions.FILE_SYMLINK_CYCLE_UNIQUENESS, new FileSymlinkCycleUniquenessFunction()); |
| 125 | skyFunctions.put( |
| 126 | SkyFunctions.FILE_SYMLINK_INFINITE_EXPANSION_UNIQUENESS, |
| 127 | new FileSymlinkInfiniteExpansionUniquenessFunction()); |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 128 | skyFunctions.put(SkyFunctions.PACKAGE, |
| 129 | new PackageFunction(null, null, null, null, null, null, null)); |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 130 | skyFunctions.put( |
| 131 | SkyFunctions.PACKAGE_LOOKUP, |
| 132 | new PackageLookupFunction( |
| 133 | new AtomicReference<>(ImmutableSet.<PackageIdentifier>of()), |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 134 | CrossRepositoryLabelViolationStrategy.ERROR, |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 135 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY)); |
Damien Martin-Guillerez | 4ecfe51 | 2016-01-22 11:03:23 +0000 | [diff] [blame] | 136 | skyFunctions.put(SkyFunctions.WORKSPACE_AST, |
| 137 | new WorkspaceASTFunction(TestRuleClassProvider.getRuleClassProvider())); |
carmi | d6a9828 | 2018-03-13 19:19:16 -0700 | [diff] [blame] | 138 | skyFunctions.put( |
| 139 | SkyFunctions.WORKSPACE_FILE, |
| 140 | new WorkspaceFileFunction( |
| 141 | TestRuleClassProvider.getRuleClassProvider(), |
| 142 | TestConstants.PACKAGE_FACTORY_BUILDER_FACTORY_FOR_TESTING |
| 143 | .builder(directories) |
nharmata | e57e9a3 | 2018-04-02 15:10:24 -0700 | [diff] [blame] | 144 | .build(TestRuleClassProvider.getRuleClassProvider()), |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 145 | directories)); |
Damien Martin-Guillerez | bc8b5e0 | 2016-02-05 22:09:09 +0000 | [diff] [blame] | 146 | skyFunctions.put(SkyFunctions.EXTERNAL_PACKAGE, new ExternalPackageFunction()); |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 147 | |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 148 | differencer = new SequencedRecordingDifferencer(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 149 | evaluator = new InMemoryMemoizingEvaluator(skyFunctions.build(), differencer); |
| 150 | driver = new SequentialBuildDriver(evaluator); |
| 151 | PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID()); |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 152 | PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get()); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 155 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 156 | public void testEmpty() throws Exception { |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 157 | FilesystemValueChecker checker = new FilesystemValueChecker(null, null); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 158 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 161 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 162 | public void testSimple() throws Exception { |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 163 | FilesystemValueChecker checker = new FilesystemValueChecker(null, null); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 164 | |
| 165 | Path path = fs.getPath("/foo"); |
| 166 | FileSystemUtils.createEmptyFile(path); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 167 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 168 | |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 169 | SkyKey skyKey = |
| 170 | FileStateValue.key( |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 171 | RootedPath.toRootedPath(Root.absoluteRoot(fs), PathFragment.create("/foo"))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 172 | EvaluationResult<SkyValue> result = |
| 173 | driver.evaluate( |
| 174 | ImmutableList.of(skyKey), |
| 175 | false, |
| 176 | SkyframeExecutor.DEFAULT_THREAD_COUNT, |
| 177 | NullEventHandler.INSTANCE); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 178 | assertThat(result.hasError()).isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 179 | |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 180 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 181 | |
| 182 | FileSystemUtils.writeContentAsLatin1(path, "hello"); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 183 | assertDiffWithNewValues(getDirtyFilesystemKeys(evaluator, checker), skyKey); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 184 | |
| 185 | // The dirty bits are not reset until the FileValues are actually revalidated. |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 186 | assertDiffWithNewValues(getDirtyFilesystemKeys(evaluator, checker), skyKey); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 187 | |
| 188 | differencer.invalidate(ImmutableList.of(skyKey)); |
| 189 | result = |
| 190 | driver.evaluate( |
| 191 | ImmutableList.of(skyKey), |
| 192 | false, |
| 193 | SkyframeExecutor.DEFAULT_THREAD_COUNT, |
| 194 | NullEventHandler.INSTANCE); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 195 | assertThat(result.hasError()).isFalse(); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 196 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Tests that an already-invalidated value can still be marked changed: symlink points at sym1. |
| 201 | * Invalidate symlink by changing sym1 from pointing at path to point to sym2. This only dirties |
| 202 | * (rather than changes) symlink because sym2 still points at path, so all symlink stats remain |
| 203 | * the same. Then do a null build, change sym1 back to point at path, and change symlink to not be |
| 204 | * a symlink anymore. The fact that it is not a symlink should be detected. |
| 205 | */ |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 206 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 207 | public void testDirtySymlink() throws Exception { |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 208 | FilesystemValueChecker checker = new FilesystemValueChecker(null, null); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 209 | |
| 210 | Path path = fs.getPath("/foo"); |
| 211 | FileSystemUtils.writeContentAsLatin1(path, "foo contents"); |
| 212 | // We need the intermediate sym1 and sym2 so that we can dirty a child of symlink without |
| 213 | // actually changing the FileValue calculated for symlink (if we changed the contents of foo, |
Laszlo Csomor | a278aec | 2018-03-09 04:07:31 -0800 | [diff] [blame] | 214 | // the FileValue created for symlink would notice, since it stats foo). |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 215 | Path sym1 = fs.getPath("/sym1"); |
| 216 | Path sym2 = fs.getPath("/sym2"); |
| 217 | Path symlink = fs.getPath("/bar"); |
| 218 | FileSystemUtils.ensureSymbolicLink(symlink, sym1); |
| 219 | FileSystemUtils.ensureSymbolicLink(sym1, path); |
| 220 | FileSystemUtils.ensureSymbolicLink(sym2, path); |
| 221 | SkyKey fooKey = |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 222 | FileValue.key(RootedPath.toRootedPath(Root.absoluteRoot(fs), PathFragment.create("/foo"))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 223 | RootedPath symlinkRootedPath = |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 224 | RootedPath.toRootedPath(Root.absoluteRoot(fs), PathFragment.create("/bar")); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 225 | SkyKey symlinkKey = FileValue.key(symlinkRootedPath); |
| 226 | SkyKey symlinkFileStateKey = FileStateValue.key(symlinkRootedPath); |
| 227 | RootedPath sym1RootedPath = |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 228 | RootedPath.toRootedPath(Root.absoluteRoot(fs), PathFragment.create("/sym1")); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 229 | SkyKey sym1FileStateKey = FileStateValue.key(sym1RootedPath); |
| 230 | Iterable<SkyKey> allKeys = ImmutableList.of(symlinkKey, fooKey); |
| 231 | |
| 232 | // First build -- prime the graph. |
| 233 | EvaluationResult<FileValue> result = |
| 234 | driver.evaluate( |
| 235 | allKeys, false, SkyframeExecutor.DEFAULT_THREAD_COUNT, NullEventHandler.INSTANCE); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 236 | assertThat(result.hasError()).isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 237 | FileValue symlinkValue = result.get(symlinkKey); |
| 238 | FileValue fooValue = result.get(fooKey); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 239 | assertWithMessage(symlinkValue.toString()).that(symlinkValue.isSymlink()).isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 240 | // Digest is not always available, so use size as a proxy for contents. |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 241 | assertThat(symlinkValue.getSize()).isEqualTo(fooValue.getSize()); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 242 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 243 | |
| 244 | // Before second build, move sym1 to point to sym2. |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 245 | assertThat(sym1.delete()).isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 246 | FileSystemUtils.ensureSymbolicLink(sym1, sym2); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 247 | assertDiffWithNewValues(getDirtyFilesystemKeys(evaluator, checker), sym1FileStateKey); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 248 | |
| 249 | differencer.invalidate(ImmutableList.of(sym1FileStateKey)); |
| 250 | result = |
| 251 | driver.evaluate( |
| 252 | ImmutableList.<SkyKey>of(), |
| 253 | false, |
| 254 | SkyframeExecutor.DEFAULT_THREAD_COUNT, |
| 255 | NullEventHandler.INSTANCE); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 256 | assertThat(result.hasError()).isFalse(); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 257 | assertDiffWithNewValues(getDirtyFilesystemKeys(evaluator, checker), sym1FileStateKey); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 258 | |
| 259 | // Before third build, move sym1 back to original (so change pruning will prevent signaling of |
| 260 | // its parents, but change symlink for real. |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 261 | assertThat(sym1.delete()).isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 262 | FileSystemUtils.ensureSymbolicLink(sym1, path); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 263 | assertThat(symlink.delete()).isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 264 | FileSystemUtils.writeContentAsLatin1(symlink, "new symlink contents"); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 265 | assertDiffWithNewValues(getDirtyFilesystemKeys(evaluator, checker), symlinkFileStateKey); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 266 | differencer.invalidate(ImmutableList.of(symlinkFileStateKey)); |
| 267 | result = |
| 268 | driver.evaluate( |
| 269 | allKeys, false, SkyframeExecutor.DEFAULT_THREAD_COUNT, NullEventHandler.INSTANCE); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 270 | assertThat(result.hasError()).isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 271 | symlinkValue = result.get(symlinkKey); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 272 | assertWithMessage(symlinkValue.toString()).that(symlinkValue.isSymlink()).isFalse(); |
| 273 | assertThat(result.get(fooKey)).isEqualTo(fooValue); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 274 | assertThat(symlinkValue.getSize()).isNotEqualTo(fooValue.getSize()); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 275 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 278 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 279 | public void testExplicitFiles() throws Exception { |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 280 | FilesystemValueChecker checker = new FilesystemValueChecker(null, null); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 281 | |
| 282 | Path path1 = fs.getPath("/foo1"); |
| 283 | Path path2 = fs.getPath("/foo2"); |
| 284 | FileSystemUtils.createEmptyFile(path1); |
| 285 | FileSystemUtils.createEmptyFile(path2); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 286 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 287 | |
| 288 | SkyKey key1 = |
| 289 | FileStateValue.key( |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 290 | RootedPath.toRootedPath(Root.absoluteRoot(fs), PathFragment.create("/foo1"))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 291 | SkyKey key2 = |
| 292 | FileStateValue.key( |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 293 | RootedPath.toRootedPath(Root.absoluteRoot(fs), PathFragment.create("/foo2"))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 294 | Iterable<SkyKey> skyKeys = ImmutableList.of(key1, key2); |
| 295 | EvaluationResult<SkyValue> result = |
| 296 | driver.evaluate( |
| 297 | skyKeys, false, SkyframeExecutor.DEFAULT_THREAD_COUNT, NullEventHandler.INSTANCE); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 298 | assertThat(result.hasError()).isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 299 | |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 300 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 301 | |
Laszlo Csomor | a278aec | 2018-03-09 04:07:31 -0800 | [diff] [blame] | 302 | // Wait for the timestamp granularity to elapse, so updating the files will observably advance |
| 303 | // their ctime. |
laszlocsomor | 29bdf63 | 2018-03-13 02:50:46 -0700 | [diff] [blame] | 304 | TimestampGranularityUtils.waitForTimestampGranularity( |
| 305 | System.currentTimeMillis(), OutErr.SYSTEM_OUT_ERR); |
Laszlo Csomor | a278aec | 2018-03-09 04:07:31 -0800 | [diff] [blame] | 306 | // Update path1's contents and mtime. This will update the file's ctime. |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 307 | FileSystemUtils.writeContentAsLatin1(path1, "hello1"); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 308 | path1.setLastModifiedTime(27); |
Laszlo Csomor | a278aec | 2018-03-09 04:07:31 -0800 | [diff] [blame] | 309 | // Update path2's mtime but not its contents. We expect that an mtime change suffices to update |
| 310 | // the ctime. |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 311 | path2.setLastModifiedTime(42); |
Laszlo Csomor | a278aec | 2018-03-09 04:07:31 -0800 | [diff] [blame] | 312 | // Assert that both files changed. The change detection relies, among other things, on ctime |
| 313 | // change. |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 314 | assertDiffWithNewValues(getDirtyFilesystemKeys(evaluator, checker), key1, key2); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 315 | |
| 316 | differencer.invalidate(skyKeys); |
| 317 | result = |
| 318 | driver.evaluate( |
| 319 | skyKeys, false, SkyframeExecutor.DEFAULT_THREAD_COUNT, NullEventHandler.INSTANCE); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 320 | assertThat(result.hasError()).isFalse(); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 321 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 324 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 325 | public void testFileWithIOExceptionNotConsideredDirty() throws Exception { |
| 326 | Path path = fs.getPath("/testroot/foo"); |
| 327 | path.getParentDirectory().createDirectory(); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 328 | path.createSymbolicLink(PathFragment.create("bar")); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 329 | |
| 330 | fs.readlinkThrowsIoException = true; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 331 | SkyKey fileKey = |
| 332 | FileStateValue.key( |
| 333 | RootedPath.toRootedPath(Root.fromPath(pkgRoot), PathFragment.create("foo"))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 334 | EvaluationResult<SkyValue> result = |
| 335 | driver.evaluate( |
| 336 | ImmutableList.of(fileKey), |
| 337 | false, |
| 338 | SkyframeExecutor.DEFAULT_THREAD_COUNT, |
| 339 | NullEventHandler.INSTANCE); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 340 | assertThat(result.hasError()).isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 341 | |
| 342 | fs.readlinkThrowsIoException = false; |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 343 | FilesystemValueChecker checker = new FilesystemValueChecker(null, null); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 344 | Diff diff = getDirtyFilesystemKeys(evaluator, checker); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 345 | assertThat(diff.changedKeysWithoutNewValues()).isEmpty(); |
| 346 | assertThat(diff.changedKeysWithNewValues()).isEmpty(); |
| 347 | } |
| 348 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 349 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 350 | public void testFilesInCycleNotConsideredDirty() throws Exception { |
| 351 | Path path1 = pkgRoot.getRelative("foo1"); |
| 352 | Path path2 = pkgRoot.getRelative("foo2"); |
| 353 | Path path3 = pkgRoot.getRelative("foo3"); |
| 354 | FileSystemUtils.ensureSymbolicLink(path1, path2); |
| 355 | FileSystemUtils.ensureSymbolicLink(path2, path3); |
| 356 | FileSystemUtils.ensureSymbolicLink(path3, path1); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 357 | SkyKey fileKey1 = FileValue.key(RootedPath.toRootedPath(Root.fromPath(pkgRoot), path1)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 358 | |
| 359 | EvaluationResult<SkyValue> result = |
| 360 | driver.evaluate( |
| 361 | ImmutableList.of(fileKey1), |
| 362 | false, |
| 363 | SkyframeExecutor.DEFAULT_THREAD_COUNT, |
| 364 | NullEventHandler.INSTANCE); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 365 | assertThat(result.hasError()).isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 366 | |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 367 | FilesystemValueChecker checker = new FilesystemValueChecker(null, null); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 368 | Diff diff = getDirtyFilesystemKeys(evaluator, checker); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 369 | assertThat(diff.changedKeysWithoutNewValues()).isEmpty(); |
| 370 | assertThat(diff.changedKeysWithNewValues()).isEmpty(); |
| 371 | } |
| 372 | |
| 373 | public void checkDirtyActions(BatchStat batchStatter, boolean forceDigests) throws Exception { |
| 374 | Artifact out1 = createDerivedArtifact("fiz"); |
| 375 | Artifact out2 = createDerivedArtifact("pop"); |
| 376 | |
| 377 | FileSystemUtils.writeContentAsLatin1(out1.getPath(), "hello"); |
| 378 | FileSystemUtils.writeContentAsLatin1(out2.getPath(), "fizzlepop"); |
| 379 | |
janakr | baf52ae | 2018-02-14 09:03:18 -0800 | [diff] [blame] | 380 | ActionLookupKey actionLookupKey = |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 381 | new ActionLookupKey() { |
| 382 | @Override |
| 383 | public SkyFunctionName functionName() { |
| 384 | return SkyFunctionName.FOR_TESTING; |
| 385 | } |
| 386 | }; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 387 | SkyKey actionKey1 = ActionExecutionValue.key(actionLookupKey, 0); |
| 388 | SkyKey actionKey2 = ActionExecutionValue.key(actionLookupKey, 1); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 389 | differencer.inject( |
| 390 | ImmutableMap.<SkyKey, SkyValue>of( |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 391 | actionKey1, |
| 392 | actionValue( |
| 393 | new TestAction( |
| 394 | Runnables.doNothing(), ImmutableSet.<Artifact>of(), ImmutableSet.of(out1)), |
| 395 | forceDigests), |
| 396 | actionKey2, |
| 397 | actionValue( |
| 398 | new TestAction( |
| 399 | Runnables.doNothing(), ImmutableSet.<Artifact>of(), ImmutableSet.of(out2)), |
| 400 | forceDigests))); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 401 | assertThat( |
| 402 | driver |
| 403 | .evaluate(ImmutableList.<SkyKey>of(), false, 1, NullEventHandler.INSTANCE) |
| 404 | .hasError()) |
| 405 | .isFalse(); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 406 | assertThat(new FilesystemValueChecker(null, null).getDirtyActionValues(evaluator.getValues(), |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 407 | batchStatter, ModifiedFileSet.EVERYTHING_MODIFIED)).isEmpty(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 408 | |
| 409 | FileSystemUtils.writeContentAsLatin1(out1.getPath(), "goodbye"); |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 410 | assertThat( |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 411 | new FilesystemValueChecker(null, null) |
| 412 | .getDirtyActionValues( |
| 413 | evaluator.getValues(), batchStatter, ModifiedFileSet.EVERYTHING_MODIFIED)) |
| 414 | .containsExactly(actionKey1); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 415 | assertThat( |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 416 | new FilesystemValueChecker(null, null) |
| 417 | .getDirtyActionValues( |
| 418 | evaluator.getValues(), |
| 419 | batchStatter, |
| 420 | new ModifiedFileSet.Builder().modify(out1.getExecPath()).build())) |
| 421 | .containsExactly(actionKey1); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 422 | assertThat( |
| 423 | new FilesystemValueChecker(null, null).getDirtyActionValues(evaluator.getValues(), |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 424 | batchStatter, |
| 425 | new ModifiedFileSet.Builder().modify( |
| 426 | out1.getExecPath().getParentDirectory()).build())).isEmpty(); |
| 427 | assertThat( |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 428 | new FilesystemValueChecker(null, null).getDirtyActionValues(evaluator.getValues(), |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 429 | batchStatter, ModifiedFileSet.NOTHING_MODIFIED)).isEmpty(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 432 | public void checkDirtyTreeArtifactActions(BatchStat batchStatter) |
| 433 | throws Exception { |
| 434 | // Normally, an Action specifies the contents of a TreeArtifact when it executes. |
| 435 | // To decouple FileSystemValueTester checking from Action execution, we inject TreeArtifact |
| 436 | // contents into ActionExecutionValues. |
| 437 | |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 438 | SpecialArtifact out1 = createTreeArtifact("one"); |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 439 | TreeFileArtifact file11 = treeFileArtifact(out1, "fizz"); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 440 | FileSystemUtils.createDirectoryAndParents(out1.getPath()); |
| 441 | FileSystemUtils.writeContentAsLatin1(file11.getPath(), "buzz"); |
| 442 | |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 443 | SpecialArtifact out2 = createTreeArtifact("two"); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 444 | FileSystemUtils.createDirectoryAndParents(out2.getPath().getChild("subdir")); |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 445 | TreeFileArtifact file21 = treeFileArtifact(out2, "moony"); |
| 446 | TreeFileArtifact file22 = treeFileArtifact(out2, "subdir/wormtail"); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 447 | FileSystemUtils.writeContentAsLatin1(file21.getPath(), "padfoot"); |
| 448 | FileSystemUtils.writeContentAsLatin1(file22.getPath(), "prongs"); |
| 449 | |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 450 | SpecialArtifact outEmpty = createTreeArtifact("empty"); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 451 | FileSystemUtils.createDirectoryAndParents(outEmpty.getPath()); |
| 452 | |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 453 | SpecialArtifact outUnchanging = createTreeArtifact("untouched"); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 454 | FileSystemUtils.createDirectoryAndParents(outUnchanging.getPath()); |
| 455 | |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 456 | SpecialArtifact last = createTreeArtifact("zzzzzzzzzz"); |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 457 | FileSystemUtils.createDirectoryAndParents(last.getPath()); |
| 458 | |
janakr | baf52ae | 2018-02-14 09:03:18 -0800 | [diff] [blame] | 459 | ActionLookupKey actionLookupKey = |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 460 | new ActionLookupKey() { |
| 461 | @Override |
| 462 | public SkyFunctionName functionName() { |
| 463 | return SkyFunctionName.FOR_TESTING; |
| 464 | } |
| 465 | }; |
janakr | 93e3eea | 2017-03-30 22:09:37 +0000 | [diff] [blame] | 466 | SkyKey actionKey1 = ActionExecutionValue.key(actionLookupKey, 0); |
| 467 | SkyKey actionKey2 = ActionExecutionValue.key(actionLookupKey, 1); |
| 468 | SkyKey actionKeyEmpty = ActionExecutionValue.key(actionLookupKey, 2); |
| 469 | SkyKey actionKeyUnchanging = ActionExecutionValue.key(actionLookupKey, 3); |
| 470 | SkyKey actionKeyLast = ActionExecutionValue.key(actionLookupKey, 4); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 471 | differencer.inject( |
| 472 | ImmutableMap.<SkyKey, SkyValue>of( |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 473 | actionKey1, |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 474 | actionValueWithTreeArtifacts(ImmutableList.of(file11)), |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 475 | actionKey2, |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 476 | actionValueWithTreeArtifacts(ImmutableList.of(file21, file22)), |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 477 | actionKeyEmpty, |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 478 | actionValueWithEmptyDirectory(outEmpty), |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 479 | actionKeyUnchanging, |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 480 | actionValueWithEmptyDirectory(outUnchanging), |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 481 | actionKeyLast, |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 482 | actionValueWithEmptyDirectory(last))); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 483 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 484 | assertThat( |
| 485 | driver |
| 486 | .evaluate(ImmutableList.<SkyKey>of(), false, 1, NullEventHandler.INSTANCE) |
| 487 | .hasError()) |
| 488 | .isFalse(); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 489 | assertThat(new FilesystemValueChecker(null, null).getDirtyActionValues(evaluator.getValues(), |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 490 | batchStatter, ModifiedFileSet.EVERYTHING_MODIFIED)).isEmpty(); |
| 491 | |
| 492 | // Touching the TreeArtifact directory should have no effect |
| 493 | FileSystemUtils.touchFile(out1.getPath()); |
| 494 | assertThat( |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 495 | new FilesystemValueChecker(null, null).getDirtyActionValues(evaluator.getValues(), |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 496 | batchStatter, ModifiedFileSet.EVERYTHING_MODIFIED)).isEmpty(); |
| 497 | // Neither should touching a subdirectory. |
| 498 | FileSystemUtils.touchFile(out2.getPath().getChild("subdir")); |
| 499 | assertThat( |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 500 | new FilesystemValueChecker(null, null).getDirtyActionValues(evaluator.getValues(), |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 501 | batchStatter, ModifiedFileSet.EVERYTHING_MODIFIED)).isEmpty(); |
| 502 | |
| 503 | /* **** Tests for directories **** */ |
| 504 | |
| 505 | // Removing a directory (even if empty) should have an effect |
| 506 | outEmpty.getPath().delete(); |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 507 | assertThat( |
| 508 | new FilesystemValueChecker(null, null) |
| 509 | .getDirtyActionValues( |
| 510 | evaluator.getValues(), |
| 511 | batchStatter, |
| 512 | new ModifiedFileSet.Builder().modify(outEmpty.getExecPath()).build())) |
| 513 | .containsExactly(actionKeyEmpty); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 514 | // Symbolic links should count as dirty |
| 515 | Path dummyEmptyDir = fs.getPath("/bin").getRelative("symlink"); |
| 516 | FileSystemUtils.createDirectoryAndParents(dummyEmptyDir); |
| 517 | FileSystemUtils.ensureSymbolicLink(outEmpty.getPath(), dummyEmptyDir); |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 518 | assertThat( |
| 519 | new FilesystemValueChecker(null, null) |
| 520 | .getDirtyActionValues( |
| 521 | evaluator.getValues(), |
| 522 | batchStatter, |
| 523 | new ModifiedFileSet.Builder().modify(outEmpty.getExecPath()).build())) |
| 524 | .containsExactly(actionKeyEmpty); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 525 | |
| 526 | // We're done fiddling with this... restore the original state |
| 527 | outEmpty.getPath().delete(); |
| 528 | FileSystemUtils.deleteTree(dummyEmptyDir); |
| 529 | FileSystemUtils.createDirectoryAndParents(outEmpty.getPath()); |
| 530 | |
| 531 | /* **** Tests for files and directory contents ****/ |
| 532 | |
| 533 | // Test that file contents matter. This is covered by existing tests already, |
| 534 | // so it's just a sanity check. |
| 535 | FileSystemUtils.writeContentAsLatin1(file11.getPath(), "goodbye"); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 536 | assertThat( |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 537 | new FilesystemValueChecker(null, null) |
| 538 | .getDirtyActionValues( |
| 539 | evaluator.getValues(), |
| 540 | batchStatter, |
| 541 | new ModifiedFileSet.Builder().modify(file11.getExecPath()).build())) |
| 542 | .containsExactly(actionKey1); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 543 | |
| 544 | // Test that directory contents (and nested contents) matter |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 545 | Artifact out1new = treeFileArtifact(out1, "julius/caesar"); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 546 | FileSystemUtils.createDirectoryAndParents(out1.getPath().getChild("julius")); |
| 547 | FileSystemUtils.writeContentAsLatin1(out1new.getPath(), "octavian"); |
| 548 | // even for empty directories |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 549 | Artifact outEmptyNew = treeFileArtifact(outEmpty, "marcus"); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 550 | FileSystemUtils.writeContentAsLatin1(outEmptyNew.getPath(), "aurelius"); |
| 551 | // so does removing |
| 552 | file21.getPath().delete(); |
| 553 | // now, let's test our changes are actually visible |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 554 | assertThat( |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 555 | new FilesystemValueChecker(null, null) |
| 556 | .getDirtyActionValues( |
| 557 | evaluator.getValues(), batchStatter, ModifiedFileSet.EVERYTHING_MODIFIED)) |
| 558 | .containsExactly(actionKey1, actionKey2, actionKeyEmpty); |
| 559 | assertThat( |
| 560 | new FilesystemValueChecker(null, null) |
| 561 | .getDirtyActionValues( |
| 562 | evaluator.getValues(), |
| 563 | batchStatter, |
| 564 | new ModifiedFileSet.Builder() |
| 565 | .modify(file21.getExecPath()) |
| 566 | .modify(out1new.getExecPath()) |
| 567 | .modify(outEmptyNew.getExecPath()) |
| 568 | .build())) |
| 569 | .containsExactly(actionKey1, actionKey2, actionKeyEmpty); |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 570 | // We also check that if the modified file set does not contain our modified files on disk, |
| 571 | // we are not going to check and return them. |
| 572 | assertThat( |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 573 | new FilesystemValueChecker(null, null) |
| 574 | .getDirtyActionValues( |
| 575 | evaluator.getValues(), |
| 576 | batchStatter, |
| 577 | new ModifiedFileSet.Builder() |
| 578 | .modify(file21.getExecPath()) |
| 579 | .modify(outEmptyNew.getExecPath()) |
| 580 | .build())) |
| 581 | .containsExactly(actionKey2, actionKeyEmpty); |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 582 | assertThat( |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 583 | new FilesystemValueChecker(null, null) |
| 584 | .getDirtyActionValues( |
| 585 | evaluator.getValues(), |
| 586 | batchStatter, |
| 587 | new ModifiedFileSet.Builder() |
| 588 | .modify(file21.getExecPath()) |
| 589 | .modify(out1new.getExecPath()) |
| 590 | .build())) |
| 591 | .containsExactly(actionKey1, actionKey2); |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 592 | // Check modifying the last (lexicographically) tree artifact. |
| 593 | last.getPath().delete(); |
| 594 | assertThat( |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 595 | new FilesystemValueChecker(null, null) |
| 596 | .getDirtyActionValues( |
| 597 | evaluator.getValues(), |
| 598 | batchStatter, |
| 599 | new ModifiedFileSet.Builder() |
| 600 | .modify(file21.getExecPath()) |
| 601 | .modify(out1new.getExecPath()) |
| 602 | .modify(last.getExecPath()) |
| 603 | .build())) |
| 604 | .containsExactly(actionKey1, actionKey2, actionKeyLast); |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 605 | // Check ModifiedFileSet without the last (lexicographically) tree artifact. |
| 606 | assertThat( |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 607 | new FilesystemValueChecker(null, null) |
| 608 | .getDirtyActionValues( |
| 609 | evaluator.getValues(), |
| 610 | batchStatter, |
| 611 | new ModifiedFileSet.Builder() |
| 612 | .modify(file21.getExecPath()) |
| 613 | .modify(out1new.getExecPath()) |
| 614 | .build())) |
| 615 | .containsExactly(actionKey1, actionKey2); |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 616 | // Restore |
| 617 | last.getPath().delete(); |
| 618 | FileSystemUtils.createDirectoryAndParents(last.getPath()); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 619 | // We add a test for NOTHING_MODIFIED, because FileSystemValueChecker doesn't |
| 620 | // pay attention to file sets for TreeArtifact directory listings. |
| 621 | assertThat( |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 622 | new FilesystemValueChecker(null, null).getDirtyActionValues(evaluator.getValues(), |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 623 | batchStatter, ModifiedFileSet.NOTHING_MODIFIED)).isEmpty(); |
| 624 | } |
| 625 | |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 626 | private Artifact createDerivedArtifact(String relPath) throws IOException { |
| 627 | Path outputPath = fs.getPath("/bin"); |
| 628 | outputPath.createDirectory(); |
| 629 | return new Artifact( |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 630 | outputPath.getRelative(relPath), ArtifactRoot.asDerivedRoot(fs.getPath("/"), outputPath)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 631 | } |
| 632 | |
cpeyser | ac09f0a | 2018-02-05 09:33:15 -0800 | [diff] [blame] | 633 | private SpecialArtifact createTreeArtifact(String relPath) throws IOException { |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 634 | Path outputDir = fs.getPath("/bin"); |
| 635 | Path outputPath = outputDir.getRelative(relPath); |
| 636 | outputDir.createDirectory(); |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 637 | ArtifactRoot derivedRoot = ArtifactRoot.asDerivedRoot(fs.getPath("/"), outputDir); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 638 | return new SpecialArtifact( |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 639 | derivedRoot, |
| 640 | derivedRoot.getExecPath().getRelative(derivedRoot.getRoot().relativize(outputPath)), |
janakr | 192c890 | 2018-01-19 22:29:42 -0800 | [diff] [blame] | 641 | ArtifactOwner.NullArtifactOwner.INSTANCE, |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 642 | SpecialArtifactType.TREE); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 645 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 646 | public void testDirtyActions() throws Exception { |
| 647 | checkDirtyActions(null, false); |
| 648 | } |
| 649 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 650 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 651 | public void testDirtyActionsBatchStat() throws Exception { |
| 652 | checkDirtyActions( |
| 653 | new BatchStat() { |
| 654 | @Override |
| 655 | public List<FileStatusWithDigest> batchStat( |
| 656 | boolean useDigest, boolean includeLinks, Iterable<PathFragment> paths) |
| 657 | throws IOException { |
| 658 | List<FileStatusWithDigest> stats = new ArrayList<>(); |
| 659 | for (PathFragment pathFrag : paths) { |
| 660 | stats.add( |
| 661 | FileStatusWithDigestAdapter.adapt( |
tomlu | 6c91906 | 2018-01-11 17:32:09 -0800 | [diff] [blame] | 662 | fs.getPath("/").getRelative(pathFrag).statIfFound(Symlinks.NOFOLLOW))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 663 | } |
| 664 | return stats; |
| 665 | } |
| 666 | }, |
| 667 | false); |
| 668 | } |
| 669 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 670 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 671 | public void testDirtyActionsBatchStatWithDigest() throws Exception { |
| 672 | checkDirtyActions( |
| 673 | new BatchStat() { |
| 674 | @Override |
| 675 | public List<FileStatusWithDigest> batchStat( |
| 676 | boolean useDigest, boolean includeLinks, Iterable<PathFragment> paths) |
| 677 | throws IOException { |
| 678 | List<FileStatusWithDigest> stats = new ArrayList<>(); |
| 679 | for (PathFragment pathFrag : paths) { |
tomlu | 6c91906 | 2018-01-11 17:32:09 -0800 | [diff] [blame] | 680 | final Path path = fs.getPath("/").getRelative(pathFrag); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 681 | stats.add(statWithDigest(path, path.statIfFound(Symlinks.NOFOLLOW))); |
| 682 | } |
| 683 | return stats; |
| 684 | } |
| 685 | }, |
| 686 | true); |
| 687 | } |
| 688 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 689 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 690 | public void testDirtyActionsBatchStatFallback() throws Exception { |
| 691 | checkDirtyActions( |
| 692 | new BatchStat() { |
| 693 | @Override |
| 694 | public List<FileStatusWithDigest> batchStat( |
| 695 | boolean useDigest, boolean includeLinks, Iterable<PathFragment> paths) |
| 696 | throws IOException { |
| 697 | throw new IOException("try again"); |
| 698 | } |
| 699 | }, |
| 700 | false); |
| 701 | } |
| 702 | |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 703 | @Test |
| 704 | public void testDirtyTreeArtifactActions() throws Exception { |
| 705 | checkDirtyTreeArtifactActions(null); |
| 706 | } |
| 707 | |
| 708 | @Test |
| 709 | public void testDirtyTreeArtifactActionsBatchStat() throws Exception { |
| 710 | checkDirtyTreeArtifactActions( |
| 711 | new BatchStat() { |
| 712 | @Override |
| 713 | public List<FileStatusWithDigest> batchStat( |
| 714 | boolean useDigest, boolean includeLinks, Iterable<PathFragment> paths) |
| 715 | throws IOException { |
| 716 | List<FileStatusWithDigest> stats = new ArrayList<>(); |
| 717 | for (PathFragment pathFrag : paths) { |
| 718 | stats.add( |
| 719 | FileStatusWithDigestAdapter.adapt( |
tomlu | 6c91906 | 2018-01-11 17:32:09 -0800 | [diff] [blame] | 720 | fs.getPath("/").getRelative(pathFrag).statIfFound(Symlinks.NOFOLLOW))); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 721 | } |
| 722 | return stats; |
| 723 | } |
| 724 | }); |
| 725 | } |
| 726 | |
| 727 | // TODO(bazel-team): Add some tests for FileSystemValueChecker#changedKeys*() methods. |
| 728 | // Presently these appear to be untested. |
| 729 | |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 730 | private ActionExecutionValue actionValue(Action action, boolean forceDigest) { |
| 731 | Map<Artifact, FileValue> artifactData = new HashMap<>(); |
| 732 | for (Artifact output : action.getOutputs()) { |
| 733 | try { |
| 734 | Path path = output.getPath(); |
| 735 | FileStatusWithDigest stat = |
| 736 | forceDigest ? statWithDigest(path, path.statIfFound(Symlinks.NOFOLLOW)) : null; |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 737 | artifactData.put(output, |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 738 | ActionMetadataHandler.fileValueFromArtifact(output, stat, null)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 739 | } catch (IOException e) { |
| 740 | throw new IllegalStateException(e); |
| 741 | } |
| 742 | } |
Michael Thvedt | 8d5a7bb | 2016-02-09 03:06:34 +0000 | [diff] [blame] | 743 | return new ActionExecutionValue( |
| 744 | artifactData, |
| 745 | ImmutableMap.<Artifact, TreeArtifactValue>of(), |
kush | 2ce45a2 | 2018-05-02 14:15:37 -0700 | [diff] [blame] | 746 | ImmutableMap.<Artifact, FileArtifactValue>of(), |
| 747 | /*outputSymlinks=*/ null); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 750 | private ActionExecutionValue actionValueWithEmptyDirectory(Artifact emptyDir) { |
| 751 | TreeArtifactValue emptyValue = TreeArtifactValue.create |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 752 | (ImmutableMap.<TreeFileArtifact, FileArtifactValue>of()); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 753 | |
| 754 | return new ActionExecutionValue( |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 755 | ImmutableMap.<Artifact, FileValue>of(), |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 756 | ImmutableMap.of(emptyDir, emptyValue), |
kush | 2ce45a2 | 2018-05-02 14:15:37 -0700 | [diff] [blame] | 757 | ImmutableMap.<Artifact, FileArtifactValue>of(), |
| 758 | /*outputSymlinks=*/ null); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 761 | private ActionExecutionValue actionValueWithTreeArtifacts(List<TreeFileArtifact> contents) { |
| 762 | Map<Artifact, FileValue> fileData = new HashMap<>(); |
| 763 | Map<Artifact, Map<TreeFileArtifact, FileArtifactValue>> directoryData = new HashMap<>(); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 764 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 765 | for (TreeFileArtifact output : contents) { |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 766 | try { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 767 | Map<TreeFileArtifact, FileArtifactValue> dirDatum = |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 768 | directoryData.get(output.getParent()); |
| 769 | if (dirDatum == null) { |
| 770 | dirDatum = new HashMap<>(); |
| 771 | directoryData.put(output.getParent(), dirDatum); |
| 772 | } |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 773 | FileValue fileValue = ActionMetadataHandler.fileValueFromArtifact(output, null, null); |
Janak Ramakrishnan | cba1645 | 2016-07-29 02:17:02 +0000 | [diff] [blame] | 774 | dirDatum.put(output, FileArtifactValue.create(output, fileValue)); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 775 | fileData.put(output, fileValue); |
| 776 | } catch (IOException e) { |
| 777 | throw new IllegalStateException(e); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | Map<Artifact, TreeArtifactValue> treeArtifactData = new HashMap<>(); |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 782 | for (Map.Entry<Artifact, Map<TreeFileArtifact, FileArtifactValue>> dirDatum : |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 783 | directoryData.entrySet()) { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 784 | treeArtifactData.put(dirDatum.getKey(), TreeArtifactValue.create(dirDatum.getValue())); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 785 | } |
| 786 | |
kush | 2ce45a2 | 2018-05-02 14:15:37 -0700 | [diff] [blame] | 787 | return new ActionExecutionValue( |
| 788 | fileData, |
| 789 | treeArtifactData, |
| 790 | ImmutableMap.<Artifact, FileArtifactValue>of(), |
| 791 | /*outputSymlinks=*/ null); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 794 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 795 | public void testPropagatesRuntimeExceptions() throws Exception { |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 796 | Collection<SkyKey> values = |
| 797 | ImmutableList.of( |
| 798 | FileValue.key( |
| 799 | RootedPath.toRootedPath(Root.fromPath(pkgRoot), PathFragment.create("foo")))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 800 | driver.evaluate( |
| 801 | values, false, SkyframeExecutor.DEFAULT_THREAD_COUNT, NullEventHandler.INSTANCE); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 802 | FilesystemValueChecker checker = new FilesystemValueChecker(null, null); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 803 | |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 804 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 805 | |
| 806 | fs.statThrowsRuntimeException = true; |
| 807 | try { |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 808 | getDirtyFilesystemKeys(evaluator, checker); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 809 | fail(); |
| 810 | } catch (RuntimeException e) { |
| 811 | assertThat(e).hasMessage("bork"); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | private static void assertEmptyDiff(Diff diff) { |
| 816 | assertDiffWithNewValues(diff); |
| 817 | } |
| 818 | |
| 819 | private static void assertDiffWithNewValues(Diff diff, SkyKey... keysWithNewValues) { |
| 820 | assertThat(diff.changedKeysWithoutNewValues()).isEmpty(); |
| 821 | assertThat(diff.changedKeysWithNewValues().keySet()) |
| 822 | .containsExactlyElementsIn(Arrays.asList(keysWithNewValues)); |
| 823 | } |
| 824 | |
| 825 | private class MockFileSystem extends InMemoryFileSystem { |
| 826 | |
| 827 | boolean statThrowsRuntimeException; |
| 828 | boolean readlinkThrowsIoException; |
| 829 | |
| 830 | MockFileSystem() { |
| 831 | super(); |
| 832 | } |
| 833 | |
| 834 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 835 | public FileStatus stat(Path path, boolean followSymlinks) throws IOException { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 836 | if (statThrowsRuntimeException) { |
| 837 | throw new RuntimeException("bork"); |
| 838 | } |
| 839 | return super.stat(path, followSymlinks); |
| 840 | } |
| 841 | |
| 842 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 843 | protected PathFragment readSymbolicLink(Path path) throws IOException { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 844 | if (readlinkThrowsIoException) { |
| 845 | throw new IOException("readlink failed"); |
| 846 | } |
| 847 | return super.readSymbolicLink(path); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | private static FileStatusWithDigest statWithDigest(final Path path, final FileStatus stat) { |
| 852 | return new FileStatusWithDigest() { |
| 853 | @Nullable |
| 854 | @Override |
| 855 | public byte[] getDigest() throws IOException { |
olaola | bfd1d33 | 2017-06-19 16:55:24 -0400 | [diff] [blame] | 856 | return path.getDigest(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | @Override |
| 860 | public boolean isFile() { |
| 861 | return stat.isFile(); |
| 862 | } |
| 863 | |
| 864 | @Override |
| 865 | public boolean isSpecialFile() { |
| 866 | return stat.isSpecialFile(); |
| 867 | } |
| 868 | |
| 869 | @Override |
| 870 | public boolean isDirectory() { |
| 871 | return stat.isDirectory(); |
| 872 | } |
| 873 | |
| 874 | @Override |
| 875 | public boolean isSymbolicLink() { |
| 876 | return stat.isSymbolicLink(); |
| 877 | } |
| 878 | |
| 879 | @Override |
| 880 | public long getSize() throws IOException { |
| 881 | return stat.getSize(); |
| 882 | } |
| 883 | |
| 884 | @Override |
| 885 | public long getLastModifiedTime() throws IOException { |
| 886 | return stat.getLastModifiedTime(); |
| 887 | } |
| 888 | |
| 889 | @Override |
| 890 | public long getLastChangeTime() throws IOException { |
| 891 | return stat.getLastChangeTime(); |
| 892 | } |
| 893 | |
| 894 | @Override |
| 895 | public long getNodeId() throws IOException { |
| 896 | return stat.getNodeId(); |
| 897 | } |
| 898 | }; |
| 899 | } |
| 900 | |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 901 | private static Diff getDirtyFilesystemKeys(MemoizingEvaluator evaluator, |
| 902 | FilesystemValueChecker checker) throws InterruptedException { |
| 903 | return checker.getDirtyKeys(evaluator.getValues(), new BasicFilesystemDirtinessChecker()); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 904 | } |
| 905 | } |