OSDN Git Service

Merge branch 'master' of https://github.com/Bytom/bytom-btmhash
[bytom/bytom.git] / config / toml_test.go
1 package config
2
3 import (
4         "io/ioutil"
5         "os"
6         "path/filepath"
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         assert, require := assert.New(t), require.New(t)
23
24         // setup temp dir for test
25         tmpDir, err := ioutil.TempDir("", "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 := ioutil.ReadFile(filepath.Join(tmpDir, "config.toml"))
34         require.Nil(err)
35         assert.Equal([]byte(defaultConfig("anonymous")), data)
36
37         ensureFiles(t, tmpDir, "data")
38 }
39
40 func TestEnsureTestRoot(t *testing.T) {
41         assert, require := assert.New(t), require.New(t)
42
43         testName := "ensureTestRoot"
44
45         // create root dir
46         cfg := ResetTestRoot(testName)
47         rootDir := cfg.RootDir
48
49         // make sure config is set properly
50         data, err := ioutil.ReadFile(filepath.Join(rootDir, "config.toml"))
51         require.Nil(err)
52         assert.Equal([]byte(testConfig("anonymous")), data)
53
54         // TODO: make sure the cfg returned and testconfig are the same!
55
56         ensureFiles(t, rootDir, "data", "genesis.json", "priv_validator.json")
57 }