Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ender Tunc committed Mar 31, 2023
1 parent 50df298 commit 38ec23d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,22 @@ object Cli {
val processTimeout = "process-timeout"
}

implicit val mergeRequestApprovalsConfigArgument: Argument[MergeRequestApprovalRulesCfg] =
implicit val mergeRequestApprovalsCfgArgument: Argument[MergeRequestApprovalRulesCfg] =
Argument.from("approvals_rule_name=required_approvals") { s =>
s.split(":").toList match {
s.trim.split("=").toList match {
case approvalRuleName :: requiredApprovalsAsString :: Nil =>
Try(requiredApprovalsAsString.trim.toInt) match {
case Failure(_) =>
s"[$requiredApprovalsAsString] is not a valid Integer".invalidNel
Validated.invalidNel(s"[$requiredApprovalsAsString] is not a valid Integer")
case Success(requiredApprovals) =>
MergeRequestApprovalRulesCfg(approvalRuleName.trim, requiredApprovals).validNel
Validated.valid(
MergeRequestApprovalRulesCfg(approvalRuleName.trim, requiredApprovals)
)
}
case _ =>
s"The value is expected in the following format: APPROVALS_RULE_NAME:REQUIRED_APPROVALS.".invalidNel
Validated.invalidNel(
"The value is expected in the following format: APPROVALS_RULE_NAME=REQUIRED_APPROVALS"
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.scalasteward.core.application

import better.files.File
import cats.data.Validated
import cats.data.Validated.Valid
import munit.FunSuite
import org.http4s.syntax.literals._
Expand Down Expand Up @@ -171,7 +172,7 @@ class CliTest extends FunSuite {
val params = minimumRequiredParams ++ List(
List("--gitlab-merge-when-pipeline-succeeds"),
List("--gitlab-remove-source-branch"),
List("--merge-request-level-approval-rule", "All eligible users:0")
List("--merge-request-level-approval-rule", "All eligible users=0")
)
val Success(StewardUsage.Regular(obtained)) = Cli.parseArgs(params.flatten)

Expand All @@ -185,8 +186,8 @@ class CliTest extends FunSuite {

test("parseArgs: multiple Gitlab merge request level approval rule") {
val params = minimumRequiredParams ++ List(
List("--merge-request-level-approval-rule", "All eligible users:1"),
List("--merge-request-level-approval-rule", "Only Main:2")
List("--merge-request-level-approval-rule", "All eligible users=1"),
List("--merge-request-level-approval-rule", "Only Main=2")
)
val Success(StewardUsage.Regular(obtained)) = Cli.parseArgs(params.flatten)

Expand All @@ -205,7 +206,7 @@ class CliTest extends FunSuite {

test("parseArgs: only allow one way to define Gitlab required approvals arguments") {
val params = minimumRequiredParams ++ List(
List("--merge-request-level-approval-rule", "All eligible users:0"),
List("--merge-request-level-approval-rule", "All eligible users=0"),
List("--gitlab-required-reviewers", "5")
)
val Error(errorMsg) = Cli.parseArgs(params.flatten)
Expand All @@ -229,7 +230,7 @@ class CliTest extends FunSuite {

test("parseArgs: invalid GitLab merge request level approval rule") {
val params = minimumRequiredParams ++ List(
List("--merge-request-level-approval-rule", "All eligible users:-3")
List("--merge-request-level-approval-rule", "All eligible users=-3")
)
val Error(errorMsg) = Cli.parseArgs(params.flatten)

Expand Down Expand Up @@ -304,4 +305,21 @@ class CliTest extends FunSuite {
)
assert(error.startsWith("Missing value for option: --azure-repos-organization"))
}

test("mergeRequestApprovalsConfigArgument: without equals sign") {
assertEquals(
Cli.mergeRequestApprovalsCfgArgument.read("only-main"),
Validated.invalidNel(
s"The value is expected in the following format: APPROVALS_RULE_NAME=REQUIRED_APPROVALS"
)
)
}

test("mergeRequestApprovalsConfigArgument: non-integer required approvals") {
val nonIntegerRequiredApprovals = "two"
assertEquals(
Cli.mergeRequestApprovalsCfgArgument.read(s"only-main=$nonIntegerRequiredApprovals"),
Validated.invalidNel(s"[$nonIntegerRequiredApprovals] is not a valid Integer")
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,38 @@ class GitLabApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
assertIO(prOut, expected)
}

test("createPullRequest -- no fail upon list approval rules error") {
val localApp = HttpApp[MockEff] { req =>
(req: @unchecked) match {
case GET -> Root / "projects" / "foo/bar" / "merge_requests" / "150" / "approval_rules" =>
BadRequest(s"Cannot get merge request approval rules")
}
}

val localState = MockState.empty.copy(clientResponses = auth <+> localApp <+> httpApp)

val prOut = gitlabApiAlgWithApprovalRules
.createPullRequest(
Repo("foo", "bar"),
newPRData
)
.runA(localState)

val expected = PullRequestOut(
uri"https://gitlab.com/foo/bar/merge_requests/150",
PullRequestState.Open,
PullRequestNumber(150),
"title"
)

assertIO(prOut, expected)
}

test("createPullRequest -- no fail upon update approval rule error") {
val localApp = HttpApp[MockEff] { req =>
(req: @unchecked) match {
case PUT -> Root / "projects" / "foo/bar" / "merge_requests" / "150" / "approval_rules" / "101" =>
BadRequest(s"Cannot update merge requests approval rules")
BadRequest(s"Cannot update merge request approval rule")
}
}

Expand Down

0 comments on commit 38ec23d

Please sign in to comment.