New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use temporary directory for coverage reports. #647
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,14 +9,17 @@ import ( | |
"flag" | ||
"fmt" | ||
"io/fs" | ||
"io/ioutil" | ||
"log" | ||
"net" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
"os/signal" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"syscall" | ||
"time" | ||
|
||
"golang.org/x/tools/go/packages" | ||
|
@@ -31,38 +34,50 @@ import ( | |
) | ||
|
||
func init() { | ||
flags() | ||
folders() | ||
} | ||
func flags() { | ||
flag.IntVar(&port, "port", 8080, "The port at which to serve http.") | ||
flag.StringVar(&host, "host", "127.0.0.1", "The host at which to serve http.") | ||
flag.DurationVar(&nap, "poll", quarterSecond, "The interval to wait between polling the file system for changes.") | ||
flag.IntVar(¶llelPackages, "packages", 10, "The number of packages to test in parallel. Higher == faster but more costly in terms of computing.") | ||
flag.StringVar(&gobin, "gobin", "go", "The path to the 'go' binary (default: search on the PATH).") | ||
flag.BoolVar(&cover, "cover", true, "Enable package-level coverage statistics. Requires Go 1.2+ and the go cover tool.") | ||
flag.BoolVar(&cover, "cover", true, "Enable package-level coverage statistics.") | ||
flag.IntVar(&depth, "depth", -1, "The directory scanning depth. If -1, scan infinitely deep directory structures. 0: scan working directory. 1+: Scan into nested directories, limited to value.") | ||
flag.StringVar(&timeout, "timeout", "0", "The test execution timeout if none is specified in the *.goconvey file (default is '0', which is the same as not providing this option).") | ||
flag.StringVar(&watchedSuffixes, "watchedSuffixes", ".go", "A comma separated list of file suffixes to watch for modifications.") | ||
flag.StringVar(&excludedDirs, "excludedDirs", "vendor,node_modules", "A comma separated list of directories that will be excluded from being watched.") | ||
flag.StringVar(&workDir, "workDir", "", "set goconvey working directory (default current directory).") | ||
flag.BoolVar(&autoLaunchBrowser, "launchBrowser", true, "toggle auto launching of browser.") | ||
flag.BoolVar(&leakTemp, "leakTemp", false, "leak temp dir with coverage reports.") | ||
|
||
log.SetOutput(os.Stdout) | ||
log.SetFlags(log.LstdFlags | log.Lshortfile) | ||
} | ||
func folders() { | ||
_, file, _, _ := runtime.Caller(0) | ||
here := filepath.Dir(file) | ||
reports = filepath.Join(filepath.Join(here, "/web/client"), "reports") | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
log.Printf(initialConfiguration, host, port, nap, cover) | ||
|
||
tmpDir, err := ioutil.TempDir("", "*.goconvey") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
reports := filepath.Join(tmpDir, "coverage_out") | ||
if err := os.Mkdir(reports, 0700); err != nil { | ||
log.Fatal(err) | ||
} | ||
if leakTemp { | ||
log.Printf("leaking temporary directory %q\n", tmpDir) | ||
} else { | ||
defer func() { | ||
if err := os.RemoveAll(tmpDir); err != nil { | ||
log.Printf("failed to clean temporary directory %q: %s\n", tmpDir, err) | ||
} | ||
}() | ||
} | ||
|
||
done := make(chan os.Signal) | ||
signal.Notify(done, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) | ||
|
||
working := getWorkDir() | ||
cover = coverageEnabled(cover, reports) | ||
shell := system.NewShell(gobin, reports, cover, timeout) | ||
|
||
watcherInput := make(chan messaging.WatcherCommand) | ||
|
@@ -83,7 +98,13 @@ func main() { | |
if autoLaunchBrowser { | ||
go launchBrowser(listener.Addr().String()) | ||
} | ||
serveHTTP(server, listener) | ||
srv := serveHTTP(reports, server, listener) | ||
|
||
<-done | ||
log.Println("shutting down") | ||
if err := srv.Shutdown(nil); err != nil { | ||
log.Printf("failed to shutdown: %s\n", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed this Fatalf as well |
||
} | ||
} | ||
|
||
func browserCmd() (string, bool) { | ||
|
@@ -179,24 +200,16 @@ func createListener() net.Listener { | |
return l | ||
} | ||
|
||
func serveHTTP(server contract.Server, listener net.Listener) { | ||
serveStaticResources() | ||
serveAjaxMethods(server) | ||
activateServer(listener) | ||
} | ||
|
||
//go:embed web/client | ||
var static embed.FS | ||
|
||
func serveStaticResources() { | ||
func serveHTTP(reports string, server contract.Server, listener net.Listener) *http.Server { | ||
webclient, err := fs.Sub(static, "web/client") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
http.Handle("/", http.FileServer(http.FS(webclient))) | ||
} | ||
|
||
func serveAjaxMethods(server contract.Server) { | ||
http.HandleFunc("/watch", server.Watch) | ||
http.HandleFunc("/ignore", server.Ignore) | ||
http.HandleFunc("/reinstate", server.Reinstate) | ||
|
@@ -205,36 +218,18 @@ func serveAjaxMethods(server contract.Server) { | |
http.HandleFunc("/status", server.Status) | ||
http.HandleFunc("/status/poll", server.LongPollStatus) | ||
http.HandleFunc("/pause", server.TogglePause) | ||
} | ||
|
||
func activateServer(listener net.Listener) { | ||
log.Printf("Serving HTTP at: http://%s\n", listener.Addr()) | ||
err := http.Serve(listener, nil) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
} | ||
|
||
func coverageEnabled(cover bool, reports string) bool { | ||
return (cover && | ||
ensureReportDirectoryExists(reports)) | ||
} | ||
|
||
func ensureReportDirectoryExists(reports string) bool { | ||
result, err := exists(reports) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if result { | ||
return true | ||
} | ||
http.Handle("/reports", http.FileServer(http.Dir(reports))) | ||
|
||
if err := os.Mkdir(reports, 0755); err == nil { | ||
return true | ||
} | ||
|
||
log.Printf(reportDirectoryUnavailable, reports) | ||
return false | ||
log.Printf("Serving HTTP at: http://%s\n", listener.Addr()) | ||
ret := &http.Server{} | ||
go func() { | ||
err := http.Serve(listener, nil) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
}() | ||
return ret | ||
} | ||
|
||
func exists(path string) (bool, error) { | ||
|
@@ -281,20 +276,16 @@ var ( | |
watchedSuffixes string | ||
excludedDirs string | ||
autoLaunchBrowser bool | ||
|
||
reports string | ||
leakTemp bool | ||
|
||
quarterSecond = time.Millisecond * 250 | ||
workDir string | ||
) | ||
|
||
const ( | ||
initialConfiguration = "Initial configuration: [host: %s] [port: %d] [poll: %v] [cover: %v]\n" | ||
pleaseUpgradeGoVersion = "Go version is less that 1.2 (%s), please upgrade to the latest stable version to enable coverage reporting.\n" | ||
coverToolMissing = "Go cover tool is not installed or not accessible: for Go < 1.5 run`go get golang.org/x/tools/cmd/cover`\n For >= Go 1.5 run `go install $GOROOT/src/cmd/cover`\n" | ||
reportDirectoryUnavailable = "Could not find or create the coverage report directory (at: '%s'). You probably won't see any coverage statistics...\n" | ||
separator = string(filepath.Separator) | ||
endGoPath = separator + "src" + separator | ||
initialConfiguration = "Initial configuration: [host: %s] [port: %d] [poll: %v] [cover: %v]\n" | ||
separator = string(filepath.Separator) | ||
endGoPath = separator + "src" + separator | ||
) | ||
|
||
// This method exists because of a bug in the go cover tool that | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this will compile on Windows. Try GOOS=windows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surprisingly, it does exist: https://cs.opensource.google/go/go/+/master:src/syscall/types_windows.go;l=64?q=SIGTERM&ss=go%2Fgo:src%2Fsyscall%2F
Though I believe its just a noop