Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating template check for go templates #12097

Merged
merged 11 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .ci/containers/build-environment/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ RUN git config --global user.email "[email protected]"
RUN go install golang.org/x/tools/cmd/goimports@d088b475e3360caabc032aaee1dc66351d4e729a
RUN go install github.com/github/[email protected]+incompatible

ADD "https://raw.githubusercontent.com/GoogleCloudPlatform/magic-modules/main/mmv1/Gemfile" Gemfile
ADD "https://raw.githubusercontent.com/GoogleCloudPlatform/magic-modules/main/mmv1/Gemfile.lock" Gemfile.lock
ADD "https://raw.githubusercontent.com/GoogleCloudPlatform/magic-modules/refs/heads/legacy-ruby/mmv1/Gemfile" Gemfile
ADD "https://raw.githubusercontent.com/GoogleCloudPlatform/magic-modules/refs/heads/legacy-ruby/mmv1/Gemfile.lock" Gemfile.lock
RUN bundle install
RUN rm Gemfile Gemfile.lock

Expand Down
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.

Loading