github.com/vipernet-xyz/tm@v0.34.24/config/toml_test.go (about) 1 package config 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func ensureFiles(t *testing.T, rootDir string, files ...string) { 14 for _, f := range files { 15 p := rootify(rootDir, f) 16 _, err := os.Stat(p) 17 assert.Nil(t, err, p) 18 } 19 } 20 21 func TestEnsureRoot(t *testing.T) { 22 require := require.New(t) 23 24 // setup temp dir for test 25 tmpDir, err := os.MkdirTemp("", "config-test") 26 require.Nil(err) 27 defer os.RemoveAll(tmpDir) 28 29 // create root dir 30 EnsureRoot(tmpDir) 31 32 // make sure config is set properly 33 data, err := os.ReadFile(filepath.Join(tmpDir, defaultConfigFilePath)) 34 require.Nil(err) 35 36 if !checkConfig(string(data)) { 37 t.Fatalf("config file missing some information") 38 } 39 40 ensureFiles(t, tmpDir, "data") 41 } 42 43 func TestEnsureTestRoot(t *testing.T) { 44 require := require.New(t) 45 46 testName := "ensureTestRoot" 47 48 // create root dir 49 cfg := ResetTestRoot(testName) 50 defer os.RemoveAll(cfg.RootDir) 51 rootDir := cfg.RootDir 52 53 // make sure config is set properly 54 data, err := os.ReadFile(filepath.Join(rootDir, defaultConfigFilePath)) 55 require.Nil(err) 56 57 if !checkConfig(string(data)) { 58 t.Fatalf("config file missing some information") 59 } 60 61 // TODO: make sure the cfg returned and testconfig are the same! 62 baseConfig := DefaultBaseConfig() 63 ensureFiles(t, rootDir, defaultDataDir, baseConfig.Genesis, baseConfig.PrivValidatorKey, baseConfig.PrivValidatorState) 64 } 65 66 func checkConfig(configFile string) bool { 67 var valid bool 68 69 // list of words we expect in the config 70 elems := []string{ 71 "moniker", 72 "seeds", 73 "proxy_app", 74 "fast_sync", 75 "create_empty_blocks", 76 "peer", 77 "timeout", 78 "broadcast", 79 "send", 80 "addr", 81 "wal", 82 "propose", 83 "max", 84 "genesis", 85 } 86 for _, e := range elems { 87 if !strings.Contains(configFile, e) { 88 valid = false 89 } else { 90 valid = true 91 } 92 } 93 return valid 94 }