Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/config_envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ set environment variable from a secret
}

if np != nil || vp != nil {
if np != nil && vp == nil {
return fmt.Errorf("--value is required when --name is provided")
}
Comment thread
Ankitsinghsisodya marked this conversation as resolved.
if np != nil {
if err := utils.ValidateEnvVarName(*np); err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions cmd/config_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ the local machine.
return loaderSaver.Save(function)
}

if np != nil {
return fmt.Errorf("--value is required when --name is provided")
}
if vp != nil {
return fmt.Errorf("--name is required when --value is provided")
}
Comment thread
Ankitsinghsisodya marked this conversation as resolved.

return runAddLabelsPrompt(cmd.Context(), function, loaderSaver)
},
}
Expand Down
76 changes: 76 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,82 @@ func TestListEnvAdd(t *testing.T) {
}
}

// TestEnvsAddNameWithoutValue ensures that providing --name without --value returns an error
// rather than silently writing a broken fn.Env{Name: &key, Value: nil} to func.yaml.
func TestEnvsAddNameWithoutValue(t *testing.T) {
Comment thread
Ankitsinghsisodya marked this conversation as resolved.
mock := common.NewMockLoaderSaver()
mock.LoadFn = func(path string) (fn.Function, error) {
return fn.Function{}, nil
}
mock.SaveFn = func(f fn.Function) error {
t.Error("Save must not be called when validation fails")
return nil
}

cmd := setupConfigEnvCmd(mock, "add", "--name=API_KEY")
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)

err := cmd.Execute()
if err == nil {
t.Fatal("expected error when --name is provided without --value, got nil")
}
}

func setupConfigLabelsCmd(mock common.FunctionLoaderSaver, args ...string) *cobra.Command {
cmd := fnCmd.NewConfigCmd(
mock,
ci.NewBufferWriter(),
common.CurrentBranchStub("", nil),
common.WorkDirStub("", nil),
fnCmd.NewClient,
)
cmd.SetArgs(append([]string{"labels"}, args...))
return cmd
}

// TestLabelsAddNameWithoutValue ensures that providing --name without --value returns an error.
func TestLabelsAddNameWithoutValue(t *testing.T) {
mock := common.NewMockLoaderSaver()
mock.LoadFn = func(path string) (fn.Function, error) {
return fn.Function{}, nil
}
mock.SaveFn = func(f fn.Function) error {
t.Error("Save must not be called when validation fails")
return nil
}

cmd := setupConfigLabelsCmd(mock, "add", "--name=env")
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)

err := cmd.Execute()
if err == nil {
t.Fatal("expected error when --name is provided without --value, got nil")
}
}

// TestLabelsAddValueWithoutName ensures that providing --value without --name returns an error.
func TestLabelsAddValueWithoutName(t *testing.T) {
mock := common.NewMockLoaderSaver()
mock.LoadFn = func(path string) (fn.Function, error) {
return fn.Function{}, nil
}
mock.SaveFn = func(f fn.Function) error {
t.Error("Save must not be called when validation fails")
return nil
}

cmd := setupConfigLabelsCmd(mock, "add", "--value=prod")
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)

err := cmd.Execute()
if err == nil {
t.Fatal("expected error when --value is provided without --name, got nil")
}
}

func envsEqual(a, b []fn.Env) bool {
if len(a) != len(b) {
return false
Expand Down
Loading