Skip to content

Commit

Permalink
Clean up code smells
Browse files Browse the repository at this point in the history
Signed-off-by: reidspencer <reid.spencer@yoppworks.com>
  • Loading branch information
reid-spencer committed Nov 14, 2022
1 parent 8f78b89 commit 61c98f8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 82 deletions.
9 changes: 9 additions & 0 deletions .sonarcloud.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sonar.scala.coverage.reportPaths=\
commands/target/scala-2.13/scoverage-report/scoverage.xml,\
hugo/target/scala-2.13/scoverage-report/scoverage.xml,\
language/target/scala-2.13/scoverage-report/scoverage.xml,\
prettify/target/scala-2.13/scoverage-report/scoverage.xml,\
riddlc/target/scala-2.13/scoverage-report/scoverage.xml,\
testkit/target/scala-2.13/scoverage-report/scoverage.xml,\
utils/target/scala-2.13/scoverage-report/scoverage.xml

Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ object CommandPlugin {
result
}

private def handleCommandResult(
result: Either[Messages, Unit],
commonOptions: CommonOptions,
log: Logger
): Int = {
result match {
case Right(_) =>
if (commonOptions.quiet) { System.out.println(log.summary) }
0
case Left(messages) =>
if (commonOptions.quiet) { highestSeverity(messages) + 1 }
else { Messages.logMessages(messages, log, commonOptions) + 1 }
}
}

private def handleCommandRun(
remaining: Array[String],
commonOptions: CommonOptions
Expand All @@ -172,14 +187,7 @@ object CommandPlugin {
if (commonOptions.quiet) { log = StringLogger() }
val result = CommandPlugin
.runCommandWithArgs(name, remaining, log, commonOptions)
result match {
case Right(_) =>
if (commonOptions.quiet) { System.out.println(log.summary) }
0
case Left(messages) =>
if (commonOptions.quiet) { highestSeverity(messages) + 1 }
else { Messages.logMessages(messages, log, commonOptions) + 1 }
}
handleCommandResult(result, commonOptions, log)
}
}

Expand Down Expand Up @@ -257,15 +265,6 @@ abstract class CommandPlugin[OPT <: CommandOptions: ClassTag](
}
}

def runFrom(
configFile: Path,
commonOptions: CommonOptions,
log: Logger
): Either[Messages, Unit] = {
loadOptionsFrom(configFile, commonOptions)
.flatMap(run(_, commonOptions, log, None))
}

/** Execute the command given the options. Error should be returned as
* Left(messages) and not directly logged. The log is for verbose or debug
* output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,75 +13,47 @@ import org.scalatest.wordspec.AnyWordSpec
class CommandsTest extends AnyWordSpec with Matchers {

def runCommand(
args: Array[String] = Array.empty[String]
args: Seq[String] = Seq.empty[String]
): Assertion = {
val rc = CommandPlugin.runMain(args)
val rc = CommandPlugin.runMain(args.toArray)
rc mustBe 0
}

val inputFile = "testkit/src/test/input/rbbq.riddl"
val hugoConfig = "testkit/src/test/input/hugo.conf"
val validateConfig = "testkit/src/test/input/validate.conf"
val quiet = "--quiet"
val suppressMissing = "--suppress-missing-warnings"
val suppressStyle = "--suppress-style-warnings"
val outputDir: String => String =
(name: String) => s"riddlc/target/test/$name"

val common = Seq(quiet, suppressMissing, suppressStyle)

"Commands" should {
"handle dump" in {
val args = Array(
"--quiet",
"--suppress-missing-warnings",
"--suppress-style-warnings",
"dump",
inputFile
)
val args = common ++ Seq("dump", inputFile)
runCommand(args)
}
"handle from" in {
val args = Array(
"--quiet",
"--suppress-missing-warnings",
"--suppress-style-warnings",
"from",
validateConfig,
"validate"
)
val args = common ++ Seq("from", validateConfig, "validate")
runCommand(args)
}
"handle parse" in {
val args = Array("--quiet", "parse", inputFile)
val args = common ++ Seq("parse", inputFile)
runCommand(args)
}
"handle repeat" in {
val args = Array(
"--quiet",
"--suppress-missing-warnings",
"--suppress-style-warnings",
"repeat",
validateConfig,
"validate",
"1s",
"2"
)
val args = common ++
Seq("repeat", validateConfig, "validate", "1s", "2")
runCommand(args)
}
"handle stats" in {
val args = Array(
"--quiet",
"--suppress-missing-warnings",
"--suppress-style-warnings",
"stats",
inputFile
)
val args = common ++ Seq("stats", inputFile)
runCommand(args)
}
"handle validate" in {
val args = Array(
"--quiet",
"--suppress-missing-warnings",
"--suppress-style-warnings",
"validate",
inputFile
)
val args = common ++ Seq("validate", inputFile)
runCommand(args)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ class HugoCommandTest extends RunCommandSpecBase {
"hugo"
)
runWith(args)
// runHugo(path)
// val root = Path.of(output).resolve(path)
// val img = root.resolve("static/images/RBBQ.png")
// Files.exists(img) mustBe true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ trait TypeExpression extends AbstractDefinitions {
extends AggregateTypeExpression {}

object Aggregation {
// val empty: Aggregation = { Aggregation(Location.empty) }
def empty(loc: Location = Location.empty): Aggregation = {
Aggregation(loc)
}
Expand Down
48 changes: 30 additions & 18 deletions utils/src/main/scala/com/reactific/riddl/utils/FileWatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ object FileWatcher {
Files.walkFileTree(root, sfv)
}

private def handlePolledEvents(
key: WatchKey,
events: Seq[WatchEvent[?]],
interval: Int
)(onEvents: Seq[WatchEvent[?]] => Boolean
)(notOnEvents: => Boolean
): Unit = {
events match {
case x: Seq[WatchEvent[?]] if x.isEmpty =>
if (notOnEvents) {
key.reset()
Thread.sleep(interval)
} else {
// they want to stop
key.cancel()
}
case events =>
if (onEvents(events)) {
// reset the key for the next trip around
key.reset()
} else {
// they want to stop
key.cancel()
}
}
}

def watchForChanges(
path: Path,
periodInSeconds: Int,
Expand All @@ -54,24 +81,9 @@ object FileWatcher {
case key: WatchKey if key != null =>
saveKey = key
val events = key.pollEvents().asScala.toSeq
events match {
case x: Seq[WatchEvent[?]] if x.isEmpty =>
if (notOnEvents) {
key.reset()
Thread.sleep(intervalInMillis)
} else {
// they want to stop
key.cancel()
}
case events =>
if (onEvents(events)) {
// reset the key for the next trip around
key.reset()
} else {
// they want to stop
key.cancel()
}
}
handlePolledEvents(key, events, intervalInMillis)(onEvents)(
notOnEvents
)
}
} while (saveKey.isValid && Instant.now().toEpochMilli < deadline)
System.currentTimeMillis() < deadline
Expand Down

0 comments on commit 61c98f8

Please sign in to comment.