Testing plugin action using testData, but no repository found
已回答
Hello. I am trying to test a plugin action that is only visible if there is git installed for a project. I created a directory in testdata that has mock git data (with configs, refs, etc.), and somewhere in my test I rename the mock git directory to .git in another temporary directory when setting up the test.
I tried using BasePlatformTestCase so that I can use configureByFile() to refer to the temporary directory. However, my tests fail because GitRepository.getRepositories() returns an empty list when using Git4Idea functions in my source code, resulting in null.
I don't know if I'm misunderstanding the documentation or Git4Idea source code, so I was wondering if anyone could help me with this.
Thanks in advance
请先登录再写评论。
I'm sorry, but I couldn't find GitRepository.getRepositories() which you refer. Did you mean GitRepositoryManager.getRepositories()?
Can you share your code?
Hi Jakub,
My apologies, yes I meant to say GitRepositoryManager.getRepositories().
I'll try to simplify how my code looks at the moment:
MyAction.java
public class MyAction extends AnAction {
...
public class MyAction extends AnAction {
@Override
public void update(AnActionEvent e) {
GitRepository repo = MyUtilFile.getCurrentRepository(e.getProject());
e.getPresentation().setVisible(repo != null);
}
}
}
MyUtilFile.java
public class MyUtilFile {
...
public static GitRepository getCurrentRepository(Project project) {
// Returns null here in my test because no repositories are found
if (GitRepositoryManager.getInstance(project).getRepositories().isEmpty()) {
return null;
}
return GitRepositoryManager.getInstance(project)
.getRepositories()
.get(0);
}
}
MyActionTest.java
public class MyActionTest extends BasePlatformTestCase {
@Override
public void setUp() throws Exception {
super.setUp();
String thisDir = System.getProperty("user.dir");
String path = createTempDirTestFixture().getTempDirPath();
File src = new File(thisDir + "/src/test/testData/exampleProject");
File dst = new File(path);
FileUtils.copyDirectory(src, dst);
// exampleProject has a gitdir inside with mock git data
File oldGDir = new File(path + "/gitdir");
File newGitDir = new File(path + "/.git");
// Attempting to rename gitdir to .git
oldGitDir.renameTo(newGitDir);
}
@Override
protected TempDirTestFixture createTempDirTestFixture() {
return super.createTempDirTestFixture();
}
@Override
protected Project getProject() {
return super.getProject();
}
@Override
public String getTestDataPath() {
return "src/test/testdata/exampleProject";
}
public void testIsVisibleWithGitRepo() {
myFixture.configureByFile("src/file.txt");
AnAction action = new MyAction();
Presentation presentation = myFixture.testAction(action);
assertTrue(presentation.isVisible());
Can you try, after renaming the GIT directory to .git, call the following method?
It is supposed to refresh the information about the repository.
Hi sorry for the late follow-up (timezones).
When running the following, repo is still null:
I also tried newGitDir.getAbsolutePath() to get the virtual file using findFileByPath. But not even with this a repo instance is defined. However, I can confirm that the renaming does work as intended. Here is how the path dst.getAbsolutePath() looks like: file:///Users/svorden/git/myproject/temp:/root
It's better to explicitly make sure that IDE knows about renamed directory, see VfsUtil.markDirtyAndRefresh().
Main issue is that test root needs to be registered in vcs mappings (the ones from "Settings | Version Control" tab).
This can be done using ProjectLevelVcsManager.getInstance(project).setDirectoryMappings(singletonList(new VcsDirectoryMapping(virtualFile.getPath(), GitVcs.NAME))).
See GitSingleRepoTest in IJ-community sources as an example.
Hi Aleksey,
By running the code you provided using ProjectLevelVcsManager, repo is no longer null. Thanks! This is how my code looks like:
I'm now facing an issue though when I run my test. I don't know if it's related to my test though and I'd like some of your input:
Memory leak might be caused by second createTempDirTestFixture call. Using 'myFixture.getTempDirPath()' should fix the issue.
Also, you're using in-memory mock VFS paths together with java.io.File. This is likely to cause issues.
UPD: It should not be an issue, as "temp:///" file system is disabled by default.