Skip to content

Commit c520d51

Browse files
author
Rodrigo Fernandes
committed
Merge pull request #3 from codacy/add-badges
Add badges and tests
2 parents 3a8db17 + 65aed29 commit c520d51

File tree

11 files changed

+230
-36
lines changed

11 files changed

+230
-36
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[![Codacy Badge](https://api.codacy.com/project/badge/grade/bc3a79d1b12649158a1eb4758e872141)](https://www.codacy.com/app/Codacy/codacy-engine-scala-seed)
2+
[![Codacy Badge](https://api.codacy.com/project/badge/coverage/bc3a79d1b12649158a1eb4758e872141)](https://www.codacy.com/app/Codacy/codacy-engine-scala-seed)
3+
[![Build Status](https://circleci.com/gh/codacy/codacy-engine-scala-seed.svg?style=shield&circle-token=:circle-token)](https://circleci.com/gh/codacy/codacy-engine-scala-seed)
4+
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.codacy/codacy-engine-scala-seed_2.11/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.codacy/codacy-engine-scala-seed_2.11)
5+
6+
# Engine Scala Seed
7+
8+
We use external tools at Codacy, this is the library we use across the multiple external tools integrations.
9+
For more details and examples of tools that use this project, you can check
10+
[PMDJava](https://github.com/codacy/codacy-pmdjava),
11+
[ESLint](https://github.com/codacy/codacy-eslint) and
12+
[Pylint](https://github.com/codacy/codacy-pylint).
13+
14+
### Usage
15+
16+
Add to your SBT dependencies:
17+
18+
```
19+
"com.codacy" %% "codacy-engine-scala-seed" % "1.4.0"
20+
```
21+
22+
You shouldn't worry about the library itself, we use it as a core in our tools,
23+
and everything is well explained in our Docs section.
24+
25+
## Docs
26+
27+
[Docker Docs](http://docs.codacy.com/v1.5/docs/tool-developer-guide)
28+
29+
[Scala Docker Template Docs](http://docs.codacy.com/v1.5/docs/tool-developer-guide-using-scala)
30+
31+
## Test
32+
33+
Follow the instructions at [codacy-plugins-test](https://github.com/codacy/codacy-plugins-test/blob/master/README.md#test-definition)

build.sbt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ scalacOptions := Seq("-deprecation", "-feature", "-unchecked", "-Ywarn-adapted-a
1212

1313
resolvers += "Bintray Typesafe Repo" at "http://dl.bintray.com/typesafe/maven-releases/"
1414

15-
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.3.10"
15+
libraryDependencies ++= Seq(
16+
"com.typesafe.play" %% "play-json" % "2.3.10",
17+
"org.scalatest" %% "scalatest" % "2.2.4" % "test"
18+
)
1619

1720
organizationName := "Codacy"
1821

circle.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
machine:
2+
java:
3+
version: oraclejdk8
4+
5+
test:
6+
override:
7+
- sbt clean coverage test
8+
- sbt coverageReport
9+
- sbt coverageAggregate
10+
- sbt codacyCoverage

project/plugins.sbt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "0.5.1")
1313
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0")
1414

1515
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.1.9")
16+
17+
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.3.3")
18+
19+
addSbtPlugin("com.codacy" % "sbt-codacy-coverage" % "1.2.1")

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: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,8 @@ object FileHelper {
2727
}
2828

2929
def stripPath(filename: String, prefix: String): String = {
30-
31-
val FilenameRegex = s""".*$prefix/(.*)""".r
32-
33-
filename match {
34-
case FilenameRegex(res) => res;
35-
case _ => filename
36-
}
30+
filename.stripPrefix(prefix)
31+
.stripPrefix("/")
3732
}
3833

3934
def listAllFiles(path: String): Seq[File] = {

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
}

src/test/scala/HelloSpec.scala

Lines changed: 0 additions & 7 deletions
This file was deleted.
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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 with no common prefix" in {
22+
val pathString: String = "filename.ext"
23+
val pathString2: String = "c/d/filename.ext"
24+
25+
val result: String = FileHelper.stripPath(pathString, "/a/b/")
26+
result should be(pathString)
27+
28+
val result2: String = FileHelper.stripPath(pathString2, "/a/b/")
29+
result2 should be(pathString2)
30+
}
31+
32+
"FileHelper" should "stripPath from Path" in {
33+
val path: java.nio.file.Path = java.nio.file.Paths.get("a/b/c/filename.ext")
34+
val pathPrefix: java.nio.file.Path = java.nio.file.Paths.get("a/b")
35+
36+
val path2: java.nio.file.Path = java.nio.file.Paths.get("/a/b/filename.ext")
37+
val pathPrefix2: java.nio.file.Path = java.nio.file.Paths.get("/a/b/")
38+
39+
val result: String = FileHelper.stripPath(path, pathPrefix)
40+
result should be("c/filename.ext")
41+
42+
val result2: String = FileHelper.stripPath(path2, pathPrefix2)
43+
result2 should be("filename.ext")
44+
}
45+
46+
"FileHelper" should "createTmpFile" in {
47+
val fileTmp = FileHelper.createTmpFile("foo", "prefix", ".ext").toString
48+
49+
java.nio.file.Paths.get(fileTmp).getFileName.toString should startWith ("prefix")
50+
fileTmp should endWith (".ext")
51+
io.Source.fromFile(fileTmp).mkString should be("foo")
52+
}
53+
54+
"FileHelper" should "createFile" in {
55+
val pathString: String = "/tmp/fileHelperFilename.ext"
56+
new java.io.File(pathString).delete()
57+
58+
val file = FileHelper.createFile(pathString, "foo").toString
59+
io.Source.fromFile(file).mkString should be("foo")
60+
}
61+
}

0 commit comments

Comments
 (0)