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

Show user guidance to configuration file if file not exists #18

Merged
merged 2 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion 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.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,21 @@ 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) =>
val exception = new IllegalArgumentException(
s"""Configuration file ${e.getFile} not found.
Provide the file in reference to https://github.com/tanishiking/scalaunfmt#configuration""".stripMargin
)
exception.setStackTrace(Array.empty)
throw exception
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,26 @@
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."))
assert(ex.getStackTrace.isEmpty)
}

it("should throw exception for unparsable config") {
val configPath = Files.createTempFile("temp", ".conf")
val unparsableConf =
Expand Down