20
20
import com .goide .GoModuleType ;
21
21
import com .goide .project .GoApplicationLibrariesService ;
22
22
import com .goide .project .GoModuleLibrariesInitializer ;
23
+ import com .intellij .openapi .application .ApplicationManager ;
23
24
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 .*;
28
26
import com .intellij .openapi .roots .impl .OrderEntryUtil ;
29
27
import com .intellij .openapi .roots .impl .libraries .LibraryEx ;
30
28
import com .intellij .openapi .util .io .FileUtil ;
@@ -54,21 +52,56 @@ public ModuleType getModuleType() {
54
52
}
55
53
};
56
54
}
55
+ private final Collection <VirtualFile > contentRootsToClean = ContainerUtil .newHashSet ();
56
+ private final Collection <VirtualFile > tempRootsToClean = ContainerUtil .newHashSet ();
57
57
58
58
@ Override
59
59
protected void setUp () throws Exception {
60
60
super .setUp ();
61
61
GoModuleLibrariesInitializer .setTestingMode (getTestRootDisposable ());
62
62
}
63
63
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
+
64
97
/**
65
98
* src <content root>
66
99
* goPath <gopath>
67
100
* - src
68
101
* -- test
69
102
*/
70
103
public void testAddGoPathAsLibrary () throws IOException {
71
- VirtualFile goPath = VfsUtil . findFileByIoFile ( FileUtil . createTempDirectory ( "go" , "path" ), true );
104
+ VirtualFile goPath = createGoPath ( );
72
105
VirtualFile goPathContent = goPath .createChildDirectory (this , "src" ).createChildDirectory (this , "test" );
73
106
GoApplicationLibrariesService .getInstance ().setLibraryRootUrls (goPath .getUrl ());
74
107
assertLibrary (Collections .singletonList (goPathContent .getUrl ()), "temp:///src" );
@@ -82,7 +115,7 @@ public void testAddGoPathAsLibrary() throws IOException {
82
115
* -- notContentRoot
83
116
*/
84
117
public void testExcludeChildContentRootFromLibrary () throws IOException {
85
- VirtualFile goPath = VfsUtil . findFileByIoFile ( FileUtil . createTempDirectory ( "go" , "path" ), true );
118
+ VirtualFile goPath = createGoPath ( );
86
119
VirtualFile src = goPath .createChildDirectory (this , "src" );
87
120
VirtualFile contentRoot = src .createChildDirectory (this , "contentRoot" );
88
121
VirtualFile notContentRoot = src .createChildDirectory (this , "notContentRoot" );
@@ -102,11 +135,11 @@ public void testExcludeChildContentRootFromLibrary() throws IOException {
102
135
* --- test
103
136
*/
104
137
public void testExcludeParentContentRootFromLibrary () throws IOException {
105
- VirtualFile contentRoot = VfsUtil . findFileByIoFile ( FileUtil . createTempDirectory ( "go" , "contentRoot" ), true );
138
+ VirtualFile contentRoot = createGoPath ( );
106
139
VirtualFile goPath = contentRoot .createChildDirectory (this , "gopath" );
107
140
VirtualFile goPathContent = goPath .createChildDirectory (this , "src" ).createChildDirectory (this , "test" );
108
141
109
- VirtualFile otherGoPath = VfsUtil . findFileByIoFile ( FileUtil . createTempDirectory ( "go" , "otherGoPath" ), true );
142
+ VirtualFile otherGoPath = createGoPath ( );
110
143
VirtualFile otherGoPathContent = otherGoPath .createChildDirectory (this , "src" ).createChildDirectory (this , "test" );
111
144
GoApplicationLibrariesService .getInstance ().setLibraryRootUrls (goPath .getUrl (), otherGoPath .getUrl ());
112
145
assertLibrary (ContainerUtil .newHashSet (goPathContent .getUrl (), otherGoPathContent .getUrl ()), "temp:///src" );
@@ -123,7 +156,7 @@ public void testExcludeParentContentRootFromLibrary() throws IOException {
123
156
* --- contentRoot <content root>
124
157
*/
125
158
public void testUpdateLibraryOnAddingContentRoot () throws IOException {
126
- VirtualFile goPath = VfsUtil . findFileByIoFile ( FileUtil . createTempDirectory ( "go" , "path" ), true );
159
+ VirtualFile goPath = createGoPath ( );
127
160
VirtualFile goPathContent = goPath .createChildDirectory (this , "src" ).createChildDirectory (this , "subdir" );
128
161
129
162
GoApplicationLibrariesService .getInstance ().setLibraryRootUrls (goPath .getUrl ());
@@ -136,8 +169,9 @@ public void testUpdateLibraryOnAddingContentRoot() throws IOException {
136
169
137
170
private void addContentRoot (@ NotNull VirtualFile contentRoot ) {
138
171
ModifiableRootModel model = ModuleRootManager .getInstance (myModule ).getModifiableModel ();
139
- try {
172
+ try {
140
173
model .addContentEntry (contentRoot );
174
+ contentRootsToClean .add (contentRoot );
141
175
model .commit ();
142
176
}
143
177
finally {
@@ -161,6 +195,12 @@ private void assertLibrary(@NotNull Collection<String> libUrls, String... exclus
161
195
assertSameElements (library .getExcludedRootUrls (), exclusionUrls );
162
196
}
163
197
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
+
164
204
@ Override
165
205
protected boolean isWriteActionRequired () {
166
206
return true ;
0 commit comments