Skip to content

Commit

Permalink
Updating template check for go templates (GoogleCloudPlatform#12097)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickElliot authored and BBBmau committed Nov 5, 2024
1 parent 7eddcfd commit b477589
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 69 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/mmv1-check-templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ permissions: read-all
on:
pull_request:
paths:
- 'mmv1/**/*.erb'
- 'mmv1/**/*.tmpl'

jobs:
version-guard-check:
Expand All @@ -27,4 +27,4 @@ jobs:
- name: Check for invalid version guards
run: |
cd repo/tools/template-check
git diff --name-only --diff-filter=d origin/${{ github.base_ref }} ../../*.erb | sed 's=^=../../=g' | go run main.go
git diff --name-only --diff-filter=d origin/${{ github.base_ref }} ../../*.tmpl | sed 's=^=../../=g' | go run main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ruby
package gotemplate

import (
"bufio"
Expand All @@ -13,18 +13,22 @@ import (
// need to add more options to this list in the future, like `private`. The main thing we want to
// prevent is targeting `beta` in version guards, because it mishandles either `ga` or `private`.
var allowedGuards = []string{
"<% unless version == 'ga' -%>",
"<% if version == 'ga' -%>",
"<% unless version == \"ga\" -%>",
"<% if version == \"ga\" -%>",
`{{- if ne $.TargetVersionName "ga" }}`,
`{{ if ne $.TargetVersionName "ga" }}`,
`{{ if ne $.TargetVersionName "ga" -}}`,
`{{- if ne $.TargetVersionName "ga" -}}`,
`{{- if eq $.TargetVersionName "ga" }}`,
`{{ if eq $.TargetVersionName "ga" }}`,
`{{ if eq $.TargetVersionName "ga" -}}`,
`{{- if eq $.TargetVersionName "ga" -}}`,
}

// Note: this does not account for _every_ possible use of a version guard (for example, those
// starting with `version.nil?`), because the logic would start to get more complicated. Instead,
// the goal is to capture (and validate) all "standard" version guards that would be added for new
// resources/fields.
func isVersionGuard(line string) bool {
re := regexp.MustCompile("<% [a-z]+ version ")
re := regexp.MustCompile(`{{-? if (eq|ne) \$\.TargetVersionName`)
return re.MatchString(line)
}

Expand Down
47 changes: 47 additions & 0 deletions tools/template-check/gotemplate/gotemplate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package gotemplate

import (
"strings"
"testing"
)

func TestCheckVersionGuards(t *testing.T) {
cases := map[string]struct {
fileText string
expectedResults []string
}{
"allow standard format targeting ga": {
fileText: "some text\n{{- if ne $.TargetVersionName \"ga\" }}\nmore text",
expectedResults: nil,
},
"disallow targeting beta": {
fileText: "some text\n{{- if ne $.TargetVersionName \"beta\" }}\nmore text",
expectedResults: []string{`2: {{- if ne $.TargetVersionName "beta" }}`},
},
"one valid, one invalid": {
fileText: "some text\n{{- if ne $.TargetVersionName \"beta\" }}\nmore text\n{{- if ne $.TargetVersionName \"ga\" }}",
expectedResults: []string{`2: {{- if ne $.TargetVersionName "beta" }}`},
},
"multiple invalid": {
fileText: "some text\n{{- if ne $.TargetVersionName \"beta\" }}\nmore text\n\n\n{{- if eq $.TargetVersionName \"beta\" }}",
expectedResults: []string{`2: {{- if ne $.TargetVersionName "beta" }}`, `6: {{- if eq $.TargetVersionName "beta" }}`},
},
}

for tn, tc := range cases {
tc := tc
t.Run(tn, func(t *testing.T) {
t.Parallel()
results := CheckVersionGuards(strings.NewReader(tc.fileText))
if len(results) != len(tc.expectedResults) {
t.Errorf("Expected length %d, got %d", len(tc.expectedResults), len(results))
return
}
for i, result := range results {
if result != tc.expectedResults[i] {
t.Errorf("Expected %v, got %v", tc.expectedResults[i], result)
}
}
})
}
}
4 changes: 2 additions & 2 deletions tools/template-check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"fmt"
"os"

"github.com/GoogleCloudPlatform/magic-modules/tools/template-check/ruby"
"github.com/GoogleCloudPlatform/magic-modules/tools/template-check/gotemplate"
)

func isValidTemplate(filename string) (bool, error) {
results, err := ruby.CheckVersionGuardsForFile(filename)
results, err := gotemplate.CheckVersionGuardsForFile(filename)
if err != nil {
return false, err
}
Expand Down
59 changes: 0 additions & 59 deletions tools/template-check/ruby/ruby_test.go

This file was deleted.

0 comments on commit b477589

Please sign in to comment.