Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AbstractDependencyTracker #3510

Merged
merged 3 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public Path getLibraryPath() {

@Override
public void processWatchEvent(WatchService.Kind kind, Path path) {
File file = path.toFile();
File file = libraryPath.resolve(path).toFile();
if (!file.isHidden() && (kind == DELETE || (file.canRead() && (kind == CREATE || kind == MODIFY)))) {
dependencyChanged(file.getPath());
dependencyChanged(file.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
Expand All @@ -40,15 +43,27 @@
public class AbstractScriptDependencyTrackerTest {

private static final String WATCH_DIR = "test";
private static final Path DEPENDENCY = Path.of("depFile");
private static final Path DEPENDENCY2 = Path.of("depFile2");

private @NonNullByDefault({}) AbstractScriptDependencyTracker scriptDependencyTracker;
private @Mock @NonNullByDefault({}) WatchService watchServiceMock;
private @NonNullByDefault({}) @TempDir Path rootWatchPath;
private @NonNullByDefault({}) Path depPath;
private @NonNullByDefault({}) Path depPath2;

@BeforeEach
public void setup() {
when(watchServiceMock.getWatchPath()).thenReturn(Path.of(""));
public void setup() throws IOException {
when(watchServiceMock.getWatchPath()).thenReturn(rootWatchPath);
scriptDependencyTracker = new AbstractScriptDependencyTracker(watchServiceMock, WATCH_DIR) {
};

depPath = rootWatchPath.resolve(WATCH_DIR).resolve(DEPENDENCY);
depPath2 = rootWatchPath.resolve(WATCH_DIR).resolve(DEPENDENCY2);

Files.createFile(depPath);

Files.createFile(depPath2);
}

@AfterEach
Expand All @@ -58,7 +73,7 @@ public void tearDown() {

@Test
public void testScriptLibraryWatcherIsCreatedAndActivated() {
verify(watchServiceMock).registerListener(eq(scriptDependencyTracker), eq(Path.of(WATCH_DIR)));
verify(watchServiceMock).registerListener(eq(scriptDependencyTracker), eq(rootWatchPath.resolve(WATCH_DIR)));
}

@Test
Expand All @@ -69,15 +84,15 @@ public void testScriptLibraryWatchersIsDeactivatedOnShutdown() {
}

@Test
public void testDependencyChangeIsForwardedToMultipleListeners() {
public void testDependencyChangeIsForwardedToMultipleListeners() throws IOException {
ScriptDependencyTracker.Listener listener1 = mock(ScriptDependencyTracker.Listener.class);
ScriptDependencyTracker.Listener listener2 = mock(ScriptDependencyTracker.Listener.class);

scriptDependencyTracker.addChangeTracker(listener1);
scriptDependencyTracker.addChangeTracker(listener2);

scriptDependencyTracker.startTracking("scriptId", "depPath");
scriptDependencyTracker.dependencyChanged("depPath");
scriptDependencyTracker.startTracking("scriptId", depPath.toString());
scriptDependencyTracker.processWatchEvent(WatchService.Kind.CREATE, DEPENDENCY);

verify(listener1).onDependencyChange(eq("scriptId"));
verify(listener2).onDependencyChange(eq("scriptId"));
Expand All @@ -91,10 +106,9 @@ public void testDependencyChangeIsForwardedForMultipleScriptIds() {

scriptDependencyTracker.addChangeTracker(listener);

scriptDependencyTracker.startTracking("scriptId1", "depPath");
scriptDependencyTracker.startTracking("scriptId2", "depPath");

scriptDependencyTracker.dependencyChanged("depPath");
scriptDependencyTracker.startTracking("scriptId1", depPath.toString());
scriptDependencyTracker.startTracking("scriptId2", depPath.toString());
scriptDependencyTracker.processWatchEvent(WatchService.Kind.MODIFY, DEPENDENCY);

verify(listener).onDependencyChange(eq("scriptId1"));
verify(listener).onDependencyChange(eq("scriptId2"));
Expand All @@ -107,11 +121,10 @@ public void testDependencyChangeIsForwardedForMultipleDependencies() {

scriptDependencyTracker.addChangeTracker(listener);

scriptDependencyTracker.startTracking("scriptId", "depPath1");
scriptDependencyTracker.startTracking("scriptId", "depPath2");

scriptDependencyTracker.dependencyChanged("depPath1");
scriptDependencyTracker.dependencyChanged("depPath2");
scriptDependencyTracker.startTracking("scriptId", depPath.toString());
scriptDependencyTracker.startTracking("scriptId", depPath2.toString());
scriptDependencyTracker.processWatchEvent(WatchService.Kind.MODIFY, DEPENDENCY);
scriptDependencyTracker.processWatchEvent(WatchService.Kind.DELETE, DEPENDENCY2);

verify(listener, times(2)).onDependencyChange(eq("scriptId"));
verifyNoMoreInteractions(listener);
Expand All @@ -123,10 +136,10 @@ public void testDependencyChangeIsForwardedForCorrectDependencies() {

scriptDependencyTracker.addChangeTracker(listener);

scriptDependencyTracker.startTracking("scriptId1", "depPath1");
scriptDependencyTracker.startTracking("scriptId2", "depPath2");
scriptDependencyTracker.startTracking("scriptId1", depPath.toString());
scriptDependencyTracker.startTracking("scriptId2", depPath2.toString());

scriptDependencyTracker.dependencyChanged("depPath1");
scriptDependencyTracker.processWatchEvent(WatchService.Kind.CREATE, DEPENDENCY);

verify(listener).onDependencyChange(eq("scriptId1"));
verifyNoMoreInteractions(listener);
Expand Down