Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 gazelle/manifest/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ def gazelle_python_manifest(
if requirements:
attrs = {
"env": {
"_TEST_MANIFEST": "$(rootpath {})".format(manifest),
"_TEST_MANIFEST": "$(rlocationpath {})".format(manifest),
"_TEST_MANIFEST_GENERATOR_HASH": "$(rlocationpath {})".format(manifest_generator_hash),
"_TEST_REQUIREMENTS": "$(rootpath {})".format(requirements),
"_TEST_REQUIREMENTS": "$(rlocationpath {})".format(requirements),
},
"size": "small",
}
Expand Down
43 changes: 24 additions & 19 deletions gazelle/manifest/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,45 +26,50 @@ import (
"path/filepath"
"testing"

"github.com/bazelbuild/rules_go/go/runfiles"
"github.com/bazel-contrib/rules_python/gazelle/manifest"
"github.com/bazelbuild/rules_go/go/runfiles"
)

func TestGazelleManifestIsUpdated(t *testing.T) {
requirementsPath := os.Getenv("_TEST_REQUIREMENTS")
if requirementsPath == "" {
t.Fatal("_TEST_REQUIREMENTS must be set")
// getResolvedRunfile resolves an environment variable to a runfiles path.
// It handles getting the env var, checking it's set, and resolving it through
// the runfiles mechanism, providing detailed error messages if anything fails.
func getResolvedRunfile(t *testing.T, envVar string) string {
t.Helper()
path := os.Getenv(envVar)
if path == "" {
t.Fatalf("%s must be set", envVar)
}

manifestPath := os.Getenv("_TEST_MANIFEST")
if manifestPath == "" {
t.Fatal("_TEST_MANIFEST must be set")
resolvedPath, err := runfiles.Rlocation(path)
if err != nil {
t.Fatalf("failed to resolve runfiles path for %s (%q): %v", envVar, path, err)
}
return resolvedPath
}

func TestGazelleManifestIsUpdated(t *testing.T) {
requirementsPathResolved := getResolvedRunfile(t, "_TEST_REQUIREMENTS")
manifestPathResolved := getResolvedRunfile(t, "_TEST_MANIFEST")

manifestFile := new(manifest.File)
if err := manifestFile.Decode(manifestPath); err != nil {
if err := manifestFile.Decode(manifestPathResolved); err != nil {
t.Fatalf("decoding manifest file: %v", err)
}

if manifestFile.Integrity == "" {
t.Fatal("failed to find the Gazelle manifest file integrity")
}

manifestGeneratorHashPath, err := runfiles.Rlocation(
os.Getenv("_TEST_MANIFEST_GENERATOR_HASH"))
if err != nil {
t.Fatalf("failed to resolve runfiles path of manifest: %v", err)
}
manifestGeneratorHashPath := getResolvedRunfile(t, "_TEST_MANIFEST_GENERATOR_HASH")

manifestGeneratorHash, err := os.Open(manifestGeneratorHashPath)
if err != nil {
t.Fatalf("opening %q: %v", manifestGeneratorHashPath, err)
}
defer manifestGeneratorHash.Close()

requirements, err := os.Open(requirementsPath)
requirements, err := os.Open(requirementsPathResolved)
if err != nil {
t.Fatalf("opening %q: %v", requirementsPath, err)
t.Fatalf("opening %q: %v", requirementsPathResolved, err)
}
defer requirements.Close()

Expand All @@ -73,9 +78,9 @@ func TestGazelleManifestIsUpdated(t *testing.T) {
t.Fatalf("verifying integrity: %v", err)
}
if !valid {
manifestRealpath, err := filepath.EvalSymlinks(manifestPath)
manifestRealpath, err := filepath.EvalSymlinks(manifestPathResolved)
if err != nil {
t.Fatalf("evaluating symlink %q: %v", manifestPath, err)
t.Fatalf("evaluating symlink %q: %v", manifestPathResolved, err)
}
t.Errorf(
"%q is out-of-date. Follow the update instructions in that file to resolve this",
Expand Down