github.com/oNaiPs/go-generate-fast@v0.3.0/src/core/config/config_test.go (about) 1 package config 2 3 import ( 4 "os" 5 "path" 6 "strconv" 7 "testing" 8 9 "github.com/spf13/viper" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestConfigGet(t *testing.T) { 14 expectedConfigDir := t.TempDir() 15 expectedCacheDir := t.TempDir() 16 expectedDisable := true 17 expectedReadOnly := false 18 expectedReCache := true 19 20 os.Setenv("GO_GENERATE_FAST_DIR", expectedConfigDir) 21 os.Setenv("GO_GENERATE_FAST_CACHE_DIR", expectedCacheDir) 22 os.Setenv("GO_GENERATE_FAST_DISABLE", strconv.FormatBool(expectedDisable)) 23 os.Setenv("GO_GENERATE_FAST_READ_ONLY", strconv.FormatBool(expectedReadOnly)) 24 os.Setenv("GO_GENERATE_FAST_RECACHE", strconv.FormatBool(expectedReCache)) 25 26 Init() 27 28 // Ensure viper's current configuration is cleared after the test 29 defer viper.Reset() 30 31 config := Get() 32 33 assert.Equal(t, expectedConfigDir, config.ConfigDir) 34 assert.Equal(t, expectedCacheDir, config.CacheDir) 35 assert.Equal(t, expectedDisable, config.Disable) 36 assert.Equal(t, expectedReadOnly, config.ReadOnly) 37 assert.Equal(t, expectedReCache, config.ReCache) 38 } 39 40 func TestConfigCreateDirIfNotExists(t *testing.T) { 41 // Create a temporary directory for testing 42 tmpDir := path.Join(t.TempDir(), "create-dir-test") 43 44 // Call the function with the test directory 45 CreateDirIfNotExists(tmpDir) 46 47 // Assert that the directory exists 48 _, err := os.Stat(tmpDir) 49 assert.False(t, os.IsNotExist(err), "directory doesn't exist") 50 51 // Assert that the directory has the correct permissions 52 info, _ := os.Stat(tmpDir) 53 assert.True(t, info.IsDir(), "path is not a directory") 54 assert.Equal(t, os.FileMode(0700), info.Mode().Perm(), "incorrect directory permissions") 55 }