mirror of
https://github.com/pldubouilh/gossa
synced 2025-12-06 08:22:32 +01:00
add coverage test
This commit is contained in:
parent
5b55ab0a0a
commit
65a6087c60
4 changed files with 66 additions and 20 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -5,7 +5,9 @@ gossa-linux-arm64
|
|||
gossa-mac
|
||||
gossa-windows.exe
|
||||
build-all
|
||||
gossa.test
|
||||
|
||||
*.out
|
||||
.vscode
|
||||
test-fixture/*
|
||||
test-fixture/*/*
|
||||
|
|
|
|||
27
Makefile
27
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
|
||||
|
|
|
|||
21
gossa.go
21
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue