diff options
Diffstat (limited to 'internal/osutil')
-rw-r--r-- | internal/osutil/osutil.go | 6 | ||||
-rw-r--r-- | internal/osutil/osutil_test.go | 23 |
2 files changed, 29 insertions, 0 deletions
diff --git a/internal/osutil/osutil.go b/internal/osutil/osutil.go index 656f2e2a..827988a0 100644 --- a/internal/osutil/osutil.go +++ b/internal/osutil/osutil.go @@ -17,6 +17,12 @@ func IsFile(path string) bool { return !f.IsDir() } +// IsExist returns true if a file or directory exists. +func IsExist(path string) bool { + _, err := os.Stat(path) + return err == nil || os.IsExist(err) +} + // CurrentUsername returns the current system user via environment variables. func CurrentUsername() string { curUserName := os.Getenv("USER") diff --git a/internal/osutil/osutil_test.go b/internal/osutil/osutil_test.go index adaebebf..4c9a2ad7 100644 --- a/internal/osutil/osutil_test.go +++ b/internal/osutil/osutil_test.go @@ -32,3 +32,26 @@ func TestIsFile(t *testing.T) { }) } } + +func TestIsExist(t *testing.T) { + tests := []struct { + path string + expVal bool + }{ + { + path: "osutil.go", + expVal: true, + }, { + path: "../osutil", + expVal: true, + }, { + path: "not_found", + expVal: false, + }, + } + for _, test := range tests { + t.Run("", func(t *testing.T) { + assert.Equal(t, test.expVal, IsExist(test.path)) + }) + } +} |