Skip to content

Commit

Permalink
Merge pull request #67 from eed3si9n/wip/exit_code
Browse files Browse the repository at this point in the history
Return exit code 100 for lint errors
  • Loading branch information
eed3si9n authored Apr 2, 2021
2 parents 214df1e + 478dcd1 commit aaadad4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ case class LintCommand(
@PositionalArguments queryExpressions: List[String] = Nil,
app: Application = Application.default
) extends Command {
def run(): Int = app.complete(runResult())
def runResult(): Result[Unit] = {
def run(): Int = app.completeEither(runResult())

def runResult(): Result[Either[Diagnostic, Unit]] = {
val expr = queryExpressions.mkString(" ")
for {
targets <- getTargets(expr)
Expand All @@ -41,7 +42,12 @@ case class LintCommand(
conflicts = targets.map(findConflicts(_, index)).flatten.sortBy(_.toString)
_ = writeLintReport(conflicts, lintReportPath)
diagnostic = Diagnostic.fromDiagnostics(conflicts)
result <- diagnostic.map(Result.error).getOrElse(Result.value(()))
result <-
diagnostic
.map { d =>
Result.value(Left(d))
}
.getOrElse(Result.value(Right(())))
} yield result
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ object MultidepsEnrichments {
implicit class XtensionApplication(app: Application) {
def isTesting: Boolean =
app.env.isSettingTrue("MULTIDEPS_TESTING")

def completeEither(result: Result[Either[Diagnostic, Unit]]): Int =
result match {
case ValueResult(Right(())) =>
app.reporter.exitCode()
case ValueResult(Left(diagnostic)) =>
app.reporter.log(diagnostic)
100
case ErrorResult(error) =>
app.reporter.log(error)
1
}

def complete(result: Result[Unit]): Int =
result match {
case ValueResult(()) =>
Expand Down
21 changes: 15 additions & 6 deletions tests/src/test/scala/tests/commands/LintCommandSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import moped.reporters.Diagnostic
import moped.reporters.Input
import multiversion.commands.LintCommand
import multiversion.diagnostics.LintDiagnostic
import multiversion.diagnostics.MultidepsEnrichments._
import multiversion.resolvers.SimpleModule
import munit.TestOptions
import tests.BaseSuite
Expand Down Expand Up @@ -77,6 +78,7 @@ class LintCommandSuite extends BaseSuite with ConfigSyntax {
expectedErrors = List(
lintWarn("com.google.guava", "guava", "20.0", "16.0.1")
),
expectedExitCode = 100,
tags = "dupped_3rdparty" :: Nil,
expectedReport =
"""|"//foo:foo": {"failure": false, "conflicts": {"com.google.guava:guava": ["16.0.1", "20.0"]}}""".stripMargin
Expand All @@ -94,6 +96,7 @@ class LintCommandSuite extends BaseSuite with ConfigSyntax {
expectedErrors = List(
lintError("com.google.guava", "guava", "20.0", "16.0.1")
),
expectedExitCode = 100,
expectedReport =
"""|"//foo:foo": {"failure": true, "conflicts": {"com.google.guava:guava": ["16.0.1", "20.0"]}}""".stripMargin
)
Expand All @@ -112,7 +115,9 @@ class LintCommandSuite extends BaseSuite with ConfigSyntax {
lintError("com.google.guava", "guava", "20.0", "16.0.1"),
lintError("com.google.guava", "guava", "20.0", "16.0.1").copy(target = "//foo:my-alias")
),
"""|"//foo:foo": {"failure": true, "conflicts": {"com.google.guava:guava": ["16.0.1", "20.0"]}}
expectedExitCode = 100,
expectedReport =
"""|"//foo:foo": {"failure": true, "conflicts": {"com.google.guava:guava": ["16.0.1", "20.0"]}}
|"//foo:my-alias": {"failure": true, "conflicts": {"com.google.guava:guava": ["16.0.1", "20.0"]}}""".stripMargin
)

Expand All @@ -122,6 +127,7 @@ class LintCommandSuite extends BaseSuite with ConfigSyntax {
combine: List[String],
extraBuild: String = "",
expectedErrors: List[LintDiagnostic] = Nil,
expectedExitCode: Int = 0,
expectedReport: String = "",
tags: List[String] = Nil,
): Unit = {
Expand All @@ -147,19 +153,22 @@ class LintCommandSuite extends BaseSuite with ConfigSyntax {
expectedOutput = defaultExpectedOutput,
workingDirectoryLayout = workingDirectoryLayout
)
val expectedResult = Diagnostic.fromDiagnostics(expectedErrors.sortBy(_.toString)) match {
case None => Result.value(())
case Some(err) => Result.error(err)
}
val expectedResult: Result[Either[Diagnostic, Unit]] =
Diagnostic.fromDiagnostics(expectedErrors.sortBy(_.toString)) match {
case None => Result.value(Right(()))
case Some(err) => Result.value(Left(err))
}
val reportName = "report.yaml"
val obtainedResult =
LintCommand(
lintReportPath = Some(Paths.get(reportName)),
queryExpressions = "//foo:all" :: Nil,
app = app()
).runResult()
assertEquals(obtainedResult, expectedResult)
val actualExitCode = app().completeEither(obtainedResult)
assert(actualExitCode == expectedExitCode)

assertEquals(obtainedResult, expectedResult)
val obtainedReport = Input.path(app().env.workingDirectory.resolve(reportName)).text
assertNoDiff(obtainedReport, expectedReport)
}
Expand Down

0 comments on commit aaadad4

Please sign in to comment.