aboutsummaryrefslogtreecommitdiffhomepage
path: root/helpers
diff options
context:
space:
mode:
authorOleksandr Redko <[email protected]>2023-05-15 11:52:06 +0300
committerBjørn Erik Pedersen <[email protected]>2023-05-16 18:35:19 +0200
commit3d90871e9e8a353e34a44ac50ab5cb1e61bc1005 (patch)
tree20f8fdd05bd5a9c1862c6a59d42d4f4654aa0d1b /helpers
parentbda082c98c7a0abdb019f12bb21cbaa7b0e8f60e (diff)
downloadhugo-3d90871e9e8a353e34a44ac50ab5cb1e61bc1005.tar.gz
hugo-3d90871e9e8a353e34a44ac50ab5cb1e61bc1005.zip
helpers: simplify path tests with T.TempDir
Diffstat (limited to 'helpers')
-rw-r--r--helpers/path_test.go51
1 files changed, 21 insertions, 30 deletions
diff --git a/helpers/path_test.go b/helpers/path_test.go
index 85081c5be..45b692923 100644
--- a/helpers/path_test.go
+++ b/helpers/path_test.go
@@ -239,42 +239,35 @@ func TestIsDir(t *testing.T) {
}
}
-func createZeroSizedFileInTempDir() (*os.File, error) {
+func createZeroSizedFileInTempDir(t *testing.T) *os.File {
+ t.Helper()
+
filePrefix := "_path_test_"
- f, e := os.CreateTemp("", filePrefix) // dir is os.TempDir()
- if e != nil {
- // if there was an error no file was created.
- // => no requirement to delete the file
- return nil, e
+ f, err := os.CreateTemp(t.TempDir(), filePrefix)
+ if err != nil {
+ t.Error(err)
}
- return f, nil
+ if err := f.Close(); err != nil {
+ t.Error(err)
+ }
+ return f
}
-func createNonZeroSizedFileInTempDir() (*os.File, error) {
- f, err := createZeroSizedFileInTempDir()
- if err != nil {
- // no file ??
- return nil, err
- }
+func createNonZeroSizedFileInTempDir(t *testing.T) *os.File {
+ t.Helper()
+
+ f := createZeroSizedFileInTempDir(t)
byteString := []byte("byteString")
- err = os.WriteFile(f.Name(), byteString, 0644)
+ err := os.WriteFile(f.Name(), byteString, 0644)
if err != nil {
- // delete the file
- deleteFileInTempDir(f)
- return nil, err
+ t.Error(err)
}
- return f, nil
-}
-
-func deleteFileInTempDir(f *os.File) {
- _ = os.Remove(f.Name())
+ return f
}
func TestExists(t *testing.T) {
- zeroSizedFile, _ := createZeroSizedFileInTempDir()
- defer deleteFileInTempDir(zeroSizedFile)
- nonZeroSizedFile, _ := createNonZeroSizedFileInTempDir()
- defer deleteFileInTempDir(nonZeroSizedFile)
+ zeroSizedFile := createZeroSizedFileInTempDir(t)
+ nonZeroSizedFile := createNonZeroSizedFileInTempDir(t)
emptyDirectory := t.TempDir()
nonExistentFile := os.TempDir() + "/this-file-does-not-exist.txt"
nonExistentDir := os.TempDir() + "/this/directory/does/not/exist/"
@@ -420,8 +413,7 @@ func TestFindCWD(t *testing.T) {
}
func TestSafeWriteToDisk(t *testing.T) {
- emptyFile, _ := createZeroSizedFileInTempDir()
- defer deleteFileInTempDir(emptyFile)
+ emptyFile := createZeroSizedFileInTempDir(t)
tmpDir := t.TempDir()
randomString := "This is a random string!"
@@ -461,8 +453,7 @@ func TestSafeWriteToDisk(t *testing.T) {
}
func TestWriteToDisk(t *testing.T) {
- emptyFile, _ := createZeroSizedFileInTempDir()
- defer deleteFileInTempDir(emptyFile)
+ emptyFile := createZeroSizedFileInTempDir(t)
tmpDir := t.TempDir()
randomString := "This is a random string!"