diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4362bb2970..a924c1be9f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -33,9 +33,9 @@ This section describes a typical sequence performed when developing locally. Ful Once you have the repository cloned, there's a couple of additional steps you'll need to take. Since most of the testing is acceptance or integration testing, we need to manipulate real GitHub resources in order to run it. Useful setup steps are listed below: - If you haven't already, [create a GitHub organization you can use for testing](#github-organization). - - Optional: you may find it beneficial to create a test user as well in order to avoid potential rate-limiting issues on your main account. - - Your organization _must_ have a repository called `terraform-template-module`. The [terraformtesting/terraform-template-module](https://github.com/terraformtesting/terraform-template-module) repo is a good, re-usable example. - - You _must_ make sure that the "Template Repository" item in Settings is checked for this repo. + - Optional: you may find it beneficial to create a test user as well in order to avoid potential rate-limiting issues on your main account. + - Your organization _must_ have a repository called `terraform-template-module`. The [terraformtesting/terraform-template-module](https://github.com/terraformtesting/terraform-template-module) repo is a good, re-usable example. + - You _must_ make sure that the "Template Repository" item in Settings is checked for this repo. - If you haven't already, generate a Personal Access Token (PAT) for authenticating your test runs. - Export the necessary configuration for authenticating your provider with GitHub @@ -52,7 +52,7 @@ Once you have the repository cloned, there's a couple of additional steps you'll ### Local Development Iteration 1. Write a test describing what you will fix. See [`github_label`](./github/resource_github_issue_label_test.go) for an example format. -1. Run your test and observe it fail. Enabling debug output allows for observing the underlying requests and responses made as well as viewing state (search `STATE:`) generated during the acceptance test run. +2. Run your test and observe it fail. Enabling debug output allows for observing the underlying requests and responses made as well as viewing state (search `STATE:`) generated during the acceptance test run. ```sh TF_LOG=DEBUG TF_ACC=1 go test -v ./... -run ^TestAccGithubIssueLabel @@ -182,6 +182,9 @@ export GH_TEST_ENTERPRISE_EMU_GROUP_ID= # Configure test options export GH_TEST_ADVANCED_SECURITY= + +# Configure if the enterprise is an EMU enterprise +export GH_TEST_ENTERPRISE_IS_EMU= ``` There are also a small amount of unit tests in the provider. Due to the nature of the provider, such tests are currently only recommended for exercising functionality completely internal to the provider. These may be executed by running `make test`. diff --git a/github/acc_test.go b/github/acc_test.go index 5105c913fe..b3479fa5cc 100644 --- a/github/acc_test.go +++ b/github/acc_test.go @@ -43,7 +43,8 @@ type testAccConfig struct { token string // Enterprise configuration - enterpriseSlug string + enterpriseSlug string + enterpriseIsEMU bool // Global test configuration testPublicRepository string @@ -76,6 +77,9 @@ type testAccConfig struct { // Test options testAdvancedSecurity bool + + // Test repository configuration + testRepositoryVisibility string } var testAccConf *testAccConfig @@ -130,6 +134,8 @@ func TestMain(m *testing.M) { testExternalUserToken: os.Getenv("GH_TEST_EXTERNAL_USER_TOKEN"), testExternalUser2: os.Getenv("GH_TEST_EXTERNAL_USER2"), testAdvancedSecurity: os.Getenv("GH_TEST_ADVANCED_SECURITY") == "true", + testRepositoryVisibility: "public", + enterpriseIsEMU: authMode == enterprise && os.Getenv("GH_TEST_ENTERPRISE_IS_EMU") == "true", } if config.authMode != anonymous { @@ -165,6 +171,10 @@ func TestMain(m *testing.M) { if err == nil { config.testEnterpriseEMUGroupId = i } + + if config.enterpriseIsEMU { + config.testRepositoryVisibility = "private" + } } i, err := strconv.Atoi(os.Getenv("GH_TEST_ORG_APP_INSTALLATION_ID")) @@ -318,6 +328,18 @@ func skipUnlessHasAppInstallations(t *testing.T) { } } +func skipUnlessEMUEnterprise(t *testing.T) { + if !testAccConf.enterpriseIsEMU { + t.Skip("Skipping as test mode is not EMU enterprise") + } +} + +func skipIfEMUEnterprise(t *testing.T) { + if testAccConf.enterpriseIsEMU { + t.Skip("Skipping as this test is not supported for EMU enterprise") + } +} + func skipUnlessMode(t *testing.T, testModes ...testMode) { if !slices.Contains(testModes, testAccConf.authMode) { t.Skip("Skipping as not supported test mode") diff --git a/github/resource_github_emu_group_mapping_test.go b/github/resource_github_emu_group_mapping_test.go index aeee3f8e32..69bc257149 100644 --- a/github/resource_github_emu_group_mapping_test.go +++ b/github/resource_github_emu_group_mapping_test.go @@ -21,7 +21,7 @@ func TestAccGithubEMUGroupMapping(t *testing.T) { teamName := fmt.Sprintf("%steam-emu-%s", testResourcePrefix, randomID) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEMUEnterprise(t) }, ProviderFactories: providerFactories, CheckDestroy: testAccCheckGithubEMUGroupMappingDestroy, Steps: []resource.TestStep{ @@ -43,7 +43,7 @@ func TestAccGithubEMUGroupMapping(t *testing.T) { rn := "github_emu_group_mapping.test" resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEMUEnterprise(t) }, ProviderFactories: providerFactories, CheckDestroy: testAccCheckGithubEMUGroupMappingDestroy, Steps: []resource.TestStep{ @@ -84,7 +84,7 @@ func TestAccGithubEMUGroupMapping(t *testing.T) { `, teamName, teamName2, groupID) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEMUEnterprise(t) }, ProviderFactories: providerFactories, CheckDestroy: testAccCheckGithubEMUGroupMappingDestroy, Steps: []resource.TestStep{ @@ -107,7 +107,7 @@ func TestAccGithubEMUGroupMapping(t *testing.T) { teamName2 := fmt.Sprintf("%s-upd", teamName1) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessEMUEnterprise(t) }, ProviderFactories: providerFactories, CheckDestroy: testAccCheckGithubEMUGroupMappingDestroy, Steps: []resource.TestStep{ @@ -149,7 +149,7 @@ resource "github_emu_group_mapping" "test" { ` resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessEnterprise(t) }, + PreCheck: func() { skipUnlessEMUEnterprise(t) }, ProviderFactories: providerFactories, CheckDestroy: testAccCheckGithubEMUGroupMappingDestroy, Steps: []resource.TestStep{ diff --git a/github/resource_github_repository_ruleset_test.go b/github/resource_github_repository_ruleset_test.go index a10affa289..3cb74e2117 100644 --- a/github/resource_github_repository_ruleset_test.go +++ b/github/resource_github_repository_ruleset_test.go @@ -12,13 +12,6 @@ import ( ) func TestAccGithubRepositoryRuleset(t *testing.T) { - baseRepoVisibility := "public" - - if testAccConf.authMode == enterprise { - // This enables repos to be created even in GHEC EMU - baseRepoVisibility = "private" - } - t.Run("create_branch_ruleset", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) repoName := fmt.Sprintf("%srepo-ruleset-%s", testResourcePrefix, randomID) @@ -120,7 +113,7 @@ resource "github_repository_ruleset" "test" { non_fast_forward = true } } -`, repoName, baseRepoVisibility) +`, repoName, testAccConf.testRepositoryVisibility) resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnauthenticated(t) }, @@ -189,7 +182,7 @@ resource "github_repository_ruleset" "test" { } } } -`, repoName, baseRepoVisibility) +`, repoName, testAccConf.testRepositoryVisibility) resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnauthenticated(t) }, @@ -308,13 +301,13 @@ resource "github_repository_ruleset" "test" { ProviderFactories: providerFactories, Steps: []resource.TestStep{ { - Config: fmt.Sprintf(config, repoName, randomID, baseRepoVisibility, name), + Config: fmt.Sprintf(config, repoName, randomID, testAccConf.testRepositoryVisibility, name), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository_ruleset.test", "name", name), ), }, { - Config: fmt.Sprintf(config, repoName, randomID, baseRepoVisibility, nameUpdated), + Config: fmt.Sprintf(config, repoName, randomID, testAccConf.testRepositoryVisibility, nameUpdated), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository_ruleset.test", "name", nameUpdated), ), @@ -367,9 +360,9 @@ resource "github_repository_ruleset" "test" { } } ` - config := fmt.Sprintf(baseConfig, repoName, baseRepoVisibility, bypassActorsConfig) + config := fmt.Sprintf(baseConfig, repoName, testAccConf.testRepositoryVisibility, bypassActorsConfig) - configUpdated := fmt.Sprintf(baseConfig, repoName, baseRepoVisibility, "") + configUpdated := fmt.Sprintf(baseConfig, repoName, testAccConf.testRepositoryVisibility, "") resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnauthenticated(t) }, ProviderFactories: providerFactories, @@ -435,13 +428,13 @@ resource "github_repository_ruleset" "test" { ProviderFactories: providerFactories, Steps: []resource.TestStep{ { - Config: fmt.Sprintf(config, repoName, randomID, baseRepoVisibility, bypassMode), + Config: fmt.Sprintf(config, repoName, randomID, testAccConf.testRepositoryVisibility, bypassMode), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository_ruleset.test", "bypass_actors.0.bypass_mode", bypassMode), ), }, { - Config: fmt.Sprintf(config, repoName, randomID, baseRepoVisibility, bypassModeUpdated), + Config: fmt.Sprintf(config, repoName, randomID, testAccConf.testRepositoryVisibility, bypassModeUpdated), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository_ruleset.test", "bypass_actors.0.bypass_mode", bypassModeUpdated), ), @@ -486,7 +479,7 @@ resource "github_repository_ruleset" "test" { creation = true } } - `, repoName, randomID, baseRepoVisibility) + `, repoName, randomID, testAccConf.testRepositoryVisibility) resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnauthenticated(t) }, @@ -508,13 +501,6 @@ resource "github_repository_ruleset" "test" { } func TestAccGithubRepositoryRulesetArchived(t *testing.T) { - baseRepoVisibility := "public" - - if testAccConf.authMode == enterprise { - // This enables repos to be created even in GHEC EMU - baseRepoVisibility = "private" - } - t.Run("skips update and delete on archived repository", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) repoName := fmt.Sprintf("%srepo-ruleset-arch-%s", testResourcePrefix, randomID) @@ -543,9 +529,9 @@ func TestAccGithubRepositoryRulesetArchived(t *testing.T) { PreCheck: func() { skipUnauthenticated(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ - {Config: fmt.Sprintf(config, repoName, archivedBefore, baseRepoVisibility, enforcementBefore)}, - {Config: fmt.Sprintf(config, repoName, archivedAfter, baseRepoVisibility, enforcementBefore)}, - {Config: fmt.Sprintf(config, repoName, archivedAfter, baseRepoVisibility, enforcementAfter)}, + {Config: fmt.Sprintf(config, repoName, archivedBefore, testAccConf.testRepositoryVisibility, enforcementBefore)}, + {Config: fmt.Sprintf(config, repoName, archivedAfter, testAccConf.testRepositoryVisibility, enforcementBefore)}, + {Config: fmt.Sprintf(config, repoName, archivedAfter, testAccConf.testRepositoryVisibility, enforcementAfter)}, }, }) }) @@ -577,10 +563,10 @@ resource "github_repository_ruleset" "test" { ProviderFactories: providerFactories, Steps: []resource.TestStep{ { - Config: fmt.Sprintf(repoConfig, repoName, false, baseRepoVisibility, ""), + Config: fmt.Sprintf(repoConfig, repoName, false, testAccConf.testRepositoryVisibility, ""), }, { - Config: fmt.Sprintf(repoConfig, repoName, true, baseRepoVisibility, rulesetConfig), + Config: fmt.Sprintf(repoConfig, repoName, true, testAccConf.testRepositoryVisibility, rulesetConfig), ExpectError: regexp.MustCompile("cannot create ruleset on archived repository"), }, }, @@ -764,12 +750,6 @@ func TestAccGithubRepositoryRuleset_requiredReviewers(t *testing.T) { repoName := fmt.Sprintf("%srepo-ruleset-req-rev-%s", testResourcePrefix, randomID) teamName := fmt.Sprintf("%steam-req-rev-%s", testResourcePrefix, randomID) rulesetName := fmt.Sprintf("%s-ruleset-req-rev-%s", testResourcePrefix, randomID) - baseRepoVisibility := "public" - - if testAccConf.authMode == enterprise { - // This enables repos to be created even in GHEC EMU - baseRepoVisibility = "private" - } config := fmt.Sprintf(` resource "github_repository" "test" { @@ -822,7 +802,7 @@ resource "github_repository_ruleset" "test" { depends_on = [github_team_repository.test] } -`, repoName, baseRepoVisibility, teamName, rulesetName) +`, repoName, testAccConf.testRepositoryVisibility, teamName, rulesetName) // Updated config: change minimum_approvals from 1 to 2 configUpdated := fmt.Sprintf(` @@ -876,7 +856,7 @@ resource "github_repository_ruleset" "test" { depends_on = [github_team_repository.test] } -`, repoName, baseRepoVisibility, teamName, rulesetName) +`, repoName, testAccConf.testRepositoryVisibility, teamName, rulesetName) resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnlessHasOrgs(t) }, diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index 7726a58479..d0c363ea2d 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -34,8 +34,9 @@ func TestAccGithubRepository(t *testing.T) { merge_commit_message = "PR_TITLE" auto_init = false web_commit_signoff_required = true + visibility = "%s" } - `, testRepoName) + `, testRepoName, testAccConf.testRepositoryVisibility) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -81,8 +82,9 @@ func TestAccGithubRepository(t *testing.T) { resource "github_repository" "test" { name = "%[1]s" description = "Terraform acceptance tests %[2]s" + visibility = "%s" } - `, oldName, randomID) + `, oldName, randomID, testAccConf.testRepositoryVisibility) checks := map[string]resource.TestCheckFunc{ "before": resource.ComposeTestCheckFunc( @@ -135,8 +137,9 @@ func TestAccGithubRepository(t *testing.T) { name = "%s" description = "Terraform acceptance tests %[1]s" auto_init = false + visibility = "%s" } - `, testRepoName) + `, testRepoName, testAccConf.testRepositoryVisibility) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("github_repository.test", "name"), @@ -168,6 +171,7 @@ func TestAccGithubRepository(t *testing.T) { resource "github_repository" "test" { name = "%s" archived = %s + visibility = "%s" } ` @@ -176,13 +180,13 @@ resource "github_repository" "test" { ProviderFactories: providerFactories, Steps: []resource.TestStep{ { - Config: fmt.Sprintf(config, testRepoName, "false"), + Config: fmt.Sprintf(config, testRepoName, "false", testAccConf.testRepositoryVisibility), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository.test", "archived", "false"), ), }, { - Config: fmt.Sprintf(config, testRepoName, "true"), + Config: fmt.Sprintf(config, testRepoName, "true", testAccConf.testRepositoryVisibility), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository.test", "archived", "true"), ), @@ -199,8 +203,9 @@ resource "github_repository" "test" { name = "%s" description = "Terraform acceptance tests %[1]s" has_projects = false + visibility = "%s" } - `, testRepoName) + `, testRepoName, testAccConf.testRepositoryVisibility) checks := map[string]resource.TestCheckFunc{ "before": resource.ComposeTestCheckFunc( @@ -244,13 +249,14 @@ resource "github_repository" "test" { description = "Terraform acceptance tests %[1]s" default_branch = "main" auto_init = true + visibility = "%s" } resource "github_branch" "default" { repository = github_repository.test.name branch = "default" } - `, testRepoName) + `, testRepoName, testAccConf.testRepositoryVisibility) checks := map[string]resource.TestCheckFunc{ "before": resource.ComposeTestCheckFunc( @@ -302,8 +308,9 @@ resource "github_repository" "test" { name = "%s" description = "Terraform acceptance tests %[1]s" default_branch = "main" + visibility = "%s" } - `, testRepoName) + `, testRepoName, testAccConf.testRepositoryVisibility) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -342,8 +349,9 @@ resource "github_repository" "test" { description = "Terraform acceptance tests %[1]s" license_template = "ms-pl" gitignore_template = "C++" + visibility = "%s" } - `, testRepoName) + `, testRepoName, testAccConf.testRepositoryVisibility) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -378,6 +386,7 @@ resource "github_repository" "test" { name = "%s" description = "Terraform acceptance tests %[1]s" topics = %s + visibility = "%s" } ` @@ -386,13 +395,13 @@ resource "github_repository" "test" { ProviderFactories: providerFactories, Steps: []resource.TestStep{ { - Config: fmt.Sprintf(config, testRepoName, topicsBefore), + Config: fmt.Sprintf(config, testRepoName, topicsBefore, testAccConf.testRepositoryVisibility), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository.test", "topics.#", "2"), ), }, { - Config: fmt.Sprintf(config, testRepoName, topicsAfter), + Config: fmt.Sprintf(config, testRepoName, topicsAfter, testAccConf.testRepositoryVisibility), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository.test", "topics.#", "3"), ), @@ -408,17 +417,17 @@ resource "github_repository" "test" { resource "github_repository" "test" { name = "%s" description = "Terraform acceptance tests %[1]s" - + visibility = "%s" template { owner = "%s" repository = "%s" } } - `, testRepoName, testAccConf.testPublicTemplateRepositoryOwner, testAccConf.testPublicTemplateRepository) + `, testRepoName, testAccConf.testRepositoryVisibility, testAccConf.testPublicTemplateRepositoryOwner, testAccConf.testPublicTemplateRepository) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnauthenticated(t) }, + PreCheck: func() { skipUnauthenticated(t); skipIfEMUEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -438,14 +447,14 @@ resource "github_repository" "test" { resource "github_repository" "test" { name = "%s" description = "Terraform acceptance tests %[1]s" - + visibility = "%s" template { owner = "%s" repository = "%s" } } - `, testRepoName, testAccConf.owner, testAccConf.testOrgTemplateRepository) + `, testRepoName, testAccConf.testRepositoryVisibility, testAccConf.owner, testAccConf.testOrgTemplateRepository) resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnlessHasOrgs(t) }, @@ -471,6 +480,7 @@ resource "github_repository" "test" { auto_init = true archive_on_destroy = true archived = %s + visibility = "%s" } ` @@ -479,13 +489,13 @@ resource "github_repository" "test" { ProviderFactories: providerFactories, Steps: []resource.TestStep{ { - Config: fmt.Sprintf(config, testRepoName, "false"), + Config: fmt.Sprintf(config, testRepoName, "false", testAccConf.testRepositoryVisibility), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository.test", "archived", "false"), ), }, { - Config: fmt.Sprintf(config, testRepoName, "true"), + Config: fmt.Sprintf(config, testRepoName, "true", testAccConf.testRepositoryVisibility), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository.test", "archived", "true"), ), @@ -603,7 +613,10 @@ resource "github_repository" "test" { `, testRepoName) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnauthenticated(t) }, + PreCheck: func() { + skipUnauthenticated(t) + skipIfEMUEnterprise(t) + }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -638,11 +651,11 @@ resource "github_repository" "test" { config := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" - visibility = "public" + visibility = "%s" vulnerability_alerts = true } - `, repoName) + `, repoName, testAccConf.testRepositoryVisibility) resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnauthenticated(t) }, @@ -665,11 +678,11 @@ resource "github_repository" "test" { config := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" - visibility = "public" + visibility = "%s" vulnerability_alerts = false } - `, repoName) + `, repoName, testAccConf.testRepositoryVisibility) resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnauthenticated(t) }, @@ -692,9 +705,9 @@ resource "github_repository" "test" { config := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" - visibility = "public" + visibility = "%s" } - `, repoName) + `, repoName, testAccConf.testRepositoryVisibility) resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnauthenticated(t) }, @@ -714,36 +727,27 @@ resource "github_repository" "test" { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) repoName := fmt.Sprintf("%s%s", testResourcePrefix, randomID) - config := fmt.Sprintf(` - resource "github_repository" "test" { - name = "%s" - visibility = "public" - - vulnerability_alerts = false - } - `, repoName) - - configUpdate := fmt.Sprintf(` + config := ` resource "github_repository" "test" { name = "%s" - visibility = "public" + visibility = "%s" - vulnerability_alerts = true + vulnerability_alerts = %t } - `, repoName) + ` resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnauthenticated(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { - Config: config, + Config: fmt.Sprintf(config, repoName, testAccConf.testRepositoryVisibility, false), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository.test", "vulnerability_alerts", "false"), ), }, { - Config: configUpdate, + Config: fmt.Sprintf(config, repoName, testAccConf.testRepositoryVisibility, true), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository.test", "vulnerability_alerts", "true"), ), @@ -759,40 +763,30 @@ resource "github_repository" "test" { mergeCommitMessage := "BLANK" updatedMergeCommitTitle := "MERGE_MESSAGE" updatedMergeCommitMessage := "PR_TITLE" + config := ` +resource "github_repository" "test" { - configs := map[string]string{ - "before": fmt.Sprintf(` - resource "github_repository" "test" { - - name = "%[1]s" - allow_merge_commit = true - merge_commit_title = "%s" - merge_commit_message = "%s" - } - `, testRepoName, mergeCommitTitle, mergeCommitMessage), - "after": fmt.Sprintf(` - resource "github_repository" "test" { - name = "%[1]s" - allow_merge_commit = true - merge_commit_title = "%s" - merge_commit_message = "%s" - } - `, testRepoName, updatedMergeCommitTitle, updatedMergeCommitMessage), - } + name = "%[1]s" + allow_merge_commit = true + merge_commit_title = "%s" + merge_commit_message = "%s" + visibility = "%s" +} +` resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnauthenticated(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { - Config: configs["before"], + Config: fmt.Sprintf(config, testRepoName, mergeCommitTitle, mergeCommitMessage, testAccConf.testRepositoryVisibility), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository.test", "merge_commit_title", mergeCommitTitle), resource.TestCheckResourceAttr("github_repository.test", "merge_commit_message", mergeCommitMessage), ), }, { - Config: configs["after"], + Config: fmt.Sprintf(config, testRepoName, updatedMergeCommitTitle, updatedMergeCommitMessage, testAccConf.testRepositoryVisibility), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository.test", "merge_commit_title", updatedMergeCommitTitle), resource.TestCheckResourceAttr("github_repository.test", "merge_commit_message", updatedMergeCommitMessage), @@ -811,24 +805,15 @@ resource "github_repository" "test" { updatedSquashMergeCommitTitle := "COMMIT_OR_PR_TITLE" updatedSquashMergeCommitMessage := "COMMIT_MESSAGES" - configs := map[string]string{ - "before": fmt.Sprintf(` - resource "github_repository" "test" { - name = "%s" - allow_squash_merge = true - squash_merge_commit_title = "%s" - squash_merge_commit_message = "%s" - } - `, testRepoName, squashMergeCommitTitle, squashMergeCommitMessage), - "after": fmt.Sprintf(` - resource "github_repository" "test" { - name = "%s" - allow_squash_merge = true - squash_merge_commit_title = "%s" - squash_merge_commit_message = "%s" - } - `, testRepoNameAfter, updatedSquashMergeCommitTitle, updatedSquashMergeCommitMessage), + config := ` + resource "github_repository" "test" { + name = "%s" + allow_squash_merge = true + squash_merge_commit_title = "%s" + squash_merge_commit_message = "%s" + visibility = "%s" } +` checks := map[string]resource.TestCheckFunc{ "before": resource.ComposeTestCheckFunc( @@ -846,11 +831,11 @@ resource "github_repository" "test" { ProviderFactories: providerFactories, Steps: []resource.TestStep{ { - Config: configs["before"], + Config: fmt.Sprintf(config, testRepoName, squashMergeCommitTitle, squashMergeCommitMessage, testAccConf.testRepositoryVisibility), Check: checks["before"], }, { - Config: configs["after"], + Config: fmt.Sprintf(config, testRepoNameAfter, updatedSquashMergeCommitTitle, updatedSquashMergeCommitMessage, testAccConf.testRepositoryVisibility), Check: checks["after"], }, }, @@ -900,6 +885,7 @@ resource "github_repository" "test" { resource "github_repository" "test" { name = "%s" auto_init = true + visibility = "%s" pages { build_type = "legacy" @@ -908,7 +894,7 @@ resource "github_repository" "test" { } } } - `, testRepoName) + `, testRepoName, testAccConf.testRepositoryVisibility) resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnauthenticated(t) }, @@ -932,11 +918,12 @@ resource "github_repository" "test" { resource "github_repository" "test" { name = "%s" auto_init = true + visibility = "%s" pages { build_type = "workflow" } } - `, testRepoName) + `, testRepoName, testAccConf.testRepositoryVisibility) resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnauthenticated(t) }, @@ -987,39 +974,20 @@ resource "github_repository" "test" { } `, testRepoName) - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_repository.test", "security_and_analysis.0.advanced_security.0.status", - "enabled", - ), - resource.TestCheckResourceAttr( - "github_repository.test", "security_and_analysis.0.code_security.0.status", - "enabled", - ), - resource.TestCheckResourceAttr( - "github_repository.test", "security_and_analysis.0.secret_scanning.0.status", - "enabled", - ), - resource.TestCheckResourceAttr( - "github_repository.test", "security_and_analysis.0.secret_scanning_push_protection.0.status", - "enabled", - ), - resource.TestCheckResourceAttr( - "github_repository.test", "security_and_analysis.0.secret_scanning_ai_detection.0.status", - "enabled", - ), - resource.TestCheckResourceAttr( - "github_repository.test", "security_and_analysis.0.secret_scanning_non_provider_patterns.0.status", - "enabled", - ), - ) resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnauthenticated(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { Config: config, - Check: check, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_repository.test", "security_and_analysis.0.advanced_security.0.status", "enabled"), + resource.TestCheckResourceAttr("github_repository.test", "security_and_analysis.0.code_security.0.status", "enabled"), + resource.TestCheckResourceAttr("github_repository.test", "security_and_analysis.0.secret_scanning.0.status", "enabled"), + resource.TestCheckResourceAttr("github_repository.test", "security_and_analysis.0.secret_scanning_push_protection.0.status", "enabled"), + resource.TestCheckResourceAttr("github_repository.test", "security_and_analysis.0.secret_scanning_ai_detection.0.status", "enabled"), + resource.TestCheckResourceAttr("github_repository.test", "security_and_analysis.0.secret_scanning_non_provider_patterns.0.status", "enabled"), + ), }, }, }) @@ -1045,23 +1013,19 @@ resource "github_repository" "test" { } `, testRepoName) - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_repository.test", "security_and_analysis.0.secret_scanning.0.status", - "enabled", - ), - resource.TestCheckResourceAttr( - "github_repository.test", "security_and_analysis.0.secret_scanning_push_protection.0.status", - "disabled", - ), - ) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnauthenticated(t) }, + PreCheck: func() { + skipUnauthenticated(t) + skipIfEMUEnterprise(t) + }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { Config: config, - Check: check, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_repository.test", "security_and_analysis.0.secret_scanning.0.status", "enabled"), + resource.TestCheckResourceAttr("github_repository.test", "security_and_analysis.0.secret_scanning_push_protection.0.status", "disabled"), + ), }, }, }) @@ -1070,19 +1034,19 @@ resource "github_repository" "test" { t.Run("creates repos with private visibility", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) testRepoName := fmt.Sprintf("%svisibility-private-%s", testResourcePrefix, randomID) - config := fmt.Sprintf(` + config := ` resource "github_repository" "private" { name = "%s" visibility = "private" } - `, testRepoName) + ` resource.Test(t, resource.TestCase{ PreCheck: func() { skipUnauthenticated(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { - Config: fmt.Sprintf(config, testRepoName, "foo"), + Config: fmt.Sprintf(config, testRepoName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("github_repository.private", "visibility", "private"), ), @@ -1127,7 +1091,7 @@ resource "github_repository" "test" { ` resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnauthenticated(t) }, + PreCheck: func() { skipUnauthenticated(t); skipIfEMUEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -1256,7 +1220,7 @@ resource "github_repository" "test" { ) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnauthenticated(t) }, + PreCheck: func() { skipUnauthenticated(t); skipIfEMUEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ { @@ -1282,7 +1246,7 @@ resource "github_repository" "test" { `, testRepoName, testAccConf.testPublicTemplateRepositoryOwner, testAccConf.testPublicTemplateRepository) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, + PreCheck: func() { skipUnlessMode(t, enterprise); skipIfEMUEnterprise(t) }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ {