Skip to content

Commit

Permalink
Use write_source_file / diff_test for Gazelle manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
adzenith committed Jan 4, 2024
1 parent 619fa0c commit 01547e5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 134 deletions.
1 change: 1 addition & 0 deletions gazelle/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "aspect_bazel_lib", version = "2.1.0")
bazel_dep(name = "rules_python", version = "0.18.0")
bazel_dep(name = "rules_go", version = "0.41.0", repo_name = "io_bazel_rules_go")
bazel_dep(name = "gazelle", version = "0.33.0", repo_name = "bazel_gazelle")
Expand Down
1 change: 0 additions & 1 deletion gazelle/manifest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ filegroup(
srcs = glob(["**"]) + [
"//manifest/generate:distribution",
"//manifest/hasher:distribution",
"//manifest/test:distribution",
],
visibility = ["//:__pkg__"],
)
63 changes: 29 additions & 34 deletions gazelle/manifest/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
for updating and testing the Gazelle manifest file.
"""

load("@io_bazel_rules_go//go:def.bzl", "GoSource", "go_binary", "go_test")
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file")
load("@io_bazel_rules_go//go:def.bzl", "GoSource")

def gazelle_python_manifest(
name,
Expand All @@ -40,8 +41,8 @@ def gazelle_python_manifest(
pip_deps_repository_name: deprecated - the old pip_install target name.
modules_mapping: the target for the generated modules_mapping.json file.
manifest: the target for the Gazelle manifest file.
**kwargs: other bazel attributes passed to the target target generated by
this macro.
**kwargs: other bazel attributes passed to the generate and test targets
generated by this macro.
"""
if pip_deps_repository_name != "":
# buildifier: disable=print
Expand All @@ -55,9 +56,13 @@ def gazelle_python_manifest(
# This is a temporary check while pip_deps_repository_name exists as deprecated.
fail("pip_repository_name must be set in //{}:{}".format(native.package_name(), name))

test_target = "{}.test".format(name)
update_target = "{}.update".format(name)
update_target_label = "//{}:{}".format(native.package_name(), update_target)

manifest_genrule = manifest.replace(":", "") + ".genrule"
generated_manifest = manifest + ".generated"
manifest_generator = Label("//manifest/generate:generate")
manifest_generator_hash = Label("//manifest/generate:generate_lib_sources_hash")

if type(requirements) == "list":
Expand All @@ -72,15 +77,15 @@ def gazelle_python_manifest(

update_args = [
"--manifest-generator-hash",
"$(rootpath {})".format(manifest_generator_hash),
"$(location {})".format(manifest_generator_hash),
"--requirements",
"$(rootpath {})".format(requirements),
"$(location {})".format(requirements),
"--pip-repository-name",
pip_repository_name,
"--modules-mapping",
"$(rootpath {})".format(modules_mapping),
"$(location {})".format(modules_mapping),
"--output",
"$(rootpath {})".format(manifest),
"$(location {})".format(generated_manifest),
"--update-target",
update_target_label,
]
Expand All @@ -103,40 +108,30 @@ def gazelle_python_manifest(
"false",
]

go_binary(
name = update_target,
embed = [Label("//manifest/generate:generate_lib")],
data = [
native.genrule(
name = manifest_genrule,
outs = [generated_manifest],
cmd = "$(location {}) {}".format(manifest_generator, " ".join(update_args)),
tools = [manifest_generator],
srcs = [
manifest,
modules_mapping,
requirements,
manifest_generator_hash,
],
args = update_args,
visibility = ["//visibility:private"],
tags = ["manual"],
)

attrs = {
"env": {
"_TEST_MANIFEST": "$(rootpath {})".format(manifest),
"_TEST_MANIFEST_GENERATOR_HASH": "$(rootpath {})".format(manifest_generator_hash),
"_TEST_REQUIREMENTS": "$(rootpath {})".format(requirements),
},
"size": "small",
}
go_test(
name = "{}.test".format(name),
srcs = [Label("//manifest/test:test.go")],
data = [
manifest,
requirements,
manifest_generator_hash,
],
rundir = ".",
deps = [Label("//manifest")],
# kwargs could contain test-specific attributes like size or timeout
**dict(attrs, **kwargs)
write_source_file(
name = update_target,
in_file = generated_manifest,
out_file = manifest,
**kwargs
)

# For backward compatibility, add an alias to the write_source_file's diff_test
native.test_suite(
name = test_target,
tests = ["{}_test".format(update_target)],
)

native.filegroup(
Expand Down
13 changes: 1 addition & 12 deletions gazelle/manifest/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ import (
"github.com/bazelbuild/rules_python/gazelle/manifest"
)

func init() {
if os.Getenv("BUILD_WORKSPACE_DIRECTORY") == "" {
log.Fatalln("ERROR: this program must run under Bazel")
}
}

func main() {
var (
manifestGeneratorHashPath string
Expand Down Expand Up @@ -173,12 +167,7 @@ func writeOutput(
manifestGeneratorHashPath string,
requirementsPath string,
) error {
stat, err := os.Stat(outputPath)
if err != nil {
return fmt.Errorf("failed to write output: %w", err)
}

outputFile, err := os.OpenFile(outputPath, os.O_WRONLY|os.O_TRUNC, stat.Mode())
outputFile, err := os.OpenFile(outputPath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
return fmt.Errorf("failed to write output: %w", err)
}
Expand Down
9 changes: 0 additions & 9 deletions gazelle/manifest/test/BUILD.bazel

This file was deleted.

78 changes: 0 additions & 78 deletions gazelle/manifest/test/test.go

This file was deleted.

0 comments on commit 01547e5

Please sign in to comment.