Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 1 | // Copyright 2016 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 | |
| 15 | package com.google.devtools.build.lib.skyframe; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.devtools.build.lib.skyframe.DiffAwareness.View; |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.skyframe.LocalDiffAwareness.Options; |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.vfs.PathFragment; |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 23 | import com.google.devtools.common.options.OptionsBase; |
| 24 | import com.google.devtools.common.options.OptionsClassProvider; |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 25 | import java.io.IOException; |
| 26 | import java.nio.charset.StandardCharsets; |
| 27 | import java.nio.file.FileVisitResult; |
| 28 | import java.nio.file.Files; |
| 29 | import java.nio.file.Path; |
| 30 | import java.nio.file.SimpleFileVisitor; |
| 31 | import java.nio.file.attribute.BasicFileAttributes; |
| 32 | import org.junit.After; |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | import org.junit.runners.JUnit4; |
| 37 | |
| 38 | /** Tests for {@link MacOSXFsEventsDiffAwareness} */ |
| 39 | @RunWith(JUnit4.class) |
| 40 | public class MacOSXFsEventsDiffAwarenessTest { |
| 41 | |
| 42 | private static void rmdirs(Path directory) throws IOException { |
| 43 | Files.walkFileTree( |
| 44 | directory, |
| 45 | new SimpleFileVisitor<Path>() { |
| 46 | @Override |
| 47 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) |
| 48 | throws IOException { |
| 49 | Files.delete(file); |
| 50 | return FileVisitResult.CONTINUE; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 55 | Files.delete(dir); |
| 56 | return FileVisitResult.CONTINUE; |
| 57 | } |
| 58 | }); |
| 59 | } |
| 60 | |
| 61 | private MacOSXFsEventsDiffAwareness underTest; |
| 62 | private Path watchedPath; |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 63 | private OptionsClassProvider watchFsEnabledProvider; |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 64 | |
| 65 | @Before |
| 66 | public void setUp() throws Exception { |
| 67 | watchedPath = com.google.common.io.Files.createTempDir().getCanonicalFile().toPath(); |
| 68 | underTest = new MacOSXFsEventsDiffAwareness(watchedPath.toString()); |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 69 | LocalDiffAwareness.Options localDiffOptions = new LocalDiffAwareness.Options(); |
| 70 | localDiffOptions.watchFS = true; |
| 71 | watchFsEnabledProvider = new LocalDiffAwarenessOptionsProvider(localDiffOptions); |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @After |
| 75 | public void tearDown() throws Exception { |
| 76 | underTest.close(); |
| 77 | rmdirs(watchedPath); |
| 78 | } |
| 79 | |
| 80 | private void scratchFile(String path, String content) throws IOException { |
| 81 | Path p = watchedPath.resolve(path); |
| 82 | p.getParent().toFile().mkdirs(); |
| 83 | com.google.common.io.Files.write(content.getBytes(StandardCharsets.UTF_8), p.toFile()); |
| 84 | } |
| 85 | |
| 86 | private void scratchFile(String path) throws IOException { |
| 87 | scratchFile(path, ""); |
| 88 | } |
| 89 | |
| 90 | private void assertDiff(View view1, View view2, Object... paths) |
| 91 | throws IncompatibleViewException, BrokenDiffAwarenessException { |
| 92 | ImmutableSet<PathFragment> modifiedSourceFiles = |
| 93 | underTest.getDiff(view1, view2).modifiedSourceFiles(); |
| 94 | ImmutableSet<String> toStringSourceFiles = toString(modifiedSourceFiles); |
| 95 | assertThat(toStringSourceFiles).containsExactly(paths); |
| 96 | } |
| 97 | |
| 98 | private static ImmutableSet<String> toString(ImmutableSet<PathFragment> modifiedSourceFiles) { |
| 99 | ImmutableSet.Builder<String> builder = ImmutableSet.builder(); |
| 100 | for (PathFragment path : modifiedSourceFiles) { |
| 101 | if (!path.toString().isEmpty()) { |
| 102 | builder.add(path.toString()); |
| 103 | } |
| 104 | } |
| 105 | return builder.build(); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testSimple() throws Exception { |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 110 | View view1 = underTest.getCurrentView(watchFsEnabledProvider); |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 111 | scratchFile("a/b/c"); |
| 112 | scratchFile("b/c/d"); |
| 113 | Thread.sleep(200); // Wait until the events propagate |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 114 | View view2 = underTest.getCurrentView(watchFsEnabledProvider); |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 115 | assertDiff(view1, view2, "a", "a/b", "a/b/c", "b", "b/c", "b/c/d"); |
| 116 | rmdirs(watchedPath.resolve("a")); |
| 117 | rmdirs(watchedPath.resolve("b")); |
| 118 | Thread.sleep(200); // Wait until the events propagate |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 119 | View view3 = underTest.getCurrentView(watchFsEnabledProvider); |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 120 | assertDiff(view2, view3, "a", "a/b", "a/b/c", "b", "b/c", "b/c/d"); |
| 121 | } |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * Only returns a fixed options class for {@link LocalDiffAwareness.Options}. |
| 125 | */ |
| 126 | private static final class LocalDiffAwarenessOptionsProvider implements OptionsClassProvider { |
| 127 | private final Options localDiffOptions; |
| 128 | |
| 129 | private LocalDiffAwarenessOptionsProvider(Options localDiffOptions) { |
| 130 | this.localDiffOptions = localDiffOptions; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public <O extends OptionsBase> O getOptions(Class<O> optionsClass) { |
| 135 | if (optionsClass.equals(LocalDiffAwareness.Options.class)) { |
| 136 | return optionsClass.cast(localDiffOptions); |
| 137 | } |
| 138 | return null; |
| 139 | } |
| 140 | } |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 141 | } |