Skip to content

Commit 3a5355b

Browse files
authored
Merge pull request #6 from jf-tech/files
Add FileExists and DirExists to ios package
2 parents 7c96261 + c36173d commit 3a5355b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

ios/files.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ios
2+
3+
import (
4+
"os"
5+
)
6+
7+
// FileExists checks if a file exists or not.
8+
func FileExists(file string) bool {
9+
fi, err := os.Stat(file)
10+
if os.IsNotExist(err) {
11+
return false
12+
}
13+
return !fi.IsDir()
14+
}
15+
16+
// DirExists checks if a directory exists or not.
17+
func DirExists(dir string) bool {
18+
fi, err := os.Stat(dir)
19+
if os.IsNotExist(err) {
20+
return false
21+
}
22+
return fi.IsDir()
23+
}

ios/files_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ios
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
10+
"github.com/jf-tech/go-corelib/testlib"
11+
)
12+
13+
func TestFileExists(t *testing.T) {
14+
// non existing case.
15+
assert.False(t, FileExists("non-existing"))
16+
tmp := testlib.CreateTempFileWithContent(t, "", "", "test")
17+
defer os.Remove(tmp.Name())
18+
// file existing case.
19+
assert.True(t, FileExists(tmp.Name()))
20+
// existing but not a file case.
21+
assert.False(t, FileExists(filepath.Dir(tmp.Name())))
22+
}
23+
24+
func TestDirExists(t *testing.T) {
25+
// non existing case.
26+
assert.False(t, DirExists("non-existing"))
27+
tmp := testlib.CreateTempFileWithContent(t, "", "", "test")
28+
defer os.Remove(tmp.Name())
29+
// dir existing case.
30+
assert.True(t, DirExists(filepath.Dir(tmp.Name())))
31+
// existing but not a dir case.
32+
assert.False(t, DirExists(tmp.Name()))
33+
}

0 commit comments

Comments
 (0)