diff --git a/tests/jats.nim b/tests/jats.nim index a36b1de..e1b4eef 100644 --- a/tests/jats.nim +++ b/tests/jats.nim @@ -16,7 +16,7 @@ import nim/nimtests -import testutils +import testutils, logutils import os, osproc, strformat, streams, parseopt, strutils diff --git a/tests/logutils.nim b/tests/logutils.nim new file mode 100644 index 0000000..33ee911 --- /dev/null +++ b/tests/logutils.nim @@ -0,0 +1,59 @@ + +# logging stuff + +type LogLevel* {.pure.} = enum + Debug, # always written to file only (large outputs, such as the entire output of the failing test or stacktrace) + Info, # important information about the progress of the test suite + Error, # failing tests (printed with red) + Stdout, # always printed to stdout only (for cli experience) + + +const echoedLogs = {LogLevel.Info, LogLevel.Error, LogLevel.Stdout} +const echoedLogsSilent = {LogLevel.Error} +const savedLogs = {LogLevel.Debug, LogLevel.Info, LogLevel.Error} + +const logColors = [LogLevel.Debug: fgDefault, LogLevel.Info: fgGreen, LogLevel.Error: fgRed, LogLevel.Stdout: fgYellow] + +var totalLog = "" +var verbose = true +proc setVerbosity*(verb: bool) = + verbose = verb + +proc log*(level: LogLevel, msg: string) = + let msg = &"[{$level} - {$getTime()}] {msg}" + if level in savedLogs: + totalLog &= msg & "\n" + if (verbose and (level in echoedLogs)) or ((not verbose) and (level in echoedLogsSilent)): + setForegroundColor(logColors[level]) + echo msg + setForegroundColor(fgDefault) + +proc getTotalLog*: string = + totalLog + +const progbarLength = 25 +type Buffer* = ref object + contents: string + previous: string + +proc newBuffer*: Buffer = + new(result) + +proc updateProgressBar*(buf: Buffer, text: string, total: int, current: int) = + var newline = "" + newline &= "[" + let ratio = current / total + let filledCount = int(ratio * progbarLength) + for i in countup(1, filledCount): + newline &= "=" + for i in countup(filledCount + 1, progbarLength): + newline &= " " + newline &= &"] ({current}/{total}) {text}" + # to avoid process switching during half-written progress bars and whatnot all terminal editing happens at the end + buf.contents = newline + +proc render*(buf: Buffer) = + if verbose and buf.previous != buf.contents: + echo buf.contents + buf.previous = buf.contents + diff --git a/tests/testutils.nim b/tests/testutils.nim index 8ec4599..113a363 100644 --- a/tests/testutils.nim +++ b/tests/testutils.nim @@ -12,8 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Common code from between the JAPL testing suites -# (during transition from runtests -> Just Another Test Runner +# Test object and helpers import re, strutils, terminal, osproc, strformat, times @@ -33,64 +32,6 @@ type process*: Process cycles*: int -# logging stuff - -type LogLevel* {.pure.} = enum - Debug, # always written to file only (large outputs, such as the entire output of the failing test or stacktrace) - Info, # important information about the progress of the test suite - Error, # failing tests (printed with red) - Stdout, # always printed to stdout only (for cli experience) - - -const echoedLogs = {LogLevel.Info, LogLevel.Error, LogLevel.Stdout} -const echoedLogsSilent = {LogLevel.Error} -const savedLogs = {LogLevel.Debug, LogLevel.Info, LogLevel.Error} - -const logColors = [LogLevel.Debug: fgDefault, LogLevel.Info: fgGreen, LogLevel.Error: fgRed, LogLevel.Stdout: fgYellow] - -var totalLog = "" -var verbose = true -proc setVerbosity*(verb: bool) = - verbose = verb - -proc log*(level: LogLevel, msg: string) = - let msg = &"[{$level} - {$getTime()}] {msg}" - if level in savedLogs: - totalLog &= msg & "\n" - if (verbose and (level in echoedLogs)) or ((not verbose) and (level in echoedLogsSilent)): - setForegroundColor(logColors[level]) - echo msg - setForegroundColor(fgDefault) - -proc getTotalLog*: string = - totalLog - -const progbarLength = 25 -type Buffer* = ref object - contents: string - previous: string - -proc newBuffer*: Buffer = - new(result) - -proc updateProgressBar*(buf: Buffer, text: string, total: int, current: int) = - var newline = "" - newline &= "[" - let ratio = current / total - let filledCount = int(ratio * progbarLength) - for i in countup(1, filledCount): - newline &= "=" - for i in countup(filledCount + 1, progbarLength): - newline &= " " - newline &= &"] ({current}/{total}) {text}" - # to avoid process switching during half-written progress bars and whatnot all terminal editing happens at the end - buf.contents = newline - -proc render*(buf: Buffer) = - if verbose and buf.previous != buf.contents: - echo buf.contents - buf.previous = buf.contents - # parsing the test notation proc compileExpectedOutput*(source: string): string =