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
37 changes: 37 additions & 0 deletions internal/headless/scope_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package headless

import (
"reflect"
"testing"

"github.com/cheatmd-dev/cheatmd/pkg/config"
"github.com/cheatmd-dev/cheatmd/pkg/parser"
)

func TestLiteralChainSeesEarlierResolvedValues(t *testing.T) {
old := config.Get().VarSyntax
t.Cleanup(func() { config.Get().VarSyntax = old })
config.Get().VarSyntax = "dollar"
for _, name := range []string{"a", "b", "c"} {
t.Setenv(name, "")
}
cheat := &parser.Cheat{
Command: "echo $c",
Vars: []parser.VarDef{
{Name: "a", Literal: "alpha"},
{Name: "b", Literal: "$a-beta"},
{Name: "c", Literal: "$b-gamma"},
},
}
session := &RunnerSession{Cheat: cheat, Index: parser.NewCheatIndex()}
session.initializeVariables()

if err := session.resolveInteractively(); err != nil {
t.Fatal(err)
}

want := map[string]string{"a": "alpha", "b": "alpha-beta", "c": "alpha-beta-gamma"}
if !reflect.DeepEqual(cheat.Scope, want) {
t.Fatalf("resolved scope = %v, want %v", cheat.Scope, want)
}
}
1 change: 1 addition & 0 deletions internal/headless/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (s *RunnerSession) attemptAutoResolvePass() bool {
for i := range s.Vars {
if s.tryResolveVariable(&s.Vars[i], scope) {
progress = true
scope[s.Vars[i].Def.Name] = s.Vars[i].Value
}
}

Expand Down