-
Notifications
You must be signed in to change notification settings - Fork 374
/
config_init_test.go
130 lines (102 loc) · 2.84 KB
/
config_init_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"context"
"path/filepath"
"testing"
"github.com/gnolang/gno/tm2/pkg/bft/config"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfig_Init(t *testing.T) {
t.Parallel()
t.Run("invalid output path", func(t *testing.T) {
t.Parallel()
// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"config",
"init",
"--config-path",
"",
}
// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
assert.ErrorContains(t, cmdErr, errInvalidConfigOutputPath.Error())
})
t.Run("default config initialized", func(t *testing.T) {
t.Parallel()
// Create a temporary directory
tempDir := t.TempDir()
path := filepath.Join(tempDir, "config.toml")
// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"config",
"init",
"--config-path",
path,
}
// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.NoError(t, cmdErr)
// Verify the config is valid
cfg, err := config.LoadConfigFile(path)
require.NoError(t, err)
assert.NoError(t, cfg.ValidateBasic())
assert.Equal(t, cfg, config.DefaultConfig())
})
t.Run("unable to overwrite config", func(t *testing.T) {
t.Parallel()
// Create a temporary directory
tempDir := t.TempDir()
path := filepath.Join(tempDir, "config.toml")
// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"config",
"init",
"--config-path",
path,
}
// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.NoError(t, cmdErr)
// Verify the config is valid
cfg, err := config.LoadConfigFile(path)
require.NoError(t, err)
assert.NoError(t, cfg.ValidateBasic())
assert.Equal(t, cfg, config.DefaultConfig())
// Try to initialize again, expecting failure
cmd = newRootCmd(commands.NewTestIO())
cmdErr = cmd.ParseAndRun(context.Background(), args)
assert.ErrorIs(t, cmdErr, errOverwriteNotEnabled)
})
t.Run("config overwritten", func(t *testing.T) {
t.Parallel()
// Create a temporary directory
tempDir := t.TempDir()
path := filepath.Join(tempDir, "config.toml")
// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"config",
"init",
"--force",
"--config-path",
path,
}
// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.NoError(t, cmdErr)
// Verify the config is valid
cfg, err := config.LoadConfigFile(path)
require.NoError(t, err)
assert.NoError(t, cfg.ValidateBasic())
assert.Equal(t, cfg, config.DefaultConfig())
// Try to initialize again, expecting success
cmd = newRootCmd(commands.NewTestIO())
cmdErr = cmd.ParseAndRun(context.Background(), args)
assert.NoError(t, cmdErr)
})
}