|
7 | 7 | "os"
|
8 | 8 | "strings"
|
9 | 9 | "testing"
|
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
10 | 12 | )
|
11 | 13 |
|
12 | 14 | // Mock HTTP server for testing downloading markdown from URL
|
@@ -76,3 +78,99 @@ func TestResolveMarkdownSource(t *testing.T) {
|
76 | 78 | }
|
77 | 79 | })
|
78 | 80 | }
|
| 81 | + |
| 82 | +func TestVariableOverrides(t *testing.T) { |
| 83 | + // Test overriding environment variables |
| 84 | + t.Run("Override a standard variable declaration", func(t *testing.T) { |
| 85 | + scenario, err := CreateScenarioFromMarkdown( |
| 86 | + "../../scenarios/testing/variables.md", |
| 87 | + []string{"bash"}, |
| 88 | + map[string]string{ |
| 89 | + "MY_VAR": "my_value", |
| 90 | + }, |
| 91 | + ) |
| 92 | + |
| 93 | + assert.NoError(t, err) |
| 94 | + assert.Equal(t, "my_value", scenario.Environment["MY_VAR"]) |
| 95 | + assert.Contains(t, scenario.Steps[0].CodeBlocks[0].Content, "export MY_VAR=my_value") |
| 96 | + }) |
| 97 | + |
| 98 | + t.Run( |
| 99 | + "Override a variable that is declared on the same line as another variable, separated by &&", |
| 100 | + func(t *testing.T) { |
| 101 | + scenario, err := CreateScenarioFromMarkdown( |
| 102 | + "../../scenarios/testing/variables.md", |
| 103 | + []string{"bash"}, |
| 104 | + map[string]string{ |
| 105 | + "NEXT_VAR": "next_value", |
| 106 | + }, |
| 107 | + ) |
| 108 | + |
| 109 | + assert.NoError(t, err) |
| 110 | + assert.Equal(t, "next_value", scenario.Environment["NEXT_VAR"]) |
| 111 | + assert.Contains( |
| 112 | + t, |
| 113 | + scenario.Steps[1].CodeBlocks[0].Content, |
| 114 | + `export NEXT_VAR=next_value && export OTHER_VAR="Hello, World!"`, |
| 115 | + ) |
| 116 | + }, |
| 117 | + ) |
| 118 | + |
| 119 | + t.Run( |
| 120 | + "Override a variable that is declared on the same line as another variable, separated by ;", |
| 121 | + func(t *testing.T) { |
| 122 | + scenario, err := CreateScenarioFromMarkdown( |
| 123 | + "../../scenarios/testing/variables.md", |
| 124 | + []string{"bash"}, |
| 125 | + map[string]string{ |
| 126 | + "THIS_VAR": "this_value", |
| 127 | + "THAT_VAR": "that_value", |
| 128 | + }, |
| 129 | + ) |
| 130 | + |
| 131 | + assert.NoError(t, err) |
| 132 | + assert.Equal(t, "this_value", scenario.Environment["THIS_VAR"]) |
| 133 | + assert.Equal(t, "that_value", scenario.Environment["THAT_VAR"]) |
| 134 | + assert.Contains( |
| 135 | + t, |
| 136 | + scenario.Steps[2].CodeBlocks[0].Content, |
| 137 | + `export THIS_VAR=this_value ; export THAT_VAR=that_value`, |
| 138 | + ) |
| 139 | + }) |
| 140 | + |
| 141 | + t.Run("Override a variable that has a subshell command as it's value", func(t *testing.T) { |
| 142 | + scenario, err := CreateScenarioFromMarkdown( |
| 143 | + "../../scenarios/testing/variables.md", |
| 144 | + []string{"bash"}, |
| 145 | + map[string]string{ |
| 146 | + "SUBSHELL_VARIABLE": "subshell_value", |
| 147 | + }, |
| 148 | + ) |
| 149 | + |
| 150 | + assert.NoError(t, err) |
| 151 | + assert.Equal(t, "subshell_value", scenario.Environment["SUBSHELL_VARIABLE"]) |
| 152 | + assert.Contains( |
| 153 | + t, |
| 154 | + scenario.Steps[3].CodeBlocks[0].Content, |
| 155 | + `export SUBSHELL_VARIABLE=subshell_value`, |
| 156 | + ) |
| 157 | + }) |
| 158 | + |
| 159 | + t.Run("Override a variable that references another variable", func(t *testing.T) { |
| 160 | + scenario, err := CreateScenarioFromMarkdown( |
| 161 | + "../../scenarios/testing/variables.md", |
| 162 | + []string{"bash"}, |
| 163 | + map[string]string{ |
| 164 | + "VAR2": "var2_value", |
| 165 | + }, |
| 166 | + ) |
| 167 | + |
| 168 | + assert.NoError(t, err) |
| 169 | + assert.Equal(t, "var2_value", scenario.Environment["VAR2"]) |
| 170 | + assert.Contains( |
| 171 | + t, |
| 172 | + scenario.Steps[4].CodeBlocks[0].Content, |
| 173 | + `export VAR2=var2_value`, |
| 174 | + ) |
| 175 | + }) |
| 176 | +} |
0 commit comments