Skip to content

Commit 17a4ac2

Browse files
committed
Fix tests in 171 branch
1 parent 889de6d commit 17a4ac2

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

tests/com/goide/sdk/GoPathLibraryTest.java

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
import com.goide.GoModuleType;
2121
import com.goide.project.GoApplicationLibrariesService;
2222
import com.goide.project.GoModuleLibrariesInitializer;
23+
import com.intellij.openapi.application.ApplicationManager;
2324
import com.intellij.openapi.module.ModuleType;
24-
import com.intellij.openapi.roots.LibraryOrderEntry;
25-
import com.intellij.openapi.roots.ModifiableRootModel;
26-
import com.intellij.openapi.roots.ModuleRootManager;
27-
import com.intellij.openapi.roots.OrderRootType;
25+
import com.intellij.openapi.roots.*;
2826
import com.intellij.openapi.roots.impl.OrderEntryUtil;
2927
import com.intellij.openapi.roots.impl.libraries.LibraryEx;
3028
import com.intellij.openapi.util.io.FileUtil;
@@ -54,21 +52,56 @@ public ModuleType getModuleType() {
5452
}
5553
};
5654
}
55+
private final Collection<VirtualFile> contentRootsToClean = ContainerUtil.newHashSet();
56+
private final Collection<VirtualFile> tempRootsToClean = ContainerUtil.newHashSet();
5757

5858
@Override
5959
protected void setUp() throws Exception {
6060
super.setUp();
6161
GoModuleLibrariesInitializer.setTestingMode(getTestRootDisposable());
6262
}
6363

64+
@Override
65+
protected void tearDown() throws Exception {
66+
try {
67+
for (VirtualFile file : tempRootsToClean) {
68+
ApplicationManager.getApplication().runWriteAction(() -> {
69+
try {
70+
file.delete(this);
71+
}
72+
catch (IOException e) {
73+
e.printStackTrace();
74+
}
75+
});
76+
}
77+
}
78+
finally {
79+
tempRootsToClean.clear();
80+
}
81+
ModifiableRootModel model = ModuleRootManager.getInstance(myModule).getModifiableModel();
82+
try {
83+
for (ContentEntry entry : model.getContentEntries()) {
84+
if (contentRootsToClean.contains(entry.getFile())) {
85+
model.removeContentEntry(entry);
86+
}
87+
}
88+
ApplicationManager.getApplication().runWriteAction(model::commit);
89+
}
90+
finally {
91+
contentRootsToClean.clear();
92+
if (!model.isDisposed()) model.dispose();
93+
}
94+
super.tearDown();
95+
}
96+
6497
/**
6598
* src <content root>
6699
* goPath <gopath>
67100
* - src
68101
* -- test
69102
*/
70103
public void testAddGoPathAsLibrary() throws IOException {
71-
VirtualFile goPath = VfsUtil.findFileByIoFile(FileUtil.createTempDirectory("go", "path"), true);
104+
VirtualFile goPath = createGoPath();
72105
VirtualFile goPathContent = goPath.createChildDirectory(this, "src").createChildDirectory(this, "test");
73106
GoApplicationLibrariesService.getInstance().setLibraryRootUrls(goPath.getUrl());
74107
assertLibrary(Collections.singletonList(goPathContent.getUrl()), "temp:///src");
@@ -82,7 +115,7 @@ public void testAddGoPathAsLibrary() throws IOException {
82115
* -- notContentRoot
83116
*/
84117
public void testExcludeChildContentRootFromLibrary() throws IOException {
85-
VirtualFile goPath = VfsUtil.findFileByIoFile(FileUtil.createTempDirectory("go", "path"), true);
118+
VirtualFile goPath = createGoPath();
86119
VirtualFile src = goPath.createChildDirectory(this, "src");
87120
VirtualFile contentRoot = src.createChildDirectory(this, "contentRoot");
88121
VirtualFile notContentRoot = src.createChildDirectory(this, "notContentRoot");
@@ -102,11 +135,11 @@ public void testExcludeChildContentRootFromLibrary() throws IOException {
102135
* --- test
103136
*/
104137
public void testExcludeParentContentRootFromLibrary() throws IOException {
105-
VirtualFile contentRoot = VfsUtil.findFileByIoFile(FileUtil.createTempDirectory("go", "contentRoot"), true);
138+
VirtualFile contentRoot = createGoPath();
106139
VirtualFile goPath = contentRoot.createChildDirectory(this, "gopath");
107140
VirtualFile goPathContent = goPath.createChildDirectory(this, "src").createChildDirectory(this, "test");
108141

109-
VirtualFile otherGoPath = VfsUtil.findFileByIoFile(FileUtil.createTempDirectory("go", "otherGoPath"), true);
142+
VirtualFile otherGoPath = createGoPath();
110143
VirtualFile otherGoPathContent = otherGoPath.createChildDirectory(this, "src").createChildDirectory(this, "test");
111144
GoApplicationLibrariesService.getInstance().setLibraryRootUrls(goPath.getUrl(), otherGoPath.getUrl());
112145
assertLibrary(ContainerUtil.newHashSet(goPathContent.getUrl(), otherGoPathContent.getUrl()), "temp:///src");
@@ -123,7 +156,7 @@ public void testExcludeParentContentRootFromLibrary() throws IOException {
123156
* --- contentRoot <content root>
124157
*/
125158
public void testUpdateLibraryOnAddingContentRoot() throws IOException {
126-
VirtualFile goPath = VfsUtil.findFileByIoFile(FileUtil.createTempDirectory("go", "path"), true);
159+
VirtualFile goPath = createGoPath();
127160
VirtualFile goPathContent = goPath.createChildDirectory(this, "src").createChildDirectory(this, "subdir");
128161

129162
GoApplicationLibrariesService.getInstance().setLibraryRootUrls(goPath.getUrl());
@@ -136,8 +169,9 @@ public void testUpdateLibraryOnAddingContentRoot() throws IOException {
136169

137170
private void addContentRoot(@NotNull VirtualFile contentRoot) {
138171
ModifiableRootModel model = ModuleRootManager.getInstance(myModule).getModifiableModel();
139-
try {
172+
try {
140173
model.addContentEntry(contentRoot);
174+
contentRootsToClean.add(contentRoot);
141175
model.commit();
142176
}
143177
finally {
@@ -161,6 +195,12 @@ private void assertLibrary(@NotNull Collection<String> libUrls, String... exclus
161195
assertSameElements(library.getExcludedRootUrls(), exclusionUrls);
162196
}
163197

198+
private VirtualFile createGoPath() throws IOException {
199+
VirtualFile goPath = VfsUtil.findFileByIoFile(FileUtil.createTempDirectory("go", "path"), true);
200+
tempRootsToClean.add(goPath);
201+
return goPath;
202+
}
203+
164204
@Override
165205
protected boolean isWriteActionRequired() {
166206
return true;

0 commit comments

Comments
 (0)