diff --git a/src/main/scala/com/github/tanishiking/scalaunfmt/cli/Cli.scala b/src/main/scala/com/github/tanishiking/scalaunfmt/cli/Cli.scala index 5b21389..81ddec5 100644 --- a/src/main/scala/com/github/tanishiking/scalaunfmt/cli/Cli.scala +++ b/src/main/scala/com/github/tanishiking/scalaunfmt/cli/Cli.scala @@ -1,6 +1,7 @@ package com.github.tanishiking.scalaunfmt.cli import java.io.PrintStream +import java.nio.file.NoSuchFileException import com.github.tanishiking.scalaunfmt.core.Runner import metaconfig.{Conf, Configured} @@ -22,7 +23,17 @@ object Cli { val runner = new Runner(errStream) - Conf.parseFile(confPath.toFile) match { + val conf = try { + Conf.parseFile(confPath.toFile) + } catch { + case e: NoSuchFileException => + throw new IllegalArgumentException( + s"""Configuration file ${e.getFile} not found. + |Provide the file in reference to https://github.com/tanishiking/scalaunfmt#configuration""".stripMargin + ) + } + + conf match { case Configured.Ok(value) => runner.run( files, diff --git a/src/test/scala/com/github/tanishiking/scalaunfmt/cli/CliSpec.scala b/src/test/scala/com/github/tanishiking/scalaunfmt/cli/CliSpec.scala index 0934e94..48e0295 100644 --- a/src/test/scala/com/github/tanishiking/scalaunfmt/cli/CliSpec.scala +++ b/src/test/scala/com/github/tanishiking/scalaunfmt/cli/CliSpec.scala @@ -1,13 +1,25 @@ package com.github.tanishiking.scalaunfmt.cli import java.io.{ByteArrayOutputStream, PrintStream} -import java.nio.file.Files +import java.nio.file.{Files, NoSuchFileException, Paths} import org.scalatest.{FunSpec, Matchers} class CliSpec extends FunSpec with Matchers { describe("Cli") { describe("run") { + it("should show error message to guide users to create a config file") { + val configPath = Paths.get("NO.SUCH.FILE.conf") + val opt = CliOptions( + config = configPath, + version = "2.0.0-RC5" + ) + val ex = intercept[IllegalArgumentException] { + Cli.run(opt, System.out, System.err) + } + assert(ex.getMessage.contains("Configuration file NO.SUCH.FILE.conf not found.")) + } + it("should throw exception for unparsable config") { val configPath = Files.createTempFile("temp", ".conf") val unparsableConf =