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 17, 2019
1 parent 9744ffd commit cb9fac1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/main/scala/com/github/tanishiking/scalaunfmt/cli/Cli.scala
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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,
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 cb9fac1

Please sign in to comment.