Skip to content

Commit

Permalink
Adding unit tests for AddTerraformBlock() (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Nov 15, 2023
1 parent d417058 commit bd351c0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
9 changes: 2 additions & 7 deletions internal/teststep/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,9 @@ type ConfigString string
// present.
func (c ConfigString) AddTerraformBlock() string {
if !c.IsJSON() && !c.HasTerraformBlock() {
if strings.HasPrefix(string(c), "\n") {
return fmt.Sprintf(`terraform {}
%s`, c)
} else {
return fmt.Sprintf(`terraform {}
str := strings.TrimLeft(string(c), "\n")

%s`, c)
}
return fmt.Sprintf("terraform {}\n\n%s", str)
}

return string(c)
Expand Down
59 changes: 59 additions & 0 deletions internal/teststep/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,62 @@ func TestConfiguration(t *testing.T) {
})
}
}

func TestConfigString_AddTerraformBlock(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
configString ConfigString
expected string
}{
"json": {
configString: `{"resource":{}}`,
expected: `{"resource":{}}`,
},
"terraform-block-present": {
configString: `terraform {}
resource {}`,
expected: `terraform {}
resource {}`,
},
"terraform-block-absent-newline-absent": {
configString: `resource {}`,
expected: `terraform {}
resource {}`,
},
"terraform-block-absent-newline-present": {
configString: `
resource {}`,
expected: `terraform {}
resource {}`,
},
"terraform-block-absent-multiple-newlines-present": {
configString: `
resource {}`,
expected: `terraform {}
resource {}`,
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.configString.AddTerraformBlock()

if diff := cmp.Diff(testCase.expected, got); diff != "" {
t.Errorf("expected %+v, got %+v", testCase.expected, got)
}
})
}
}

0 comments on commit bd351c0

Please sign in to comment.