Skip to content

Commit

Permalink
Tidy up tests & improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Oct 17, 2019
1 parent 4c87212 commit 2011254
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 39 deletions.
7 changes: 7 additions & 0 deletions env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# env file for Goland & co.
alfred_workflow_bundleid=net.deanishe.awgo
alfred_workflow_data=./testenv/data
alfred_workflow_cache=./testenv/cache
alfred_workflow_version=1.2.0
alfred_workflow_name=AwGo
AW_SESSION_ID=test-session-id
3 changes: 1 addition & 2 deletions modd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ env.sh
modd.conf
run-tests.sh
**/*.go
!_examples/**
!vendor/** {
!_examples/** {
prep: "
# run unit tests
GO111MODULE=on ./run-tests.sh -ic @dirmods
Expand Down
8 changes: 2 additions & 6 deletions update/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,11 @@ type giteaSource struct {
func (src *giteaSource) Downloads() ([]Download, error) {
if src.dls == nil {
src.dls = []Download{}
// rels := []*Release{}
js, err := src.fetch(src.url())
if err != nil {
// log.Printf("error: fetch GitHub releases: %s", err)
return nil, err
}
// log.Printf("%d bytes of JSON", len(js))
if src.dls, err = parseGiteaReleases(js); err != nil {
// log.Printf("error: parse GitHub releases: %s", err)
return nil, err
}
}
Expand Down Expand Up @@ -102,8 +98,8 @@ type giteaAsset struct {
// parseGiteaReleases parses Gitea releases JSON.
func parseGiteaReleases(js []byte) ([]Download, error) {
var (
rels = []*giteaRelease{}
dls = []Download{}
rels []*giteaRelease
dls []Download
)
if err := json.Unmarshal(js, &rels); err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
// UpdateInterval is how often to check for updates.
UpdateInterval = time.Duration(24 * time.Hour)
// HTTPTimeout is the timeout for establishing an HTTP(S) connection.
HTTPTimeout = (60 * time.Second)
HTTPTimeout = 60 * time.Second

// HTTP client used to talk to APIs
client *http.Client
Expand Down Expand Up @@ -265,7 +265,7 @@ func (u *Updater) clearCache() {
util.MustExist(u.cacheDir)
}

// cacheLastCheck saves time to cachepath.
// cacheLastCheck saves time to cache.
func (u *Updater) cacheLastCheck() {
data, err := u.LastCheck.MarshalText()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,7 @@ func TestHTTPClient(t *testing.T) {
}
})
}

func TestRunCommand(t *testing.T) {
assert.Nil(t, runCommand("/usr/bin/true"), `exec "/usr/bin/true" returned error`)
}
6 changes: 5 additions & 1 deletion util/build/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ func withTempDir(fn func(dir string)) {

fn(path)

defer os.RemoveAll(tmp)
defer func() {
if err := os.RemoveAll(tmp); err != nil {
panic(fmt.Sprintf("remove temp dir: %v", err))
}
}()
}

func TestSymlink(t *testing.T) {
Expand Down
19 changes: 19 additions & 0 deletions util/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"strings"
"time"
)

// Shorten paths by replacing user's home directory with ~
Expand Down Expand Up @@ -261,3 +262,21 @@ func ExampleRun_scripts() {
}
}
}

// Timed logs the execution duration of a function with a message.
// Call with defer and time.Now().
func ExampleTimed() {
doThing := func() {
//
defer Timed(time.Now(), "long-running thing")
fmt.Printf("doing long-running thing ...")
// simulate work
time.Sleep(time.Second)
}

// Call function. NOTE: the output from deferred functions is not
// captured.
doThing()
// Output:
// doing long-running thing ...
}
1 change: 0 additions & 1 deletion util/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ type Runners []Runner

// CanRun returns true if one of the runners can run this file.
func (rs Runners) CanRun(filename string) bool {

for _, r := range rs {
if r.CanRun(filename) {
return true
Expand Down
56 changes: 29 additions & 27 deletions util/scripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestExecutableRunner(t *testing.T) {
Expand Down Expand Up @@ -107,14 +109,16 @@ func TestRun(t *testing.T) {
t.Run(fmt.Sprintf("Run(%s)", script), func(t *testing.T) {
t.Parallel()
x := filepath.Base(script)

// test Run
out, err := Run(script, x)
if err != nil {
t.Errorf("failed: %v", err)
}
v := strings.TrimSpace(string(out))
if v != x {
t.Errorf("Bad output. Expected=%v, Got=%v", x, v)
}
assert.Nil(t, err, "script %q failed: %v", script, err)
assert.Equal(t, strings.TrimSpace(string(out)), x, "bad output")

// test runners
out, err = runners.Run(script, x)
assert.Nil(t, err, "script %q failed: %v", script, err)
assert.Equal(t, strings.TrimSpace(string(out)), x, "bad output")
})
}
}
Expand All @@ -131,21 +135,30 @@ func TestNoRun(t *testing.T) {
{"testdata/non-existent", false, true},
{"testdata/plain.txt", true, false},
{"testdata/perl.pl", true, false},
{"testdata", true, false},
}

for _, td := range tests {
td := td // capture variable
t.Run(fmt.Sprintf("Run(%s)", td.in), func(t *testing.T) {
t.Parallel()

_, err := Run(td.in, "blah")
if err == nil {
t.Errorf("Ran bad script %q", td.in)
assert.NotNil(t, err, "ran invalid script %q", td.in)
if td.unknown {
assert.Equal(t, ErrUnknownFileType, err, "invalid file recognised")
}
if td.missing {
assert.True(t, os.IsNotExist(err), "non-existent file accepted")
}
if td.unknown && err != ErrUnknownFileType {
t.Errorf("Unknown file recognised %q. Expected=%v, Got=%v", td.in, ErrUnknownFileType, err)

_, err = runners.Run(td.in, "blah")
assert.NotNil(t, err, "ran invalid script %q", td.in)
if td.unknown {
assert.Equal(t, ErrUnknownFileType, err, "invalid file recognised")
}
if td.missing && !os.IsNotExist(err) {
t.Errorf("Missing file found %q. Expected=ErrNotExist, Got=%v", td.in, err)
if td.missing {
assert.True(t, os.IsNotExist(err), "non-existent file accepted")
}
})
}
Expand Down Expand Up @@ -196,13 +209,8 @@ func TestNewScriptRunner(t *testing.T) {
bad++
}
}

if good != td.good {
t.Errorf("Bad good. Expected=%d, Got=%d", td.good, good)
}
if bad != td.bad {
t.Errorf("Bad bad. Expected=%d, Got=%d", td.bad, bad)
}
assert.Equal(t, td.good, good, "unexpected good count")
assert.Equal(t, td.bad, bad, "unexpected bad count")
})
}
}
Expand All @@ -222,12 +230,6 @@ func TestQuoteJS(t *testing.T) {
}

for _, td := range data {

s := QuoteJS(td.in)

if s != td.out {
t.Errorf("Bad JS for %#v. Expected=%v, Got=%v", td.in, td.out, s)
}

assert.Equal(t, td.out, QuoteJS(td.in), "unexpected quoted JS")
}
}

0 comments on commit 2011254

Please sign in to comment.