@@ -26,45 +26,50 @@ import (
2626 "path/filepath"
2727 "testing"
2828
29- "github.com/bazelbuild/rules_go/go/runfiles"
3029 "github.com/bazel-contrib/rules_python/gazelle/manifest"
30+ "github.com/bazelbuild/rules_go/go/runfiles"
3131)
3232
33- func TestGazelleManifestIsUpdated (t * testing.T ) {
34- requirementsPath := os .Getenv ("_TEST_REQUIREMENTS" )
35- if requirementsPath == "" {
36- t .Fatal ("_TEST_REQUIREMENTS must be set" )
33+ // getResolvedRunfile resolves an environment variable to a runfiles path.
34+ // It handles getting the env var, checking it's set, and resolving it through
35+ // the runfiles mechanism, providing detailed error messages if anything fails.
36+ func getResolvedRunfile (t * testing.T , envVar string ) string {
37+ t .Helper ()
38+ path := os .Getenv (envVar )
39+ if path == "" {
40+ t .Fatalf ("%s must be set" , envVar )
3741 }
38-
39- manifestPath := os .Getenv ("_TEST_MANIFEST" )
40- if manifestPath == "" {
41- t .Fatal ("_TEST_MANIFEST must be set" )
42+ resolvedPath , err := runfiles .Rlocation (path )
43+ if err != nil {
44+ t .Fatalf ("failed to resolve runfiles path for %s (%q): %v" , envVar , path , err )
4245 }
46+ return resolvedPath
47+ }
48+
49+ func TestGazelleManifestIsUpdated (t * testing.T ) {
50+ requirementsPathResolved := getResolvedRunfile (t , "_TEST_REQUIREMENTS" )
51+ manifestPathResolved := getResolvedRunfile (t , "_TEST_MANIFEST" )
4352
4453 manifestFile := new (manifest.File )
45- if err := manifestFile .Decode (manifestPath ); err != nil {
54+ if err := manifestFile .Decode (manifestPathResolved ); err != nil {
4655 t .Fatalf ("decoding manifest file: %v" , err )
4756 }
4857
4958 if manifestFile .Integrity == "" {
5059 t .Fatal ("failed to find the Gazelle manifest file integrity" )
5160 }
5261
53- manifestGeneratorHashPath , err := runfiles .Rlocation (
54- os .Getenv ("_TEST_MANIFEST_GENERATOR_HASH" ))
55- if err != nil {
56- t .Fatalf ("failed to resolve runfiles path of manifest: %v" , err )
57- }
62+ manifestGeneratorHashPath := getResolvedRunfile (t , "_TEST_MANIFEST_GENERATOR_HASH" )
5863
5964 manifestGeneratorHash , err := os .Open (manifestGeneratorHashPath )
6065 if err != nil {
6166 t .Fatalf ("opening %q: %v" , manifestGeneratorHashPath , err )
6267 }
6368 defer manifestGeneratorHash .Close ()
6469
65- requirements , err := os .Open (requirementsPath )
70+ requirements , err := os .Open (requirementsPathResolved )
6671 if err != nil {
67- t .Fatalf ("opening %q: %v" , requirementsPath , err )
72+ t .Fatalf ("opening %q: %v" , requirementsPathResolved , err )
6873 }
6974 defer requirements .Close ()
7075
@@ -73,9 +78,9 @@ func TestGazelleManifestIsUpdated(t *testing.T) {
7378 t .Fatalf ("verifying integrity: %v" , err )
7479 }
7580 if ! valid {
76- manifestRealpath , err := filepath .EvalSymlinks (manifestPath )
81+ manifestRealpath , err := filepath .EvalSymlinks (manifestPathResolved )
7782 if err != nil {
78- t .Fatalf ("evaluating symlink %q: %v" , manifestPath , err )
83+ t .Fatalf ("evaluating symlink %q: %v" , manifestPathResolved , err )
7984 }
8085 t .Errorf (
8186 "%q is out-of-date. Follow the update instructions in that file to resolve this" ,
0 commit comments