Skip to content

Commit

Permalink
Scan files in (project) root directory even when it is hidden (#2377)
Browse files Browse the repository at this point in the history
Fix searching from inside a hidden directory

When searching files like this:
```
mkdir .hidden_folder
cd .hidden_folder
touch File.kt
ktlint '*.kt'
```
ktlint reported that it found no files.
The fact that the current directory is hidden should have no effect.
---------

Co-authored-by: paul-dingemans <paul-dingemans@users.noreply.github.com>
  • Loading branch information
kitterion and paul-dingemans committed Nov 26, 2023
1 parent 352c0c4 commit c2697f6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,13 @@ internal fun FileSystem.fileSequence(
dirAttr: BasicFileAttributes,
): FileVisitResult =
if (Files.isHidden(dirPath)) {
LOGGER.trace { "- Dir: $dirPath: Ignore" }
FileVisitResult.SKIP_SUBTREE
if (dirPath == commonRootDir) {
LOGGER.trace { "- Dir: $dirPath: Traverse started from hidden directory" }
FileVisitResult.CONTINUE
} else {
LOGGER.trace { "- Dir: $dirPath: Ignore traversal of hidden directory" }
FileVisitResult.SKIP_SUBTREE
}
} else {
LOGGER.trace { "- Dir: $dirPath: Traverse" }
FileVisitResult.CONTINUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ internal class FileUtilsTest {
private val javaFileRootDirectory = "Root.java"
private val ktFileRootDirectory = "Root.kt"
private val ktsFileRootDirectory = "Root.kts"
private val javaFileInHiddenDirectory = "project1/.git/Ignored.java"
private val ktFileInHiddenDirectory = "project1/.git/Ignored.kt"
private val ktsFileInHiddenDirectory = "project1/.git/Ignored.kts"
private val javaFileInHiddenDirectory = "project1/.hidden/Ignored.java"
private val ktFileInHiddenDirectory = "project1/.hidden/Ignored.kt"
private val ktsFileInHiddenDirectory = "project1/.hidden/Ignored.kts"
private val javaFileInProjectRootDirectory = "project1/ProjectRoot.java"
private val ktFileInProjectRootDirectory = "project1/ProjectRoot.kt"
private val ktsFileInProjectRootDirectory = "project1/ProjectRoot.kts"
Expand Down Expand Up @@ -106,6 +106,17 @@ internal class FileUtilsTest {
)
}

@Test
fun `Given the root directory where scanning starts is hidden, then the patterns should work`() {
val foundFiles =
getFiles(
patterns = listOf("*.kt"),
rootDir = ktlintTestFileSystem.resolve("project1/.hidden"),
)

assertThat(foundFiles).containsExactlyInAnyOrder(ktFileInHiddenDirectory)
}

@Test
fun `Given some patterns including a negate pattern and no workdir then select all files except files in the negate pattern`() {
val foundFiles =
Expand Down

0 comments on commit c2697f6

Please sign in to comment.