aboutsummaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorArne Juul <arnej@yahooinc.com>2022-10-25 09:22:48 +0000
committerArne Juul <arnej@yahooinc.com>2022-10-25 09:22:48 +0000
commita171610a7db5abef10bb4fc412130092bc1b1bed (patch)
tree895406a017478de040db56c96cd9457f72b8feac /client
parent70026cc89de5a1586f7b70e261d0f09c437a2263 (diff)
avoid dereferencing nil, add unit test
Diffstat (limited to 'client')
-rw-r--r--client/go/util/io.go8
-rw-r--r--client/go/util/io_test.go43
2 files changed, 47 insertions, 4 deletions
diff --git a/client/go/util/io.go b/client/go/util/io.go
index 89f14c1a643..6aab64b8827 100644
--- a/client/go/util/io.go
+++ b/client/go/util/io.go
@@ -16,20 +16,20 @@ import (
// Returns true if the given path exists
func PathExists(path string) bool {
- _, err := os.Stat(path)
- return !errors.Is(err, os.ErrNotExist)
+ info, err := os.Stat(path)
+ return !errors.Is(err, os.ErrNotExist) && info != nil
}
// Returns true if the given path points to an existing directory
func IsDirectory(path string) bool {
info, err := os.Stat(path)
- return !errors.Is(err, os.ErrNotExist) && info.IsDir()
+ return !errors.Is(err, os.ErrNotExist) && info != nil && info.IsDir()
}
// Returns true if the given path points to an existing file
func IsRegularFile(path string) bool {
info, err := os.Stat(path)
- return !errors.Is(err, os.ErrNotExist) && info.Mode().IsRegular()
+ return !errors.Is(err, os.ErrNotExist) && info != nil && info.Mode().IsRegular()
}
// Returns the content of a reader as a string
diff --git a/client/go/util/io_test.go b/client/go/util/io_test.go
new file mode 100644
index 00000000000..ddfa2e624dd
--- /dev/null
+++ b/client/go/util/io_test.go
@@ -0,0 +1,43 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package util
+
+import (
+ "os"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestPathExists(t *testing.T) {
+ assert.Equal(t, true, PathExists("io.go"))
+ assert.Equal(t, false, PathExists("nosuchthing.go"))
+
+ tmpDir := t.TempDir()
+ err := os.MkdirAll(tmpDir+"/no", 0755)
+ assert.Nil(t, err)
+ err = os.MkdirAll(tmpDir+"/no/such", 0)
+ assert.Nil(t, err)
+ assert.Equal(t, false, PathExists(tmpDir+"/no/such/thing.go"))
+}
+
+func TestIsDir(t *testing.T) {
+ tmpDir := t.TempDir()
+ err := os.MkdirAll(tmpDir+"/no", 0755)
+ assert.Nil(t, err)
+ assert.Equal(t, true, IsDirectory(tmpDir+"/no"))
+ err = os.MkdirAll(tmpDir+"/no/such", 0)
+ assert.Nil(t, err)
+ assert.Equal(t, true, IsDirectory(tmpDir+"/no/such"))
+ assert.Equal(t, false, IsDirectory(tmpDir+"/no/such/thing.go"))
+}
+
+func TestIsRegularFile(t *testing.T) {
+ assert.Equal(t, true, IsRegularFile("io.go"))
+ assert.Equal(t, false, IsRegularFile("."))
+ tmpDir := t.TempDir()
+ err := os.MkdirAll(tmpDir+"/no", 0755)
+ assert.Nil(t, err)
+ err = os.MkdirAll(tmpDir+"/no/such", 0)
+ assert.Nil(t, err)
+ assert.Equal(t, false, IsRegularFile(tmpDir+"/no/such/thing.go"))
+}