github.com/hattya/nazuna@v0.7.1-0.20240331055452-55e14c275c1c/nazuna_test.go (about) 1 // 2 // nazuna :: nazuna_test.go 3 // 4 // Copyright (c) 2013-2022 Akinori Hattori <hattya@gmail.com> 5 // 6 // SPDX-License-Identifier: MIT 7 // 8 9 package nazuna_test 10 11 import ( 12 "bytes" 13 "os" 14 "os/exec" 15 "path/filepath" 16 "testing" 17 18 "github.com/hattya/nazuna" 19 ) 20 21 func init() { 22 nazuna.Discover(false) 23 } 24 25 type testUI struct { 26 bytes.Buffer 27 } 28 29 func (*testUI) Print(...interface{}) (int, error) { return 0, nil } 30 func (*testUI) Printf(string, ...interface{}) (int, error) { return 0, nil } 31 func (*testUI) Println(...interface{}) (int, error) { return 0, nil } 32 func (*testUI) Error(...interface{}) (int, error) { return 0, nil } 33 func (*testUI) Errorf(string, ...interface{}) (int, error) { return 0, nil } 34 func (*testUI) Errorln(...interface{}) (int, error) { return 0, nil } 35 36 func (ui *testUI) Exec(cmd *exec.Cmd) error { 37 cmd.Stdout = ui 38 cmd.Stderr = ui 39 return cmd.Run() 40 } 41 42 func mkdir(s ...string) error { 43 return os.MkdirAll(filepath.Join(s...), 0o777) 44 } 45 46 func sandbox(t *testing.T) string { 47 t.Helper() 48 49 wd, err := os.Getwd() 50 if err != nil { 51 t.Fatal("sandbox:", err) 52 } 53 dir := t.TempDir() 54 if err := os.Chdir(dir); err != nil { 55 t.Fatal("sandbox:", err) 56 } 57 t.Setenv("PWD", dir) 58 t.Cleanup(func() { 59 if err := os.Chdir(wd); err != nil { 60 t.Error("sandbox:", err) 61 } 62 }) 63 return dir 64 } 65 66 func touch(s ...string) error { 67 return os.WriteFile(filepath.Join(s...), []byte{}, 0o666) 68 }