aboutsummaryrefslogtreecommitdiff
path: root/atomictemp/atomictemp_test.go
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-22 21:57:44 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-22 21:57:44 -0500
commitb6c5f5f40635ed83c0d0439fe145ed5b96cab3c5 (patch)
tree5e4cf8b02978e27ed7685735018ba157f2df7138 /atomictemp/atomictemp_test.go
parent0d22d317612fee62a000aa576af1c129c49207b0 (diff)
downloadcvez-b6c5f5f40635ed83c0d0439fe145ed5b96cab3c5.tar.bz2
cvez-b6c5f5f40635ed83c0d0439fe145ed5b96cab3c5.zip
add atomictemp
Diffstat (limited to 'atomictemp/atomictemp_test.go')
-rw-r--r--atomictemp/atomictemp_test.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/atomictemp/atomictemp_test.go b/atomictemp/atomictemp_test.go
new file mode 100644
index 0000000..401c730
--- /dev/null
+++ b/atomictemp/atomictemp_test.go
@@ -0,0 +1,68 @@
+package atomictemp
+
+import (
+ "errors"
+ "io"
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+func TestCreate(t *testing.T) {
+ dir, err := os.MkdirTemp("", "")
+ if err != nil {
+ t.Error(err)
+ }
+
+ // test good file
+ goodTests := []string { "foo", "bar", "baz" }
+ for _, test := range(goodTests) {
+ t.Run(test, func(t *testing.T) {
+ // build destination path
+ path := filepath.Join(dir, test)
+
+ // create temp file
+ err := Create(path, func(f io.Writer) error {
+ _, err := f.Write([]byte(test))
+ return err
+ })
+
+ if err != nil {
+ t.Error(err)
+ return
+ }
+
+ if got, err := os.ReadFile(path); err != nil {
+ t.Error(err)
+ } else if string(got) != test {
+ t.Errorf("got \"%s\", exp \"%s\"", string(got), test)
+ }
+ })
+ }
+
+ t.Run("badDir", func(t *testing.T) {
+ // build nonsense path to destination file
+ badPath := filepath.Join(dir, "does/not/exist")
+
+ err := Create(badPath, func(_ io.Writer) error {
+ return nil
+ })
+
+ if err == nil {
+ t.Errorf("got success, exp error")
+ }
+ })
+
+ t.Run("badFunc", func(t *testing.T) {
+ // build path
+ path := filepath.Join(dir, "badFunc")
+
+ err := Create(path, func(_ io.Writer) error {
+ return errors.New("ack!")
+ })
+
+ if err == nil {
+ t.Errorf("got success, exp error")
+ }
+ })
+}