Skip to content

Commit

Permalink
Start increasing test coverage
Browse files Browse the repository at this point in the history
* Add DotWritingProgressMonitor Test
* Rename git-check as "onchange"
* Fix RunCommandOnExamplesTest to actually run commands on examples
* Make RunCommandOnExamplesTest print out what its skipping
* Attempt to fix SLF4J warnings

Signed-off-by: reidspencer <reid.spencer@yoppworks.com>
  • Loading branch information
reid-spencer committed Nov 12, 2022
1 parent 86e47a0 commit 3738879
Show file tree
Hide file tree
Showing 12 changed files with 279 additions and 98 deletions.
12 changes: 6 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ lazy val riddl = (project in file(".")).enablePlugins(ScoverageSbtPlugin)
testkit,
prettify,
hugo,
`git-check`,
onchange,
doc,
riddlc,
plugin
Expand Down Expand Up @@ -122,10 +122,10 @@ lazy val hugo: Project = project.in(file("hugo")).configure(C.withCoverage(0))
.dependsOn(language % "compile->compile", commands, testkit % "test->compile")
.dependsOn(utils)

lazy val GitCheck = config("git-check")
lazy val `git-check`: Project = project.in(file("git-check"))
lazy val OnChange = config("onchange")
lazy val onchange: Project = project.in(file("onchange"))
.configure(C.withCoverage(0)).configure(C.mavenPublish).settings(
name := "riddl-git-check",
name := "riddl-onchange",
Compile / unmanagedResourceDirectories += {
baseDirectory.value / "resources"
},
Expand All @@ -139,7 +139,7 @@ lazy val scaladocSiteProjects = List(
(commands, Commands),
(testkit, TestKit),
(prettify, Prettify),
(`git-check`, GitCheck),
(onchange, OnChange),
(hugo, HugoTrans),
(riddlc, Riddlc)
)
Expand Down Expand Up @@ -186,7 +186,7 @@ lazy val riddlc: Project = project.in(file("riddlc"))
commands,
language,
hugo,
`git-check`,
onchange,
testkit % "test->compile"
).settings(
name := "riddlc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ import com.reactific.riddl.utils.Logger

import java.nio.file.Path

object ParseCommand {
val cmdName = "parse"
}

/** A Command for Parsing RIDDL input
*/
class ParseCommand extends InputFileCommandPlugin("parse") {
class ParseCommand extends InputFileCommandPlugin(ParseCommand.cmdName) {
import InputFileCommandPlugin.Options
override def run(
options: Options,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2019 Ossum, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

package com.reactific.riddl.translator.onchange

import com.reactific.riddl.language.CommonOptions
import com.reactific.riddl.utils.Logger
import org.eclipse.jgit.lib.ProgressMonitor

import java.io.PrintStream

case class DotWritingProgressMonitor(out: PrintStream, log: Logger,
options: CommonOptions)
extends ProgressMonitor {
override def start(totalTasks: Int): Unit = {
if (options.verbose) { log.info(s"Starting Fetch with $totalTasks tasks.") }
else { out.print("\n.") }
}

override def beginTask(title: String, totalWork: Int): Unit = {
if (options.verbose) {
log.info(s"Starting Task '$title', $totalWork remaining.")
} else { out.print(".") }
}

override def update(completed: Int): Unit = {
if (options.verbose) { log.info(s"$completed tasks completed.") }
else { out.print(".") }
}

override def endTask(): Unit = {
if (options.verbose) { log.info(s"Task completed.") }
else { out.println(".") }
}

override def isCancelled: Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

package com.reactific.riddl.translator.hugo_git_check
package com.reactific.riddl.translator.onchange

import com.reactific.riddl.language.Messages.Messages
import com.reactific.riddl.language.*
import com.reactific.riddl.utils.Logger
import org.eclipse.jgit.api.*
import org.eclipse.jgit.api.errors.GitAPIException
import org.eclipse.jgit.lib.ProgressMonitor
import org.eclipse.jgit.merge.MergeStrategy
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import org.eclipse.jgit.submodule.SubmoduleWalk
Expand All @@ -24,9 +23,9 @@ import java.time.Instant
import scala.collection.mutable.ArrayBuffer
import scala.jdk.CollectionConverters.*

object GitCheck {
object OnChange {

private def creds(options: GitCheckCommand.Options) =
private def creds(options: OnChangeCommand.Options) =
new UsernamePasswordCredentialsProvider(
options.userName,
options.accessToken
Expand All @@ -36,12 +35,12 @@ object GitCheck {
root: AST.RootContainer,
log: Logger,
commonOptions: CommonOptions,
options: GitCheckCommand.Options
options: OnChangeCommand.Options
)(doit: (
AST.RootContainer,
Logger,
CommonOptions,
GitCheckCommand.Options
OnChangeCommand.Options
) => Either[Messages, Unit]
): Either[Messages, Unit] = {
require(
Expand Down Expand Up @@ -81,7 +80,7 @@ object GitCheck {
def gitHasChanges(
log: Logger,
commonOptions: CommonOptions,
options: GitCheckCommand.Options,
options: OnChangeCommand.Options,
git: Git,
minTime: FileTime
): Boolean = {
Expand All @@ -93,9 +92,9 @@ object GitCheck {
val relativized = top.relativize(relativeDir)
if (relativized.getNameCount > 1) relativized.toString else "."
} else { "." }
val status = git.status()
.setProgressMonitor(DotWritingProgressMonitor(log, commonOptions))
.setIgnoreSubmodules(SubmoduleWalk.IgnoreSubmoduleMode.ALL)
val status = git.status().setProgressMonitor(
DotWritingProgressMonitor(System.out, log, commonOptions)
).setIgnoreSubmodules(SubmoduleWalk.IgnoreSubmoduleMode.ALL)
.addPath(subPath).call()

val potentiallyChangedFiles =
Expand All @@ -113,7 +112,7 @@ object GitCheck {
def pullCommits(
log: Logger,
commonOptions: CommonOptions,
options: GitCheckCommand.Options,
options: OnChangeCommand.Options,
git: Git
): Boolean = {
try {
Expand All @@ -133,8 +132,8 @@ object GitCheck {
}

def prepareOptions(
options: GitCheckCommand.Options
): GitCheckCommand.Options = {
options: OnChangeCommand.Options
): OnChangeCommand.Options = {
require(options.inputFile.isEmpty, "inputFile not used by this command")
options
}
Expand Down Expand Up @@ -175,30 +174,4 @@ object GitCheck {
}
}

case class DotWritingProgressMonitor(log: Logger, options: CommonOptions)
extends ProgressMonitor {
override def start(totalTasks: Int): Unit = {
if (options.verbose) {
log.info(s"Starting Fetch with $totalTasks tasks.")
} else { System.out.print("\n.") }
}

override def beginTask(title: String, totalWork: Int): Unit = {
if (options.verbose) {
log.info(s"Starting Task '$title', $totalWork remaining.")
} else { System.out.print(".") }
}

override def update(completed: Int): Unit = {
if (options.verbose) { log.info(s"$completed tasks completed.") }
else { System.out.print(".") }
}

override def endTask(): Unit = {
if (options.verbose) { log.info(s"Task completed.") }
else { System.out.println(".") }
}

override def isCancelled: Boolean = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

package com.reactific.riddl.translator.hugo_git_check
package com.reactific.riddl.translator.onchange

import com.reactific.riddl.commands.CommandOptions.optional
import com.reactific.riddl.commands.CommandOptions
import com.reactific.riddl.commands.CommandPlugin
Expand All @@ -19,28 +20,30 @@ import scopt.OParser
import java.io.File
import java.nio.file.Path

object GitCheckCommand {
object OnChangeCommand {
final val cmdName: String = "onchange"
case class Options(
gitCloneDir: Option[Path] = None,
relativeDir: Option[Path] = None,
userName: String = "",
accessToken: String = "")
extends CommandOptions {
def command: String = "hugo-git-check"
def command: String = cmdName
def inputFile: Option[Path] = None
}
}

/** HugoGitCheck Command */
class GitCheckCommand
extends CommandPlugin[GitCheckCommand.Options]("hugo-git-check") {
import GitCheckCommand.Options
class OnChangeCommand
extends CommandPlugin[OnChangeCommand.Options](OnChangeCommand.cmdName)
{
import OnChangeCommand.Options

override def getOptions: (OParser[Unit, Options], Options) = {
val builder = OParser.builder[Options]
import builder.*
OParser.sequence(
cmd("git-check").children(
cmd("onchange").children(
opt[File]("git-clone-dir").required()
.action((f, opts) => opts.copy(gitCloneDir = Some(f.toPath)))
.text("""Provides the top directory of a git repo clone that
Expand All @@ -59,7 +62,7 @@ class GitCheckCommand
) -> Options()
}

implicit val hugoGitCheckReader: ConfigReader[Options] = {
implicit val onChangeReader: ConfigReader[Options] = {
(cur: ConfigCursor) =>
{
for {
Expand All @@ -73,7 +76,7 @@ class GitCheckCommand
accessTokenRes <- objCur.atKey("access-token")
accessTokenStr <- accessTokenRes.asString
} yield {
GitCheckCommand.Options(
OnChangeCommand.Options(
gitCloneDir = Some(gitCloneDir.toPath),
userName = userNameStr,
accessToken = accessTokenStr
Expand All @@ -82,7 +85,7 @@ class GitCheckCommand
}
}

override def getConfigReader: ConfigReader[Options] = hugoGitCheckReader
override def getConfigReader: ConfigReader[Options] = onChangeReader

/** Execute the command given the options. Error should be returned as
* Left(messages) and not directly logged. The log is for verbose or debug
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2019 Ossum, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

package com.reactific.riddl.translator.onchange
import com.reactific.riddl.language.CommonOptions
import com.reactific.riddl.utils.StringBuildingPrintStream
import com.reactific.riddl.utils.StringLogger
import org.scalatest.matchers.must.Matchers
import org.scalatest.wordspec.AnyWordSpec

class DotWritingProgressMonitorTest extends AnyWordSpec with Matchers {
def runTest(verbose: Boolean): (String, String) = {
val log = StringLogger(1024)
val capture = StringBuildingPrintStream()
val monitor =
DotWritingProgressMonitor(capture, log, CommonOptions(verbose = verbose))
monitor.start(3)
def runTask(name: String, work: Int): Unit = {
monitor.beginTask(name, work)
for (i <- 1 to work) { monitor.update(i) }
monitor.endTask()
}
runTask("One", 5)
runTask("Two", 5)
runTask("Three", 5)
(capture.mkString(), log.toString())

}
"DotWritingProgressMonitor" should {
"product correct output for a set of tasks" in {
val (capture, log) = runTest(true)
capture mustBe empty
log must be("""|[info] Starting Fetch with 3 tasks.
|[info] Starting Task 'One', 5 remaining.
|[info] 1 tasks completed.
|[info] 2 tasks completed.
|[info] 3 tasks completed.
|[info] 4 tasks completed.
|[info] 5 tasks completed.
|[info] Task completed.
|[info] Starting Task 'Two', 5 remaining.
|[info] 1 tasks completed.
|[info] 2 tasks completed.
|[info] 3 tasks completed.
|[info] 4 tasks completed.
|[info] 5 tasks completed.
|[info] Task completed.
|[info] Starting Task 'Three', 5 remaining.
|[info] 1 tasks completed.
|[info] 2 tasks completed.
|[info] 3 tasks completed.
|[info] 4 tasks completed.
|[info] 5 tasks completed.
|[info] Task completed.
|""".stripMargin)
}
"produce correct output in non-verbose mode" in {
val (capture, log) = runTest(false)
log mustBe empty
capture must be("""
|........
|.......
|.......
|""".stripMargin)

}
}

}
Loading

0 comments on commit 3738879

Please sign in to comment.