Skip to content

Commit d848fc1

Browse files
authored
Merge pull request #18 from codacy/improve-config-file-lookup
Improve config file lookup
2 parents cdb45c9 + 707ecce commit d848fc1

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ libraryDependencies ++= Seq(
2020
"com.typesafe.play" %% "play-json" % "2.4.8",
2121
"org.scalatest" %% "scalatest" % "2.2.4" % "test",
2222
"com.codacy" %% "codacy-plugins-api" % "1.0.11" withSources(),
23-
"com.github.pathikrit" %% "better-files" % "2.17.1" withSources()
23+
"com.github.pathikrit" %% "better-files" % "3.5.0" withSources()
2424
)
2525

2626
organizationName := "Codacy"

src/main/scala/codacy/dockerApi/utils/FileHelper.scala

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,22 @@ object FileHelper {
3636
(these ++ these.filter(_.isDirectory).flatMap(recursiveListFiles)).toList
3737
}
3838

39-
def findConfigurationFile(candidates: Set[String], path: Path): Option[Path] = {
40-
candidates.flatMap { nativeConfigFileName =>
41-
better.files.File(path).listRecursively
42-
.filter(f => f.name == nativeConfigFileName)
43-
.map(_.path)
44-
}
45-
.to[List]
46-
.sortBy(_.toString.length)
47-
.headOption
39+
/**
40+
* Find the configuration file path
41+
*
42+
* @param root path to search recursively
43+
* @param configFileNames to match the files while searching
44+
* @param maxDepth to search
45+
* @return config file path closest to the root
46+
*/
47+
def findConfigurationFile(root: Path, configFileNames: Set[String], maxDepth: Int = 5): Option[Path] = {
48+
val allFiles = better.files.File(root).walk(maxDepth = maxDepth)
49+
50+
val configFiles: List[Path] = configFileNames.flatMap { nativeConfigFileName =>
51+
allFiles.filter(_.name == nativeConfigFileName).map(_.path)
52+
}(collection.breakOut)
53+
54+
configFiles.sortBy(_.toString.length).headOption
4855
}
4956

5057
}

src/test/scala/codacy/dockerApi/utils/FileHelperTest.scala

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package codacy.dockerApi.utils
22

3+
import java.nio.file.Path
4+
5+
import better.files.File
36
import org.scalatest._
47

58
class FileHelperTest extends FlatSpec with Matchers {
@@ -46,8 +49,40 @@ class FileHelperTest extends FlatSpec with Matchers {
4649
"FileHelper" should "createTmpFile" in {
4750
val fileTmp = FileHelper.createTmpFile("foo", "prefix", ".ext").toString
4851

49-
java.nio.file.Paths.get(fileTmp).getFileName.toString should startWith("prefix")
52+
java.nio.file.Paths.get(fileTmp).getFileName.toString should startWith(
53+
"prefix")
5054
fileTmp should endWith(".ext")
5155
io.Source.fromFile(fileTmp).mkString should be("foo")
5256
}
57+
58+
"FileHelper#findConfigurationFile" should "find the configuration file closest to the root" in {
59+
(for {
60+
root <- File.temporaryDirectory()
61+
} yield {
62+
root./("test.json").write("content")
63+
64+
val configFile: Option[Path] =
65+
FileHelper.findConfigurationFile(root.path,
66+
configFileNames = Set("test.json"))
67+
68+
configFile should be(Option(root./("test.json").path))
69+
}).get()
70+
}
71+
72+
"FileHelper#findConfigurationFile" should "not find the configuration file closest to the root if its deeper then 5 in the path" in {
73+
(for {
74+
root <- File.temporaryDirectory()
75+
} yield {
76+
val subDirectory: File = root / "one" / "two" / "three" / "four" / "five"
77+
subDirectory.createDirectories()
78+
subDirectory./("test.json").write("content")
79+
80+
val configFile: Option[Path] =
81+
FileHelper.findConfigurationFile(root.path,
82+
configFileNames = Set("test.json"))
83+
84+
configFile should be(Option.empty[Path])
85+
}).get()
86+
}
87+
5388
}

0 commit comments

Comments
 (0)