@@ -236,7 +236,13 @@ func TestAccGithubRepositories(t *testing.T) {
236236 resource "github_repository" "test" {
237237 name = "tf-acc-test-branch-%[1]s"
238238 description = "Terraform acceptance tests %[1]s"
239- default_branch = "main"
239+ default_branch = "main"
240+ auto_init = true
241+ }
242+
243+ resource "github_branch" "default" {
244+ repository = github_repository.test.name
245+ branch = "default"
240246 }
241247 ` , randomID )
242248
@@ -247,14 +253,12 @@ func TestAccGithubRepositories(t *testing.T) {
247253 "main" ,
248254 ),
249255 ),
250- // FIXME: Deferred until https://github.com/integrations/terraform-provider-github/issues/513
251- // > Cannot update default branch for an empty repository. Please init the repository and push first
252- // "after": resource.ComposeTestCheckFunc(
253- // resource.TestCheckResourceAttr(
254- // "github_repository.test", "default_branch",
255- // "default",
256- // ),
257- // ),
256+ "after" : resource .ComposeTestCheckFunc (
257+ resource .TestCheckResourceAttr (
258+ "github_repository.test" , "default_branch" ,
259+ "default" ,
260+ ),
261+ ),
258262 }
259263
260264 testCase := func (t * testing.T , mode string ) {
@@ -266,12 +270,74 @@ func TestAccGithubRepositories(t *testing.T) {
266270 Config : config ,
267271 Check : checks ["before" ],
268272 },
269- // {
270- // Config: strings.Replace(config,
271- // `default_branch = "main"`,
272- // `default_branch = "default"`, 1),
273- // Check: checks["after"],
274- // },
273+ // Test changing default_branch
274+ {
275+ Config : strings .Replace (config ,
276+ `default_branch = "main"` ,
277+ `default_branch = "default"` , 1 ),
278+ Check : checks ["after" ],
279+ },
280+ // Test changing default_branch back to main again
281+ {
282+ Config : config ,
283+ Check : checks ["before" ],
284+ },
285+ },
286+ })
287+ }
288+
289+ t .Run ("with an anonymous account" , func (t * testing.T ) {
290+ t .Skip ("anonymous account not supported for this operation" )
291+ })
292+
293+ t .Run ("with an individual account" , func (t * testing.T ) {
294+ testCase (t , individual )
295+ })
296+
297+ t .Run ("with an organization account" , func (t * testing.T ) {
298+ testCase (t , organization )
299+ })
300+
301+ })
302+
303+ t .Run ("allows setting default_branch on an empty repository" , func (t * testing.T ) {
304+
305+ // Although default_branch is deprecated, for backwards compatibility
306+ // we allow setting it to "main".
307+
308+ config := fmt .Sprintf (`
309+ resource "github_repository" "test" {
310+ name = "tf-acc-test-empty-%[1]s"
311+ description = "Terraform acceptance tests %[1]s"
312+ default_branch = "main"
313+ }
314+ ` , randomID )
315+
316+ check := resource .ComposeTestCheckFunc (
317+ resource .TestCheckResourceAttr (
318+ "github_repository.test" , "default_branch" ,
319+ "main" ,
320+ ),
321+ )
322+
323+ testCase := func (t * testing.T , mode string ) {
324+ resource .Test (t , resource.TestCase {
325+ PreCheck : func () { skipUnlessMode (t , mode ) },
326+ Providers : testAccProviders ,
327+ Steps : []resource.TestStep {
328+ // Test creation with default_branch set
329+ {
330+ Config : config ,
331+ Check : check ,
332+ },
333+ // Test that changing another property does not try to set
334+ // default_branch (which would crash).
335+ {
336+ Config : strings .Replace (config ,
337+ `acceptance tests` ,
338+ `acceptance test` , 1 ),
339+ Check : check ,
340+ },
275341 },
276342 })
277343 }
0 commit comments