From 57cbbe8785d4a53feecdf937def7eff06b1de1ca Mon Sep 17 00:00:00 2001 From: Nevkontakte Date: Sat, 30 Oct 2021 15:47:00 +0100 Subject: [PATCH] Improve temporary file management in `gopherjs test`. - Include Go package name in the temp file name. - Close file handle to the temp file early, since we are not actually using it. - Clean the temporary file up after test execution finished to reduce the clutter in the current directory. I was also going to move temporary files into the OS's temp directory, but it turns out using current directory is deliberate: https://github.com/gopherjs/gopherjs/issues/303. --- tool.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tool.go b/tool.go index b63de17c3..848454671 100644 --- a/tool.go +++ b/tool.go @@ -432,18 +432,19 @@ func main() { return err } } else { - outfile, err = ioutil.TempFile(currentDirectory, "test.") + outfile, err = os.CreateTemp(currentDirectory, pkg.Package.Name+"_test.*.js") if err != nil { return err } + outfile.Close() // Release file handle early, we only need the name. } - defer func() { - outfile.Close() + cleanupTemp := func() { if *outputFilename == "" { os.Remove(outfile.Name()) os.Remove(outfile.Name() + ".map") } - }() + } + defer cleanupTemp() // Safety net in case cleanup after execution doesn't happen. if err := s.WriteCommandPackage(mainPkgArchive, outfile.Name()); err != nil { return err @@ -487,6 +488,8 @@ func main() { err := runNode(outfile.Name(), args, runTestDir(pkg), options.Quiet, testOut) + cleanupTemp() // Eagerly cleanup temporary compiled files after execution. + if testOut != nil { io.Copy(os.Stdout, testOut) }