Skip to content

Commit 503e6d1

Browse files
committed
Cleaning some tests
Signed-off-by: Timo Sand <[email protected]>
1 parent c820025 commit 503e6d1

1 file changed

Lines changed: 28 additions & 32 deletions

File tree

github/resource_github_repository_test.go

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/go-cty/cty"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1111

12+
"github.com/hashicorp/terraform-plugin-testing/compare"
1213
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1314
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1415
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
@@ -64,34 +65,33 @@ func TestAccGithubRepository(t *testing.T) {
6465
oldName := fmt.Sprintf(`%srename-%s`, testResourcePrefix, randomID)
6566
newName := fmt.Sprintf(`%s-renamed`, oldName)
6667

67-
config := fmt.Sprintf(`
68+
config := `
6869
resource "github_repository" "test" {
6970
name = "%[1]s"
7071
description = "Terraform acceptance tests %[2]s"
7172
visibility = "%s"
7273
}
73-
`, oldName, randomID, testAccConf.testRepositoryVisibility)
74+
`
7475

76+
nameDiffer := statecheck.CompareValue(compare.ValuesDiffer())
7577
resource.ParallelTest(t, resource.TestCase{
7678
PreCheck: func() { skipUnauthenticated(t) },
7779
ProviderFactories: providerFactories,
7880
Steps: []resource.TestStep{
7981
{
80-
Config: config,
82+
Config: fmt.Sprintf(config, oldName, randomID, testAccConf.testRepositoryVisibility),
8183
ConfigStateChecks: []statecheck.StateCheck{
8284
statecheck.ExpectKnownValue("github_repository.test", tfjsonpath.New("name"), knownvalue.StringExact(oldName)),
8385
statecheck.ExpectKnownValue("github_repository.test", tfjsonpath.New("full_name"), knownvalue.StringRegexp(regexp.MustCompile(regexp.QuoteMeta(oldName)))),
86+
nameDiffer.AddStateValue("github_repository.test", tfjsonpath.New("name")),
8487
},
8588
},
8689
{
87-
// Rename the repo to something else
88-
Config: strings.Replace(
89-
config,
90-
oldName,
91-
newName, 1),
90+
Config: fmt.Sprintf(config, newName, randomID, testAccConf.testRepositoryVisibility),
9291
ConfigStateChecks: []statecheck.StateCheck{
9392
statecheck.ExpectKnownValue("github_repository.test", tfjsonpath.New("name"), knownvalue.StringExact(newName)),
9493
statecheck.ExpectKnownValue("github_repository.test", tfjsonpath.New("full_name"), knownvalue.StringRegexp(regexp.MustCompile(regexp.QuoteMeta(newName)))),
94+
nameDiffer.AddStateValue("github_repository.test", tfjsonpath.New("name")),
9595
},
9696
},
9797
},
@@ -199,11 +199,11 @@ resource "github_repository" "test" {
199199
t.Run("manages the default branch feature for a repository", func(t *testing.T) {
200200
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
201201
testRepoName := fmt.Sprintf("%sbranch-%s", testResourcePrefix, randomID)
202-
config := fmt.Sprintf(`
202+
config := `
203203
resource "github_repository" "test" {
204204
name = "%s"
205205
description = "Terraform acceptance tests %[1]s"
206-
default_branch = "main"
206+
default_branch = "%s"
207207
auto_init = true
208208
visibility = "%s"
209209
}
@@ -212,39 +212,36 @@ resource "github_repository" "test" {
212212
repository = github_repository.test.name
213213
branch = "default"
214214
}
215-
`, testRepoName, testAccConf.testRepositoryVisibility)
215+
`
216216

217+
defaultBranchChangeCheck := statecheck.CompareValue(compare.ValuesDiffer())
217218
resource.ParallelTest(t, resource.TestCase{
218219
PreCheck: func() { skipUnauthenticated(t) },
219220
ProviderFactories: providerFactories,
220221
Steps: []resource.TestStep{
221222
{
222-
Config: config,
223+
Config: fmt.Sprintf(config, testRepoName, "main", testAccConf.testRepositoryVisibility),
223224
ConfigStateChecks: []statecheck.StateCheck{
224-
statecheck.ExpectKnownValue("github_repository.test", tfjsonpath.New("default_branch"), knownvalue.StringExact("main")),
225+
defaultBranchChangeCheck.AddStateValue("github_repository.test", tfjsonpath.New("default_branch")),
225226
},
226227
},
227-
// Test changing default_branch
228228
{
229-
Config: strings.Replace(config,
230-
`default_branch = "main"`,
231-
`default_branch = "default"`, 1),
229+
Config: fmt.Sprintf(config, testRepoName, "default", testAccConf.testRepositoryVisibility),
232230
ConfigStateChecks: []statecheck.StateCheck{
233-
statecheck.ExpectKnownValue("github_repository.test", tfjsonpath.New("default_branch"), knownvalue.StringExact("default")),
231+
defaultBranchChangeCheck.AddStateValue("github_repository.test", tfjsonpath.New("default_branch")),
234232
},
235233
},
236-
// Test changing default_branch back to main again
237234
{
238-
Config: config,
235+
Config: fmt.Sprintf(config, testRepoName, "main", testAccConf.testRepositoryVisibility),
239236
ConfigStateChecks: []statecheck.StateCheck{
240-
statecheck.ExpectKnownValue("github_repository.test", tfjsonpath.New("default_branch"), knownvalue.StringExact("main")),
237+
defaultBranchChangeCheck.AddStateValue("github_repository.test", tfjsonpath.New("default_branch")),
241238
},
242239
},
243240
},
244241
})
245242
})
246243

247-
t.Run("allows setting default_branch on an empty repository", func(t *testing.T) {
244+
t.Run("updates_default_branchon_an_empty_repository_without_error", func(t *testing.T) {
248245
// Although default_branch is deprecated, for backwards compatibility
249246
// we allow setting it to "main".
250247

@@ -259,26 +256,25 @@ resource "github_repository" "test" {
259256
}
260257
`, testRepoName, testAccConf.testRepositoryVisibility)
261258

262-
defaultBranchChecks := []statecheck.StateCheck{
263-
statecheck.ExpectKnownValue("github_repository.test", tfjsonpath.New("default_branch"), knownvalue.StringExact("main")),
264-
}
265-
259+
defaultBranchChangeCheck := statecheck.CompareValue(compare.ValuesSame())
266260
resource.ParallelTest(t, resource.TestCase{
267261
PreCheck: func() { skipUnauthenticated(t) },
268262
ProviderFactories: providerFactories,
269263
Steps: []resource.TestStep{
270-
// Test creation with default_branch set
271264
{
272-
Config: config,
273-
ConfigStateChecks: defaultBranchChecks,
265+
Config: config,
266+
ConfigStateChecks: []statecheck.StateCheck{
267+
statecheck.ExpectKnownValue("github_repository.test", tfjsonpath.New("default_branch"), knownvalue.StringExact("main")),
268+
defaultBranchChangeCheck.AddStateValue("github_repository.test", tfjsonpath.New("default_branch")),
269+
},
274270
},
275-
// Test that changing another property does not try to set
276-
// default_branch (which would crash).
277271
{
278272
Config: strings.Replace(config,
279273
`acceptance tests`,
280274
`acceptance test`, 1),
281-
ConfigStateChecks: defaultBranchChecks,
275+
ConfigStateChecks: []statecheck.StateCheck{
276+
defaultBranchChangeCheck.AddStateValue("github_repository.test", tfjsonpath.New("default_branch")),
277+
},
282278
},
283279
},
284280
})

0 commit comments

Comments
 (0)