From 7f134f30e92c92d48500af50d2200e9bc677ccae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 7 Jun 2018 11:56:55 +0100 Subject: [PATCH] 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. --- cmd/fdroidcl/endtoend_test.go | 69 +++++++++++++++++++++++++++++++++++ cmd/fdroidcl/main.go | 8 ++++ 2 files changed, 77 insertions(+) create mode 100644 cmd/fdroidcl/endtoend_test.go diff --git a/cmd/fdroidcl/endtoend_test.go b/cmd/fdroidcl/endtoend_test.go new file mode 100644 index 0000000..1d7ecee --- /dev/null +++ b/cmd/fdroidcl/endtoend_test.go @@ -0,0 +1,69 @@ +// Copyright (c) 2018, Daniel Martí +// 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) + } +} diff --git a/cmd/fdroidcl/main.go b/cmd/fdroidcl/main.go index 5974834..71e6631 100644 --- a/cmd/fdroidcl/main.go +++ b/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")