Skip to content

Commit 6ec5526

Browse files
committed
Refactor to use ConfigStateChecks in Resource
Signed-off-by: Timo Sand <[email protected]>
1 parent 7f9672c commit 6ec5526

1 file changed

Lines changed: 25 additions & 36 deletions

File tree

github/resource_github_repository_pages_test.go

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package github
22

33
import (
44
"fmt"
5-
"strings"
5+
"regexp"
66
"testing"
77

88
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
99
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10-
"github.com/hashicorp/terraform-plugin-testing/terraform"
10+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
11+
"github.com/hashicorp/terraform-plugin-testing/statecheck"
12+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
1113
)
1214

1315
func TestAccGithubRepositoryPages(t *testing.T) {
@@ -45,12 +47,12 @@ func TestAccGithubRepositoryPages(t *testing.T) {
4547
Steps: []resource.TestStep{
4648
{
4749
Config: config,
48-
Check: resource.ComposeTestCheckFunc(
49-
resource.TestCheckResourceAttr("github_repository_pages.test", "build_type", "legacy"),
50-
resource.TestCheckResourceAttr("github_repository_pages.test", "source.0.branch", "main"),
51-
resource.TestCheckResourceAttr("github_repository_pages.test", "source.0.path", "/"),
52-
resource.TestCheckResourceAttrSet("github_repository_pages.test", "api_url"),
53-
),
50+
ConfigStateChecks: []statecheck.StateCheck{
51+
statecheck.ExpectKnownValue("github_repository_pages.test", tfjsonpath.New("build_type"), knownvalue.StringExact("legacy")),
52+
statecheck.ExpectKnownValue("github_repository_pages.test", tfjsonpath.New("source").AtSliceIndex(0).AtMapKey("branch"), knownvalue.StringExact("main")),
53+
statecheck.ExpectKnownValue("github_repository_pages.test", tfjsonpath.New("source").AtSliceIndex(0).AtMapKey("path"), knownvalue.StringExact("/")),
54+
statecheck.ExpectKnownValue("github_repository_pages.test", tfjsonpath.New("api_url"), knownvalue.StringRegexp(regexp.MustCompile("https://.*"))),
55+
},
5456
},
5557
},
5658
})
@@ -81,9 +83,9 @@ func TestAccGithubRepositoryPages(t *testing.T) {
8183
Steps: []resource.TestStep{
8284
{
8385
Config: config,
84-
Check: resource.ComposeTestCheckFunc(
85-
resource.TestCheckResourceAttr("github_repository_pages.test", "build_type", "workflow"),
86-
),
86+
ConfigStateChecks: []statecheck.StateCheck{
87+
statecheck.ExpectKnownValue("github_repository_pages.test", tfjsonpath.New("build_type"), knownvalue.StringExact("workflow")),
88+
},
8789
},
8890
},
8991
})
@@ -121,15 +123,15 @@ source {
121123
Steps: []resource.TestStep{
122124
{
123125
Config: fmt.Sprintf(config, repoName, baseRepoVisibility, testAccConf.owner, "legacy", sourceConfig),
124-
Check: resource.ComposeTestCheckFunc(
125-
resource.TestCheckResourceAttr("github_repository_pages.test", "build_type", "legacy"),
126-
),
126+
ConfigStateChecks: []statecheck.StateCheck{
127+
statecheck.ExpectKnownValue("github_repository_pages.test", tfjsonpath.New("build_type"), knownvalue.StringExact("legacy")),
128+
},
127129
},
128130
{
129131
Config: fmt.Sprintf(config, repoName, baseRepoVisibility, testAccConf.owner, "workflow", ""),
130-
Check: resource.ComposeTestCheckFunc(
131-
resource.TestCheckResourceAttr("github_repository_pages.test", "build_type", "workflow"),
132-
),
132+
ConfigStateChecks: []statecheck.StateCheck{
133+
statecheck.ExpectKnownValue("github_repository_pages.test", tfjsonpath.New("build_type"), knownvalue.StringExact("workflow")),
134+
},
133135
},
134136
},
135137
})
@@ -164,27 +166,14 @@ source {
164166
Steps: []resource.TestStep{
165167
{
166168
Config: config,
167-
Check: resource.ComposeTestCheckFunc(
168-
resource.TestCheckResourceAttr("github_repository_pages.test", "build_type", "legacy"),
169-
),
169+
ConfigStateChecks: []statecheck.StateCheck{
170+
statecheck.ExpectKnownValue("github_repository_pages.test", tfjsonpath.New("build_type"), knownvalue.StringExact("legacy")),
171+
},
170172
},
171173
{
172-
ResourceName: "github_repository_pages.test",
173-
ImportState: true,
174-
ImportStateVerify: true,
175-
ImportStateIdFunc: func(state *terraform.State) (string, error) {
176-
repo := state.RootModule().Resources["github_repository.test"]
177-
178-
if repo == nil {
179-
return "", fmt.Errorf("github_repository.test not found in state")
180-
}
181-
repoID := repo.Primary.ID
182-
if repoID == "" {
183-
return "", fmt.Errorf("github_repository.test does not have an id in terraform state")
184-
}
185-
return fmt.Sprintf("%s:%s", strings.Split(repo.Primary.Attributes["full_name"], "/")[0], repoID), nil
186-
},
187-
ImportStateVerifyIgnore: []string{"build_status"},
174+
ResourceName: "github_repository_pages.test",
175+
ImportState: true,
176+
ImportStateId: fmt.Sprintf("%s:%s", testAccConf.owner, repoName),
188177
},
189178
},
190179
})

0 commit comments

Comments
 (0)