@@ -296,6 +296,138 @@ func TestAccGithubBranchDefault(t *testing.T) {
296296 },
297297 })
298298 })
299+ t .Run ("destroys_without_error" , func (t * testing.T ) {
300+ randomID := acctest .RandString (5 )
301+ repoName := fmt .Sprintf ("%sbranch-def-%s" , testResourcePrefix , randomID )
302+
303+ repoOnlyConfig := fmt .Sprintf (`
304+ resource "github_repository" "test" {
305+ name = "%s"
306+ auto_init = true
307+ }
308+ ` , repoName )
309+
310+ fullConfig := fmt .Sprintf (`
311+ resource "github_repository" "test" {
312+ name = "%s"
313+ auto_init = true
314+ }
315+
316+ resource "github_branch_default" "test" {
317+ repository = github_repository.test.name
318+ branch = "main"
319+ }
320+ ` , repoName )
321+
322+ resource .Test (t , resource.TestCase {
323+ PreCheck : func () { skipUnauthenticated (t ) },
324+ ProviderFactories : providerFactories ,
325+ Steps : []resource.TestStep {
326+ {
327+ Config : fullConfig ,
328+ },
329+ {
330+ Config : repoOnlyConfig ,
331+ ConfigStateChecks : []statecheck.StateCheck {
332+ statecheck .ExpectKnownValue ("github_repository.test" , tfjsonpath .New ("default_branch" ), knownvalue .StringExact ("main" )),
333+ },
334+ },
335+ },
336+ })
337+ })
338+
339+ t .Run ("destroys_does_not_modify_remote_branch" , func (t * testing.T ) {
340+ // The Delete function sends a nil DefaultBranch in the PATCH body, which
341+ // go-github omits via omitempty — making it a no-op on the GitHub side.
342+ // This test pins that behavior: the remote default branch must be unchanged
343+ // after the resource is removed from Terraform state.
344+ randomID := acctest .RandString (5 )
345+ repoName := fmt .Sprintf ("%sbranch-def-%s" , testResourcePrefix , randomID )
346+
347+ repoOnlyConfig := fmt .Sprintf (`
348+ resource "github_repository" "test" {
349+ name = "%s"
350+ auto_init = true
351+ }
352+ ` , repoName )
353+
354+ fullConfig := fmt .Sprintf (`
355+ resource "github_repository" "test" {
356+ name = "%s"
357+ auto_init = true
358+ }
359+
360+ resource "github_branch_default" "test" {
361+ repository = github_repository.test.name
362+ branch = "development"
363+ rename = true
364+ }
365+ ` , repoName )
366+
367+ resource .Test (t , resource.TestCase {
368+ PreCheck : func () { skipUnauthenticated (t ) },
369+ ProviderFactories : providerFactories ,
370+ Steps : []resource.TestStep {
371+ {
372+ Config : fullConfig ,
373+ ConfigStateChecks : []statecheck.StateCheck {
374+ statecheck .ExpectKnownValue ("github_branch_default.test" , tfjsonpath .New ("branch" ), knownvalue .StringExact ("development" )),
375+ },
376+ },
377+ {
378+ Config : repoOnlyConfig ,
379+ ConfigStateChecks : []statecheck.StateCheck {
380+ statecheck .ExpectKnownValue ("github_repository.test" , tfjsonpath .New ("default_branch" ), knownvalue .StringExact ("development" )),
381+ },
382+ },
383+ },
384+ })
385+ })
386+
387+ t .Run ("gracefully_handles_repository_deleted_out_of_band" , func (t * testing.T ) {
388+ // This tests the Read 404 path: when the repository is deleted externally,
389+ // a state refresh discovers the 404, removes both resources from state
390+ // without error, and does not attempt to call Delete.
391+ randomID := acctest .RandString (5 )
392+ repoName := fmt .Sprintf ("%sbranch-def-%s" , testResourcePrefix , randomID )
393+
394+ config := fmt .Sprintf (`
395+ resource "github_repository" "test" {
396+ name = "%s"
397+ auto_init = true
398+ }
399+
400+ resource "github_branch_default" "test" {
401+ repository = github_repository.test.name
402+ branch = "main"
403+ }
404+ ` , repoName )
405+
406+ resource .Test (t , resource.TestCase {
407+ PreCheck : func () { skipUnauthenticated (t ) },
408+ ProviderFactories : providerFactories ,
409+ Steps : []resource.TestStep {
410+ {
411+ Config : config ,
412+ },
413+ {
414+ PreConfig : func () {
415+ meta , err := getTestMeta ()
416+ if err != nil {
417+ t .Errorf ("failed to get test meta: %s" , err )
418+ return
419+ }
420+ if _ , err := meta .v3client .Repositories .Delete (t .Context (), meta .name , repoName ); err != nil {
421+ t .Errorf ("failed to delete repository out-of-band: %s" , err )
422+ }
423+ },
424+ RefreshState : true ,
425+ ExpectNonEmptyPlan : true ,
426+ },
427+ },
428+ })
429+ })
430+
299431 t .Run ("regression_prevent_trying_rename_to_same_name" , func (t * testing.T ) {
300432 randomID := acctest .RandString (5 )
301433 repoName := fmt .Sprintf ("%sbranch-def-%s" , testResourcePrefix , randomID )
0 commit comments