File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments