blob: a76184c989f05a28b0fde4c0f725e15b99a570e3 [file] [log] [blame]
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +00001// 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
15package com.google.devtools.build.lib.skyframe;
16
17import static com.google.common.truth.Truth.assertThat;
18
19import com.google.common.collect.ImmutableSet;
20import com.google.devtools.build.lib.skyframe.DiffAwareness.View;
Ulf Adamsde14ade2016-10-14 14:20:31 +000021import com.google.devtools.build.lib.skyframe.LocalDiffAwareness.Options;
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000022import com.google.devtools.build.lib.vfs.PathFragment;
Ulf Adamsde14ade2016-10-14 14:20:31 +000023import com.google.devtools.common.options.OptionsBase;
24import com.google.devtools.common.options.OptionsClassProvider;
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000025import java.io.IOException;
26import java.nio.charset.StandardCharsets;
27import java.nio.file.FileVisitResult;
28import java.nio.file.Files;
29import java.nio.file.Path;
30import java.nio.file.SimpleFileVisitor;
31import java.nio.file.attribute.BasicFileAttributes;
32import org.junit.After;
33import org.junit.Before;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.junit.runners.JUnit4;
37
38/** Tests for {@link MacOSXFsEventsDiffAwareness} */
39@RunWith(JUnit4.class)
40public 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 Adamsde14ade2016-10-14 14:20:31 +000063 private OptionsClassProvider watchFsEnabledProvider;
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000064
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 Adamsde14ade2016-10-14 14:20:31 +000069 LocalDiffAwareness.Options localDiffOptions = new LocalDiffAwareness.Options();
70 localDiffOptions.watchFS = true;
71 watchFsEnabledProvider = new LocalDiffAwarenessOptionsProvider(localDiffOptions);
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000072 }
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 Adamsde14ade2016-10-14 14:20:31 +0000110 View view1 = underTest.getCurrentView(watchFsEnabledProvider);
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +0000111 scratchFile("a/b/c");
112 scratchFile("b/c/d");
113 Thread.sleep(200); // Wait until the events propagate
Ulf Adamsde14ade2016-10-14 14:20:31 +0000114 View view2 = underTest.getCurrentView(watchFsEnabledProvider);
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +0000115 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 Adamsde14ade2016-10-14 14:20:31 +0000119 View view3 = underTest.getCurrentView(watchFsEnabledProvider);
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +0000120 assertDiff(view2, view3, "a", "a/b", "a/b/c", "b", "b/c", "b/c/d");
121 }
Ulf Adamsde14ade2016-10-14 14:20:31 +0000122
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-Guillerez2988e102016-10-13 20:29:41 +0000141}