grim/resticide
Clone
Summary
Browse
Changes
Graph
Give the --help output some tlc, also added a --version argument
develop
2016-07-09, Gary Kramlich
32bc94f303e2
Give the --help output some tlc, also added a --version argument
package
reporter
import
(
"fmt"
"os"
"strings"
"sync"
"text/tabwriter"
"bitbucket.org/rw_grim/resticide/test"
)
// ConsoleReporter is a report that outputs to the console
type
ConsoleReporter
struct
{
verbose
bool
lock
*
sync
.
Mutex
}
// Init is called to initialize the reporter
func
(
reporter
*
ConsoleReporter
)
Init
(
verbose
bool
)
{
reporter
.
verbose
=
verbose
reporter
.
lock
=
new
(
sync
.
Mutex
)
}
// Start is called when testing is started
func
(
reporter
*
ConsoleReporter
)
Start
(
tests
int
)
{
}
// TestStart is called when a test starts running
func
(
reporter
*
ConsoleReporter
)
TestStart
(
test
test
.
Test
)
{
}
// TestFinish is called when a test finishes running
func
(
reporter
*
ConsoleReporter
)
TestFinish
(
test
test
.
Test
,
res
test
.
Result
)
{
// This sucks, but yes we need to lock stdout so our results make sense..
reporter
.
lock
.
Lock
()
defer
reporter
.
lock
.
Unlock
()
status
:=
"failed"
if
res
.
Passed
{
status
=
"passed"
}
fmt
.
Printf
(
"%s %s %s\n"
,
test
.
Name
,
status
,
test
.
Duration
.
String
())
if
!
res
.
Passed
{
fmt
.
Printf
(
" %s\n"
,
res
.
Error
.
Error
())
}
if
reporter
.
verbose
{
writer
:=
new
(
tabwriter
.
Writer
)
writer
.
Init
(
os
.
Stdout
,
2
,
2
,
2
,
' '
,
0
)
fmt
.
Fprintf
(
writer
,
"\tStatus Code\t%d\n"
,
res
.
HTTPResponse
.
StatusCode
)
fmt
.
Fprintln
(
writer
,
"\tHeaders:"
)
for
name
,
value
:=
range
res
.
HTTPResponse
.
Header
{
fmt
.
Fprintf
(
writer
,
"\t\t%s\t%s\n"
,
name
,
strings
.
Join
(
value
,
";"
))
}
writer
.
Flush
()
}
}
// Finish is called when testing has completed.
func
(
reporter
*
ConsoleReporter
)
Finish
(
tests
int
,
passed
int
,
failed
int
)
{
fmt
.
Printf
(
"\n%s\n"
,
strings
.
Repeat
(
"-"
,
40
))
fmt
.
Printf
(
"%d tests, %d passed, %d failed\n"
,
tests
,
passed
,
failed
)
}
// LoadTestsFailed is called when multiple tests fail to load
func
(
reporter
*
ConsoleReporter
)
LoadTestsFailed
(
err
error
)
{
}
// LoadTestFailed is called when a test fails to load
func
(
reporter
*
ConsoleReporter
)
LoadTestFailed
(
path
string
,
err
error
)
{
fmt
.
Printf
(
"failed to load test '%s': %s\n"
,
path
,
err
.
Error
())
}