1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.scanner.scan.filesystem;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.Optional;
import org.apache.commons.lang3.SystemUtils;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.scanner.bootstrap.SonarUserHome;
import org.sonar.scanner.scan.ModuleConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class HiddenFilesVisitorHelperTest {
@ClassRule
public static TemporaryFolder temp = new TemporaryFolder();
private static final SonarUserHome sonarUserHome = mock(SonarUserHome.class);
private static final DefaultInputModule inputModule = mock(DefaultInputModule.class);
private final ModuleConfiguration moduleConfig = mock(ModuleConfiguration.class);
private final HiddenFilesProjectData hiddenFilesProjectData = spy(new HiddenFilesProjectData(sonarUserHome));
private HiddenFilesVisitorHelper underTest;
@BeforeClass
public static void setUp() throws IOException {
File userHomeFolder = temp.newFolder(".userhome");
setAsHiddenOnWindows(userHomeFolder);
when(sonarUserHome.getPath()).thenReturn(userHomeFolder.toPath());
File workDir = temp.newFolder(".sonar");
setAsHiddenOnWindows(workDir);
when(inputModule.getWorkDir()).thenReturn(workDir.toPath());
}
@Before
public void before() {
hiddenFilesProjectData.clearHiddenFilesData();
underTest = spy(new HiddenFilesVisitorHelper(hiddenFilesProjectData, inputModule, moduleConfig));
}
@Test
public void verifyDefaultOnConstruction() {
assertThat(underTest.excludeHiddenFiles).isFalse();
assertThat(underTest.rootHiddenDir).isNull();
}
@Test
public void excludeHiddenFilesShouldBeSetToFalseFromConfigurationWhenNotConfigured() {
when(moduleConfig.getBoolean("sonar.scanner.excludeHiddenFiles")).thenReturn(Optional.empty());
HiddenFilesVisitorHelper configuredVisitorHelper = spy(new HiddenFilesVisitorHelper(hiddenFilesProjectData, inputModule, moduleConfig));
assertThat(configuredVisitorHelper.excludeHiddenFiles).isFalse();
}
@Test
public void excludeHiddenFilesShouldBeSetToFalseFromConfigurationWhenDisabled() {
when(moduleConfig.getBoolean("sonar.scanner.excludeHiddenFiles")).thenReturn(Optional.of(false));
HiddenFilesVisitorHelper configuredVisitorHelper = spy(new HiddenFilesVisitorHelper(hiddenFilesProjectData, inputModule, moduleConfig));
assertThat(configuredVisitorHelper.excludeHiddenFiles).isFalse();
}
@Test
public void excludeHiddenFilesShouldBeSetToTrueFromConfigurationWhenEnabled() {
when(moduleConfig.getBoolean("sonar.scanner.excludeHiddenFiles")).thenReturn(Optional.of(true));
HiddenFilesVisitorHelper configuredVisitorHelper = spy(new HiddenFilesVisitorHelper(hiddenFilesProjectData, inputModule, moduleConfig));
assertThat(configuredVisitorHelper.excludeHiddenFiles).isTrue();
}
@Test
public void shouldVisitHiddenDirectory() throws IOException {
File hiddenDir = temp.newFolder(".hiddenVisited");
setAsHiddenOnWindows(hiddenDir);
boolean visitDir = underTest.shouldVisitDir(hiddenDir.toPath());
assertThat(visitDir).isTrue();
assertThat(underTest.insideHiddenDirectory()).isTrue();
assertThat(underTest.rootHiddenDir).isEqualTo(hiddenDir.toPath());
verify(underTest).enterHiddenDirectory(hiddenDir.toPath());
}
@Test
public void shouldNotVisitHiddenDirectoryWhenHiddenFilesVisitIsExcluded() throws IOException {
when(moduleConfig.getBoolean("sonar.scanner.excludeHiddenFiles")).thenReturn(Optional.of(true));
HiddenFilesVisitorHelper configuredVisitorHelper = spy(new HiddenFilesVisitorHelper(hiddenFilesProjectData, inputModule, moduleConfig));
File hidden = temp.newFolder(".hiddenNotVisited");
setAsHiddenOnWindows(hidden);
boolean visitDir = configuredVisitorHelper.shouldVisitDir(hidden.toPath());
assertThat(visitDir).isFalse();
assertThat(configuredVisitorHelper.insideHiddenDirectory()).isFalse();
verify(configuredVisitorHelper, never()).enterHiddenDirectory(any());
}
@Test
public void shouldVisitNonHiddenDirectoryWhenHiddenFilesVisitIsExcluded() throws IOException {
when(moduleConfig.getBoolean("sonar.scanner.excludeHiddenFiles")).thenReturn(Optional.of(true));
HiddenFilesVisitorHelper configuredVisitorHelper = spy(new HiddenFilesVisitorHelper(hiddenFilesProjectData, inputModule, moduleConfig));
File nonHiddenFolder = temp.newFolder();
boolean visitDir = configuredVisitorHelper.shouldVisitDir(nonHiddenFolder.toPath());
assertThat(visitDir).isTrue();
assertThat(configuredVisitorHelper.insideHiddenDirectory()).isFalse();
verify(configuredVisitorHelper, never()).enterHiddenDirectory(any());
}
@Test
public void shouldVisitNonHiddenDirectory() throws IOException {
File nonHiddenFolder = temp.newFolder();
boolean visitDir = underTest.shouldVisitDir(nonHiddenFolder.toPath());
assertThat(visitDir).isTrue();
assertThat(underTest.insideHiddenDirectory()).isFalse();
verify(underTest, never()).enterHiddenDirectory(any());
assertThat(underTest.excludeHiddenFiles).isFalse();
}
@Test
public void shouldNotVisitModuleWorkDir() throws IOException {
Path workingDirectory = inputModule.getWorkDir().toRealPath(LinkOption.NOFOLLOW_LINKS).toAbsolutePath().normalize();
boolean visitDir = underTest.shouldVisitDir(workingDirectory);
assertThat(visitDir).isFalse();
assertThat(underTest.insideHiddenDirectory()).isFalse();
verify(underTest, never()).enterHiddenDirectory(any());
}
@Test
public void shouldNotVisitSonarUserHome() throws IOException {
Path userHome = sonarUserHome.getPath().toRealPath(LinkOption.NOFOLLOW_LINKS).toAbsolutePath().normalize();
boolean visitDir = underTest.shouldVisitDir(userHome);
assertThat(visitDir).isFalse();
assertThat(underTest.insideHiddenDirectory()).isFalse();
verify(underTest, never()).enterHiddenDirectory(any());
}
@Test
public void hiddenFileShouldBeVisited() throws IOException {
File hiddenFile = temp.newFile(".hiddenFileShouldBeVisited");
setAsHiddenOnWindows(hiddenFile);
assertThat(underTest.insideHiddenDirectory()).isFalse();
boolean visitFile = underTest.shouldVisitFile(hiddenFile.toPath());
assertThat(visitFile).isTrue();
verify(hiddenFilesProjectData).markAsHiddenFile(hiddenFile.toPath(), inputModule);
}
@Test
public void nonHiddenFileShouldBeVisitedInHiddenFolder() throws IOException {
File hidden = temp.newFolder(".hiddenFolder");
setAsHiddenOnWindows(hidden);
File nonHiddenFile = temp.newFile();
underTest.shouldVisitDir(hidden.toPath());
assertThat(underTest.insideHiddenDirectory()).isTrue();
boolean shouldVisitFile = underTest.shouldVisitFile(nonHiddenFile.toPath());
assertThat(shouldVisitFile).isTrue();
verify(hiddenFilesProjectData).markAsHiddenFile(nonHiddenFile.toPath(), inputModule);
}
@Test
public void shouldNotSetAsRootHiddenDirectoryWhenAlreadyEnteredHiddenDirectory() throws IOException {
File hidden = temp.newFolder(".outerHiddenFolder");
File nestedHiddenFolder = temp.newFolder(".outerHiddenFolder", ".nestedHiddenFolder");
setAsHiddenOnWindows(hidden);
setAsHiddenOnWindows(nestedHiddenFolder);
underTest.shouldVisitDir(hidden.toPath());
assertThat(underTest.insideHiddenDirectory()).isTrue();
boolean shouldVisitNestedDir = underTest.shouldVisitDir(nestedHiddenFolder.toPath());
assertThat(shouldVisitNestedDir).isTrue();
assertThat(underTest.rootHiddenDir).isEqualTo(hidden.toPath());
verify(underTest).enterHiddenDirectory(nestedHiddenFolder.toPath());
}
@Test
public void hiddenFileShouldNotBeVisitedWhenHiddenFileVisitExcluded() throws IOException {
when(moduleConfig.getBoolean("sonar.scanner.excludeHiddenFiles")).thenReturn(Optional.of(true));
HiddenFilesVisitorHelper configuredVisitorHelper = spy(new HiddenFilesVisitorHelper(hiddenFilesProjectData, inputModule, moduleConfig));
File hiddenFile = temp.newFile(".hiddenFileNotVisited");
setAsHiddenOnWindows(hiddenFile);
assertThat(configuredVisitorHelper.insideHiddenDirectory()).isFalse();
configuredVisitorHelper.shouldVisitFile(hiddenFile.toPath());
boolean shouldVisitFile = configuredVisitorHelper.shouldVisitFile(hiddenFile.toPath());
assertThat(shouldVisitFile).isFalse();
verify(hiddenFilesProjectData, never()).markAsHiddenFile(hiddenFile.toPath(), inputModule);
}
@Test
public void shouldCorrectlyExitHiddenFolderOnlyOnHiddenFolderThatEntered() throws IOException {
File hiddenFolder = temp.newFolder(".hiddenRootFolder");
setAsHiddenOnWindows(hiddenFolder);
boolean shouldVisitDir = underTest.shouldVisitDir(hiddenFolder.toPath());
assertThat(shouldVisitDir).isTrue();
assertThat(underTest.insideHiddenDirectory()).isTrue();
assertThat(underTest.rootHiddenDir).isEqualTo(hiddenFolder.toPath());
verify(underTest).enterHiddenDirectory(hiddenFolder.toPath());
File folder1 = temp.newFolder(".hiddenRootFolder", "myFolderExit");
File folder2 = temp.newFolder("myFolderExit");
File folder3 = temp.newFolder(".myFolderExit");
setAsHiddenOnWindows(folder3);
underTest.exitDirectory(folder1.toPath());
underTest.exitDirectory(folder2.toPath());
underTest.exitDirectory(folder3.toPath());
assertThat(underTest.insideHiddenDirectory()).isTrue();
assertThat(underTest.rootHiddenDir).isEqualTo(hiddenFolder.toPath());
verify(underTest, never()).resetRootHiddenDir();
underTest.exitDirectory(hiddenFolder.toPath());
assertThat(underTest.insideHiddenDirectory()).isFalse();
assertThat(underTest.rootHiddenDir).isNull();
verify(underTest).resetRootHiddenDir();
}
@Test
public void shouldNotInitiateResetRootDirWhenNotInHiddenDirectory() throws IOException {
File hiddenFolder = temp.newFolder(".hiddenFolderNonRoot");
setAsHiddenOnWindows(hiddenFolder);
underTest.exitDirectory(hiddenFolder.toPath());
verify(underTest, never()).resetRootHiddenDir();
}
@Test
public void filesShouldBeCorrectlyMarkedAsHidden() throws IOException {
File hiddenFolder = temp.newFolder(".hiddenFolderRoot");
setAsHiddenOnWindows(hiddenFolder);
File file1 = temp.newFile();
File file2 = temp.newFile();
File file3 = temp.newFile(".markedHiddenFile");
setAsHiddenOnWindows(file3);
File file4 = temp.newFile();
File file5 = temp.newFile(".markedHiddenFile2");
setAsHiddenOnWindows(file5);
underTest.shouldVisitFile(file1.toPath());
underTest.shouldVisitDir(hiddenFolder.toPath());
underTest.shouldVisitFile(file2.toPath());
underTest.shouldVisitFile(file3.toPath());
underTest.exitDirectory(hiddenFolder.toPath());
underTest.shouldVisitFile(file4.toPath());
underTest.shouldVisitFile(file5.toPath());
verify(hiddenFilesProjectData, never()).markAsHiddenFile(file1.toPath(), inputModule);
verify(hiddenFilesProjectData).markAsHiddenFile(file2.toPath(), inputModule);
verify(hiddenFilesProjectData).markAsHiddenFile(file3.toPath(), inputModule);
verify(hiddenFilesProjectData, never()).markAsHiddenFile(file4.toPath(), inputModule);
verify(hiddenFilesProjectData).markAsHiddenFile(file5.toPath(), inputModule);
}
private static void setAsHiddenOnWindows(File file) throws IOException {
if (SystemUtils.IS_OS_WINDOWS) {
Files.setAttribute(file.toPath(), "dos:hidden", true, LinkOption.NOFOLLOW_LINKS);
}
}
}
|