Skip to content

Commit

Permalink
return custom error when no rule was requested
Browse files Browse the repository at this point in the history
  • Loading branch information
bjaglin committed Jun 27, 2022
1 parent 57042bd commit 0c27957
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
3 changes: 2 additions & 1 deletion scalafix-cli/src/main/scala/scalafix/cli/ExitStatus.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ object ExitStatus {
StaleSemanticdbError,
TestError,
LinterError,
NoFilesError
NoFilesError,
NoRulesError
: ExitStatus = generateExitStatus
// format: on
lazy val all: List[ExitStatus] = allInternal.toList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ object MainOps {
args.validate match {
case Configured.Ok(validated) =>
if (validated.rules.isEmpty) {
out.println("Missing --rules")
ExitStatus.CommandLineError
out.println("No rules requested to run")
ExitStatus.NoRulesError
} else {
MainOps.run(validated)
}
Expand All @@ -95,8 +95,8 @@ object MainOps {
args.rules.rules match {
case Nil => {
ScalafixEvaluationImpl(
ExitStatus.CommandLineError,
Some("missing rules")
ExitStatus.NoRulesError,
Some("No rules requested to run")
)
}
case _: Seq[Rule] =>
Expand Down Expand Up @@ -382,6 +382,7 @@ object MainOps {
case _ =>
ex.printStackTrace(args.args.out)
}
println(e)
ExitStatus.UnexpectedError
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@ public enum ScalafixError {
/**
* No files were provided to Scalafix so nothing happened.
*/
NoFilesError
NoFilesError,
/**
* No rules were provided to Scalafix so nothing happened.
*/
NoRulesError
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ class CliSyntacticSuite extends BaseCliSuite {
check(
name = "file not found",
originalLayout = s"/foobar.scala\n",
args = Array("unknown-file.scala"),
args = Array("-r", "ProcedureSyntax", "unknown-file.scala"),
expectedLayout = "/foobar.scala",
expectedExit = ExitStatus.CommandLineError
expectedExit = ExitStatus.UnexpectedError
)

check(
name = "empty rule",
originalLayout = s"/foobar.scala\n",
args = Array("foobar.scala"),
expectedLayout = "/foobar.scala",
expectedExit = ExitStatus.CommandLineError
expectedExit = ExitStatus.NoRulesError
)

check(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,17 @@ class ScalafixArgumentsSuite extends AnyFunSuite with DiffAssertions {
assert(eval.getErrorMessage.get == "No files to fix")
}

test("Scalafix-evaluation-error-messages: NoRulesError", SkipWindows) {
// don't get rules from the project's .scalafix.conf
val noscalafixconfig = Files.createTempDirectory("scalafix")
val eval = api
.withPaths(Seq(main).asJava)
.withWorkingDirectory(noscalafixconfig)
.evaluate()
assert(!eval.isSuccessful)
assert(eval.getErrorMessage.get == "No rules requested to run")
}

test("Scalafix-evaluation-error-messages: missing semanticdb", SkipWindows) {
val eval = api
.withPaths(Seq(main).asJava)
Expand Down

0 comments on commit 0c27957

Please sign in to comment.