From 814926b9a51b258d5be0b71f6ca031a408ea8c28 Mon Sep 17 00:00:00 2001 From: yoanm <4410697+yoanm@users.noreply.github.com> Date: Wed, 27 Mar 2024 18:50:26 +0100 Subject: [PATCH] Fix CI (#23) --- .github/workflows/codeql.yml | 8 ++ .golangci.yml | 1 + Makefile | 6 +- core/README.md | 12 +- core/config_computer_test.go | 10 +- core/config_schema_test.go | 16 +-- core/gh2tf_repo_mapper_test.go | 25 +--- core/main_test.go | 125 +++++++++--------- core/repo_config_computer.go | 10 +- core/repo_config_computer_test.go | 88 +++++-------- core/repo_schema_test.go | 210 ++++++++++++------------------ core/terraform_writer_test.go | 17 +-- core/yaml_loader_test.go | 83 ++++++------ core/yaml_validator_test.go | 35 ++--- go.mod | 2 +- go.sum | 3 + main_test.go | 5 +- workspace.go | 3 + 18 files changed, 285 insertions(+), 374 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index dbf5bba..9812b25 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -52,6 +52,14 @@ jobs: # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs # queries: security-extended,security-and-quality + # Ack, see https://github.com/github/codeql/issues/15647 - To remove when v1.22 will be correctly supported + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version-file: ./go.mod + check-latest: true + cache: true + cache-dependency-path: ./go.sum # Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java). # If this step fails, then you should remove it and run the build manually (see below) diff --git a/.golangci.yml b/.golangci.yml index b3a934f..10efc31 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -20,6 +20,7 @@ linters: - revive # see disabled linters below - unused # see disabled linters below disable: + - depguard # Should not be enabled by default but seems anyway ..... - structcheck # deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter. Replaced by unused. - ifshort # deprecated (since v1.48.0) due to: The repository of the linter has been deprecated by the owner. - interfacer # deprecated (since v1.38.0) due to: The repository of the linter has been archived by the owner. diff --git a/Makefile b/Makefile index 947f88c..5346e06 100644 --- a/Makefile +++ b/Makefile @@ -28,12 +28,12 @@ help: ## ❓ Dislay this help .PHONY: configure-dev-env configure-dev-env: ## 🤖 Install required libraries for dev environment configure-dev-env: - go install github.com/posener/goreadme/cmd/goreadme@latest + go install github.com/posener/goreadme/cmd/goreadme@v1 .PHONY: configure-test-env configure-test-env: ## 🤖 Install required libraries for test environment (golint, staticcheck, etc) configure-test-env: configure-dev-env configure-test-env: - # Nothing to install currently + go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57 ##—— 📝 Documentation ————————————————————————————————————————————————— .PHONY: build-doc @@ -60,7 +60,7 @@ build: ## 🗜️ Build package #### Use build_o="..." to specify build options $(eval build_o ?=) build: - go build -v $(build_o) + go build -v -o github-tf $(build_o) .PHONY: verify-deps verify-deps: ## 🗜️ Verify dependencies diff --git a/core/README.md b/core/README.md index 4e2b8f9..9879197 100644 --- a/core/README.md +++ b/core/README.md @@ -86,11 +86,11 @@ var ( ## Functions -### func [ApplyBranchProtectionsTemplate](./repo_config_computer.go#L112) +### func [ApplyBranchProtectionsTemplate](./repo_config_computer.go#L110) `func ApplyBranchProtectionsTemplate(config *GhRepoConfig, templates *TemplatesConfig) error` -### func [ApplyBranchesTemplate](./repo_config_computer.go#L63) +### func [ApplyBranchesTemplate](./repo_config_computer.go#L61) `func ApplyBranchesTemplate(repoConfig *GhRepoConfig, templates *TemplatesConfig) error` @@ -340,7 +340,7 @@ var ( `type GhBranchConfig struct { ... }` -#### func [ApplyBranchTemplate](./repo_config_computer.go#L154) +#### func [ApplyBranchTemplate](./repo_config_computer.go#L152) `func ApplyBranchTemplate(branchConfig *GhBranchConfig, templates *TemplatesConfig) (*GhBranchConfig, error)` @@ -398,7 +398,7 @@ No schema validation will be performed, use loadBranchTemplateFromFile instead ! `type GhBranchProtectionConfig struct { ... }` -#### func [ApplyBranchProtectionTemplate](./repo_config_computer.go#L138) +#### func [ApplyBranchProtectionTemplate](./repo_config_computer.go#L136) `func ApplyBranchProtectionTemplate( branchProtectionConfig *GhBranchProtectionConfig, @@ -454,11 +454,11 @@ No schema validation will be performed, use loadBranchProtectionTemplateFromFile `type GhRepoConfig struct { ... }` -#### func [ApplyRepositoryTemplate](./repo_config_computer.go#L50) +#### func [ApplyRepositoryTemplate](./repo_config_computer.go#L48) `func ApplyRepositoryTemplate(repoConfig *GhRepoConfig, templates *TemplatesConfig) (*GhRepoConfig, error)` -#### func [ComputeRepoConfig](./repo_config_computer.go#L11) +#### func [ComputeRepoConfig](./repo_config_computer.go#L9) `func ComputeRepoConfig(base *GhRepoConfig, templates *TemplatesConfig) (*GhRepoConfig, error)` diff --git a/core/config_computer_test.go b/core/config_computer_test.go index 66066a3..0e89798 100644 --- a/core/config_computer_test.go +++ b/core/config_computer_test.go @@ -1,7 +1,7 @@ package core_test import ( - "fmt" + "errors" "testing" differ "github.com/andreyvit/diff" @@ -69,7 +69,7 @@ func TestComputeConfig(t *testing.T) { }, }, nil, - fmt.Errorf("error during computation:\n\t - repo #0: repository name is mandatory\n\t - repo #1: repository name is mandatory"), + errors.New("error during computation:\n\t - repo #0: repository name is mandatory\n\t - repo #1: repository name is mandatory"), }, "Underlying computation error": { &core.Config{ @@ -82,18 +82,16 @@ func TestComputeConfig(t *testing.T) { }, }, nil, - fmt.Errorf("error during computation:\n\t - repository a_name: repository template not found as none available"), + errors.New("error during computation:\n\t - repository a_name: repository template not found as none available"), }, } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel tests - tc := tc // Reinit var for parallel tests - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual, err := core.ComputeConfig(tc.value) if tc.error != nil { if err == nil { diff --git a/core/config_schema_test.go b/core/config_schema_test.go index c0286fa..bb76fc2 100644 --- a/core/config_schema_test.go +++ b/core/config_schema_test.go @@ -35,13 +35,11 @@ func TestConfig_GetRepo(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel tests - tc := tc // Reinit var for parallel tests - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual := tc.value.GetRepo(tc.name) if diff := cmp.Diff(tc.expected, actual); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) @@ -79,13 +77,11 @@ func TestTemplatesConfig_GetRepo(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel tests - tc := tc // Reinit var for parallel tests - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual := tc.value.GetRepo(tc.name) if diff := cmp.Diff(tc.expected, actual); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) @@ -123,13 +119,11 @@ func TestTemplatesConfig_GetBranch(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel tests - tc := tc // Reinit var for parallel tests - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual := tc.value.GetBranch(tc.name) if diff := cmp.Diff(tc.expected, actual); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) @@ -167,13 +161,11 @@ func TestTemplatesConfig_GetBranchProtection(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel tests - tc := tc // Reinit var for parallel tests - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual := tc.value.GetBranchProtection(tc.name) if diff := cmp.Diff(tc.expected, actual); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) diff --git a/core/gh2tf_repo_mapper_test.go b/core/gh2tf_repo_mapper_test.go index 5bb77b7..be6252b 100644 --- a/core/gh2tf_repo_mapper_test.go +++ b/core/gh2tf_repo_mapper_test.go @@ -64,13 +64,11 @@ func TestMapToRepositoryRes(t *testing.T) { diffOpts := []cmp.Option{cmp.AllowUnexported(tfsig.ValueGenerator{}), cmp.AllowUnexported(tfsig.IdentTokenMatcher{})} for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual := core.MapToRepositoryRes(tc.value, valGen, tfRepoId) if diff := cmp.Diff(tc.expected, actual, diffOpts...); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) @@ -176,13 +174,11 @@ func TestMapToBranchRes(t *testing.T) { diffOpts := []cmp.Option{cmp.AllowUnexported(tfsig.ValueGenerator{}), cmp.AllowUnexported(tfsig.IdentTokenMatcher{})} for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual := core.MapToBranchRes(tc.name, tc.value, valGen, tc.repo, tfId, tc.links...) if diff := cmp.Diff(tc.expected, actual, diffOpts...); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) @@ -270,13 +266,11 @@ func TestMapToDefaultBranchRes(t *testing.T) { diffOpts := []cmp.Option{cmp.AllowUnexported(tfsig.ValueGenerator{}), cmp.AllowUnexported(tfsig.IdentTokenMatcher{})} for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual := core.MapToDefaultBranchRes(tc.value, valGen, tc.repo, tfId, tc.links...) if diff := cmp.Diff(tc.expected, actual, diffOpts...); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) @@ -380,13 +374,11 @@ func TestMapDefaultBranchToBranchProtectionRes(t *testing.T) { diffOpts := []cmp.Option{cmp.AllowUnexported(tfsig.ValueGenerator{}), cmp.AllowUnexported(tfsig.IdentTokenMatcher{})} for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual := core.MapDefaultBranchToBranchProtectionRes(tc.value, valGen, tc.repo, tfId, tc.links...) if diff := cmp.Diff(tc.expected, actual, diffOpts...); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) @@ -475,15 +467,14 @@ func TestMapBranchToBranchProtectionRes(t *testing.T) { diffOpts := []cmp.Option{cmp.AllowUnexported(tfsig.ValueGenerator{}), cmp.AllowUnexported(tfsig.IdentTokenMatcher{})} for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + branchNameTmp := tc.name actual := core.MapBranchToBranchProtectionRes(&branchNameTmp, tc.value, valGen, tc.repo, tfId, tc.links...) + if diff := cmp.Diff(tc.expected, actual, diffOpts...); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -575,13 +566,11 @@ func TestMapToBranchProtectionRes(t *testing.T) { diffOpts := []cmp.Option{cmp.AllowUnexported(tfsig.ValueGenerator{}), cmp.AllowUnexported(tfsig.IdentTokenMatcher{})} for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual := core.MapToBranchProtectionRes(tc.value, valGen, tc.repo, tfId, tc.links...) if diff := cmp.Diff(tc.expected, actual, diffOpts...); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) diff --git a/core/main_test.go b/core/main_test.go index 402ab36..fa0e4b9 100644 --- a/core/main_test.go +++ b/core/main_test.go @@ -2,6 +2,7 @@ package core_test import ( "fmt" + "strconv" "testing" differ "github.com/andreyvit/diff" @@ -67,24 +68,24 @@ func GetFullConfig(id int) *core.GhRepoConfig { defaultBranchName := fmt.Sprintf("master%d", id) // Repo->DefaultBranch->Protection defaultBranchBranchProtectionTemplate := fmt.Sprintf("default-branch-branch-protection-template%d", id) - defaultBranchBranchProtectionEnforceAdmins := fmt.Sprintf("%s", bool1) - defaultBranchBranchProtectionAllowsDeletions := fmt.Sprintf("%s", bool1) - defaultBranchBranchProtectionRequiredLinearHistory := fmt.Sprintf("%s", bool1) - defaultBranchBranchProtectionRequireSignedCommits := fmt.Sprintf("%s", bool1) + defaultBranchBranchProtectionEnforceAdmins := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + defaultBranchBranchProtectionAllowsDeletions := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + defaultBranchBranchProtectionRequiredLinearHistory := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + defaultBranchBranchProtectionRequireSignedCommits := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p // Repo->DefaultBranch->Protection->Pushes - defaultBranchBranchProtectionAllowsForcePushes := fmt.Sprintf("%s", bool1) + defaultBranchBranchProtectionAllowsForcePushes := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p defaultBranchBranchProtectionPushRestriction := fmt.Sprintf("default-branch-pushRestriction%d", id) // Repo->DefaultBranch->Protection->StatusChecks - defaultBranchBranchProtectionStrict := fmt.Sprintf("%s", bool1) + defaultBranchBranchProtectionStrict := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p defaultBranchBranchProtectionContext := fmt.Sprintf("default-branch-context%d", id) // Repo->DefaultBranch->Protection->PullRequestReviews defaultBranchBranchProtectionBypasser := fmt.Sprintf("default-branch-bypasser%d", id) - defaultBranchBranchProtectionRequiredApprovingReviewCount := fmt.Sprintf("%d", approvalCount%7) - defaultBranchBranchProtectionRequireCodeOwnerReviews := fmt.Sprintf("%s", bool1) - defaultBranchBranchProtectionResolvedConversations := fmt.Sprintf("%s", bool1) + defaultBranchBranchProtectionRequiredApprovingReviewCount := strconv.Itoa(approvalCount % 7) + defaultBranchBranchProtectionRequireCodeOwnerReviews := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + defaultBranchBranchProtectionResolvedConversations := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p // Repo->DefaultBranch->Protection->PullRequestReviews->Dismissals - defaultBranchBranchProtectionDismissStaleReviews := fmt.Sprintf("%s", bool1) - defaultBranchBranchProtectionRestrictDismissal := fmt.Sprintf("%s", bool1) + defaultBranchBranchProtectionDismissStaleReviews := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + defaultBranchBranchProtectionRestrictDismissal := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p defaultBranchBranchProtectionDismissalRestriction := fmt.Sprintf("default-branch-dismissalRestriction%d", id) // Repo->Branches // Repo->Branches[0] @@ -94,24 +95,24 @@ func GetFullConfig(id int) *core.GhRepoConfig { // branch1SourceSha := fmt.Sprintf("branch%d-source-sha%d", id, id) // Repo->Branches[0]->Protection branch1BranchProtectionTemplate := fmt.Sprintf("branch%d-branch-protection-template%d", id, id) - branch1BranchProtectionEnforceAdmins := fmt.Sprintf("%s", bool2) - branch1BranchProtectionAllowsDeletions := fmt.Sprintf("%s", bool2) - branch1BranchProtectionRequiredLinearHistory := fmt.Sprintf("%s", bool2) - branch1BranchProtectionRequireSignedCommits := fmt.Sprintf("%s", bool2) + branch1BranchProtectionEnforceAdmins := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p + branch1BranchProtectionAllowsDeletions := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p + branch1BranchProtectionRequiredLinearHistory := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p + branch1BranchProtectionRequireSignedCommits := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p // Repo->Branches[0]->Protection->Pushes - branch1BranchProtectionAllowsForcePushes := fmt.Sprintf("%s", bool2) + branch1BranchProtectionAllowsForcePushes := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p branch1BranchProtectionPushRestriction := fmt.Sprintf("branch%d-pushRestriction%d", id, id) // Repo->Branches[0]->Protection->StatusChecks - branch1BranchProtectionStrict := fmt.Sprintf("%s", bool2) + branch1BranchProtectionStrict := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p branch1BranchProtectionContext := fmt.Sprintf("branch%d-context%d", id, id) // Repo->Branches[0]->Protection->PullRequestReviews branch1BranchProtectionBypasser := fmt.Sprintf("branch%d-bypasser%d", id, id) - branch1BranchProtectionRequiredApprovingReviewCount := fmt.Sprintf("%d", (approvalCount+1)%7) - branch1BranchProtectionRequireCodeOwnerReviews := fmt.Sprintf("%s", bool2) - branch1BranchProtectionResolvedConversations := fmt.Sprintf("%s", bool2) + branch1BranchProtectionRequiredApprovingReviewCount := strconv.Itoa((approvalCount + 1) % 7) + branch1BranchProtectionRequireCodeOwnerReviews := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p + branch1BranchProtectionResolvedConversations := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p // Repo->Branches[0]->Protection->PullRequestReviews->Dismissals - branch1BranchProtectionDismissStaleReviews := fmt.Sprintf("%s", bool2) - branch1BranchProtectionRestrictDismissal := fmt.Sprintf("%s", bool2) + branch1BranchProtectionDismissStaleReviews := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p + branch1BranchProtectionRestrictDismissal := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p branch1BranchProtectionDismissalRestriction := fmt.Sprintf("branch%d-dismissalRestriction%d", id, id) // Repo->Branches[1] branch2Name := fmt.Sprintf("feature/branch%d", 1+id) @@ -120,56 +121,56 @@ func GetFullConfig(id int) *core.GhRepoConfig { branch2SourceSha := fmt.Sprintf("branch%d-source-sha%d", 1+id, id) // Repo->Branches[1]->Protection branch2BranchProtectionTemplate := fmt.Sprintf("branch%d-branch-protection-template%d", 1+id, id) - branch2BranchProtectionEnforceAdmins := fmt.Sprintf("%s", bool1) - branch2BranchProtectionAllowsDeletions := fmt.Sprintf("%s", bool1) - branch2BranchProtectionRequiredLinearHistory := fmt.Sprintf("%s", bool1) - branch2BranchProtectionRequireSignedCommits := fmt.Sprintf("%s", bool1) + branch2BranchProtectionEnforceAdmins := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + branch2BranchProtectionAllowsDeletions := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + branch2BranchProtectionRequiredLinearHistory := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + branch2BranchProtectionRequireSignedCommits := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p // Repo->Branches[1]->Protection->Pushes - branch2BranchProtectionAllowsForcePushes := fmt.Sprintf("%s", bool1) + branch2BranchProtectionAllowsForcePushes := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p branch2BranchProtectionPushRestriction := fmt.Sprintf("branch%d-pushRestriction%d", 1+id, id) // Repo->Branches[1]->Protection->StatusChecks - branch2BranchProtectionStrict := fmt.Sprintf("%s", bool1) + branch2BranchProtectionStrict := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p branch2BranchProtectionContext := fmt.Sprintf("branch%d-context%d", 1+id, id) // Repo->Branches[1]->Protection->PullRequestReviews branch2BranchProtectionBypasser := fmt.Sprintf("branch%d-bypasser%d", 1+id, id) - branch2BranchProtectionRequiredApprovingReviewCount := fmt.Sprintf("%d", (approvalCount+2)%7) - branch2BranchProtectionRequireCodeOwnerReviews := fmt.Sprintf("%s", bool1) - branch2BranchProtectionResolvedConversations := fmt.Sprintf("%s", bool1) + branch2BranchProtectionRequiredApprovingReviewCount := strconv.Itoa((approvalCount + 2) % 7) + branch2BranchProtectionRequireCodeOwnerReviews := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + branch2BranchProtectionResolvedConversations := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p // Repo->Branches[1]->Protection->PullRequestReviews->Dismissals - branch2BranchProtectionDismissStaleReviews := fmt.Sprintf("%s", bool1) - branch2BranchProtectionRestrictDismissal := fmt.Sprintf("%s", bool1) + branch2BranchProtectionDismissStaleReviews := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + branch2BranchProtectionRestrictDismissal := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p branch2BranchProtectionDismissalRestriction := fmt.Sprintf("branch%d-dismissalRestriction%d", 1+id, id) // Repo->BranchProtections // Repo->BranchProtections[0] branchProtectionTemplate := fmt.Sprintf("branch-protection-template%d", id) pattern := fmt.Sprintf("a-pattern%d", id) - forbid := fmt.Sprintf("%s", bool2) - branchProtectionEnforceAdmins := fmt.Sprintf("%s", bool2) - branchProtectionAllowsDeletions4 := fmt.Sprintf("%s", bool2) - branchProtectionRequiredLinearHistory := fmt.Sprintf("%s", bool2) - branchProtectionRequireSignedCommits := fmt.Sprintf("%s", bool2) + forbid := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p + branchProtectionEnforceAdmins := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p + branchProtectionAllowsDeletions4 := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p + branchProtectionRequiredLinearHistory := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p + branchProtectionRequireSignedCommits := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p // Repo->BranchProtections[0]->Pushes - branchProtectionAllowsForcePushes := fmt.Sprintf("%s", bool2) + branchProtectionAllowsForcePushes := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p branchProtectionPushRestriction := fmt.Sprintf("branch-protection-pushRestriction%d", id) // Repo->BranchProtections[0]->StatusChecks - branchProtectionStrict := fmt.Sprintf("%s", bool2) + branchProtectionStrict := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p branchProtectionContext := fmt.Sprintf("branch-protection-context%d", id) branchProtectionBypasser := fmt.Sprintf("branch-protection-bypasser%d", id) // Repo->BranchProtections[0]->PullRequestReviews - branchProtectionRequiredApprovingReviewCount := fmt.Sprintf("%d", (approvalCount+3)%7) - branchProtectionRequireCodeOwnerReviews := fmt.Sprintf("%s", bool2) - branchProtectionResolvedConversations := fmt.Sprintf("%s", bool2) + branchProtectionRequiredApprovingReviewCount := strconv.Itoa((approvalCount + 3) % 7) + branchProtectionRequireCodeOwnerReviews := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p + branchProtectionResolvedConversations := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p // Repo->BranchProtections[0]->PullRequestReviews->Dismissals - branchProtectionDismissStaleReviews := fmt.Sprintf("%s", bool2) - branchProtectionRestrictDismissal := fmt.Sprintf("%s", bool2) + branchProtectionDismissStaleReviews := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p + branchProtectionRestrictDismissal := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p branchProtectionDismissalRestriction := fmt.Sprintf("branch-protection-dismissalRestriction%d", id) // Repo->PullRequests // Repo->PullRequests->MergeStrategy - allowMergeCommit := fmt.Sprintf("%s", bool1) - allowRebaseMerge := fmt.Sprintf("%s", bool1) - allowSquashMerge := fmt.Sprintf("%s", bool1) - allowAutoMerge := fmt.Sprintf("%s", bool1) + allowMergeCommit := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + allowRebaseMerge := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + allowSquashMerge := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + allowAutoMerge := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p // Repo->PullRequests->MergeCommit mergeCommitTitle := fmt.Sprintf("aMergeCommitTitle%d", id) mergeCommitMessage := fmt.Sprintf("aMergeCommitMessage%d", id) @@ -177,22 +178,22 @@ func GetFullConfig(id int) *core.GhRepoConfig { squashMergeCommitTitle := fmt.Sprintf("aSquashMergeCommitTitle%d", id) squashMergeCommitMessage := fmt.Sprintf("aSquashMergeCommitMessage%d", id) // Repo->PullRequests->Branch - suggestUpdate := fmt.Sprintf("%s", bool1) - deleteBranchOnMerge := fmt.Sprintf("%s", bool1) + suggestUpdate := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + deleteBranchOnMerge := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p // Repo->Security - vulnerabilityAlerts := fmt.Sprintf("%s", bool2) + vulnerabilityAlerts := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p // Repo->Misc - archived := fmt.Sprintf("%s", bool1) + archived := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p topicCount := 2 topic1 := fmt.Sprintf("topic%d", (topicCount * id)) topic2 := fmt.Sprintf("topic%d", (topicCount*id)+1) - autoInit := fmt.Sprintf("%s", bool1) - isTemplate := fmt.Sprintf("%s", bool1) + autoInit := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + isTemplate := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p homepageUrl := fmt.Sprintf("http://localhost/%d", id) - hasDownloads := fmt.Sprintf("%s", bool1) - hasProjects := fmt.Sprintf("%s", bool1) - hasWiki := fmt.Sprintf("%s", bool1) - hasIssues := fmt.Sprintf("%s", bool1) + hasDownloads := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + hasProjects := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + hasWiki := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p + hasIssues := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p // Repo->Misc->Pages domain := fmt.Sprintf("my.domain%d", id) branch := fmt.Sprintf("branch%d", id) @@ -201,13 +202,13 @@ func GetFullConfig(id int) *core.GhRepoConfig { owner := fmt.Sprintf("owner%d", id) repository := fmt.Sprintf("repository%d", id) templateSource := owner + "/" + repository - fullClone := fmt.Sprintf("%s", bool1) + fullClone := fmt.Sprintf("%s", bool1) //nolint:perfsprint // Because :p // Repo->Misc->FileTemplates gitignore := fmt.Sprintf("gitignore-tpl-name%d", id) license := fmt.Sprintf("license-tpl-name%d", id) // Repo->Terraform - archiveOnDestroy := fmt.Sprintf("%s", bool2) - ignoreVulnerabilityAlertsDuringRead := fmt.Sprintf("%s", bool2) + archiveOnDestroy := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p + ignoreVulnerabilityAlertsDuringRead := fmt.Sprintf("%s", bool2) //nolint:perfsprint // Because :p return &core.GhRepoConfig{ &name, diff --git a/core/repo_config_computer.go b/core/repo_config_computer.go index 7a8e0b2..56ee231 100644 --- a/core/repo_config_computer.go +++ b/core/repo_config_computer.go @@ -1,8 +1,6 @@ package core import ( - "fmt" - "github.com/rs/zerolog/log" ) @@ -24,25 +22,25 @@ func ComputeRepoConfig(base *GhRepoConfig, templates *TemplatesConfig) (*GhRepoC config.Merge(base) - ConfigTrace(fmt.Sprintf("Config after merge: %s", *base.Name), config) + ConfigTrace("Config after merge: "+(*base.Name), config) if config, err = ApplyRepositoryTemplate(config, templates); err != nil { return nil, err } else { - ConfigTrace(fmt.Sprintf("Config after repo template: %s", *base.Name), config) + ConfigTrace("Config after repo template: "+(*base.Name), config) } if err = ApplyBranchesTemplate(config, templates); err != nil { return nil, err } else { - ConfigTrace(fmt.Sprintf("Config after branch template: %s", *base.Name), config) + ConfigTrace("Config after branch template: "+(*base.Name), config) } if err = ApplyBranchProtectionsTemplate(config, templates); err != nil { return nil, err } - ConfigTrace(fmt.Sprintf("Final config: %s", *base.Name), config) + ConfigTrace("Final config: "+(*base.Name), config) return config, nil } diff --git a/core/repo_config_computer_test.go b/core/repo_config_computer_test.go index 2441b1f..33cf316 100644 --- a/core/repo_config_computer_test.go +++ b/core/repo_config_computer_test.go @@ -1,7 +1,7 @@ package core_test import ( - "fmt" + "errors" "testing" differ "github.com/andreyvit/diff" @@ -307,13 +307,11 @@ func TestComputeRepoConfig(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual, err := core.ComputeRepoConfig(tc.value, tc.templates) if tc.error != nil { if err == nil { @@ -432,13 +430,11 @@ func TestComputeRepoConfig_edgeCases(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual, err := core.ComputeRepoConfig(tc.value, tc.templates) if tc.error != nil { if err == nil { @@ -472,7 +468,7 @@ func TestComputeRepoConfig_noTemplateAvailable(t *testing.T) { &core.GhRepoConfig{Name: &aName, ConfigTemplates: &[]string{aTemplate}}, nil, nil, - fmt.Errorf("repository template not found as none available"), + errors.New("repository template not found as none available"), }, "default branch - branch": { &core.GhRepoConfig{ @@ -486,7 +482,7 @@ func TestComputeRepoConfig_noTemplateAvailable(t *testing.T) { }, nil, nil, - fmt.Errorf("default branch: branch template not found as none available"), + errors.New("default branch: branch template not found as none available"), }, "default branch - branch protection": { &core.GhRepoConfig{ @@ -502,7 +498,7 @@ func TestComputeRepoConfig_noTemplateAvailable(t *testing.T) { }, nil, nil, - fmt.Errorf("default branch: branch protection template not found as none available"), + errors.New("default branch: branch protection template not found as none available"), }, "branch": { &core.GhRepoConfig{ @@ -517,7 +513,7 @@ func TestComputeRepoConfig_noTemplateAvailable(t *testing.T) { }, nil, nil, - fmt.Errorf("branch branch-name: branch template not found as none available"), + errors.New("branch branch-name: branch template not found as none available"), }, "branch - branch protection": { &core.GhRepoConfig{ @@ -534,7 +530,7 @@ func TestComputeRepoConfig_noTemplateAvailable(t *testing.T) { }, nil, nil, - fmt.Errorf("branch branch-name: branch protection template not found as none available"), + errors.New("branch branch-name: branch protection template not found as none available"), }, "branch protection": { &core.GhRepoConfig{ @@ -550,18 +546,16 @@ func TestComputeRepoConfig_noTemplateAvailable(t *testing.T) { }, nil, nil, - fmt.Errorf("branch protection #0: branch protection template not found as none available"), + errors.New("branch protection #0: branch protection template not found as none available"), }, } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual, err := core.ComputeRepoConfig(tc.value, tc.templates) if tc.error != nil { if err == nil { @@ -596,7 +590,7 @@ func TestComputeRepoConfig_unknownTemplate(t *testing.T) { &core.GhRepoConfig{Name: &aName, ConfigTemplates: &[]string{aTemplate}}, emptyTplConfig, nil, - fmt.Errorf("\"a-template\" repository template not found"), + errors.New("\"a-template\" repository template not found"), }, "default branch - branch": { &core.GhRepoConfig{ @@ -610,7 +604,7 @@ func TestComputeRepoConfig_unknownTemplate(t *testing.T) { }, emptyTplConfig, nil, - fmt.Errorf("default branch: \"a-template\" branch template not found"), + errors.New("default branch: \"a-template\" branch template not found"), }, "default branch - branch protection": { &core.GhRepoConfig{ @@ -626,7 +620,7 @@ func TestComputeRepoConfig_unknownTemplate(t *testing.T) { }, emptyTplConfig, nil, - fmt.Errorf("default branch: \"a-template\" branch protection template not found"), + errors.New("default branch: \"a-template\" branch protection template not found"), }, "branch": { &core.GhRepoConfig{ @@ -641,7 +635,7 @@ func TestComputeRepoConfig_unknownTemplate(t *testing.T) { }, emptyTplConfig, nil, - fmt.Errorf("branch branch-name: \"a-template\" branch template not found"), + errors.New("branch branch-name: \"a-template\" branch template not found"), }, "branch - branch protection": { &core.GhRepoConfig{ @@ -658,7 +652,7 @@ func TestComputeRepoConfig_unknownTemplate(t *testing.T) { }, emptyTplConfig, nil, - fmt.Errorf("branch branch-name: \"a-template\" branch protection template not found"), + errors.New("branch branch-name: \"a-template\" branch protection template not found"), }, "branch protection": { &core.GhRepoConfig{ @@ -674,18 +668,16 @@ func TestComputeRepoConfig_unknownTemplate(t *testing.T) { }, emptyTplConfig, nil, - fmt.Errorf("branch protection #0: \"a-template\" branch protection template not found"), + errors.New("branch protection #0: \"a-template\" branch protection template not found"), }, } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual, err := core.ComputeRepoConfig(tc.value, tc.templates) if tc.error != nil { if err == nil { @@ -722,18 +714,16 @@ func TestComputeRepoConfig_validationError(t *testing.T) { &core.GhRepoConfig{ConfigTemplates: nil}, nil, nil, - fmt.Errorf("repository name is mandatory"), + errors.New("repository name is mandatory"), }, } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual, err := core.ComputeRepoConfig(tc.value, tc.templates) if tc.error != nil { if err == nil { @@ -779,13 +769,13 @@ func TestApplyRepositoryTemplate(t *testing.T) { &core.GhRepoConfig{ConfigTemplates: &[]string{aTemplate}}, nil, nil, - fmt.Errorf("repository template not found as none available"), + errors.New("repository template not found as none available"), }, "unknown template": { &core.GhRepoConfig{ConfigTemplates: &[]string{aTemplate}, Description: &description}, emptyTplConfig, nil, - fmt.Errorf("\"a-template\" repository template not found"), + errors.New("\"a-template\" repository template not found"), }, "no template provided": { &core.GhRepoConfig{Description: &description}, @@ -802,13 +792,11 @@ func TestApplyRepositoryTemplate(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual, err := core.ApplyRepositoryTemplate(tc.value, tc.templates) if tc.error != nil { if err == nil { @@ -858,7 +846,7 @@ func TestApplyBranchProtectionTemplate(t *testing.T) { }, nil, nil, - fmt.Errorf("branch protection template not found as none available"), + errors.New("branch protection template not found as none available"), }, "unknown template": { &core.GhBranchProtectionConfig{ @@ -868,7 +856,7 @@ func TestApplyBranchProtectionTemplate(t *testing.T) { }, emptyTplConfig, nil, - fmt.Errorf("\"a-template\" branch protection template not found"), + errors.New("\"a-template\" branch protection template not found"), }, "no template provided": { &core.GhBranchProtectionConfig{ @@ -903,13 +891,11 @@ func TestApplyBranchProtectionTemplate(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual, err := core.ApplyBranchProtectionTemplate(tc.value, tc.templates) if tc.error != nil { if err == nil { @@ -963,7 +949,7 @@ func TestApplyBranchProtectionsTemplate(t *testing.T) { }, nil, nil, - fmt.Errorf("branch protection #0: branch protection template not found as none available"), + errors.New("branch protection #0: branch protection template not found as none available"), }, "unknown template": { &core.GhRepoConfig{ @@ -977,7 +963,7 @@ func TestApplyBranchProtectionsTemplate(t *testing.T) { }, emptyTplConfig, nil, - fmt.Errorf("branch protection #0: \"a-template\" branch protection template not found"), + errors.New("branch protection #0: \"a-template\" branch protection template not found"), }, "no template provided": { &core.GhRepoConfig{ @@ -1028,13 +1014,11 @@ func TestApplyBranchProtectionsTemplate(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + err := core.ApplyBranchProtectionsTemplate(tc.value, tc.templates) if tc.error != nil { if err == nil { @@ -1086,7 +1070,7 @@ func TestApplyBranchTemplate(t *testing.T) { }, nil, nil, - fmt.Errorf("branch template not found as none available"), + errors.New("branch template not found as none available"), }, "unknown template": { &core.GhBranchConfig{ @@ -1098,7 +1082,7 @@ func TestApplyBranchTemplate(t *testing.T) { }, emptyTplConfig, nil, - fmt.Errorf("\"a-template\" branch template not found"), + errors.New("\"a-template\" branch template not found"), }, "no template provided": { &core.GhBranchConfig{SourceBranch: &sourceBranch}, @@ -1124,13 +1108,11 @@ func TestApplyBranchTemplate(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + actual, err := core.ApplyBranchTemplate(tc.value, tc.templates) if tc.error != nil { if err == nil { @@ -1188,7 +1170,7 @@ func TestApplyBranchesTemplate(t *testing.T) { }, nil, nil, - fmt.Errorf("branch a-branch-name: branch template not found as none available"), + errors.New("branch a-branch-name: branch template not found as none available"), }, "unknown template": { &core.GhRepoConfig{ @@ -1204,7 +1186,7 @@ func TestApplyBranchesTemplate(t *testing.T) { }, emptyTplConfig, nil, - fmt.Errorf("branch a-branch-name: \"a-template\" branch template not found"), + errors.New("branch a-branch-name: \"a-template\" branch template not found"), }, "no template provided": { &core.GhRepoConfig{ @@ -1246,13 +1228,11 @@ func TestApplyBranchesTemplate(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + err := core.ApplyBranchesTemplate(tc.value, tc.templates) if tc.error != nil { if err == nil { diff --git a/core/repo_schema_test.go b/core/repo_schema_test.go index 172e3ad..1ac49de 100644 --- a/core/repo_schema_test.go +++ b/core/repo_schema_test.go @@ -199,14 +199,12 @@ func TestGhRepoConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -261,14 +259,12 @@ func TestGhDefaultBranchConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -287,18 +283,18 @@ func TestGhBranchesConfig_Merge(t *testing.T) { initialFromStringValue := "initial_from_value" // Init 'to' variables and create 'to' - toSourceBranch := fmt.Sprintf("%s", initialToStringValue) - toSourceSha := fmt.Sprintf("%s", initialToStringValue) - toEnforceAdmins := fmt.Sprintf("%s", initialToStringValue) - toAllowsDeletions := fmt.Sprintf("%s", initialToStringValue) - toAllowsForcePushes := fmt.Sprintf("%s", initialToStringValue) - toRequiredLinearHistory := fmt.Sprintf("%s", initialToStringValue) - toRequireSignedCommits := fmt.Sprintf("%s", initialToStringValue) - toStrict := fmt.Sprintf("%s", initialToStringValue) - toDismissStaleReviews := fmt.Sprintf("%s", initialToStringValue) - toRequireCodeOwnerReviews := fmt.Sprintf("%s", initialToStringValue) - toResolvedConversations := fmt.Sprintf("%s", initialToStringValue) - toRequiredApprovingReviewCount := fmt.Sprintf("%s", initialToStringValue) + toSourceBranch := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toSourceSha := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toEnforceAdmins := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toAllowsDeletions := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toAllowsForcePushes := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toRequiredLinearHistory := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toRequireSignedCommits := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toStrict := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toDismissStaleReviews := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toRequireCodeOwnerReviews := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toResolvedConversations := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toRequiredApprovingReviewCount := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p // Do nothing with PushRestrictions, see TestGhRepoConfig_Merge2 // toPushRestrictions := append([]string{}, initialToSliceValue...) // Do nothing with Contexts, see TestGhRepoConfig_Merge2 @@ -342,18 +338,18 @@ func TestGhBranchesConfig_Merge(t *testing.T) { } // Init 'from' variables and create 'from' - fromSourceBranch := fmt.Sprintf("%s", initialFromStringValue) - fromSourceSha := fmt.Sprintf("%s", initialFromStringValue) - fromEnforceAdmins := fmt.Sprintf("%s", initialFromStringValue) - fromAllowsDeletions := fmt.Sprintf("%s", initialFromStringValue) - fromAllowsForcePushes := fmt.Sprintf("%s", initialFromStringValue) - fromRequiredLinearHistory := fmt.Sprintf("%s", initialFromStringValue) - fromRequireSignedCommits := fmt.Sprintf("%s", initialFromStringValue) - fromStrict := fmt.Sprintf("%s", initialFromStringValue) - fromDismissStaleReviews := fmt.Sprintf("%s", initialFromStringValue) - fromRequireCodeOwnerReviews := fmt.Sprintf("%s", initialFromStringValue) - fromResolvedConversations := fmt.Sprintf("%s", initialToStringValue) - fromRequiredApprovingReviewCount := fmt.Sprintf("%s", initialFromStringValue) + fromSourceBranch := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromSourceSha := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromEnforceAdmins := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromAllowsDeletions := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromAllowsForcePushes := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromRequiredLinearHistory := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromRequireSignedCommits := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromStrict := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromDismissStaleReviews := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromRequireCodeOwnerReviews := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromResolvedConversations := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + fromRequiredApprovingReviewCount := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p // Do nothing with PushRestrictions, see TestGhRepoConfig_Merge2 // fromPushRestrictions := append([]string{}, initialFromSliceValue...) // Do nothing with Contexts, see TestGhRepoConfig_Merge2 @@ -439,14 +435,12 @@ func TestGhBranchesConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -502,14 +496,12 @@ func TestGhBranchConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -568,14 +560,12 @@ func TestBaseGhBranchConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -596,19 +586,19 @@ func TestGhBranchProtectionsConfig_Merge(t *testing.T) { initialFromSliceValue := []string{"initial_from_slice_value1", "initial_from_slice_value2"} // Init 'to' variables and create 'to' - toPattern := fmt.Sprintf("%s", initialToStringValue) - toForbid := fmt.Sprintf("%s", initialToStringValue) - toEnforceAdmins := fmt.Sprintf("%s", initialToStringValue) - toAllowsDeletions := fmt.Sprintf("%s", initialToStringValue) - toAllowsForcePushes := fmt.Sprintf("%s", initialToStringValue) - toRequiredLinearHistory := fmt.Sprintf("%s", initialToStringValue) - toRequireSignedCommits := fmt.Sprintf("%s", initialToStringValue) - toStrict := fmt.Sprintf("%s", initialToStringValue) - toDismissStaleReviews := fmt.Sprintf("%s", initialToStringValue) - toRestrict := fmt.Sprintf("%s", initialToStringValue) - toRequireCodeOwnerReviews := fmt.Sprintf("%s", initialToStringValue) - toResolvedConversations := fmt.Sprintf("%s", initialToStringValue) - toRequiredApprovingReviewCount := fmt.Sprintf("%s", initialToStringValue) + toPattern := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toForbid := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toEnforceAdmins := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toAllowsDeletions := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toAllowsForcePushes := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toRequiredLinearHistory := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toRequireSignedCommits := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toStrict := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toDismissStaleReviews := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toRestrict := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toRequireCodeOwnerReviews := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toResolvedConversations := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p + toRequiredApprovingReviewCount := fmt.Sprintf("%s", initialToStringValue) //nolint:perfsprint // Because :p toBypasserList := append([]string{}, initialToSliceValue...) @@ -654,19 +644,19 @@ func TestGhBranchProtectionsConfig_Merge(t *testing.T) { } // Init 'from' variables and create 'from' - fromPattern := fmt.Sprintf("%s", initialFromStringValue) - fromForbid := fmt.Sprintf("%s", initialFromStringValue) - fromEnforceAdmins := fmt.Sprintf("%s", initialFromStringValue) - fromAllowsDeletions := fmt.Sprintf("%s", initialFromStringValue) - fromAllowsForcePushes := fmt.Sprintf("%s", initialFromStringValue) - fromRequiredLinearHistory := fmt.Sprintf("%s", initialFromStringValue) - fromRequireSignedCommits := fmt.Sprintf("%s", initialFromStringValue) - fromStrict := fmt.Sprintf("%s", initialFromStringValue) - fromDismissStaleReviews := fmt.Sprintf("%s", initialFromStringValue) - fromRestrict := fmt.Sprintf("%s", initialFromStringValue) - fromRequireCodeOwnerReviews := fmt.Sprintf("%s", initialFromStringValue) - fromResolvedConversations := fmt.Sprintf("%s", initialFromStringValue) - fromRequiredApprovingReviewCount := fmt.Sprintf("%s", initialFromStringValue) + fromPattern := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromForbid := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromEnforceAdmins := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromAllowsDeletions := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromAllowsForcePushes := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromRequiredLinearHistory := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromRequireSignedCommits := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromStrict := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromDismissStaleReviews := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromRestrict := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromRequireCodeOwnerReviews := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromResolvedConversations := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + fromRequiredApprovingReviewCount := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p fromPushRestrictions := append([]string{}, initialFromSliceValue...) @@ -765,14 +755,12 @@ func TestGhBranchProtectionsConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -830,14 +818,12 @@ func TestGhBranchProtectionConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -915,14 +901,12 @@ func TestBaseGhBranchProtectionConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -976,14 +960,12 @@ func TestGhBranchProtectPushesConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -1037,14 +1019,12 @@ func TestGhBranchProtectStatusChecksConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -1107,14 +1087,12 @@ func TestGhBranchProtectPRReviewConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -1169,14 +1147,12 @@ func TestGhBranchProtectPRReviewDismissalsConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -1253,14 +1229,12 @@ func TestGhRepoPullRequestConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -1307,14 +1281,12 @@ func TestGhRepoPRMergeStrategyConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -1359,14 +1331,12 @@ func TestGhRepoPRCommitConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -1411,14 +1381,12 @@ func TestGhRepoPRBranchConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -1462,14 +1430,12 @@ func TestGhRepoSecurityConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -1550,14 +1516,12 @@ func TestGhRepoMiscellaneousConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -1602,14 +1566,12 @@ func TestGhRepoTemplateConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -1654,14 +1616,12 @@ func TestGhRepoFileTemplatesConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -1707,14 +1667,12 @@ func TestGhRepoPagesConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -1759,14 +1717,12 @@ func TestGhRepoTerraformConfig_Merge_2(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() tc.value.Merge(tc.from) + if diff := cmp.Diff(tc.expected, tc.value); diff != "" { t.Errorf("Config mismatch (-want +got):\n%s", diff) } @@ -1790,8 +1746,8 @@ func ensureMergeWithoutOverflowBetweenToAndFrom[T any](t *testing.T, empty T, me initialFromStringValue := "initial_from_value" initialFromSliceValue := []string{"initial_from_slice_value1", "initial_from_slice_value2"} - initialFromStringValueCopy := fmt.Sprintf("%s", initialFromStringValue) - initialFromSliceValueCopy := []string{fmt.Sprintf("%s", initialFromSliceValue[0]), fmt.Sprintf("%s", initialFromSliceValue[1])} + initialFromStringValueCopy := fmt.Sprintf("%s", initialFromStringValue) //nolint:perfsprint // Because :p + initialFromSliceValueCopy := []string{fmt.Sprintf("%s", initialFromSliceValue[0]), fmt.Sprintf("%s", initialFromSliceValue[1])} //nolint:perfsprint // Because :p createCopyWithFn := func(stringVal *string, sliceVal *[]string) *T { v := reflect.New(reflect.TypeOf(empty)).Interface().(*T) @@ -1846,20 +1802,20 @@ func ensureNoOverflowBetweenToAndFrom[T any](t *testing.T, to *T, from *T, creat updatedFromStringPtrValue := "updated_from_ptr_value" updatedFromSlicePtrValue := []string{"updated_from_slice_ptr_value1", "updated_from_slice_ptr_value2"} - updatedToStringValueCopy := fmt.Sprintf("%s", updatedToStringValue) - updatedFromStringValueCopy := fmt.Sprintf("%s", updatedFromStringValue) + updatedToStringValueCopy := fmt.Sprintf("%s", updatedToStringValue) //nolint:perfsprint // Because :p + updatedFromStringValueCopy := fmt.Sprintf("%s", updatedFromStringValue) //nolint:perfsprint // Because :p - updatedToSliceValueCopy := []string{fmt.Sprintf("%s", updatedToSliceValue[0]), fmt.Sprintf("%s", updatedToSliceValue[1])} - updatedFromSliceValueCopy := []string{fmt.Sprintf("%s", updatedFromSliceValue[0]), fmt.Sprintf("%s", updatedFromSliceValue[1])} + updatedToSliceValueCopy := []string{fmt.Sprintf("%s", updatedToSliceValue[0]), fmt.Sprintf("%s", updatedToSliceValue[1])} //nolint:perfsprint // Because :p + updatedFromSliceValueCopy := []string{fmt.Sprintf("%s", updatedFromSliceValue[0]), fmt.Sprintf("%s", updatedFromSliceValue[1])} //nolint:perfsprint // Because :p - updatedToStringPtrValueCopy := fmt.Sprintf("%s", updatedToStringPtrValue) + updatedToStringPtrValueCopy := fmt.Sprintf("%s", updatedToStringPtrValue) //nolint:perfsprint // Because :p updatedToStringPtrCopy := &updatedToStringPtrValueCopy - updatedFromStringPtrValueCopy := fmt.Sprintf("%s", updatedFromStringPtrValue) + updatedFromStringPtrValueCopy := fmt.Sprintf("%s", updatedFromStringPtrValue) //nolint:perfsprint // Because :p updatedFromStringPtrCopy := &updatedFromStringPtrValueCopy - updatedToSlicePtrValueCopy := []string{fmt.Sprintf("%s", updatedToSlicePtrValue[0]), fmt.Sprintf("%s", updatedToSlicePtrValue[1])} + updatedToSlicePtrValueCopy := []string{fmt.Sprintf("%s", updatedToSlicePtrValue[0]), fmt.Sprintf("%s", updatedToSlicePtrValue[1])} //nolint:perfsprint // Because :p updatedToSlicePtrCopy := &updatedToSlicePtrValueCopy - updatedFromSlicePtrValueCopy := []string{fmt.Sprintf("%s", updatedFromSlicePtrValue[0]), fmt.Sprintf("%s", updatedFromSlicePtrValue[1])} + updatedFromSlicePtrValueCopy := []string{fmt.Sprintf("%s", updatedFromSlicePtrValue[0]), fmt.Sprintf("%s", updatedFromSlicePtrValue[1])} //nolint:perfsprint // Because :p updatedFromSlicePtrCopy := &updatedFromSlicePtrValueCopy // All values must be equals to 'to' updated values - Use copy in case original values are updated ! @@ -1917,7 +1873,7 @@ func ensureNoOverflowBetweenToAndFrom[T any](t *testing.T, to *T, from *T, creat } func updateStringPtrHelper(to **string, stringToCopy *string, updatePtr bool) { - newString := fmt.Sprintf("%s", *stringToCopy) + newString := fmt.Sprintf("%s", *stringToCopy) //nolint:perfsprint //Be sure to copy the value rather than assigning the pointer ! if updatePtr { *to = &newString diff --git a/core/terraform_writer_test.go b/core/terraform_writer_test.go index 76a1f3b..01c401a 100644 --- a/core/terraform_writer_test.go +++ b/core/terraform_writer_test.go @@ -1,6 +1,7 @@ package core_test import ( + "errors" "fmt" "os" "path" @@ -38,13 +39,11 @@ func TestGenerateHclRepoFiles(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + files, err := core.GenerateHclRepoFiles(tc.value) if err != nil { t.Errorf("Case %q: %s", tcname, err) @@ -59,6 +58,7 @@ func TestGenerateHclRepoFiles(t *testing.T) { } } } + if !t.Failed() && len(files) != len(tc.expectedFiles) { t.Errorf("Case %q: expected %d files, got %d", tcname, len(tc.expectedFiles), len(files)) } @@ -101,15 +101,14 @@ func TestWriteTerraformFiles(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + root := os.TempDir() err := core.WriteTerraformFiles(root, tc.value) + if err != nil { t.Errorf("Case %q: %s", tcname, err) } else { @@ -155,7 +154,7 @@ func TestWriteTerraformFiles_onError(t *testing.T) { map[string]*hclwrite.File{ "repo.repo1.tf": file1, }, - fmt.Errorf("error while writing terraform files:\n\t - workspace path doesn't exist: /an_unknown_dir/somewhere"), + errors.New("error while writing terraform files:\n\t - workspace path doesn't exist: /an_unknown_dir/somewhere"), }, "Unable to write": { notWritableDir, @@ -167,13 +166,11 @@ func TestWriteTerraformFiles_onError(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + if err2 := core.WriteTerraformFiles(tc.root, tc.value); err2 == nil { t.Errorf("Case %q: expected an error but everything went well", tcname) } else if err2.Error() != tc.error.Error() { diff --git a/core/yaml_loader_test.go b/core/yaml_loader_test.go index 9c92f9e..4606eda 100644 --- a/core/yaml_loader_test.go +++ b/core/yaml_loader_test.go @@ -1,7 +1,7 @@ package core_test import ( - "fmt" + "errors" "testing" "github.com/yoanm/go-github-tf/core" @@ -23,17 +23,17 @@ func TestLoadRepositoryFromFile(t *testing.T) { "Not found file": { "an_unknown_file", nil, - fmt.Errorf("open an_unknown_file: no such file or directory"), + errors.New("open an_unknown_file: no such file or directory"), }, "Empty": { "testdata/invalid-config-files/empty.yml", nil, - fmt.Errorf("file testdata/invalid-config-files/empty.yml: EOF"), + errors.New("file testdata/invalid-config-files/empty.yml: EOF"), }, "Unexpected property": { "testdata/invalid-config-files/repos/repo.unexpected-property.yml", nil, - fmt.Errorf("schema validation error: file testdata/invalid-config-files/repos/repo.unexpected-property.yml: /unexpected-property not allowed"), + errors.New("schema validation error: file testdata/invalid-config-files/repos/repo.unexpected-property.yml: /unexpected-property not allowed"), }, "Working": { "testdata/repo.full.yml", @@ -74,17 +74,17 @@ func TestLoadRepositoriesFromFile(t *testing.T) { "Not found file": { "an_unknown_file", nil, - fmt.Errorf("open an_unknown_file: no such file or directory"), + errors.New("open an_unknown_file: no such file or directory"), }, "Empty": { "testdata/invalid-config-files/empty.yml", nil, - fmt.Errorf("file testdata/invalid-config-files/empty.yml: EOF"), + errors.New("file testdata/invalid-config-files/empty.yml: EOF"), }, "Unexpected property": { "testdata/invalid-config-files/repos/repos.unexpected-property.yml", nil, - fmt.Errorf("schema validation error: file testdata/invalid-config-files/repos/repos.unexpected-property.yml: /0/unexpected-property not allowed"), + errors.New("schema validation error: file testdata/invalid-config-files/repos/repos.unexpected-property.yml: /0/unexpected-property not allowed"), }, "Working": { "testdata/repos.full.yml", @@ -118,17 +118,17 @@ func TestLoadRepositoryTemplateFromFile(t *testing.T) { "Not found file": { "an_unknown_file", nil, - fmt.Errorf("open an_unknown_file: no such file or directory"), + errors.New("open an_unknown_file: no such file or directory"), }, "Empty": { "testdata/invalid-config-files/empty.yml", nil, - fmt.Errorf("file testdata/invalid-config-files/empty.yml: EOF"), + errors.New("file testdata/invalid-config-files/empty.yml: EOF"), }, "Unexpected property": { "testdata/invalid-config-files/templates/repo.unexpected-property.yml", nil, - fmt.Errorf("schema validation error: file testdata/invalid-config-files/templates/repo.unexpected-property.yml: /unexpected-property not allowed"), + errors.New("schema validation error: file testdata/invalid-config-files/templates/repo.unexpected-property.yml: /unexpected-property not allowed"), }, "Working": { "testdata/repo-template.full.yml", @@ -138,14 +138,13 @@ func TestLoadRepositoryTemplateFromFile(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + conf, err := core.LoadRepositoryTemplateFromFile(tc.filename) + EnsureConfigMatching(t, tc.expected, conf, tc.error, err) }, ) @@ -165,17 +164,17 @@ func TestLoadBranchTemplateFromFile(t *testing.T) { "Not found file": { "an_unknown_file", nil, - fmt.Errorf("open an_unknown_file: no such file or directory"), + errors.New("open an_unknown_file: no such file or directory"), }, "Empty": { "testdata/invalid-config-files/empty.yml", nil, - fmt.Errorf("file testdata/invalid-config-files/empty.yml: EOF"), + errors.New("file testdata/invalid-config-files/empty.yml: EOF"), }, "Unexpected property": { "testdata/invalid-config-files/templates/branch.unexpected-property.yml", nil, - fmt.Errorf("schema validation error: file testdata/invalid-config-files/templates/branch.unexpected-property.yml: /unexpected-property not allowed"), + errors.New("schema validation error: file testdata/invalid-config-files/templates/branch.unexpected-property.yml: /unexpected-property not allowed"), }, "Working": { "testdata/branch-template.full.yml", @@ -185,14 +184,13 @@ func TestLoadBranchTemplateFromFile(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + conf, err := core.LoadBranchTemplateFromFile(tc.filename) + EnsureConfigMatching(t, tc.expected, conf, tc.error, err) }, ) @@ -212,17 +210,17 @@ func TestLoadBranchProtectionTemplateFromFile(t *testing.T) { "Not found file": { "an_unknown_file", nil, - fmt.Errorf("open an_unknown_file: no such file or directory"), + errors.New("open an_unknown_file: no such file or directory"), }, "Empty": { "testdata/invalid-config-files/empty.yml", nil, - fmt.Errorf("file testdata/invalid-config-files/empty.yml: EOF"), + errors.New("file testdata/invalid-config-files/empty.yml: EOF"), }, "Unexpected property": { "testdata/invalid-config-files/templates/branch-protection.unexpected-property.yml", nil, - fmt.Errorf("schema validation error: file testdata/invalid-config-files/templates/branch-protection.unexpected-property.yml: /unexpected-property not allowed"), + errors.New("schema validation error: file testdata/invalid-config-files/templates/branch-protection.unexpected-property.yml: /unexpected-property not allowed"), }, "Working": { "testdata/branch-protection-template.full.yml", @@ -232,14 +230,13 @@ func TestLoadBranchProtectionTemplateFromFile(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + conf, err := core.LoadBranchProtectionTemplateFromFile(tc.filename) + EnsureConfigMatching(t, tc.expected, conf, tc.error, err) }, ) @@ -259,12 +256,12 @@ func TestLoadGhRepoConfigFromFile(t *testing.T) { "Not found file": { "an_unknown_file", nil, - fmt.Errorf("open an_unknown_file: no such file or directory"), + errors.New("open an_unknown_file: no such file or directory"), }, "Empty": { "testdata/invalid-config-files/empty.yml", nil, - fmt.Errorf("file testdata/invalid-config-files/empty.yml: EOF"), + errors.New("file testdata/invalid-config-files/empty.yml: EOF"), }, "Unexpected property": { "testdata/invalid-config-files/repos/repo.unexpected-property.yml", @@ -282,14 +279,13 @@ func TestLoadGhRepoConfigFromFile(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + conf, err := core.LoadGhRepoConfigFromFile(tc.filename) + EnsureConfigMatching(t, tc.expected, conf, tc.error, err) }, ) @@ -309,12 +305,12 @@ func TestLoadGhRepoConfigListFromFile(t *testing.T) { "Not found file": { "an_unknown_file", nil, - fmt.Errorf("open an_unknown_file: no such file or directory"), + errors.New("open an_unknown_file: no such file or directory"), }, "Empty": { "testdata/invalid-config-files/empty.yml", nil, - fmt.Errorf("file testdata/invalid-config-files/empty.yml: EOF"), + errors.New("file testdata/invalid-config-files/empty.yml: EOF"), }, "Unexpected property": { "testdata/invalid-config-files/repos/repos.unexpected-property.yml", @@ -334,14 +330,13 @@ func TestLoadGhRepoConfigListFromFile(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + conf, err := core.LoadGhRepoConfigListFromFile(tc.filename) + EnsureConfigMatching(t, tc.expected, conf, tc.error, err) }, ) @@ -362,12 +357,12 @@ func TestLoadGhRepoBranchConfigFromFile(t *testing.T) { "Not found file": { "an_unknown_file", nil, - fmt.Errorf("open an_unknown_file: no such file or directory"), + errors.New("open an_unknown_file: no such file or directory"), }, "Empty": { "testdata/invalid-config-files/empty.yml", nil, - fmt.Errorf("file testdata/invalid-config-files/empty.yml: EOF"), + errors.New("file testdata/invalid-config-files/empty.yml: EOF"), }, "Unexpected property": { "testdata/invalid-config-files/templates/branch.unexpected-property.yml", @@ -382,14 +377,13 @@ func TestLoadGhRepoBranchConfigFromFile(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + conf, err := core.LoadGhRepoBranchConfigFromFile(tc.filename) + EnsureConfigMatching(t, tc.expected, conf, tc.error, err) }, ) @@ -410,12 +404,12 @@ func TestLoadGhRepoBranchProtectionConfigFromFile(t *testing.T) { "Not found file": { "an_unknown_file", nil, - fmt.Errorf("open an_unknown_file: no such file or directory"), + errors.New("open an_unknown_file: no such file or directory"), }, "Empty": { "testdata/invalid-config-files/empty.yml", nil, - fmt.Errorf("file testdata/invalid-config-files/empty.yml: EOF"), + errors.New("file testdata/invalid-config-files/empty.yml: EOF"), }, "Unexpected property": { "testdata/invalid-config-files/templates/branch-protection.unexpected-property.yml", @@ -430,14 +424,13 @@ func TestLoadGhRepoBranchProtectionConfigFromFile(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { t.Parallel() + conf, err := core.LoadGhRepoBranchProtectionConfigFromFile(tc.filename) + EnsureConfigMatching(t, tc.expected, conf, tc.error, err) }, ) diff --git a/core/yaml_validator_test.go b/core/yaml_validator_test.go index b9cddca..a9d83f6 100644 --- a/core/yaml_validator_test.go +++ b/core/yaml_validator_test.go @@ -1,7 +1,7 @@ package core_test import ( - "fmt" + "errors" "testing" "github.com/yoanm/go-github-tf/core" @@ -16,15 +16,15 @@ func TestValidateRepositoryConfig(t *testing.T) { }{ "Not found file": { "an_unknown_file", - fmt.Errorf("open an_unknown_file: no such file or directory"), + errors.New("open an_unknown_file: no such file or directory"), }, "Empty": { "testdata/invalid-config-files/empty.yml", - fmt.Errorf("file testdata/invalid-config-files/empty.yml: EOF"), + errors.New("file testdata/invalid-config-files/empty.yml: EOF"), }, "Unwanted property": { "testdata/invalid-config-files/repos/repo.unexpected-property.yml", - fmt.Errorf("schema validation error: file testdata/invalid-config-files/repos/repo.unexpected-property.yml: /unexpected-property not allowed"), + errors.New("schema validation error: file testdata/invalid-config-files/repos/repo.unexpected-property.yml: /unexpected-property not allowed"), }, "Working": { "testdata/repo.full.yml", @@ -33,9 +33,6 @@ func TestValidateRepositoryConfig(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { @@ -55,15 +52,15 @@ func TestValidateRepositoryConfigs(t *testing.T) { }{ "Not found file": { "an_unknown_file", - fmt.Errorf("open an_unknown_file: no such file or directory"), + errors.New("open an_unknown_file: no such file or directory"), }, "Empty": { "testdata/invalid-config-files/empty.yml", - fmt.Errorf("file testdata/invalid-config-files/empty.yml: EOF"), + errors.New("file testdata/invalid-config-files/empty.yml: EOF"), }, "Unwanted property": { "testdata/invalid-config-files/repos/repos.unexpected-property.yml", - fmt.Errorf("schema validation error: file testdata/invalid-config-files/repos/repos.unexpected-property.yml: /0/unexpected-property not allowed"), + errors.New("schema validation error: file testdata/invalid-config-files/repos/repos.unexpected-property.yml: /0/unexpected-property not allowed"), }, "Working": { "testdata/repos.full.yml", @@ -72,9 +69,6 @@ func TestValidateRepositoryConfigs(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { @@ -97,15 +91,15 @@ func TestValidateRepositoryTemplateConfig(t *testing.T) { }{ "Not found file": { "an_unknown_file", - fmt.Errorf("open an_unknown_file: no such file or directory"), + errors.New("open an_unknown_file: no such file or directory"), }, "Empty": { "testdata/invalid-config-files/empty.yml", - fmt.Errorf("file testdata/invalid-config-files/empty.yml: EOF"), + errors.New("file testdata/invalid-config-files/empty.yml: EOF"), }, "Unwanted property": { "testdata/invalid-config-files/templates/repo.unexpected-property.yml", - fmt.Errorf("schema validation error: file testdata/invalid-config-files/templates/repo.unexpected-property.yml: /unexpected-property not allowed"), + errors.New("schema validation error: file testdata/invalid-config-files/templates/repo.unexpected-property.yml: /unexpected-property not allowed"), }, "Working": { "testdata/repo-template.full.yml", @@ -114,9 +108,6 @@ func TestValidateRepositoryTemplateConfig(t *testing.T) { } for tcname, tc := range cases { - tcname := tcname // Reinit var for parallel test - tc := tc // Reinit var for parallel test - t.Run( tcname, func(t *testing.T) { @@ -138,15 +129,15 @@ func TestValidateBranchProtectionTemplateConfig(t *testing.T) { }{ "Not found file": { "an_unknown_file", - fmt.Errorf("open an_unknown_file: no such file or directory"), + errors.New("open an_unknown_file: no such file or directory"), }, "Empty": { "testdata/invalid-config-files/empty.yml", - fmt.Errorf("file testdata/invalid-config-files/empty.yml: EOF"), + errors.New("file testdata/invalid-config-files/empty.yml: EOF"), }, "Unwanted property": { "testdata/invalid-config-files/templates/branch-protection.unexpected-property.yml", - fmt.Errorf("schema validation error: file testdata/invalid-config-files/templates/branch-protection.unexpected-property.yml: /unexpected-property not allowed"), + errors.New("schema validation error: file testdata/invalid-config-files/templates/branch-protection.unexpected-property.yml: /unexpected-property not allowed"), }, "Working": { "testdata/branch-protection-template.full.yml", diff --git a/go.mod b/go.mod index 4d86a07..ce744c8 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/yoanm/go-github-tf -go 1.20 +go 1.22 require ( github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 diff --git a/go.sum b/go.sum index 1f18105..589b161 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,7 @@ github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1 github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= @@ -18,6 +19,7 @@ github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+ github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= +github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/goccy/go-yaml v1.10.0 h1:rBi+5HGuznOxx0JZ+60LDY85gc0dyIJCIMvsMJTKSKQ= github.com/goccy/go-yaml v1.10.0/go.mod h1:h/18Lr6oSQ3mvmqFoWmQ47KChOgpfHpTyIHl3yVmpiY= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= @@ -72,6 +74,7 @@ github.com/zclconf/go-cty v1.13.0/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 h1:O8uGbHCqlTp2P6QJSLmCojM4mN6UemYv8K+dCnmHmu0= +golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/main_test.go b/main_test.go index 9df8791..cc9318b 100644 --- a/main_test.go +++ b/main_test.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "os" "os/exec" @@ -109,7 +110,7 @@ func configure(t *testing.T, testdataPath string) *cmdtest.TestSuite { suite.Setup = func(rootDir string) error { _, testFileName, _, ok := runtime.Caller(0) if !ok { - return fmt.Errorf("failed get real working directory from caller") + return errors.New("failed get real working directory from caller") } projectRootDir := filepath.Dir(testFileName) @@ -138,7 +139,7 @@ func configure(t *testing.T, testdataPath string) *cmdtest.TestSuite { func chmodCmd(args []string, inputFile string) ([]byte, error) { if inputFile != "" { - return nil, fmt.Errorf("input redirection not supported") + return nil, errors.New("input redirection not supported") } if err := checkPath(args[0]); err != nil { diff --git a/workspace.go b/workspace.go index 6084a45..67080de 100644 --- a/workspace.go +++ b/workspace.go @@ -149,6 +149,7 @@ func loadReposConfigFile( errList[filename] = loadErr } else { log.Debug().Msgf("Loaded '%s' as repositories config", path) + for k, v := range repoConfigs { config.AppendRepo(v) visited[fmt.Sprintf("%s[%d]", path, k)] = *v.Name @@ -268,6 +269,7 @@ func loadTemplateFromFile(config *core.Config, filePath string, decoderOpts []ya return err } else { config.Templates.BranchProtections[tplName] = tpl + log.Debug().Msgf("Loaded '%s' as branch protection template", filePath) } case strings.HasSuffix(tplName, ".branch"): @@ -279,6 +281,7 @@ func loadTemplateFromFile(config *core.Config, filePath string, decoderOpts []ya return err } else { config.Templates.Branches[tplName] = tpl + log.Debug().Msgf("Loaded '%s' as branch protection template", filePath) } default: