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")