Skip to content

Commit

Permalink
Correct CI check for examples and add unit test (#4045)
Browse files Browse the repository at this point in the history
This fixes a bug within the CI check for unversioned changes to examples, involving an exclude pattern not matching relevant file paths that contain separators.
  • Loading branch information
wheatear-dev authored Nov 21, 2024
1 parent af081ce commit ae07c81
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
6 changes: 4 additions & 2 deletions build/scripts/example-version-checker/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

const examplesDir = "examples"

var excludedPatterns = [...]string{"*.md", "*.yaml", "OWNERS", ".gitignore"}
var excludedPatterns = [...]string{"*.md", "*.yaml", "*.yml", "OWNERS", ".gitignore"}

func dirIsExample(dirName string) bool {
makefileName := fmt.Sprintf("%s/Makefile", dirName)
Expand Down Expand Up @@ -80,8 +80,10 @@ func filenameIsIrrelevant(filename string, exampleNames []string) bool {
return true
}

_, splitname := filepath.Split(filename)

for _, excludedName := range excludedPatterns {
matches, err := filepath.Match(excludedName, filename)
matches, err := filepath.Match(excludedName, splitname)
if err != nil {
log.Fatalf("Unknown error: %s", err)
}
Expand Down
31 changes: 31 additions & 0 deletions build/scripts/example-version-checker/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"log"
"testing"
)

func TestFilenameIsIrrelevant(t *testing.T) {
exampleNames := []string{"inner", "first"}

irrelevantMap := map[string]bool{
// in deny-list
"README.md": true,
"cloudbuild.yaml": true,
// in deny-list and inside an example
"examples/inner/outer/EXAMPLE.md": true,
"examples/first/second/third/test.yml": true,
// not in deny list, but outside examples
"outside/test.txt": true,
"1/2/3/4/5.go": true,
// in examples and relevant
"examples/inner/main.go": false,
}

for filename, expected := range irrelevantMap {
observed := filenameIsIrrelevant(filename, exampleNames)
if observed != expected {
log.Fatalf("%s was expected to be: %t", filename, expected)
}
}
}

0 comments on commit ae07c81

Please sign in to comment.