Skip to content

Commit

Permalink
Setenv for for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Dec 17, 2024
1 parent 4b723bd commit 934ddd5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 29 deletions.
6 changes: 3 additions & 3 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

type Option struct {
ConfigFilePath string `help:"config file path" env:"LAMBROLL_CONFIG" name:"config" json:"-"`
OptionFilePath string `help:"option file path" env:"LAMBROLL_OPTION" name:"option" json:"-"`
Function string `help:"Function file path" env:"LAMBROLL_FUNCTION" json:"function,omitempty"`
LogLevel string `help:"log level (trace, debug, info, warn, error)" default:"info" enum:",trace,debug,info,warn,error" env:"LAMBROLL_LOGLEVEL" json:"log_level"`
Color bool `help:"enable colored output" default:"true" env:"LAMBROLL_COLOR" negatable:"" json:"color,omitempty"`
Expand Down Expand Up @@ -66,7 +66,7 @@ func prepareCLI(args []string) (string, []string, error) {
return "", nil, fmt.Errorf("failed to load envfile: %w", err)
}
}
return opts.ConfigFilePath, opts.Envfile, nil
return opts.OptionFilePath, opts.Envfile, nil
}

func ParseCLI(args []string) (string, *CLIOptions, func(), error) {
Expand All @@ -87,7 +87,7 @@ func ParseCLI(args []string) (string, *CLIOptions, func(), error) {
if optionFilePath != "" {
defaultOpt, err := loadDefinitionFile[Option](nil, optionFilePath, DefaultOptionFilenames)
if err != nil {
return "", nil, nil, fmt.Errorf("failed to load config file: %w", err)
return "", nil, nil, fmt.Errorf("failed to load option file: %w", err)
}
defaultOptBytes, err := json.Marshal(defaultOpt)
if err != nil {
Expand Down
36 changes: 12 additions & 24 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ var cliTests = []struct {
},
},
{
args: []string{"render", "--config", "debug.jsonnet"},
args: []string{"render", "--option", "debug.jsonnet"},
sub: "render",
option: &lambroll.Option{
LogLevel: "debug",
Color: true,
ConfigFilePath: "debug.jsonnet",
OptionFilePath: "debug.jsonnet",
Envfile: []string{},
},
},
Expand All @@ -48,14 +48,14 @@ var cliTests = []struct {
{
args: []string{
"--envfile=envfile.local",
"--config", "global.jsonnet",
"--option", "global.jsonnet",
"--function", "function.jsonnet",
"render",
},
sub: "render",
option: &lambroll.Option{
Function: "function.jsonnet",
ConfigFilePath: "global.jsonnet",
OptionFilePath: "global.jsonnet",
Color: true,
Envfile: []string{"envfile.global", "envfile.local"},
},
Expand Down Expand Up @@ -86,35 +86,35 @@ var cliTests = []struct {
},
},
{
args: []string{"render", "--config", "useenv.jsonnet", "--envfile=envfile.useenv"},
args: []string{"render", "--option", "useenv.jsonnet", "--envfile=envfile.useenv"},
sub: "render",
option: &lambroll.Option{
ConfigFilePath: "useenv.jsonnet",
OptionFilePath: "useenv.jsonnet",
Function: "hello",
Color: true,
Envfile: []string{"envfile.useenv"},
},
},
{
args: []string{"render", "--config", "useenv.jsonnet"},
args: []string{"render", "--option", "useenv.jsonnet"},
sub: "render",
env: map[string]string{
"TEST_FUNCTION_NAME": "world",
},
option: &lambroll.Option{
ConfigFilePath: "useenv.jsonnet",
OptionFilePath: "useenv.jsonnet",
Function: "world",
Color: true,
Envfile: []string{},
},
},
{
args: []string{"render", "--config", "missing.jsonnet"},
args: []string{"render", "--option", "missing.jsonnet"},
sub: "render",
err: os.ErrNotExist,
},
{
args: []string{"render", "--config", "missing.json"},
args: []string{"render", "--option", "missing.json"},
sub: "render",
err: os.ErrNotExist,
},
Expand All @@ -128,9 +128,9 @@ func TestParseCLI(t *testing.T) {
for _, tt := range cliTests {
t.Run(strings.Join(tt.args, "_"), func(t *testing.T) {
for k, v := range tt.env {
reset := setenv(k, v)
defer reset()
lambroll.Setenv(k, v)
}
defer lambroll.ResetEnv()
sub, opt, _, err := lambroll.ParseCLI(tt.args)
if err != nil {
if tt.err == nil {
Expand All @@ -154,15 +154,3 @@ func TestParseCLI(t *testing.T) {
})
}
}

func setenv(key, value string) func() {
orig, found := os.LookupEnv(key)
os.Setenv(key, value)
return func() {
if found {
os.Setenv(key, orig)
} else {
os.Unsetenv(key)
}
}
}
30 changes: 30 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package lambroll

import (
"os"
"sync"
)

var (
CreateZipArchive = createZipArchive
ExpandExcludeFile = expandExcludeFile
Expand All @@ -22,3 +27,28 @@ func (app *App) CallerIdentity() *CallerIdentity {
func (app *App) LoadFunction(f string) (*Function, error) {
return app.loadFunction(f)
}

func init() {
Setenv = tSetenv
}

var envs = sync.Map{}

func tSetenv(key, value string) error {
orig, ok := os.LookupEnv(key)
os.Setenv(key, value)
if ok {
envs.Store(key, func() { os.Setenv(key, orig) })
} else {
envs.Store(key, func() { os.Unsetenv(key) })
}
return nil
}

func ResetEnv() {
envs.Range(func(key, value interface{}) bool {
value.(func())()
return true
})
envs = sync.Map{}
}
4 changes: 3 additions & 1 deletion jsonnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var testCaseJsonnetNativeFuncs = []struct {
name: "env FOO not set",
env: map[string]string{
"BAR": "bar",
"FOO": "",
},
expected: map[string]string{
"foo": "default",
Expand Down Expand Up @@ -63,8 +64,9 @@ func TestJsonnetNativeFuncs(t *testing.T) {
for _, c := range testCaseJsonnetNativeFuncs {
t.Run(c.name, func(t *testing.T) {
for k, v := range c.env {
t.Setenv(k, v)
lambroll.Setenv(k, v)
}
defer lambroll.ResetEnv()
out, err := vm.EvaluateAnonymousSnippet("test.jsonnet", testSrcJsonnet)
if c.errExpected {
if err == nil {
Expand Down
4 changes: 3 additions & 1 deletion lambroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ func newSnapStart(s *types.SnapStartResponse) *types.SnapStart {
}
}

var Setenv = os.Setenv

func exportEnvFile(file string) error {
if file == "" {
return nil
Expand All @@ -441,7 +443,7 @@ func exportEnvFile(file string) error {
return err
}
for key, value := range envs {
os.Setenv(key, value)
Setenv(key, value)
}
return nil
}
Expand Down

0 comments on commit 934ddd5

Please sign in to comment.