Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Commit

Permalink
Show user guidance to configuration file if file not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Jun 18, 2019
1 parent 9744ffd commit 8d065aa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/main/scala/com/github/tanishiking/scalaunfmt/cli/Cli.scala
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down

0 comments on commit 8d065aa

Please sign in to comment.