Skip to content

Commit 99159cb

Browse files
committed
Add tests for utils
1 parent 1bdd842 commit 99159cb

File tree

7 files changed

+152
-23
lines changed

7 files changed

+152
-23
lines changed

circle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
machine:
22
java:
33
version: oraclejdk8
4-
4+
55
test:
66
override:
77
- sbt clean coverage test

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ object CommandRunner {
1919

2020
val pio = new ProcessIO(_.close(), readStream(stdout), readStream(stderr))
2121

22-
val process = Process(cmd, dir).run(pio)
23-
val result = Try(process.exitValue())
22+
Try(Process(cmd, dir).run(pio)) match {
23+
case Success(process) =>
24+
Try(process.exitValue()) match {
25+
case Success(exitValue) =>
26+
Right(CommandResult(exitValue, stdout, stderr))
2427

25-
result match {
26-
case Success(exitValue) =>
27-
Right(CommandResult(exitValue, stdout, stderr))
28+
case Failure(e) =>
29+
process.destroy()
30+
Left(e)
31+
}
2832

2933
case Failure(e) =>
30-
process.destroy()
3134
Left(e)
3235
}
3336
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ object FileHelper {
2828

2929
def stripPath(filename: String, prefix: String): String = {
3030

31-
val FilenameRegex = s""".*$prefix/(.*)""".r
31+
val FilenameRegex = s""".*$prefix/*(.*)""".r
3232

3333
filename match {
3434
case FilenameRegex(res) => res;

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,29 @@ import codacy.dockerApi._
44

55
object ToolHelper {
66

7-
def getPatternsToLint(conf: Option[Seq[PatternDef]])(implicit spec: Spec): Seq[PatternDef] = {
8-
conf.fold(getDefaultConfFromSpec(spec)) {
7+
def getPatternsToLint(conf: Option[Seq[PatternDef]])(implicit spec: Spec): Option[Seq[PatternDef]] = {
8+
conf.map {
99
configuration =>
1010
configuration.map(pattern => getMissingParametersFromSpec(pattern))
1111
}
1212
}
1313

14-
private def convertToParametersDefFromSpec(parametersSpec: Option[Set[ParameterSpec]]): Option[Set[ParameterDef]] = {
15-
parametersSpec.map(params => params.map(param => ParameterDef(param.name, param.default)))
16-
}
17-
18-
private def getDefaultConfFromSpec(spec: Spec): Seq[PatternDef] = {
19-
spec.patterns.toSeq.map(pattern => PatternDef(pattern.patternId, convertToParametersDefFromSpec(pattern.parameters)))
20-
}
21-
22-
2314
private def buildParameterDefFromSpec(parameterSpec: ParameterSpec): ParameterDef = {
2415
ParameterDef(parameterSpec.name, parameterSpec.default)
2516
}
2617

27-
private def getParametersByPatternId(patternId: PatternId)(implicit spec: Spec): Set[ParameterDef] = {
18+
private def getParametersByPatternId(patternId: PatternId)(implicit spec: Spec): Option[Set[ParameterDef]] = {
2819
val specPattern = spec.patterns.find(_.patternId == patternId)
2920

3021
specPattern.flatMap {
3122
patternSpec =>
3223
patternSpec.parameters.map(params => params.map(buildParameterDefFromSpec))
33-
}.getOrElse(Set())
24+
}
3425
}
3526

3627
private def getMissingParametersFromSpec(pattern: PatternDef)(implicit spec: Spec): PatternDef = {
37-
val params = pattern.parameters.getOrElse(getParametersByPatternId(pattern.patternId))
38-
PatternDef(pattern.patternId, Some(params))
28+
val params = pattern.parameters.orElse(getParametersByPatternId(pattern.patternId))
29+
PatternDef(pattern.patternId, params)
3930
}
4031

4132
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package codacy.dockerApi.utils
2+
3+
import org.scalatest._
4+
import org.scalatest.EitherValues._
5+
6+
import scala.collection.mutable.ArrayBuffer
7+
8+
class CommandRunnerTest extends FlatSpec with Matchers {
9+
10+
val genericCMD = Seq("echo", "foo")
11+
val invalidCMD = Seq("rm", "nofile.ext")
12+
val errorCMD = Seq("rmzzz", "nofile.ext")
13+
14+
"CommandRunner" should "simpleEchoExec" in {
15+
val result: Either[Throwable, CommandResult] = CommandRunner.exec(genericCMD)
16+
17+
result.right.value.stdout should be(ArrayBuffer("foo"))
18+
result.right.value.exitCode should be(0)
19+
}
20+
21+
"CommandRunner" should "handleInvalidExec" in {
22+
val result: Either[Throwable, CommandResult] = CommandRunner.exec(invalidCMD)
23+
24+
result.right.value.stdout should be(ArrayBuffer())
25+
result.right.value.exitCode should be(1)
26+
}
27+
28+
"CommandRunner" should "handleErrorExec" in {
29+
val result: Either[Throwable, CommandResult] = CommandRunner.exec(errorCMD)
30+
31+
result should be('left)
32+
}
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package codacy.dockerApi.utils
2+
3+
import org.scalatest._
4+
5+
class FileHelperTest extends FlatSpec with Matchers {
6+
7+
"FileHelper" should "stripPath from String" in {
8+
val pathString: String = "a/b/c/filename.ext"
9+
val pathStringPrefix: String = "a/b"
10+
11+
val pathString2: String = "/a/b/filename.ext"
12+
val pathStringPrefix2: String = "/a/b/"
13+
14+
val result: String = FileHelper.stripPath(pathString, pathStringPrefix)
15+
result should be("c/filename.ext")
16+
17+
val result2: String = FileHelper.stripPath(pathString2, pathStringPrefix2)
18+
result2 should be("filename.ext")
19+
}
20+
21+
"FileHelper" should "stripPath from Path" in {
22+
val path: java.nio.file.Path = java.nio.file.Paths.get("a/b/c/filename.ext")
23+
val pathPrefix: java.nio.file.Path = java.nio.file.Paths.get("a/b")
24+
25+
val path2: java.nio.file.Path = java.nio.file.Paths.get("/a/b/filename.ext")
26+
val pathPrefix2: java.nio.file.Path = java.nio.file.Paths.get("/a/b/")
27+
28+
val result: String = FileHelper.stripPath(path, pathPrefix)
29+
result should be("c/filename.ext")
30+
31+
val result2: String = FileHelper.stripPath(path2, pathPrefix2)
32+
result2 should be("filename.ext")
33+
}
34+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package codacy.dockerApi.utils
2+
3+
import codacy.dockerApi._
4+
import org.scalatest._
5+
import play.api.libs.json.{JsNumber, JsString}
6+
7+
class ToolHelperTest extends FlatSpec with Matchers {
8+
9+
val paramSpec1 = ParameterSpec(ParameterName("param1"), JsNumber(2))
10+
val paramSpec2 = ParameterSpec(ParameterName("param2"), JsString("value2"))
11+
val patternSpec1 = PatternSpec(PatternId("id1"), Some(Set(paramSpec1, paramSpec2)))
12+
val patternSpec2 = PatternSpec(PatternId("id2"), None)
13+
val patternsSpec = Set(patternSpec1, patternSpec2)
14+
15+
val paramDef1 = ParameterDef(ParameterName("param1"), JsNumber(33))
16+
val paramDef2 = ParameterDef(ParameterName("param2"), JsString("value33"))
17+
val patternDef1 = PatternDef(PatternId("id1"), Some(Set(paramDef1, paramDef2)))
18+
val patternDef2 = PatternDef(PatternId("id2"), None)
19+
20+
val patternDef1NoParam = PatternDef(PatternId("id1"), None)
21+
val patternDef2NoParam = PatternDef(PatternId("id2"), None)
22+
23+
val genericSpec = codacy.dockerApi.Spec(ToolName("tool1"), patternsSpec)
24+
val genericConf: Option[Seq[PatternDef]] = Some(Seq(patternDef1, patternDef2))
25+
val genericConfNoParam: Option[Seq[PatternDef]] = Some(Seq(patternDef1NoParam, patternDef2NoParam))
26+
27+
"ToolHelper" should "getPatternsFromConf" in {
28+
val spec = genericSpec
29+
val conf = genericConf
30+
31+
val result = ToolHelper.getPatternsToLint(conf)(spec)
32+
33+
result should equal(conf)
34+
}
35+
36+
"ToolHelper" should "getPatternsNoneIfNoConf" in {
37+
val spec = genericSpec
38+
val conf = None
39+
40+
val result = ToolHelper.getPatternsToLint(conf)(spec)
41+
42+
result should equal(None)
43+
}
44+
45+
"ToolHelper" should "getPatternsEmptyIfEmptyConf" in {
46+
val spec = genericSpec
47+
val conf: Option[Seq[PatternDef]] = Some(Seq())
48+
49+
val result = ToolHelper.getPatternsToLint(conf)(spec)
50+
51+
result should equal(Some(Seq()))
52+
}
53+
54+
"ToolHelper" should "getParametersFromSpec" in {
55+
val spec = genericSpec
56+
val conf = genericConfNoParam
57+
58+
val expectedParamDef1 = ParameterDef(ParameterName("param1"), JsNumber(2))
59+
val expectedParamDef2 = ParameterDef(ParameterName("param2"), JsString("value2"))
60+
val expectedPatternDef1 = PatternDef(PatternId("id1"), Some(Set(expectedParamDef1, expectedParamDef2)))
61+
val expectedPatternDef2 = PatternDef(PatternId("id2"), None)
62+
val expectedResult = Some(Seq(expectedPatternDef1, expectedPatternDef2))
63+
64+
val result = ToolHelper.getPatternsToLint(conf)(spec)
65+
66+
result should equal(expectedResult)
67+
}
68+
}

0 commit comments

Comments
 (0)