Skip to content

Commit 75a57a9

Browse files
authored
fix: remove illegal characters in testdata/ (#3484)
go modproxy is unhappy zipping the repo with the testdata files having some characters in their names. Specifically, it was complaining about `'` and `:`. The check seems to be coming from [here](https://cs.opensource.google/go/x/mod/+/master:module/module.go;l=278), so I've made all the bad characters be replaced with `_`.
1 parent 09163b7 commit 75a57a9

5 files changed

+33
-38
lines changed

vulnfeeds/cves/versions_test.go

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import (
55
"fmt"
66
"log"
77
"os"
8-
"path/filepath"
98
"reflect"
10-
"strings"
119
"testing"
1210
"time"
1311

1412
"github.com/google/go-cmp/cmp"
15-
"gopkg.in/dnaeon/go-vcr.v4/pkg/recorder"
13+
"github.com/google/osv/vulnfeeds/internal/testutils"
1614
)
1715

1816
func loadTestData2(cveName string) Vulnerability {
@@ -665,15 +663,7 @@ func TestExtractGitCommit(t *testing.T) {
665663
for _, tc := range tests {
666664
t.Run(tc.description, func(t *testing.T) {
667665
t.Parallel()
668-
r, err := recorder.New(filepath.Join("testdata", strings.ReplaceAll(t.Name(), "/", "_")))
669-
if err != nil {
670-
t.Fatal(err)
671-
}
672-
t.Cleanup(func() {
673-
if err := r.Stop(); err != nil {
674-
t.Error(err)
675-
}
676-
})
666+
r := testutils.SetupVCR(t)
677667
client := r.GetDefaultClient()
678668

679669
if _, ok := os.LookupEnv("BUILD_ID"); ok && tc.skipOnCloudBuild {
@@ -981,15 +971,7 @@ func TestExtractVersionInfo(t *testing.T) {
981971
for _, tc := range tests {
982972
t.Run(tc.description, func(t *testing.T) {
983973
t.Parallel()
984-
r, err := recorder.New(filepath.Join("testdata", strings.ReplaceAll(t.Name(), "/", "_")))
985-
if err != nil {
986-
t.Fatal(err)
987-
}
988-
t.Cleanup(func() {
989-
if err := r.Stop(); err != nil {
990-
t.Error(err)
991-
}
992-
})
974+
r := testutils.SetupVCR(t)
993975
client := r.GetDefaultClient()
994976

995977
if time.Now().Before(tc.disableExpiryDate) {
@@ -1180,15 +1162,7 @@ func TestValidateAndCanonicalizeLink(t *testing.T) {
11801162
}
11811163
for _, tt := range tests {
11821164
t.Run(tt.name, func(t *testing.T) {
1183-
r, err := recorder.New(filepath.Join("testdata", strings.ReplaceAll(t.Name(), "/", "_")))
1184-
if err != nil {
1185-
t.Fatal(err)
1186-
}
1187-
t.Cleanup(func() {
1188-
if err := r.Stop(); err != nil {
1189-
t.Error(err)
1190-
}
1191-
})
1165+
r := testutils.SetupVCR(t)
11921166
client := r.GetDefaultClient()
11931167

11941168
if time.Now().Before(tt.disableExpiryDate) {

vulnfeeds/internal/testutils/testutils.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@ import (
1111
"gopkg.in/dnaeon/go-vcr.v4/pkg/recorder"
1212
)
1313

14+
// SetupVCR sets up and returns a go-vcr recorder for the current test.
15+
// It handles adding a Cleanup hook for the recorder.
16+
func SetupVCR(t *testing.T) *recorder.Recorder {
17+
t.Helper()
18+
cassetteName := t.Name()
19+
// These characters seem to be illegal in filenames for go modproxy
20+
// Strip replace them all with _ so this can actually be downloaded.
21+
// https://cs.opensource.google/go/x/mod/+/master:module/module.go;l=278
22+
const illegalChars = "\"'*/:;<>?\\`|"
23+
for _, c := range illegalChars {
24+
cassetteName = strings.ReplaceAll(cassetteName, string(c), "_")
25+
}
26+
r, err := recorder.New(filepath.Join("testdata", cassetteName))
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
t.Cleanup(func() {
31+
if err := r.Stop(); err != nil {
32+
t.Error(err)
33+
}
34+
})
35+
36+
return r
37+
}
38+
1439
// SetupGitVCR sets up go-vcr http & https capture and replay for go-git,
1540
// as well as the Cleanup for the recorder.
1641
//
@@ -19,12 +44,9 @@ import (
1944
//
2045
// Note: this only affects http/https protocols - git:// and ssh:// connections
2146
// cannot be routed through go-vcr.
22-
func SetupGitVCR(t *testing.T) {
47+
func SetupGitVCR(t *testing.T) *recorder.Recorder {
2348
t.Helper()
24-
r, err := recorder.New(filepath.Join("testdata", strings.ReplaceAll(t.Name(), "/", "_")))
25-
if err != nil {
26-
t.Fatal(err)
27-
}
49+
r := SetupVCR(t)
2850
httpClient := r.GetDefaultClient()
2951
client.InstallProtocol("http", githttp.NewClient(httpClient))
3052
client.InstallProtocol("https", githttp.NewClient(httpClient))
@@ -33,8 +55,7 @@ func SetupGitVCR(t *testing.T) {
3355
// Restore the protocols to their defaults in case another test needs them.
3456
client.InstallProtocol("http", githttp.DefaultClient)
3557
client.InstallProtocol("https", githttp.DefaultClient)
36-
if err := r.Stop(); err != nil {
37-
t.Error(err)
38-
}
3958
})
59+
60+
return r
4061
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy