Skip to content

Commit

Permalink
test: create tests for retry message function
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Dec 31, 2023
1 parent 0e013fd commit 496de89
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 13 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/docker/docker-credential-helpers v0.8.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ func runExecute(cmd *cobra.Command, opts executeOptions) error {

sauce, err := re.Execute(engine.Engine{}, values, uuid.Must(uuid.NewV4()))
if err != nil {
retryMessage := recipeutil.MakeRetryMessage(values)
retryMessage := recipeutil.MakeRetryMessage(os.Args, values)
return fmt.Errorf("%w\n\n%s", err, retryMessage)
}

// Check for conflicts
for _, s := range existingSauces {
if conflicts := s.Conflicts(sauce); conflicts != nil {
retryMessage := recipeutil.MakeRetryMessage(values)
retryMessage := recipeutil.MakeRetryMessage(os.Args, values)
return fmt.Errorf("conflict in recipe '%s': file '%s' was already created by recipe '%s'.\n\n%s", re.Name, conflicts[0].Path, s.Recipe.Name, retryMessage)
}
}
Expand Down
8 changes: 4 additions & 4 deletions internal/cli/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func runUpgrade(cmd *cobra.Command, opts upgradeOptions) error {
ignorePatterns = append(ignorePatterns, strings.Split(string(data), "\n")...)
} else if !errors.Is(err, fs.ErrNotExist) {
// something else happened than trying to read an ignore file that does not exist
return fmt.Errorf("failed to read ignore file: %w\n\n%s", err, recipeutil.MakeRetryMessage(values))
return fmt.Errorf("failed to read ignore file: %w\n\n%s", err, recipeutil.MakeRetryMessage(os.Args, values))
}
ignorePatterns = append(ignorePatterns, re.IgnorePatterns...)

Expand All @@ -205,7 +205,7 @@ func runUpgrade(cmd *cobra.Command, opts upgradeOptions) error {
skip := false
for _, pattern := range ignorePatterns {
if matched, err := filepath.Match(pattern, path); err != nil {
return fmt.Errorf("bad ignore pattern '%s': %w\n\n%s", pattern, err, recipeutil.MakeRetryMessage(values))
return fmt.Errorf("bad ignore pattern '%s': %w\n\n%s", pattern, err, recipeutil.MakeRetryMessage(os.Args, values))
} else if matched {
// file was marked as ignored for upgrades
skip = true
Expand Down Expand Up @@ -256,11 +256,11 @@ func runUpgrade(cmd *cobra.Command, opts upgradeOptions) error {

if err != nil {
if errors.Is(err, uiutil.ErrUserAborted) {
cmd.Printf("User aborted\n\n%s\n", recipeutil.MakeRetryMessage(values))
cmd.Printf("User aborted\n\n%s\n", recipeutil.MakeRetryMessage(os.Args, values))
return nil
}

return fmt.Errorf("error when prompting for question: %w\n\n%s", err, recipeutil.MakeRetryMessage(values))
return fmt.Errorf("error when prompting for question: %w\n\n%s", err, recipeutil.MakeRetryMessage(os.Args, values))
}

// NOTE: We need to save the checksum of the original file from the new sauce
Expand Down
13 changes: 6 additions & 7 deletions pkg/recipeutil/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package recipeutil

import (
"fmt"
"os"
"strings"

"github.com/futurice/jalapeno/pkg/recipe"
)

// Utility function for creating a retry message for the user such that they can re-run the cli command with the same values
func MakeRetryMessage(values recipe.VariableValues) string {
func MakeRetryMessage(args []string, values recipe.VariableValues) string {
var commandline strings.Builder
skipNext := false
for idx, arg := range os.Args {
for idx, arg := range args {
if skipNext {
skipNext = false
continue
Expand All @@ -32,17 +31,17 @@ func MakeRetryMessage(values recipe.VariableValues) string {
}

for key, value := range values {
commandline.WriteString(" --set ")
commandline.WriteString("--set ")
switch value := value.(type) {
case []map[string]string: // serialize to CSV
csv, err := TableToCSV(value, ',')
if err != nil {
panic(err)
}
commandline.WriteString(fmt.Sprintf("\"%s=%s\"", key, strings.ReplaceAll(strings.TrimRight(csv, "\n"), "\n", "\\n")))
commandline.WriteString(fmt.Sprintf("\"%s=%s\" ", key, strings.ReplaceAll(strings.TrimRight(csv, "\n"), "\n", "\\n")))
default:
commandline.WriteString(fmt.Sprintf("\"%s=%s\"", key, value))
commandline.WriteString(fmt.Sprintf("\"%s=%s\" ", key, value))
}
}
return fmt.Sprintf("To re-run the recipe with the same values, use the following command:\n\n%s", commandline.String())
return fmt.Sprintf("To re-run the recipe with the same values, use the following command:\n\n%s", strings.TrimSpace(commandline.String()))
}
47 changes: 47 additions & 0 deletions pkg/recipeutil/retry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package recipeutil_test

import (
"testing"

"github.com/futurice/jalapeno/pkg/recipe"
"github.com/futurice/jalapeno/pkg/recipeutil"
"github.com/kylelemons/godebug/diff"
)

func TestMakeRetryMessage(t *testing.T) {
testCases := []struct {
Name string
Args []string
Values recipe.VariableValues
Expected string
}{
{
"No values",
[]string{"jalapeno", "execute", "path/to/recipe"},
recipe.VariableValues{},
`To re-run the recipe with the same values, use the following command:
jalapeno execute "path/to/recipe"`,
},
{
"Non-empty values",
[]string{"jalapeno", "execute", "path/to/recipe"},
recipe.VariableValues{
"key1": "value1",
"key2": "value2",
},
`To re-run the recipe with the same values, use the following command:
jalapeno execute "path/to/recipe" --set "key1=value1" --set "key2=value2"`,
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(tt *testing.T) {
result := recipeutil.MakeRetryMessage(tc.Args, tc.Values)
if result != tc.Expected {
tt.Errorf("%s", diff.Diff(tc.Expected, result))
}
})
}
}

0 comments on commit 496de89

Please sign in to comment.