-
Notifications
You must be signed in to change notification settings - Fork 363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Technical Debt: add unit test WildcardPatternFileProviderTest #1354
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
203 changes: 203 additions & 0 deletions
203
...ors/src/test/java/org/sonar/cxx/sensors/tests/dotnet/WildcardPatternFileProviderTest.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,203 @@ | ||
/* | ||
* Sonar C++ Plugin (Community) | ||
* Copyright (C) 2010-2017 SonarOpenCommunity | ||
* http://github.com/SonarOpenCommunity/sonar-cxx | ||
* | ||
* 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.cxx.sensors.tests.dotnet; | ||
//origin https://github.com/SonarSource/sonar-dotnet-tests-library/ | ||
|
||
import com.google.common.base.Joiner; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import java.io.File; | ||
import java.util.Set; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class WildcardPatternFileProviderTest { | ||
|
||
@Rule | ||
public TemporaryFolder tmp = new TemporaryFolder(); | ||
|
||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
@Before | ||
public void init() throws Exception { | ||
tmp.newFile("foo.txt"); | ||
tmp.newFile("bar.txt"); | ||
|
||
tmp.newFolder("a"); | ||
tmp.newFile(path("a", "foo.txt")); | ||
tmp.newFolder("a", "a21"); | ||
|
||
tmp.newFolder("b"); | ||
|
||
tmp.newFolder("c"); | ||
tmp.newFolder("c", "c21"); | ||
tmp.newFile(path("c", "c21", "foo.txt")); | ||
tmp.newFolder("c", "c22"); | ||
tmp.newFolder("c", "c22", "c31"); | ||
tmp.newFile(path("c", "c22", "c31", "foo.txt")); | ||
tmp.newFile(path("c", "c22", "c31", "bar.txt")); | ||
} | ||
|
||
@Test | ||
public void absolute_paths() { | ||
assertThat(listFiles(new File(tmp.getRoot(), "foo.txt").getAbsolutePath())) | ||
.containsOnly(new File(tmp.getRoot(), "foo.txt")); | ||
|
||
assertThat(listFiles(new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt")).getAbsolutePath())) | ||
.containsOnly(new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt"))); | ||
|
||
assertThat(listFiles(new File(tmp.getRoot(), "nonexisting.txt").getAbsolutePath())).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void absolute_paths_with_current_and_parent_folder_access() { | ||
assertThat(listFiles(new File(tmp.getRoot(), path("a", "..", ".", "foo.txt")).getAbsolutePath())) | ||
.containsOnly(new File(tmp.getRoot(), path("a", "..", ".", "foo.txt"))); | ||
|
||
assertThat(listFiles(new File(tmp.getRoot(), path("a", "..", ".", "foo.txt")).getAbsolutePath())) | ||
.containsOnly(new File(tmp.getRoot(), path("a", "..", ".", "foo.txt"))); | ||
|
||
assertThat(listFiles(new File(tmp.getRoot(), path("a", "..", ".", "foo.txt")).getAbsolutePath())) | ||
.containsOnly(new File(tmp.getRoot(), path("a", "..", ".", "foo.txt"))); | ||
} | ||
|
||
@Test | ||
public void absolute_paths_with_wildcards() { | ||
assertThat(listFiles(new File(tmp.getRoot(), "*.txt").getAbsolutePath())) | ||
.containsOnly(new File(tmp.getRoot(), "foo.txt"), new File(tmp.getRoot(), "bar.txt")); | ||
|
||
assertThat(listFiles(new File(tmp.getRoot(), "f*").getAbsolutePath())) | ||
.containsOnly(new File(tmp.getRoot(), "foo.txt")); | ||
|
||
assertThat(listFiles(new File(tmp.getRoot(), "fo?.txt").getAbsolutePath())) | ||
.containsOnly(new File(tmp.getRoot(), "foo.txt")); | ||
|
||
assertThat(listFiles(new File(tmp.getRoot(), "foo?.txt").getAbsolutePath())).isEmpty(); | ||
|
||
assertThat(listFiles(new File(tmp.getRoot(), path("**", "foo.txt")).getAbsolutePath())) | ||
.containsOnly( | ||
new File(tmp.getRoot(), "foo.txt"), | ||
new File(tmp.getRoot(), path("a", "foo.txt")), | ||
new File(tmp.getRoot(), path("c", "c21", "foo.txt")), | ||
new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt"))); | ||
|
||
assertThat(listFiles(new File(tmp.getRoot(), path("**", "c31", "foo.txt")).getAbsolutePath())) | ||
.containsOnly(new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt"))); | ||
|
||
assertThat(listFiles(new File(tmp.getRoot(), path("**", "c?1", "foo.txt")).getAbsolutePath())) | ||
.containsOnly(new File(tmp.getRoot(), path("c", "c21", "foo.txt")), new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt"))); | ||
|
||
assertThat(listFiles(new File(tmp.getRoot(), path("?", "**", "foo.txt")).getAbsolutePath())) | ||
.containsOnly( | ||
new File(tmp.getRoot(), path("a", "foo.txt")), | ||
new File(tmp.getRoot(), path("c", "c21", "foo.txt")), | ||
new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt"))); | ||
} | ||
|
||
@Test | ||
public void relative_paths() { | ||
assertThat(listFiles("foo.txt", tmp.getRoot())) | ||
.containsOnly(new File(tmp.getRoot(), "foo.txt")); | ||
|
||
assertThat(listFiles(path("c", "c22", "c31", "foo.txt"), tmp.getRoot())) | ||
.containsOnly(new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt"))); | ||
|
||
assertThat(listFiles("nonexisting.txt", tmp.getRoot())).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void relative_paths_with_current_and_parent_folder_access() { | ||
assertThat(listFiles(path("..", "foo.txt"), new File(tmp.getRoot(), "a"))) | ||
.containsOnly(new File(new File(tmp.getRoot(), "a"), path("..", "foo.txt"))); | ||
|
||
assertThat(listFiles(path(".", "foo.txt"), tmp.getRoot())) | ||
.containsOnly(new File(tmp.getRoot(), path(".", "foo.txt"))); | ||
|
||
assertThat(listFiles(path("a", "..", "foo.txt"), tmp.getRoot())) | ||
.containsOnly(new File(tmp.getRoot(), path("a", "..", "foo.txt"))); | ||
} | ||
|
||
@Test | ||
public void relative_paths_with_wildcards() { | ||
assertThat(listFiles("*.txt", tmp.getRoot())) | ||
.containsOnly(new File(tmp.getRoot(), "foo.txt"), new File(tmp.getRoot(), "bar.txt")); | ||
|
||
assertThat(listFiles("f*", tmp.getRoot())) | ||
.containsOnly(new File(tmp.getRoot(), "foo.txt")); | ||
|
||
assertThat(listFiles("fo?.txt", tmp.getRoot())) | ||
.containsOnly(new File(tmp.getRoot(), "foo.txt")); | ||
|
||
assertThat(listFiles("foo?.txt", tmp.getRoot())).isEmpty(); | ||
|
||
assertThat(listFiles(path("**", "foo.txt"), tmp.getRoot())) | ||
.containsOnly( | ||
new File(tmp.getRoot(), "foo.txt"), | ||
new File(tmp.getRoot(), path("a", "foo.txt")), | ||
new File(tmp.getRoot(), path("c", "c21", "foo.txt")), | ||
new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt"))); | ||
|
||
assertThat(listFiles(path("**", "c31", "foo.txt"), tmp.getRoot())) | ||
.containsOnly(new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt"))); | ||
|
||
assertThat(listFiles(path("**", "c?1", "foo.txt"), tmp.getRoot())) | ||
.containsOnly(new File(tmp.getRoot(), path("c", "c21", "foo.txt")), new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt"))); | ||
|
||
assertThat(listFiles(path("?", "**", "foo.txt"), tmp.getRoot())) | ||
.containsOnly( | ||
new File(tmp.getRoot(), path("a", "foo.txt")), | ||
new File(tmp.getRoot(), path("c", "c21", "foo.txt")), | ||
new File(tmp.getRoot(), path("c", "c22", "c31", "foo.txt"))); | ||
} | ||
|
||
@Test | ||
public void should_fail_with_current_folder_access_after_wildcard() { | ||
thrown.expect(IllegalArgumentException.class); | ||
thrown.expectMessage("Cannot contain '.' or '..' after the first wildcard."); | ||
|
||
listFiles(new File(tmp.getRoot(), path("?", ".", "foo.txt")).getAbsolutePath()); | ||
} | ||
|
||
@Test | ||
public void should_fail_with_parent_folder_access_after_wildcard() { | ||
thrown.expect(IllegalArgumentException.class); | ||
thrown.expectMessage("Cannot contain '.' or '..' after the first wildcard."); | ||
|
||
listFiles(path("*", "..", "foo.txt"), tmp.getRoot()); | ||
} | ||
|
||
private static String path(String... elements) { | ||
return Joiner.on(File.separator).join(elements); | ||
} | ||
|
||
private static Set<File> listFiles(String pattern) { | ||
return listFiles(pattern, null); | ||
} | ||
|
||
private static Set<File> listFiles(String pattern, File baseDir) { | ||
return new WildcardPatternFileProvider(baseDir, File.separator).listFiles(pattern); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Bertk testing three times the same thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This finding is correct. I removed the redundant statements.