mirror of https://github.com/mvdan/fdroidcl.git
Browse Source
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
2 changed files with 77 additions and 0 deletions
@ -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) |
||||
} |
||||
} |
||||
Loading…
Reference in new issue