-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for autosyncing on file changes
Refactors QuerySyncAsyncFileListener to facilitate testing. Factory methods are added to the listener and sync requester, which also handle subscribing to the relevant message system, so the classes themselves no longer depend on a disposable. QuerySyncAsyncFileListener now takes a SyncRequester, and callbacks for determining project paths and auto-sync settings. Factory methods derive these from relevant components. Path checking is moved into the requiresSync method for clarity. Adds unit tests for verifying sync requests for file events. PiperOrigin-RevId: 591048704
- Loading branch information
1 parent
3b7014d
commit 041f740
Showing
3 changed files
with
207 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
...sts/integrationtests/com/google/idea/blaze/base/qsync/QuerySyncAsyncFileListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright 2023 The Bazel Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.idea.blaze.base.qsync; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.mockito.Mockito.atLeastOnce; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import com.google.idea.blaze.base.qsync.QuerySyncAsyncFileListener.SyncRequester; | ||
import com.intellij.openapi.application.WriteAction; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.openapi.vfs.VirtualFileManager; | ||
import com.intellij.testFramework.LightPlatformTestCase; | ||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase4; | ||
import java.nio.file.Path; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class QuerySyncAsyncFileListenerTest extends LightJavaCodeInsightFixtureTestCase4 { | ||
|
||
private static final Path SOURCE_ROOT = Path.of("my/project"); | ||
|
||
private SyncRequester mockSyncRequester; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
getFixture() | ||
.addFileToProject( | ||
SOURCE_ROOT.resolve("java/com/example/Class1.java").toString(), | ||
"package com.example;public class Class1 {}"); | ||
getFixture() | ||
.addFileToProject( | ||
SOURCE_ROOT.resolve("BUILD").toString(), "java_library(name=\"java\",srcs=[])"); | ||
|
||
getFixture() | ||
.getTempDirFixture() | ||
.findOrCreateDir(SOURCE_ROOT.resolve("submodule/java/com/example").toString()); | ||
mockSyncRequester = mock(SyncRequester.class); | ||
} | ||
|
||
private void setupAutoSyncListener() { | ||
setupListener(true); | ||
} | ||
|
||
private void setupNoAutoSyncListener() { | ||
setupListener(false); | ||
} | ||
|
||
private void setupListener(boolean autoSync) { | ||
Path projectSourceRoot = | ||
Path.of(LightPlatformTestCase.getSourceRoot().getPath()).resolve(SOURCE_ROOT); | ||
QuerySyncAsyncFileListener fileListener = | ||
new QuerySyncAsyncFileListener( | ||
mockSyncRequester, path -> path.startsWith(projectSourceRoot), () -> autoSync); | ||
VirtualFileManager.getInstance() | ||
.addAsyncFileListener(fileListener, getFixture().getTestRootDisposable()); | ||
} | ||
|
||
@Test | ||
public void projectFileAdded_requestsSync() { | ||
setupAutoSyncListener(); | ||
|
||
getFixture() | ||
.addFileToProject( | ||
SOURCE_ROOT.resolve("java/com/example/Class2.java").toString(), | ||
"package com.example;public class Class2{}"); | ||
verify(mockSyncRequester, atLeastOnce()).requestSync(); | ||
} | ||
|
||
@Test | ||
public void projectFileAdded_autoSyncDisabled_neverRequestsSync() { | ||
setupNoAutoSyncListener(); | ||
|
||
getFixture() | ||
.addFileToProject( | ||
SOURCE_ROOT.resolve("java/com/example/Class2.java").toString(), | ||
"package com.example;public class Class2{}"); | ||
verify(mockSyncRequester, never()).requestSync(); | ||
} | ||
|
||
@Test | ||
public void nonProjectFileAdded_neverRequestsSync() { | ||
setupAutoSyncListener(); | ||
|
||
getFixture() | ||
.addFileToProject( | ||
"some/other/path/Class1.java", "package some.other.path;public class Class1{}"); | ||
verify(mockSyncRequester, never()).requestSync(); | ||
} | ||
|
||
@Test | ||
public void projectFileMoved_requestsSync() { | ||
setupAutoSyncListener(); | ||
|
||
WriteAction.runAndWait( | ||
() -> | ||
getFixture() | ||
.moveFile( | ||
SOURCE_ROOT.resolve("java/com/example/Class1.java").toString(), | ||
SOURCE_ROOT.resolve("submodule/java/com/example").toString())); | ||
verify(mockSyncRequester, atLeastOnce()).requestSync(); | ||
} | ||
|
||
@Test | ||
public void projectFileModified_nonBuildFile_doesNotRequestSync() throws Exception { | ||
setupAutoSyncListener(); | ||
|
||
VirtualFile vf = | ||
getFixture() | ||
.findFileInTempDir(SOURCE_ROOT.resolve("java/com/example/Class1.java").toString()); | ||
|
||
WriteAction.runAndWait( | ||
() -> | ||
vf.setBinaryContent( | ||
"/**LICENSE-TEXT*/package com.example;public class Class1{}".getBytes(UTF_8))); | ||
|
||
verify(mockSyncRequester, never()).requestSync(); | ||
} | ||
|
||
@Test | ||
public void projectFileModified_buildFile_requestsSync() throws Exception { | ||
setupAutoSyncListener(); | ||
|
||
VirtualFile vf = getFixture().findFileInTempDir(SOURCE_ROOT.resolve("BUILD").toString()); | ||
|
||
WriteAction.runAndWait( | ||
() -> | ||
vf.setBinaryContent( | ||
"/**LICENSE-TEXT*/java_library(name=\"javalib\",srcs=[])".getBytes(UTF_8))); | ||
|
||
verify(mockSyncRequester, atLeastOnce()).requestSync(); | ||
} | ||
} |