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-mac
|
||||||
gossa-windows.exe
|
gossa-windows.exe
|
||||||
build-all
|
build-all
|
||||||
|
gossa.test
|
||||||
|
|
||||||
|
*.out
|
||||||
.vscode
|
.vscode
|
||||||
test-fixture/*
|
test-fixture/*
|
||||||
test-fixture/*/*
|
test-fixture/*/*
|
||||||
|
|
|
||||||
25
Makefile
25
Makefile
|
|
@ -18,24 +18,27 @@ run-extra:
|
||||||
./gossa -verb=true -prefix="/fancy-path/" -k=false -symlinks=true test-fixture
|
./gossa -verb=true -prefix="/fancy-path/" -k=false -symlinks=true test-fixture
|
||||||
|
|
||||||
test:
|
test:
|
||||||
make build
|
|
||||||
-@cd test-fixture && ln -s ../support .; true
|
-@cd test-fixture && ln -s ../support .; true
|
||||||
|
go test -cover -c -tags testrunmain
|
||||||
|
|
||||||
-@killall gossa; true
|
timeout -s SIGINT 3 ./gossa.test -test.coverprofile=normal.out -test.run '^TestRunMain' -verb=true test-fixture &
|
||||||
-make run &
|
sleep 2
|
||||||
go test -run TestNormal
|
go test -run TestNormal
|
||||||
|
|
||||||
killall gossa
|
|
||||||
sleep 1
|
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
|
go test -run TestExtra
|
||||||
|
|
||||||
killall gossa
|
|
||||||
sleep 1
|
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:
|
watch:
|
||||||
ls gossa.go gossa_test.go gossa-ui/* | entr -rc make build run
|
ls gossa.go gossa_test.go gossa-ui/* | entr -rc make build run
|
||||||
|
|
|
||||||
19
gossa.go
19
gossa.go
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
|
"context"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
@ -18,10 +19,12 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var host = flag.String("h", "127.0.0.1", "host to listen to")
|
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)
|
rel, err := filepath.Rel(zipFullPath, path)
|
||||||
check(err)
|
check(err)
|
||||||
if *skipHidden && strings.HasPrefix(rel, ".") {
|
if *skipHidden && (strings.HasPrefix(rel, ".") || strings.HasPrefix(f.Name(), ".")) {
|
||||||
return nil // hidden files not allowed
|
return nil // hidden files not allowed
|
||||||
}
|
}
|
||||||
if f.Mode()&os.ModeSymlink != 0 {
|
if f.Mode()&os.ModeSymlink != 0 {
|
||||||
|
|
@ -288,6 +291,17 @@ func main() {
|
||||||
templateParsed, err = template.New("").Parse(templateStr)
|
templateParsed, err = template.New("").Parse(templateStr)
|
||||||
check(err)
|
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 {
|
if !*ro {
|
||||||
http.HandleFunc(*extraPath+"rpc", rpc)
|
http.HandleFunc(*extraPath+"rpc", rpc)
|
||||||
http.HandleFunc(*extraPath+"post", upload)
|
http.HandleFunc(*extraPath+"post", upload)
|
||||||
|
|
@ -296,6 +310,7 @@ func main() {
|
||||||
http.HandleFunc("/", doContent)
|
http.HandleFunc("/", doContent)
|
||||||
handler = http.StripPrefix(*extraPath, http.FileServer(http.Dir(rootPath)))
|
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)
|
fmt.Printf("Gossa starting on directory %s\nListening on http://%s:%s%s\n", rootPath, *host, *port, *extraPath)
|
||||||
err = http.ListenAndServe(*host+":"+*port, nil)
|
if err = server.ListenAndServe(); err != http.ErrServerClosed {
|
||||||
check(err)
|
check(err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,11 +30,17 @@ func getRaw(t *testing.T, url string) []byte {
|
||||||
return body
|
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)
|
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)
|
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 {
|
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")
|
fmt.Println("\r\n~~~~~~~~~~ test fetching default path")
|
||||||
fetchAndTestDefault(t, url)
|
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")
|
fmt.Println("\r\n~~~~~~~~~~ test fetching an invalid path - redirected to root")
|
||||||
fetchAndTestDefault(t, url+"../../")
|
fetchAndTestDefault(t, url+"../../")
|
||||||
|
|
@ -127,11 +140,20 @@ func doTestRegular(t *testing.T, url string, testExtra bool) {
|
||||||
|
|
||||||
// ~~~~~~~~~~~~~~~~~
|
// ~~~~~~~~~~~~~~~~~
|
||||||
fmt.Println("\r\n~~~~~~~~~~ test zipping of folder 中文")
|
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")
|
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(files) != 1 || files[0].Name != "檔案.html" {
|
if len != 1 || !foundFile {
|
||||||
t.Fatal("invalid zip generated")
|
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")
|
fmt.Println("\r\n~~~~~~~~~~ test zip invalid path")
|
||||||
body0 = get(t, url+"zip?zipPath=%2Ftmp&zipName=subdir")
|
body0 = get(t, url+"zip?zipPath=%2Ftmp&zipName=subdir")
|
||||||
|
|
@ -344,3 +366,7 @@ func TestRo(t *testing.T) {
|
||||||
fmt.Println("========== testing read only ============")
|
fmt.Println("========== testing read only ============")
|
||||||
doTestReadonly(t, "http://127.0.0.1:8001/")
|
doTestReadonly(t, "http://127.0.0.1:8001/")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRunMain(t *testing.T) {
|
||||||
|
main()
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue