Skip to content

Commit 0169be7

Browse files
committed
Add a test to see if a file move/rename between nested dirs is properly observed
1 parent d97a184 commit 0169be7

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/test/java/engineering/swat/watch/SmokeTests.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,62 @@ void watchSingleFile() throws IOException {
112112
await("Single file change").untilTrue(changed);
113113
}
114114
}
115+
116+
@Test
117+
void moveRegularFileBetweenNestedDirectories() throws IOException {
118+
var parent = testDir.getTestDirectory();
119+
var child1 = Files.createDirectories(parent.resolve("from"));
120+
var child2 = Files.createDirectories(parent.resolve("to"));
121+
var file = Files.createFile(child1.resolve("file.txt"));
122+
123+
var parentWatchBookkeeper = new TestHelper.Bookkeeper();
124+
var parentWatchConfig = Watcher
125+
.watch(parent, WatchScope.PATH_AND_ALL_DESCENDANTS)
126+
.on(parentWatchBookkeeper);
127+
128+
var child1WatchBookkeeper = new TestHelper.Bookkeeper();
129+
var child1WatchConfig = Watcher
130+
.watch(child1, WatchScope.PATH_AND_CHILDREN)
131+
.on(child1WatchBookkeeper);
132+
133+
var child2WatchBookkeeper = new TestHelper.Bookkeeper();
134+
var child2WatchConfig = Watcher
135+
.watch(child2, WatchScope.PATH_AND_CHILDREN)
136+
.on(child2WatchBookkeeper);
137+
138+
var fileWatchBookkeeper = new TestHelper.Bookkeeper();
139+
var fileWatchConfig = Watcher
140+
.watch(file, WatchScope.PATH_ONLY)
141+
.on(fileWatchBookkeeper);
142+
143+
try (var parentWatch = parentWatchConfig.start();
144+
var child1Watch = child1WatchConfig.start();
145+
var child2Watch = child2WatchConfig.start();
146+
var fileWatch = fileWatchConfig.start()) {
147+
148+
var source = child1.resolve(file.getFileName());
149+
var target = child2.resolve(file.getFileName());
150+
Files.move(source, target);
151+
152+
await("Move should be observed as delete by `parent` watch (file tree)")
153+
.until(() -> parentWatchBookkeeper
154+
.events().kind(DELETED).rootPath(parent).relativePath(parent.relativize(source)).any());
155+
156+
await("Move should be observed as create by `parent` watch (file tree)")
157+
.until(() -> parentWatchBookkeeper
158+
.events().kind(CREATED).rootPath(parent).relativePath(parent.relativize(target)).any());
159+
160+
await("Move should be observed as delete by `child1` watch (single directory)")
161+
.until(() -> child1WatchBookkeeper
162+
.events().kind(DELETED).rootPath(child1).relativePath(child1.relativize(source)).any());
163+
164+
await("Move should be observed as create by `child2` watch (single directory)")
165+
.until(() -> child2WatchBookkeeper
166+
.events().kind(CREATED).rootPath(child2).relativePath(child2.relativize(target)).any());
167+
168+
await("Move should be observed as delete by `file` watch (regular file)")
169+
.until(() -> fileWatchBookkeeper
170+
.events().kind(DELETED).rootPath(source).any());
171+
}
172+
}
115173
}

src/test/java/engineering/swat/watch/TestHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class TestHelper {
4848
var os = System.getProperty("os.name", "?").toLowerCase();
4949
if (os.contains("mac")) {
5050
// OSX is SLOW on it's watches
51-
delayFactor *= 2;
51+
delayFactor *= 3;
5252
}
5353
else if (os.contains("win")) {
5454
// windows watches can be slow to get everything

0 commit comments

Comments
 (0)