Use go_test to verify manifest (#1044)
diff --git a/gazelle/manifest/defs.bzl b/gazelle/manifest/defs.bzl
index 8540c0e..78e0c27 100644
--- a/gazelle/manifest/defs.bzl
+++ b/gazelle/manifest/defs.bzl
@@ -16,7 +16,7 @@
for updating and testing the Gazelle manifest file.
"""
-load("@io_bazel_rules_go//go:def.bzl", "GoSource", "go_binary")
+load("@io_bazel_rules_go//go:def.bzl", "GoSource", "go_binary", "go_test")
def gazelle_python_manifest(
name,
@@ -81,31 +81,22 @@
tags = ["manual"],
)
- test_binary = "_{}_test_bin".format(name)
-
- go_binary(
- name = test_binary,
- embed = [Label("//manifest/test:test_lib")],
- visibility = ["//visibility:private"],
- )
-
- native.sh_test(
+ go_test(
name = "{}.test".format(name),
- srcs = [Label("//manifest/test:run.sh")],
+ srcs = [Label("//manifest/test:test.go")],
data = [
- ":{}".format(test_binary),
manifest,
requirements,
manifest_generator_hash,
],
env = {
- "_TEST_BINARY": "$(rootpath :{})".format(test_binary),
"_TEST_MANIFEST": "$(rootpath {})".format(manifest),
"_TEST_MANIFEST_GENERATOR_HASH": "$(rootpath {})".format(manifest_generator_hash),
"_TEST_REQUIREMENTS": "$(rootpath {})".format(requirements),
},
- visibility = ["//visibility:private"],
- timeout = "short",
+ rundir = ".",
+ deps = [Label("//manifest")],
+ size = "small",
)
native.filegroup(
diff --git a/gazelle/manifest/test/BUILD.bazel b/gazelle/manifest/test/BUILD.bazel
index c8b2828..28c6c54 100644
--- a/gazelle/manifest/test/BUILD.bazel
+++ b/gazelle/manifest/test/BUILD.bazel
@@ -1,20 +1,6 @@
-load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
+# gazelle:ignore
-go_library(
- name = "test_lib",
- srcs = ["test.go"],
- importpath = "github.com/bazelbuild/rules_python/gazelle/manifest/test",
- visibility = ["//visibility:public"],
- deps = ["//manifest"],
-)
-
-go_binary(
- name = "test",
- embed = [":test_lib"],
- visibility = ["//visibility:public"],
-)
-
-exports_files(["run.sh"])
+exports_files(["test.go"])
filegroup(
name = "distribution",
diff --git a/gazelle/manifest/test/run.sh b/gazelle/manifest/test/run.sh
deleted file mode 100755
index 95efef0..0000000
--- a/gazelle/manifest/test/run.sh
+++ /dev/null
@@ -1,25 +0,0 @@
-#!/usr/bin/env bash
-# Copyright 2023 The Bazel Authors. All rights reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-
-# This file exists to allow passing the runfile paths to the Go program via
-# environment variables.
-
-set -o errexit -o nounset
-
-"${_TEST_BINARY}" \
- --manifest-generator-hash "${_TEST_MANIFEST_GENERATOR_HASH}" \
- --requirements "${_TEST_REQUIREMENTS}" \
- --manifest "${_TEST_MANIFEST}"
diff --git a/gazelle/manifest/test/test.go b/gazelle/manifest/test/test.go
index ae9fdfe..72cb260 100644
--- a/gazelle/manifest/test/test.go
+++ b/gazelle/manifest/test/test.go
@@ -13,85 +13,66 @@
// limitations under the License.
/*
-test.go is a program that asserts the Gazelle YAML manifest is up-to-date in
+test.go is a unit test that asserts the Gazelle YAML manifest is up-to-date in
regards to the requirements.txt.
It re-hashes the requirements.txt and compares it to the recorded one in the
existing generated Gazelle manifest.
*/
-package main
+package test
import (
- "flag"
- "log"
"os"
"path/filepath"
+ "testing"
"github.com/bazelbuild/rules_python/gazelle/manifest"
)
-func main() {
- var manifestGeneratorHashPath string
- var requirementsPath string
- var manifestPath string
- flag.StringVar(
- &manifestGeneratorHashPath,
- "manifest-generator-hash",
- "",
- "The file containing the hash for the source code of the manifest generator."+
- "This is important to force manifest updates when the generator logic changes.")
- flag.StringVar(
- &requirementsPath,
- "requirements",
- "",
- "The requirements.txt file.")
- flag.StringVar(
- &manifestPath,
- "manifest",
- "",
- "The manifest YAML file.")
- flag.Parse()
-
+func TestGazelleManifestIsUpdated(t *testing.T) {
+ requirementsPath := os.Getenv("_TEST_REQUIREMENTS")
if requirementsPath == "" {
- log.Fatalln("ERROR: --requirements must be set")
+ t.Fatalf("_TEST_REQUIREMENTS must be set")
}
+ manifestPath := os.Getenv("_TEST_MANIFEST")
if manifestPath == "" {
- log.Fatalln("ERROR: --manifest must be set")
+ t.Fatalf("_TEST_MANIFEST must be set")
}
manifestFile := new(manifest.File)
if err := manifestFile.Decode(manifestPath); err != nil {
- log.Fatalf("ERROR: %v\n", err)
+ t.Fatalf("decoding manifest file: %v", err)
}
if manifestFile.Integrity == "" {
- log.Fatalln("ERROR: failed to find the Gazelle manifest file integrity")
+ t.Fatal("failed to find the Gazelle manifest file integrity")
}
+ manifestGeneratorHashPath := os.Getenv("_TEST_MANIFEST_GENERATOR_HASH")
manifestGeneratorHash, err := os.Open(manifestGeneratorHashPath)
if err != nil {
- log.Fatalf("ERROR: %v\n", err)
+ t.Fatalf("opening %q: %v", manifestGeneratorHashPath, err)
}
defer manifestGeneratorHash.Close()
requirements, err := os.Open(requirementsPath)
if err != nil {
- log.Fatalf("ERROR: %v\n", err)
+ t.Fatalf("opening %q: %v", requirementsPath, err)
}
defer requirements.Close()
valid, err := manifestFile.VerifyIntegrity(manifestGeneratorHash, requirements)
if err != nil {
- log.Fatalf("ERROR: %v\n", err)
+ t.Fatalf("verifying integrity: %v", err)
}
if !valid {
manifestRealpath, err := filepath.EvalSymlinks(manifestPath)
if err != nil {
- log.Fatalf("ERROR: %v\n", err)
+ t.Fatalf("evaluating symlink %q: %v", manifestPath, err)
}
- log.Fatalf(
- "ERROR: %q is out-of-date. Follow the update instructions in that file to resolve this.\n",
+ t.Errorf(
+ "%q is out-of-date. Follow the update instructions in that file to resolve this",
manifestRealpath)
}
}