Skip to content

Commit

Permalink
Merge pull request hashicorp#37543 from hashicorp/t-const-churn-help
Browse files Browse the repository at this point in the history
ci: Clean up constant ci, fixes for churn
  • Loading branch information
YakDriver authored May 15, 2024
2 parents a74ea0f + 0b4ea1c commit d0172da
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 469 deletions.
609 changes: 203 additions & 406 deletions .ci/.semgrep-constants.yml

Large diffs are not rendered by default.

72 changes: 30 additions & 42 deletions .ci/.semgrep-test-constants.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
pattern-either:
- pattern: '"0"'
- pattern: 'acctest.CtZero'
severity: ERROR
fix: "acctest.Ct0"
- id: literal-1-string-test-constant
Expand All @@ -16,8 +17,9 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
pattern-either:
- pattern: '"1"'
- pattern: 'acctest.CtOne'
severity: ERROR
fix: "acctest.Ct1"
- id: literal-10-string-test-constant
Expand All @@ -26,8 +28,9 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
pattern-either:
- pattern: '"10"'
- pattern: 'acctest.CtTen'
severity: ERROR
fix: "acctest.Ct10"
- id: literal-2-string-test-constant
Expand All @@ -36,8 +39,9 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
pattern-either:
- pattern: '"2"'
- pattern: 'acctest.CtTwo'
severity: ERROR
fix: "acctest.Ct2"
- id: literal-3-string-test-constant
Expand All @@ -46,8 +50,9 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
pattern-either:
- pattern: '"3"'
- pattern: 'acctest.CtThree'
severity: ERROR
fix: "acctest.Ct3"
- id: literal-4-string-test-constant
Expand All @@ -56,8 +61,9 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
pattern-either:
- pattern: '"4"'
- pattern: 'acctest.CtFour'
severity: ERROR
fix: "acctest.Ct4"
- id: literal-Basic-string-test-constant
Expand All @@ -66,8 +72,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"basic"'
pattern: '"basic"'
severity: ERROR
fix: "acctest.CtBasic"
- id: literal-FalseCaps-string-test-constant
Expand All @@ -76,8 +81,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"FALSE"'
pattern: '"FALSE"'
severity: ERROR
fix: "acctest.CtFalseCaps"
- id: literal-Key1-string-test-constant
Expand All @@ -86,8 +90,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"key1"'
pattern: '"key1"'
severity: ERROR
fix: "acctest.CtKey1"
- id: literal-Key2-string-test-constant
Expand All @@ -96,8 +99,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"key2"'
pattern: '"key2"'
severity: ERROR
fix: "acctest.CtKey2"
- id: literal-ProviderTags-string-test-constant
Expand All @@ -106,8 +108,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"provider_tags"'
pattern: '"provider_tags"'
severity: ERROR
fix: "acctest.CtProviderTags"
- id: literal-ProviderValue1-string-test-constant
Expand All @@ -116,8 +117,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"providervalue1"'
pattern: '"providervalue1"'
severity: ERROR
fix: "acctest.CtProviderValue1"
- id: literal-RName-string-test-constant
Expand All @@ -126,8 +126,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"rName"'
pattern: '"rName"'
severity: ERROR
fix: "acctest.CtRName"
- id: literal-ResourceValue1-string-test-constant
Expand All @@ -136,8 +135,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"resourcevalue1"'
pattern: '"resourcevalue1"'
severity: ERROR
fix: "acctest.CtResourceValue1"
- id: literal-ResourceValue2-string-test-constant
Expand All @@ -146,8 +144,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"resourcevalue2"'
pattern: '"resourcevalue2"'
severity: ERROR
fix: "acctest.CtResourceValue2"
- id: literal-RulePound-string-test-constant
Expand All @@ -156,8 +153,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"rule.#"'
pattern: '"rule.#"'
severity: ERROR
fix: "acctest.CtRulePound"
- id: literal-TagsAllPercent-string-test-constant
Expand All @@ -166,8 +162,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"tags_all.%"'
pattern: '"tags_all.%"'
severity: ERROR
fix: "acctest.CtTagsAllPercent"
- id: literal-TagsKey1-string-test-constant
Expand All @@ -176,8 +171,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"tags.key1"'
pattern: '"tags.key1"'
severity: ERROR
fix: "acctest.CtTagsKey1"
- id: literal-TagsKey2-string-test-constant
Expand All @@ -186,8 +180,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"tags.key2"'
pattern: '"tags.key2"'
severity: ERROR
fix: "acctest.CtTagsKey2"
- id: literal-TagsPercent-string-test-constant
Expand All @@ -196,8 +189,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"tags.%"'
pattern: '"tags.%"'
severity: ERROR
fix: "acctest.CtTagsPercent"
- id: literal-TrueCaps-string-test-constant
Expand All @@ -206,8 +198,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"TRUE"'
pattern: '"TRUE"'
severity: ERROR
fix: "acctest.CtTrueCaps"
- id: literal-Value1-string-test-constant
Expand All @@ -216,8 +207,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"value1"'
pattern: '"value1"'
severity: ERROR
fix: "acctest.CtValue1"
- id: literal-Value1Updated-string-test-constant
Expand All @@ -226,8 +216,7 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"value1updated"'
pattern: '"value1updated"'
severity: ERROR
fix: "acctest.CtValue1Updated"
- id: literal-Value2-string-test-constant
Expand All @@ -236,7 +225,6 @@ rules:
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"value2"'
pattern: '"value2"'
severity: ERROR
fix: "acctest.CtValue2"
4 changes: 2 additions & 2 deletions internal/generate/acctestconsts/file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

