Skip to content

Commit

Permalink
test: map arrow characters to control sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Feb 12, 2024
1 parent ae6c253 commit cb96670
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
22 changes: 17 additions & 5 deletions internal/cli/cmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestFeatures(t *testing.T) {
s.Step(`^registry credentials are provided by config file$`, generateDockerConfigFile)
s.Step(`^the recipes directory should contain recipe "([^"]*)"$`, theRecipesDirectoryShouldContainRecipe)
s.Step(`^I buffer key presses "([^"]*)"$`, bufferKeysToInput)
s.Step(`^I buffer special key press "([^"]*)"$`, bufferKeysToInput)

// Command specific steps
AddCheckSteps(s)
Expand Down Expand Up @@ -245,9 +246,20 @@ func aRecipesDirectory(ctx context.Context) (context.Context, error) {
func bufferKeysToInput(ctx context.Context, keys string) (context.Context, error) {
stdIn := ctx.Value(cmdStdInCtxKey{}).(*BlockBuffer)

keys = strings.Replace(keys, "\\r", "\r", -1)
keys = strings.Replace(keys, "\\x1b", "\x1b", -1)
stdIn.Add([]byte(keys))
replacer := strings.NewReplacer([]string{
"\\r", "\r",
"\\n", "\n",
"\\x1b", "\x1b",

// From https://github.com/charmbracelet/bubbletea/blob/master/key.go#L354
"↑", "\x1b[A",
"↓", "\x1b[B",
"→", "\x1b[C",
"←", "\x1b[D",
}...)

mappedKeys := replacer.Replace(keys)
stdIn.AddBlock([]byte(mappedKeys))

return context.WithValue(ctx, cmdStdInCtxKey{}, stdIn), nil
}
Expand Down Expand Up @@ -275,7 +287,7 @@ description: %[1]s
if err := os.WriteFile(filepath.Join(templateDir, filename), []byte(recipe), 0644); err != nil {
return ctx, err
}
return context.WithValue(ctx, recipesDirectoryPathCtxKey{}, dir), nil
return ctx, nil
}

func aLocalOCIRegistry(ctx context.Context) (context.Context, error) {
Expand Down Expand Up @@ -655,7 +667,7 @@ func NewBlockBuffer() *BlockBuffer {
}
}

func (r *BlockBuffer) Add(p []byte) {
func (r *BlockBuffer) AddBlock(p []byte) {
r.data = append(r.data, p)
}

Expand Down
2 changes: 1 addition & 1 deletion test/upgrade-recipe.feature
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Feature: Upgrade sauce
And I change recipe "foo" to version "v0.0.2"
And I change recipe "foo" template "README.md" to render "New version"
And I change project file "README.md" to contain "Locally modified"
And I buffer key presses "\x1b[C"
And I buffer special key press "→"
And I buffer key presses "\r"
When I upgrade recipe "foo"
Then CLI produced an output "README.md: override"
Expand Down

0 comments on commit cb96670

Please sign in to comment.