From 41f56ad05d12520bb90f91889fa979465d0b3d6b Mon Sep 17 00:00:00 2001 From: ᴜɴᴋɴᴡᴏɴ Date: Sat, 11 Apr 2020 20:18:05 +0800 Subject: login_source: migrate to GORM and add tests (#6090) * Use GORM in all write paths * Migrate to GORM * Fix lint errors * Use GORM to init table * dbutil: make writer detect error * Add more tests * Rename to clearTables * db: finish adding tests * osutil: add tests * Fix load source files path --- internal/osutil/osutil.go | 10 ++++++++++ internal/osutil/osutil_test.go | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) (limited to 'internal/osutil') diff --git a/internal/osutil/osutil.go b/internal/osutil/osutil.go index 3af67570..b2205a46 100644 --- a/internal/osutil/osutil.go +++ b/internal/osutil/osutil.go @@ -17,6 +17,16 @@ func IsFile(path string) bool { return !f.IsDir() } +// IsDir returns true if given path is a directory, and returns false when it's +// a file or does not exist. +func IsDir(dir string) bool { + f, e := os.Stat(dir) + if e != nil { + return false + } + return f.IsDir() +} + // IsExist returns true if a file or directory exists. func IsExist(path string) bool { _, err := os.Stat(path) diff --git a/internal/osutil/osutil_test.go b/internal/osutil/osutil_test.go index 8c45f5c0..ca2c75bf 100644 --- a/internal/osutil/osutil_test.go +++ b/internal/osutil/osutil_test.go @@ -33,6 +33,29 @@ func TestIsFile(t *testing.T) { } } +func TestIsDir(t *testing.T) { + tests := []struct { + path string + expVal bool + }{ + { + path: "osutil.go", + expVal: false, + }, { + path: "../osutil", + expVal: true, + }, { + path: "not_found", + expVal: false, + }, + } + for _, test := range tests { + t.Run("", func(t *testing.T) { + assert.Equal(t, test.expVal, IsDir(test.path)) + }) + } +} + func TestIsExist(t *testing.T) { tests := []struct { path string -- cgit v1.2.3