const (
{{- range .Constants }}
Ct{{ .Constant }} = "{{ .ConstantLower }}"
Ct{{ .Constant }} = "{{ .Literal }}"
{{- end }}
)

Expand All @@ -20,7 +20,7 @@ const (
func ConstOrQuote(constant string) string {
allConstants := map[string]string{
{{- range .Constants }}
"{{ .ConstantLower }}": "Ct{{ .Constant }}",
"{{ .Literal }}": "Ct{{ .Constant }}",
{{- end }}
}

Expand Down
30 changes: 24 additions & 6 deletions internal/generate/acctestconsts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ var tmpl string
var semgrepTmpl string

type ConstantDatum struct {
Constant string
ConstantLower string
Constant string
Literal string
AltLiteral string
}

type TemplateData struct {
Expand Down Expand Up @@ -85,10 +86,27 @@ func readConstants(filename string) ([]ConstantDatum, error) {
continue
}

constantList = append(constantList, ConstantDatum{
ConstantLower: row[0],
Constant: row[1],
})
cd := ConstantDatum{
Literal: row[0],
Constant: row[1],
}

switch cd.Literal {
case "0":
cd.AltLiteral = "acctest.CtZero"
case "1":
cd.AltLiteral = "acctest.CtOne"
case "2":
cd.AltLiteral = "acctest.CtTwo"
case "3":
cd.AltLiteral = "acctest.CtThree"
case "4":
cd.AltLiteral = "acctest.CtFour"
case "10":
cd.AltLiteral = "acctest.CtTen"
}

constantList = append(constantList, cd)
}

sort.SliceStable(constantList, func(i, j int) bool {
Expand Down
11 changes: 8 additions & 3 deletions internal/generate/acctestconsts/semgrep.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ rules:
{{- range .Constants }}
- id: literal-{{ .Constant }}-string-test-constant
languages: [go]
message: Use the constant `acctest.Ct{{ .Constant }}` for the string literal "{{ .ConstantLower }}" in test files
message: Use the constant `acctest.Ct{{ .Constant }}` for the string literal "{{ .Literal }}" in test files
paths:
include:
- "internal/service/**/*_test.go"
patterns:
- pattern: '"{{ .ConstantLower }}"'
{{- if .AltLiteral }}
pattern-either:
- pattern: '"{{ .Literal }}"'
- pattern: '{{ .AltLiteral }}'
{{- else }}
pattern: '"{{ .Literal }}"'
{{- end }}
severity: ERROR
fix: "acctest.Ct{{ .Constant }}"
{{- end }}
4 changes: 2 additions & 2 deletions internal/generate/attrconsts/file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

const (
{{- range .Constants }}
Attr{{ .Constant }} = "{{ .ConstantLower }}"
Attr{{ .Constant }} = "{{ .Literal }}"
{{- end }}
)

Expand All @@ -20,7 +20,7 @@ const (
func ConstOrQuote(constant string) string {
allConstants := map[string]string{
{{- range .Constants }}
"{{ .ConstantLower }}": "Attr{{ .Constant }}",
"{{ .Literal }}": "Attr{{ .Constant }}",
{{- end }}
}

Expand Down
8 changes: 4 additions & 4 deletions internal/generate/attrconsts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var tmpl string
var semgrepTmpl string

type ConstantDatum struct {
Constant string
ConstantLower string
Constant string
Literal string
}

type TemplateData struct {
Expand Down Expand Up @@ -90,8 +90,8 @@ func readConstants(filename string) ([]ConstantDatum, error) {
}

constantList = append(constantList, ConstantDatum{
ConstantLower: row[0],
Constant: row[1],
Literal: row[0],
Constant: row[1],
})
}

Expand Down
7 changes: 3 additions & 4 deletions internal/generate/attrconsts/semgrep.tmpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# Generated by internal/generate/attrconsts/main.go; DO NOT EDIT.
rules:
{{- range .Constants }}
- id: literal-{{ .ConstantLower }}-string-constant
- id: literal-{{ .Literal }}-string-constant
languages: [go]
message: Use the constant `names.Attr{{ .Constant }}` for the string literal "{{ .ConstantLower }}"
message: Use the constant `names.Attr{{ .Constant }}` for the string literal "{{ .Literal }}"
paths:
include:
- "internal/service/**/*.go"
patterns:
- pattern: '"{{ .ConstantLower }}"'
pattern: '"{{ .Literal }}"'
severity: ERROR
fix: "names.Attr{{ .Constant }}"
{{- end }}

0 comments on commit d0172da

Please sign in to comment.