From 478dcd1d10e5cdc236aad211a9b04bce45ecc480 Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Fri, 2 Apr 2021 16:23:54 -0400 Subject: [PATCH] Return exit code 100 for lint errors Problem ------- Currently both build errors and lint errors return 1, which is indistinguishable during stats collecting. Solution -------- We'll return 100 for lint errors. --- .../multiversion/commands/LintCommand.scala | 12 ++++++++--- .../diagnostics/MultidepsEnrichments.scala | 13 ++++++++++++ .../tests/commands/LintCommandSuite.scala | 21 +++++++++++++------ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/multiversion/src/main/scala/multiversion/commands/LintCommand.scala b/multiversion/src/main/scala/multiversion/commands/LintCommand.scala index 4e0e5f5..422238a 100644 --- a/multiversion/src/main/scala/multiversion/commands/LintCommand.scala +++ b/multiversion/src/main/scala/multiversion/commands/LintCommand.scala @@ -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) @@ -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 } diff --git a/multiversion/src/main/scala/multiversion/diagnostics/MultidepsEnrichments.scala b/multiversion/src/main/scala/multiversion/diagnostics/MultidepsEnrichments.scala index 9b48c48..7c2386c 100644 --- a/multiversion/src/main/scala/multiversion/diagnostics/MultidepsEnrichments.scala +++ b/multiversion/src/main/scala/multiversion/diagnostics/MultidepsEnrichments.scala @@ -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(()) => diff --git a/tests/src/test/scala/tests/commands/LintCommandSuite.scala b/tests/src/test/scala/tests/commands/LintCommandSuite.scala index 980e8ae..97f2e05 100644 --- a/tests/src/test/scala/tests/commands/LintCommandSuite.scala +++ b/tests/src/test/scala/tests/commands/LintCommandSuite.scala @@ -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 @@ -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 @@ -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 ) @@ -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 ) @@ -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 = { @@ -147,10 +153,11 @@ 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( @@ -158,8 +165,10 @@ class LintCommandSuite extends BaseSuite with ConfigSyntax { 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) }