Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
Add tests for project config codec
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed May 21, 2022
1 parent f7e5ea8 commit 4aece70
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 10 deletions.
5 changes: 3 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ lazy val gitlab = project

lazy val bootstrap = project
.settings(
scalaVersion := "3.1.1",
scalaVersion := Scala3,
libraryDependencies ++= List(
"org.typelevel" %% "cats-core" % "2.7.0",
"org.typelevel" %% "cats-effect" % "3.3.12",
Expand Down Expand Up @@ -193,7 +193,8 @@ lazy val pitgull =
"io.chrisdavenport" %% "cats-time" % "0.4.0",
"com.github.valskalla" %% "odin-core" % "0.13.0",
"com.github.valskalla" %% "odin-slf4j" % "0.13.0",
"io.github.vigoo" %% "prox-fs2-3" % "0.7.7"
"io.github.vigoo" %% "prox-fs2-3" % "0.7.7",
"io.circe" %% "circe-literal" % "0.14.2" % Test
)
)
.dependsOn(core, gitlab)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/io/pg/appconfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ object AppConfig {
default(Git.Host.Gitlab),
env("GIT_API_URL").as[Uri],
env("GIT_API_TOKEN").secret
).mapN(Git.apply)
).parMapN(Git.apply)

private val queuesConfig: ConfigValue[ciris.Effect, Queues] = default(100).map(Queues.apply)

Expand Down
21 changes: 14 additions & 7 deletions src/main/scala/io/pg/config/format.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import io.circe.Json

object circe {

private val decodeRegex: Decoder[Regex] = Decoder.instance {
_.value
.asString
.toRight(DecodingFailure("Failed to decode as String", Nil))
.flatMap { s =>
Either.catchNonFatal(s.r).leftMap(DecodingFailure.fromThrowable(_, Nil))
}
private val decodeRegex: Decoder[Regex] = Decoder[String].flatMap { s =>
Either
.catchNonFatal(s.r)
.leftMap(DecodingFailure.fromThrowable(_, Nil))
.liftTo[Decoder]
}

private val encodeRegex: Encoder[Regex] = Encoder.encodeString.contramap[Regex](_.toString)
Expand All @@ -29,6 +27,15 @@ import circe.regexCodec
enum TextMatcher {
case Equals(value: String)
case Matches(regex: Regex)

override def equals(another: Any) = (this, another) match {
// Regex uses reference equality by default.
// By using `.regex` we convert it back to a pattern string for better comparison.
case (Matches(p1), Matches(p2)) => p1.regex == p2.regex
case (Equals(e1), Equals(e2)) => e1 == e2
case _ => false
}

}

object TextMatcher {
Expand Down
73 changes: 73 additions & 0 deletions src/test/scala/io/pg/ProjectConfigFormatTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.pg

import weaver._
import io.circe.literal._
import io.pg.config.ProjectConfig
import io.pg.config.ProjectConfigReader
import io.pg.gitlab.webhook.Project
import io.pg.config.Rule
import io.pg.config.Action
import io.pg.config.Matcher
import io.pg.config.TextMatcher
import io.circe.syntax._

object ProjectConfigFormatTest extends FunSuite {

val asJSON = json"""{
"rules": [
{
"action": "Merge",
"matcher": {
"kind": "Many",
"values": [
{
"email": {
"kind": "Equals",
"value": "[email protected]"
},
"kind": "Author"
},
{
"kind": "Description",
"text": {
"kind": "Matches",
"regex": ".*labels:.*semver-patch.*"
}
},
{
"kind": "PipelineStatus",
"status": "success"
}
]
},
"name": "Scala Steward"
}
]
}
"""

val decoded = ProjectConfig(
rules = List(
Rule(
name = "Scala Steward",
action = Action.Merge,
matcher = Matcher.Many(
List(
Matcher.Author(TextMatcher.Equals("[email protected]")),
Matcher.Description(TextMatcher.Matches(".*labels:.*semver-patch.*".r)),
Matcher.PipelineStatus("success")
)
)
)
)
)

test("Example config can be decoded") {
val actual = asJSON.as[ProjectConfig]
assert(actual == Right(decoded))
}
test("Example config can be encoded") {
val actual = decoded.asJson
assert.eql(actual, asJSON)
}
}

0 comments on commit 4aece70

Please sign in to comment.