Browse Source

cmd/fdroidcl: start adding first endtoend tests

The tool had very little test coverage up until this point, so end to
end tests will be the best way to add proper coverage without unit
testing every single piece.

When done, the tests will only run what they can, skipping certain
subtests if they can't connect to f-droid.org or if there is no Android
device attached.
pull/32/head
Daniel Martí 8 years ago
parent
commit
7f134f30e9
  1. 69
      cmd/fdroidcl/endtoend_test.go
  2. 8
      cmd/fdroidcl/main.go

69
cmd/fdroidcl/endtoend_test.go

@ -0,0 +1,69 @@
// Copyright (c) 2018, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package main
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"testing"
)
func TestEndToEnd(t *testing.T) {
dir, err := ioutil.TempDir("", "fdroidcl")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
// Build fdroidcl in the temporary directory.
fdroidcl := filepath.Join(dir, "fdroidcl")
if out, err := exec.Command("go", "build",
"-ldflags=-X main.testBasedir="+dir,
"-o", fdroidcl).CombinedOutput(); err != nil {
t.Fatalf("%s", out)
}
mustSucceed := func(t *testing.T, want string, args ...string) {
mustRun(t, true, want, fdroidcl, args...)
}
mustFail := func(t *testing.T, want string, args ...string) {
mustRun(t, false, want, fdroidcl, args...)
}
t.Run("Help", func(t *testing.T) {
mustFail(t, `Usage: fdroidcl`, "-h")
})
t.Run("UnknownCommand", func(t *testing.T) {
mustFail(t, `Unrecognised command`, "unknown")
})
t.Run("Version", func(t *testing.T) {
mustSucceed(t, `^v`, "version")
})
t.Run("SearchBeforeUpdate", func(t *testing.T) {
mustFail(t, `could not open index`, "search")
})
t.Run("UpdateFirst", func(t *testing.T) {
mustSucceed(t, `done`, "update")
})
t.Run("UpdateCached", func(t *testing.T) {
mustSucceed(t, `not modified`, "update")
})
}
func mustRun(t *testing.T, success bool, wantRe, name string, args ...string) {
cmd := exec.Command(name, args...)
out, err := cmd.CombinedOutput()
if success && err != nil {
t.Fatalf("unexpected error: %v\n%s", err, out)
} else if !success && err == nil {
t.Fatalf("expected error, got none\n%s", out)
}
if !regexp.MustCompile(wantRe).Match(out) {
t.Fatalf("output does not match %#q:\n%s", wantRe, out)
}
}

8
cmd/fdroidcl/main.go

@ -31,7 +31,12 @@ func subdir(dir, name string) string {
return p
}
var testBasedir = ""
func mustCache() string {
if testBasedir != "" {
return subdir(testBasedir, "cache")
}
dir := basedir.Cache()
if dir == "" {
errExit("Could not determine cache dir\n")
@ -40,6 +45,9 @@ func mustCache() string {
}
func mustData() string {
if testBasedir != "" {
return subdir(testBasedir, "data")
}
dir := basedir.Data()
if dir == "" {
errExit("Could not determine data dir\n")

Loading…
Cancel
Save