From 65a6087c609df41f3d30100a91fb0264f0d57f06 Mon Sep 17 00:00:00 2001 From: Pierre Dubouilh Date: Fri, 12 Nov 2021 15:41:39 +0100 Subject: [PATCH] add coverage test --- .gitignore | 2 ++ Makefile | 27 +++++++++++++++------------ gossa.go | 21 ++++++++++++++++++--- gossa_test.go | 36 +++++++++++++++++++++++++++++++----- 4 files changed, 66 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 171e804..8df35c9 100755 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,9 @@ gossa-linux-arm64 gossa-mac gossa-windows.exe build-all +gossa.test +*.out .vscode test-fixture/* test-fixture/*/* diff --git a/Makefile b/Makefile index 8cbf132..92c750c 100755 --- a/Makefile +++ b/Makefile @@ -17,25 +17,28 @@ run-ro: run-extra: ./gossa -verb=true -prefix="/fancy-path/" -k=false -symlinks=true test-fixture -test: - make build + test: -@cd test-fixture && ln -s ../support .; true + go test -cover -c -tags testrunmain - -@killall gossa; true - -make run & + timeout -s SIGINT 3 ./gossa.test -test.coverprofile=normal.out -test.run '^TestRunMain' -verb=true test-fixture & + sleep 2 go test -run TestNormal - - killall gossa sleep 1 - -make run-extra & + + timeout -s SIGINT 3 ./gossa.test -test.coverprofile=extra.out -test.run '^TestRunMain' -prefix='/fancy-path/' -k=false -symlinks=true test-fixture & + sleep 2 go test -run TestExtra - - killall gossa sleep 1 - -make run-ro & - go test -run TestRo - killall gossa + timeout -s SIGINT 3 ./gossa.test -test.coverprofile=ro.out -test.run '^TestRunMain' -ro=true test-fixture & + sleep 2 + go test -run TestRo + sleep 1 + + # gocovmerge ro.out extra.out normal.out > all.out + # go tool cover -html all.out + # go tool cover -func=all.out | grep main | grep '9.\..\%' watch: ls gossa.go gossa_test.go gossa-ui/* | entr -rc make build run diff --git a/gossa.go b/gossa.go index a5a0b35..9d2fdaa 100755 --- a/gossa.go +++ b/gossa.go @@ -3,6 +3,7 @@ package main import ( "archive/zip" "compress/gzip" + "context" _ "embed" "encoding/base64" "encoding/json" @@ -18,10 +19,12 @@ import ( "net/http" "net/url" "os" + "os/signal" "path/filepath" "sort" "strconv" "strings" + "time" ) var host = flag.String("h", "127.0.0.1", "host to listen to") @@ -209,7 +212,7 @@ func zipRPC(w http.ResponseWriter, r *http.Request) { rel, err := filepath.Rel(zipFullPath, path) check(err) - if *skipHidden && strings.HasPrefix(rel, ".") { + if *skipHidden && (strings.HasPrefix(rel, ".") || strings.HasPrefix(f.Name(), ".")) { return nil // hidden files not allowed } if f.Mode()&os.ModeSymlink != 0 { @@ -288,6 +291,17 @@ func main() { templateParsed, err = template.New("").Parse(templateStr) check(err) + server := &http.Server{Addr: *host + ":" + *port, Handler: handler} + + go func() { + sigchan := make(chan os.Signal, 1) + signal.Notify(sigchan, os.Interrupt) + <-sigchan + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + server.Shutdown(ctx) + }() + if !*ro { http.HandleFunc(*extraPath+"rpc", rpc) http.HandleFunc(*extraPath+"post", upload) @@ -296,6 +310,7 @@ func main() { http.HandleFunc("/", doContent) handler = http.StripPrefix(*extraPath, http.FileServer(http.Dir(rootPath))) fmt.Printf("Gossa starting on directory %s\nListening on http://%s:%s%s\n", rootPath, *host, *port, *extraPath) - err = http.ListenAndServe(*host+":"+*port, nil) - check(err) + if err = server.ListenAndServe(); err != http.ErrServerClosed { + check(err) + } } diff --git a/gossa_test.go b/gossa_test.go index cd5172c..9521677 100644 --- a/gossa_test.go +++ b/gossa_test.go @@ -30,11 +30,17 @@ func getRaw(t *testing.T, url string) []byte { return body } -func getZip(t *testing.T, dest string) []*zip.File { +func getZip(t *testing.T, needle string, dest string) (int, bool) { b := getRaw(t, dest) - archive, err := zip.NewReader(bytes.NewReader(b), int64(len(b))) + unzipped, err := zip.NewReader(bytes.NewReader(b), int64(len(b))) dieMaybe(t, err) - return archive.File + length := len(unzipped.File) + for _, file := range unzipped.File { + if file.Name == needle { + return length, true + } + } + return length, false } func get(t *testing.T, url string) string { @@ -103,6 +109,13 @@ func doTestRegular(t *testing.T, url string, testExtra bool) { fmt.Println("\r\n~~~~~~~~~~ test fetching default path") fetchAndTestDefault(t, url) + // ~~~~~~~~~~~~~~~~~ + fmt.Println("\r\n~~~~~~~~~~ test fetching another page") + body0 = get(t, url+"/hols") + if !strings.Contains(body0, "glasgow.jpg") { + t.Fatal("fetching a subfolder failed") + } + // ~~~~~~~~~~~~~~~~~ fmt.Println("\r\n~~~~~~~~~~ test fetching an invalid path - redirected to root") fetchAndTestDefault(t, url+"../../") @@ -127,11 +140,20 @@ func doTestRegular(t *testing.T, url string, testExtra bool) { // ~~~~~~~~~~~~~~~~~ fmt.Println("\r\n~~~~~~~~~~ test zipping of folder 中文") - files := getZip(t, url+"zip?zipPath=%2F%E4%B8%AD%E6%96%87%2F&zipName=%E4%B8%AD%E6%96%87") - if len(files) != 1 || files[0].Name != "檔案.html" { + len, foundFile := getZip(t, "檔案.html", url+"zip?zipPath=%2F%E4%B8%AD%E6%96%87%2F&zipName=%E4%B8%AD%E6%96%87") + if len != 1 || !foundFile { t.Fatal("invalid zip generated") } + // ~~~~~~~~~~~~~~~~~ + fmt.Println("\r\n~~~~~~~~~~ test zipping of folder with hidden file") + _, foundHidden := getZip(t, ".hidden-folder/some-file", url+"zip?zipPath=%2fhols%2f&zipName=hols") + if foundHidden && !testExtra { + t.Fatal("invalid zip generated - shouldnt contain hidden folder") + } else if !foundHidden && testExtra { + t.Fatal("invalid zip generated - should contain hidden folder") + } + // ~~~~~~~~~~~~~~~~~ fmt.Println("\r\n~~~~~~~~~~ test zip invalid path") body0 = get(t, url+"zip?zipPath=%2Ftmp&zipName=subdir") @@ -344,3 +366,7 @@ func TestRo(t *testing.T) { fmt.Println("========== testing read only ============") doTestReadonly(t, "http://127.0.0.1:8001/") } + +func TestRunMain(t *testing.T) { + main() +}