mirror of https://github.com/mvdan/fdroidcl.git
Browse Source
This has multiple advantages. For one, we can simplify our code, as we can use globals like os.Stdout and flagsets directly. It will also be easier to write parallel tests which will run faster, as each testscript runs concurrently. While at it, start using $HOME if set, as that is often better than blindly relying on the current user's home directory via cgo. Go 1.11 and 1.12 add os.UserCacheDir and os.UserHomeDir respectively, so soon enough we'll be able to replace most of basedir with those. Finally, stop having the tests require on the f-droid.org repository to work. Committing under 2MB of data is plenty to get the files we need in a static local http server, which makes the tests much faster and not depend on an internet connection. Follow-up commits will finish porting the rest of the tests.pull/44/head
16 changed files with 208 additions and 115 deletions
@ -1,3 +1,6 @@
|
||||
module mvdan.cc/fdroidcl |
||||
|
||||
require github.com/kr/pretty v0.1.0 |
||||
require ( |
||||
github.com/kr/pretty v0.1.0 |
||||
github.com/rogpeppe/go-internal v1.1.0 |
||||
) |
||||
|
||||
@ -0,0 +1,82 @@
|
||||
package main |
||||
|
||||
import ( |
||||
"net" |
||||
"net/http" |
||||
"os" |
||||
"path/filepath" |
||||
"strconv" |
||||
"testing" |
||||
"text/template" |
||||
|
||||
"github.com/rogpeppe/go-internal/testscript" |
||||
) |
||||
|
||||
func TestMain(m *testing.M) { |
||||
if os.Getenv("TESTSCRIPT_COMMAND") == "" { |
||||
startStaticRepo() |
||||
} |
||||
|
||||
os.Exit(testscript.RunMain(m, map[string]func() int{ |
||||
"fdroidcl": main1, |
||||
})) |
||||
} |
||||
|
||||
var staticRepoURL string |
||||
|
||||
func startStaticRepo() { |
||||
path := filepath.Join("testdata", "staticrepo") |
||||
fs := http.FileServer(http.Dir(path)) |
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
||||
// The files are static, so add a unique etag for each file.
|
||||
w.Header().Set("Etag", strconv.Quote(r.URL.Path)) |
||||
fs.ServeHTTP(w, r) |
||||
}) |
||||
ln, err := net.Listen("tcp", ":0") |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
go http.Serve(ln, handler) |
||||
staticRepoURL = "http://" + ln.Addr().String() |
||||
} |
||||
|
||||
var testConfigTmpl = template.Must(template.New("").Parse(` |
||||
{ |
||||
"repos": [ |
||||
{ |
||||
"id": "local f-droid", |
||||
"url": "{{.}}", |
||||
"enabled": true |
||||
} |
||||
] |
||||
} |
||||
`[1:])) |
||||
|
||||
func TestScripts(t *testing.T) { |
||||
t.Parallel() |
||||
testscript.Run(t, testscript.Params{ |
||||
Dir: filepath.Join("testdata", "scripts"), |
||||
Setup: func(e *testscript.Env) error { |
||||
home := e.WorkDir + "/home" |
||||
if err := os.MkdirAll(home, 0777); err != nil { |
||||
return err |
||||
} |
||||
e.Vars = append(e.Vars, "HOME="+home) |
||||
e.Vars = append(e.Vars, "REPOURL="+staticRepoURL) |
||||
|
||||
config := home + "/config.json" |
||||
f, err := os.Create(config) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if err := testConfigTmpl.Execute(f, staticRepoURL); err != nil { |
||||
return err |
||||
} |
||||
if err := f.Close(); err != nil { |
||||
return err |
||||
} |
||||
e.Vars = append(e.Vars, "FDROIDCL_CONFIG="+config) |
||||
return nil |
||||
}, |
||||
}) |
||||
} |
||||
@ -0,0 +1,23 @@
|
||||
env HOME=$WORK/home |
||||
|
||||
! fdroidcl |
||||
stderr '^usage: fdroidcl \[-h' |
||||
|
||||
! fdroidcl -h |
||||
stderr '^usage: fdroidcl \[-h' |
||||
! stderr 'test\.' # don't include flags from testing |
||||
! stderr 'command not specified' |
||||
! stdout . |
||||
|
||||
fdroidcl version |
||||
stdout '^v0\.4' |
||||
|
||||
! fdroidcl -badflag -- somepkg |
||||
stderr '-badflag' |
||||
stderr '^usage: fdroidcl \[-h' |
||||
|
||||
! fdroidcl search -h |
||||
stderr '^usage: fdroidcl search .*regexp' |
||||
stderr '-i.*Filter installed apps' |
||||
|
||||
! fdroidcl |
||||
@ -0,0 +1,10 @@
|
||||
env HOME=$WORK/home |
||||
|
||||
! fdroidcl search |
||||
stderr 'index does not exist' |
||||
|
||||
fdroidcl update |
||||
stdout 'done' |
||||
|
||||
fdroidcl update |
||||
stdout 'not modified' |
||||
Loading…
Reference in new issue