From 8d065aa3264dc97ea1ed2eee298b6c6ddac80d94 Mon Sep 17 00:00:00 2001 From: exoego Date: Tue, 18 Jun 2019 08:25:14 +0900 Subject: [PATCH] Show user guidance to configuration file if file not exists --- .../tanishiking/scalaunfmt/cli/Cli.scala | 19 +++++++++++++++++-- .../tanishiking/scalaunfmt/cli/CliSpec.scala | 14 +++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) 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..8ac5fb4 100644 --- a/src/main/scala/com/github/tanishiking/scalaunfmt/cli/Cli.scala +++ b/src/main/scala/com/github/tanishiking/scalaunfmt/cli/Cli.scala @@ -1,11 +1,14 @@ 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} +import metaconfig.{ Conf, Configured } import metaconfig.typesafeconfig._ +import scala.util.{ Failure, Success, Try } + object Cli { def main(args: Array[String]): Unit = { CliArgParser.scoptParser.parse(args, CliOptions()) match { @@ -22,7 +25,19 @@ object Cli { val runner = new Runner(errStream) - Conf.parseFile(confPath.toFile) match { + val conf = Try { + Conf.parseFile(confPath.toFile) + } match { + case Success(v) => v + case Failure(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 + ) + case Failure(e) => throw e // unknown + } + + 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 =