-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
159 lines (128 loc) · 4.11 KB
/
config_test.go
File metadata and controls
159 lines (128 loc) · 4.11 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright © 2025 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ecdysis
import (
"context"
"fmt"
"os"
"testing"
"github.com/matryer/is"
)
var testConfigPath = "./test_parse_config_cooking_config.yaml"
type cookingConfig struct {
HeatLevel int `long:"heat-level" usage:"sets the heat level" default:"5" mapstructure:"heat-level"`
}
func newCookingConfig() cookingConfig {
return cookingConfig{HeatLevel: 5}
}
type cookCommand struct {
Cfg cookingConfig
}
func (c *cookCommand) Execute(context.Context) error {
return nil
}
func (c *cookCommand) Config() Config {
return Config{
EnvPrefix: "TestParseConfig_CookingConfig",
Parsed: &c.Cfg,
DefaultValues: cookingConfig{
HeatLevel: 5,
},
Path: testConfigPath,
}
}
func (c *cookCommand) Flags() []Flag {
flags := BuildFlags(&c.Cfg)
c.Cfg = newCookingConfig()
flags.SetDefault("heat-level", c.Cfg.HeatLevel)
return flags
}
func (c *cookCommand) Usage() string {
return "cook something"
}
func (c *cookCommand) Docs() Docs {
return Docs{
Short: "cook short description",
Long: "cook long description",
Example: "cook --heat-level 10",
}
}
func TestParseConfig_NameWithDash_EnvVar(t *testing.T) {
is := is.New(t)
t.Setenv("TESTPARSECONFIG_COOKINGCONFIG_HEAT_LEVEL", "33")
cookCmd := &cookCommand{}
cookCobraCmd := New().MustBuildCobraCommand(cookCmd)
is.NoErr(cookCobraCmd.Execute())
is.Equal(cookCmd.Cfg, cookingConfig{HeatLevel: 33})
}
func TestParseConfig_NameWithDash_Flag(t *testing.T) {
is := is.New(t)
originalArgs := os.Args
os.Args = []string{originalArgs[0], "--heat-level=22"}
defer func() {
os.Args = originalArgs
}()
cookCmd := &cookCommand{}
cookCobraCmd := New().MustBuildCobraCommand(cookCmd)
is.NoErr(cookCobraCmd.Execute())
is.Equal(cookCmd.Cfg, cookingConfig{HeatLevel: 22})
}
func TestParseConfig_NameWithDash_File(t *testing.T) {
is := is.New(t)
cookCmd := &cookCommand{}
cookCobraCmd := New().MustBuildCobraCommand(cookCmd)
is.NoErr(cookCobraCmd.Execute())
is.Equal(cookCmd.Cfg, cookingConfig{HeatLevel: 11})
}
func TestParseConfig_NameWithDash_Default(t *testing.T) {
is := is.New(t)
cfgFile, err := os.CreateTemp("", "test_parse_config_cooking_config_empty.yaml")
is.NoErr(err)
defer os.Remove(cfgFile.Name())
testConfigPath = cfgFile.Name()
cookCmd := &cookCommand{}
cookCobraCmd := New().MustBuildCobraCommand(cookCmd)
is.NoErr(cookCobraCmd.Execute())
is.Equal(cookCmd.Cfg, cookingConfig{HeatLevel: 5})
}
// customCookCommand is a command that uses a custom environment prefix for testing.
type customCookCommand struct {
cookCommand
}
// Config returns a configuration with a custom environment prefix.
func (c *customCookCommand) Config() Config {
cfg := c.cookCommand.Config()
cfg.EnvPrefix = "MY_ENV"
return cfg
}
func TestParseConfig_CustomConfigPath(t *testing.T) {
is := is.New(t)
// Create a temporary config file with a unique heat-level value
customConfigFile, err := os.CreateTemp("", "test_custom_config_*.yaml")
is.NoErr(err)
defer os.Remove(customConfigFile.Name())
// Write custom config to the temporary file
customHeatLevel := 42
_, err = fmt.Fprintf(customConfigFile, "heat-level: %d\n", customHeatLevel)
is.NoErr(err)
err = customConfigFile.Close()
is.NoErr(err)
// Set MY_ENV_CONFIG_PATH environment variable to the temporary file path
t.Setenv("MY_ENV_CONFIG_PATH", customConfigFile.Name())
// Execute the command and verify the config was loaded from the custom path
customCmd := &customCookCommand{}
cookCobraCmd := New().MustBuildCobraCommand(customCmd)
is.NoErr(cookCobraCmd.Execute())
is.Equal(customCmd.Cfg, cookingConfig{HeatLevel: customHeatLevel})
}