Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
73 changes: 73 additions & 0 deletions internal/ui/definition_syntax_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package ui

import (
"testing"

tea "github.com/charmbracelet/bubbletea"
"github.com/cheatmd-dev/cheatmd/pkg/config"
"github.com/cheatmd-dev/cheatmd/pkg/executor"
"github.com/cheatmd-dev/cheatmd/pkg/parser"
)

func TestLiteralDefinitionUsesDollarSyntaxWithAngleCommand(t *testing.T) {
old := config.Get().VarSyntax
t.Cleanup(func() { config.Get().VarSyntax = old })
config.Get().VarSyntax = "angle"
m, index := setupTestModel()
cheat := index.Cheats[0]
cheat.Command = "echo <b>"
cheat.Vars = []parser.VarDef{
{Name: "a", Literal: "alpha"},
{Name: "b", Literal: "$a-beta"},
}
m.selected = cheat

m.startVarResolution()

if got := cheat.Scope["b"]; got != "alpha-beta" {
t.Fatalf("literal value = %q, want alpha-beta", got)
}
if got := executor.NewExecutor(index).BuildFinalCommand(cheat); got != "echo alpha-beta" {
t.Fatalf("command = %q, want echo alpha-beta", got)
}
}

type definitionShellExecutor struct {
mockExecutor
command string
}

func (e *definitionShellExecutor) RunShell(command string) (string, error) {
e.command = command
return "alpha-beta", nil
}

func TestShellDefinitionUsesDollarSyntaxWithAngleCommand(t *testing.T) {
old := config.Get().VarSyntax
t.Cleanup(func() { config.Get().VarSyntax = old })
config.Get().VarSyntax = "angle"
m, index := setupTestModel()
cheat := index.Cheats[0]
cheat.Command = "echo <b>"
cheat.Vars = []parser.VarDef{
{Name: "a", Literal: "alpha"},
{Name: "b", Shell: "printf '%s' '$a-beta'"},
}
shell := &definitionShellExecutor{}
m.executor = shell
m.selected = cheat

cmd := m.startVarResolution()
if cmd == nil {
t.Fatal("shell definition returned no command")
}
m.Update(cmd())
m.Update(tea.KeyMsg{Type: tea.KeyEnter})

if shell.command != "printf '%s' 'alpha-beta'" {
t.Fatalf("shell command = %q, want substituted dollar reference", shell.command)
}
if got := executor.NewExecutor(index).BuildFinalCommand(cheat); got != "echo alpha-beta" {
t.Fatalf("command = %q, want echo alpha-beta", got)
}
}
4 changes: 2 additions & 2 deletions internal/ui/var_resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func allVariantsConditional(variants []parser.VarDef) bool {
}

func (m *mainModel) prepareLiteralVar(vs *varState, scope map[string]string) tea.Cmd {
result := executor.SubstituteVars(vs.def.Literal, scope, config.Get().VarSyntax)
result := executor.SubstituteVars(vs.def.Literal, scope, "dollar")
if vs.skipAutoCont {
m.varState.isPromptOnly = true
m.varState.options = nil
Expand Down Expand Up @@ -184,7 +184,7 @@ func (m *mainModel) preparePromptVar(vs *varState) tea.Cmd {
}

func (m *mainModel) prepareShellVar(vs *varState, scope map[string]string) tea.Cmd {
shellCmd := executor.SubstituteVars(vs.def.Shell, scope, config.Get().VarSyntax)
shellCmd := executor.SubstituteVars(vs.def.Shell, scope, "dollar")
return func() tea.Msg {
output, err := m.executor.RunShell(shellCmd)
if err != nil {
Expand Down