Skip to content

Commit

Permalink
atomicfile: clean up example output before exit
Browse files Browse the repository at this point in the history
Wire up a TestMain that creates the temp directory and cleans it up before
exiting from the test binary.  This avoids littering the user's temp directory
with example outputs.
  • Loading branch information
creachadair committed Aug 26, 2024
1 parent 3ef1948 commit e3af1b0
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/creachadair/atomicfile"
)

var tempDir string

func init() {
dir, err := os.MkdirTemp("", "example")
if err != nil {
panic(err)
}
tempDir = dir
}

func cat(path string) {
f, err := os.Open(path)
if err != nil {
Expand Down Expand Up @@ -96,3 +89,16 @@ func ExampleFile_Cancel() {
// left right
// left right
}

func TestMain(m *testing.M) {
// Set up a temporary directory for the examples that will get cleaned up
// before the tests exit.
var err error
tempDir, err = os.MkdirTemp("", "atomicfile-example")
if err != nil {
log.Fatalf("Create example output directory: %v", err)
}
code := m.Run()
os.RemoveAll(tempDir)
os.Exit(code)
}

0 comments on commit e3af1b0

Please sign in to comment.