diff --git a/README.md b/README.md index 516373ab..f2ae7566 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,15 @@ more information on how to use the projects from this repository in your workspa how-to links above and review the [the generated documentation](/doc/). +### `MODULE.bazel` Snippet + + +```python +bazel_dep(name = "cgrindel_bazel_starlib", version = "0.14.9") +``` + + + ### Workspace Configuration diff --git a/bzlrelease/BUILD.bazel b/bzlrelease/BUILD.bazel index 0a17a5a2..6452bf7e 100644 --- a/bzlrelease/BUILD.bazel +++ b/bzlrelease/BUILD.bazel @@ -16,6 +16,7 @@ bzl_library( srcs = ["defs.bzl"], deps = [ "//bzlrelease/private:create_release", + "//bzlrelease/private:generate_module_snippet", "//bzlrelease/private:generate_release_notes", "//bzlrelease/private:generate_workspace_snippet", "//bzlrelease/private:hash_sha256", diff --git a/bzlrelease/defs.bzl b/bzlrelease/defs.bzl index c3a4d13b..ab2ead4a 100644 --- a/bzlrelease/defs.bzl +++ b/bzlrelease/defs.bzl @@ -4,6 +4,10 @@ load( "//bzlrelease/private:create_release.bzl", _create_release = "create_release", ) +load( + "//bzlrelease/private:generate_module_snippet.bzl", + _generate_module_snippet = "generate_module_snippet", +) load( "//bzlrelease/private:generate_release_notes.bzl", _generate_release_notes = "generate_release_notes", @@ -23,13 +27,9 @@ load( ) create_release = _create_release - -generate_workspace_snippet = _generate_workspace_snippet - +generate_module_snippet = _generate_module_snippet generate_release_notes = _generate_release_notes - +generate_workspace_snippet = _generate_workspace_snippet hash_sha256 = _hash_sha256 - -update_readme = _update_readme - release_archive = _release_archive +update_readme = _update_readme diff --git a/bzlrelease/private/BUILD.bazel b/bzlrelease/private/BUILD.bazel index a673d401..64fca66e 100644 --- a/bzlrelease/private/BUILD.bazel +++ b/bzlrelease/private/BUILD.bazel @@ -5,6 +5,14 @@ package(default_visibility = ["//bzlrelease:__subpackages__"]) bzlformat_pkg(name = "bzlformat") +bzl_library( + name = "generate_module_snippet", + srcs = ["generate_module_snippet.bzl"], + deps = [ + "//shlib/rules:execute_binary", + ], +) + bzl_library( name = "generate_workspace_snippet", srcs = ["generate_workspace_snippet.bzl"], diff --git a/bzlrelease/private/generate_module_snippet.bzl b/bzlrelease/private/generate_module_snippet.bzl new file mode 100644 index 00000000..beeb282e --- /dev/null +++ b/bzlrelease/private/generate_module_snippet.bzl @@ -0,0 +1,19 @@ +"""Macro that defines a target that generates a Bazel module snippet.""" + +load("//shlib/rules:execute_binary.bzl", "execute_binary") + +def generate_module_snippet(name, module_name): + """Generate Bazel module snippet. + + Args: + name: The name of the target as a `string`. + module_name: The name of the Bazel module as a `string`. + + Returns: + """ + arguments = ["--module_name", module_name] + execute_binary( + name = name, + arguments = arguments, + binary = "@cgrindel_bazel_starlib//bzlrelease/tools:generate_module_snippet", + ) diff --git a/bzlrelease/private/generate_release_notes.bzl b/bzlrelease/private/generate_release_notes.bzl index c113788c..cb757bc7 100644 --- a/bzlrelease/private/generate_release_notes.bzl +++ b/bzlrelease/private/generate_release_notes.bzl @@ -2,7 +2,10 @@ load("//shlib/rules:execute_binary.bzl", "execute_binary", "file_placeholder") -def generate_release_notes(name, generate_workspace_snippet): +def generate_release_notes( + name, + generate_workspace_snippet = None, + generate_module_snippet = None): """Defines an executable target that generates release notes as Github markdown. Typically, this macro is used in conjunction with the \ @@ -12,15 +15,22 @@ def generate_release_notes(name, generate_workspace_snippet): Args: name: The name of the executable target as a `string`. - generate_workspace_snippet: The label that should be executed to - generate the workspace snippet. + generate_workspace_snippet: Optional.The label that should be executed to + generate the workspace snippet. + generate_module_snippet: Optional.The label that should be executed to + generate the Bazel module snippet. """ file_arguments = {} arguments = [] - file_key = "generate_workspace_snippet" - arguments.extend(["--generate_workspace_snippet", file_placeholder(file_key)]) - file_arguments[generate_workspace_snippet] = file_key + if generate_workspace_snippet != None: + file_key = "generate_workspace_snippet" + arguments.extend(["--generate_workspace_snippet", file_placeholder(file_key)]) + file_arguments[generate_workspace_snippet] = file_key + if generate_module_snippet != None: + file_key = "generate_module_snippet" + arguments.extend(["--generate_module_snippet", file_placeholder(file_key)]) + file_arguments[generate_module_snippet] = file_key execute_binary( name = name, diff --git a/bzlrelease/private/update_readme.bzl b/bzlrelease/private/update_readme.bzl index 57e59ac1..522af906 100644 --- a/bzlrelease/private/update_readme.bzl +++ b/bzlrelease/private/update_readme.bzl @@ -2,7 +2,11 @@ load("//shlib/rules:execute_binary.bzl", "execute_binary", "file_placeholder") -def update_readme(name, generate_workspace_snippet, readme = None): +def update_readme( + name, + generate_workspace_snippet = None, + generate_module_snippet = None, + readme = None): """Declares an executable target that updates a README.md with an updated workspace snippet. The utility will replace the lines between `` and \ @@ -11,18 +15,24 @@ def update_readme(name, generate_workspace_snippet, readme = None): Args: name: The name of the executable target as a `string`. - generate_workspace_snippet: The label that should be executed to - generate the workspace snippet. + generate_workspace_snippet: Optional. The label that should be executed to + generate the workspace snippet. + generate_module_snippet: Optional. The label that should be executed to + generate the Bazel module snippet. readme: A `string` representing the relative path to the README.md - file from the root of the workspace. + file from the root of the workspace. """ file_arguments = {} arguments = [] - file_key = "generate_workspace_snippet" - arguments.extend(["--generate_workspace_snippet", file_placeholder(file_key)]) - file_arguments[generate_workspace_snippet] = file_key - + if generate_workspace_snippet != None: + file_key = "generate_workspace_snippet" + arguments.extend(["--generate_workspace_snippet", file_placeholder(file_key)]) + file_arguments[generate_workspace_snippet] = file_key + if generate_module_snippet != None: + file_key = "generate_module_snippet" + arguments.extend(["--generate_module_snippet", file_placeholder(file_key)]) + file_arguments[generate_module_snippet] = file_key if readme != None: arguments.extend(["--readme", readme]) diff --git a/bzlrelease/tools/BUILD.bazel b/bzlrelease/tools/BUILD.bazel index 6559305d..12acd9b7 100644 --- a/bzlrelease/tools/BUILD.bazel +++ b/bzlrelease/tools/BUILD.bazel @@ -107,6 +107,16 @@ sh_binary( ], ) +sh_binary( + name = "generate_module_snippet", + srcs = ["generate_module_snippet.sh"], + visibility = ["//visibility:public"], + deps = [ + "//shlib/lib:fail", + "@bazel_tools//tools/bash/runfiles", + ], +) + # MARK: - Collect Files for Integation Tests filegroup( diff --git a/tests/bzlrelease_tests/tools_tests/generate_fake_snippet.sh b/bzlrelease/tools/generate_module_snippet.sh similarity index 56% rename from tests/bzlrelease_tests/tools_tests/generate_fake_snippet.sh rename to bzlrelease/tools/generate_module_snippet.sh index f16dab86..cb6aa62f 100755 --- a/tests/bzlrelease_tests/tools_tests/generate_fake_snippet.sh +++ b/bzlrelease/tools/generate_module_snippet.sh @@ -1,5 +1,8 @@ #!/usr/bin/env bash +# Generates a Bazel module snippet suitable for inclusion in a MODULE.bazel +# file. + # --- begin runfiles.bash initialization v3 --- # Copy-pasted from the Bazel Bash runfiles library v3. set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash @@ -11,14 +14,26 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e # --- end runfiles.bash initialization v3 --- +# MARK - Locate Dependencies + fail_sh_location=cgrindel_bazel_starlib/shlib/lib/fail.sh fail_sh="$(rlocation "${fail_sh_location}")" || \ (echo >&2 "Failed to locate ${fail_sh_location}" && exit 1) source "${fail_sh}" +# MARK - Process Args + args=() while (("$#")); do case "${1}" in + "--module_name") + module_name="${2}" + shift 2 + ;; + "--version") + version="${2}" + shift 2 + ;; "--output") output_path="${2}" shift 2 @@ -30,11 +45,36 @@ while (("$#")); do esac done -[[ -z "${output_path:-}" ]] && fail "Expected an output path." +if [[ -z "${module_name:-}" ]]; then + fail "A module name must be specified." +fi + +if [[ -z "${version:-}" ]]; then + fail "A version must be specified." +fi +if [[ "${version}" =~ ^v ]]; then + version="${version:1}" +fi + +# MARK - Generate the Snippet + +snippet="$(cat <<-EOF +bazel_dep(name = "${module_name}", version = "${version}") +EOF +)" -echo " -This is the beginning of the fake snippet. +snippet="$(cat <<-EOF +\`\`\`python +${snippet} +\`\`\` +EOF +)" +# MARK - Output the Snippet -This is the end of the fake snippet. -" > "${output_path}" +# Output the snippet +if [[ -z "${output_path:-}" ]]; then + echo "${snippet}" +else + echo "${snippet}" > "${output_path}" +fi diff --git a/bzlrelease/tools/generate_release_notes.sh b/bzlrelease/tools/generate_release_notes.sh index d233492c..a47ec6b5 100755 --- a/bzlrelease/tools/generate_release_notes.sh +++ b/bzlrelease/tools/generate_release_notes.sh @@ -42,6 +42,15 @@ while (("$#")); do generate_workspace_snippet="${starting_dir}/${generate_workspace_snippet}" shift 2 ;; + "--generate_module_snippet") + # If the input path is not absolute, then resolve it to be relative to + # the starting directory. We do this before we starting changing + # directories. + generate_module_snippet="${2}" + [[ "${generate_module_snippet}" =~ ^/ ]] || \ + generate_module_snippet="${starting_dir}/${generate_module_snippet}" + shift 2 + ;; --*) fail "Unrecognized flag ${1}." ;; @@ -55,9 +64,9 @@ done [[ ${#args[@]} == 0 ]] && fail "A tag name for the release must be specified." tag_name="${args[0]}" -[[ -z "${generate_workspace_snippet:-}" ]] && \ - fail "Expected a value for --generate_workspace_snippet." - +if [[ -z "${generate_workspace_snippet:-}" ]] && [[ -z "${generate_module_snippet:-}" ]]; then + fail "Expect at least one of the following flags to be specified: --generate_workspace_snippet, --generate_module_snippet." +fi # MARK - Generate the changelog. @@ -71,16 +80,40 @@ cd "${BUILD_WORKSPACE_DIRECTORY}" changelog_md="$( "${generate_gh_changelog_sh}" "${tag_name}" )" -workspace_snippet="$( "${generate_workspace_snippet}" --tag "${tag_name}" )" +if [[ -n "${generate_workspace_snippet:-}" ]]; then + workspace_snippet="$( "${generate_workspace_snippet}" --tag "${tag_name}" )" +fi + +if [[ -n "${generate_module_snippet:-}" ]]; then + module_snippet="$( "${generate_module_snippet}" --version "${tag_name}" )" +fi release_notes_md="$(cat <<-EOF ${changelog_md} +EOF +)" + +if [[ -n "${module_snippet:-}" ]]; then + release_notes_md="$(cat <<-EOF +${release_notes_md} + +## Bazel Module Snippet + +${module_snippet} +EOF +)" +fi + +if [[ -n "${workspace_snippet:-}" ]]; then + release_notes_md="$(cat <<-EOF +${release_notes_md} ## Workspace Snippet ${workspace_snippet} EOF )" +fi # Output the changelog if [[ -z "${output_path:-}" ]]; then diff --git a/bzlrelease/tools/update_readme.sh b/bzlrelease/tools/update_readme.sh index 73acd473..f8fe7a44 100755 --- a/bzlrelease/tools/update_readme.sh +++ b/bzlrelease/tools/update_readme.sh @@ -43,6 +43,15 @@ while (("$#")); do generate_workspace_snippet="${starting_dir}/${generate_workspace_snippet}" shift 2 ;; + "--generate_module_snippet") + # If the input path is not absolute, then resolve it to be relative to + # the starting directory. We do this before we starting changing + # directories. + generate_module_snippet="${2}" + [[ "${generate_module_snippet}" =~ ^/ ]] || \ + generate_module_snippet="${starting_dir}/${generate_module_snippet}" + shift 2 + ;; "--readme") # This is a relative path from the root of the workspace. readme_path="${2}" @@ -61,9 +70,9 @@ done [[ ${#args[@]} == 0 ]] && fail "A tag name for the release must be specified." tag_name="${args[0]}" -[[ -z "${generate_workspace_snippet:-}" ]] && \ - fail "Expected a value for --generate_workspace_snippet." - +if [[ -z "${generate_workspace_snippet:-}" ]] && [[ -z "${generate_module_snippet:-}" ]]; then + fail "Expect at least one of the following flags to be specified: --generate_workspace_snippet, --generate_module_snippet." +fi # MARK - Update README.md @@ -74,25 +83,37 @@ cd "${BUILD_WORKSPACE_DIRECTORY}" # Set up the cleanup readme_backup="${readme_path}.bak" -snippet_path="$(mktemp)" +workspace_snippet_path="$(mktemp)" +module_snippet_path="$(mktemp)" cleanup() { local exit_code="${1}" - rm -f "${snippet_path}" + rm -f "${workspace_snippet_path}" "${module_snippet_path}" if [[ ${exit_code} == 0 ]]; then rm -f "${readme_backup}" fi } trap 'cleanup $?' EXIT -# Generate the snippet -"${generate_workspace_snippet}" --tag "${tag_name}" --output "${snippet_path}" +update_doc() { + local marker="${1}" + local snippet_path="${2}" + # Copy the readme + cp "${readme_path}" "${readme_backup}" + + # Update the original + "${update_markdown_doc_sh}" \ + --marker_begin "BEGIN ${marker}" \ + --marker_end "END ${marker}" \ + --update "${snippet_path}" \ + "${readme_backup}" "${readme_path}" +} -# Copy the readme -cp "${readme_path}" "${readme_backup}" +if [[ -n "${generate_workspace_snippet:-}" ]]; then + "${generate_workspace_snippet}" --tag "${tag_name}" --output "${workspace_snippet_path}" + update_doc "WORKSPACE SNIPPET" "${workspace_snippet_path}" +fi -# Update the original -"${update_markdown_doc_sh}" \ - --marker_begin "BEGIN WORKSPACE SNIPPET" \ - --marker_end "END WORKSPACE SNIPPET" \ - --update "${snippet_path}" \ - "${readme_backup}" "${readme_path}" +if [[ -n "${generate_module_snippet:-}" ]]; then + "${generate_module_snippet}" --version "${tag_name}" --output "${module_snippet_path}" + update_doc "MODULE SNIPPET" "${module_snippet_path}" +fi diff --git a/doc/bzlrelease/generate_release_notes.md b/doc/bzlrelease/generate_release_notes.md index fde26e66..98aed460 100755 --- a/doc/bzlrelease/generate_release_notes.md +++ b/doc/bzlrelease/generate_release_notes.md @@ -7,7 +7,7 @@ ## generate_release_notes
-generate_release_notes(name, generate_workspace_snippet)
+generate_release_notes(name, generate_workspace_snippet, generate_module_snippet)
 
Defines an executable target that generates release notes as Github markdown. @@ -21,6 +21,7 @@ Typically, this macro is used in conjunction with the `generate_workspace_sn | Name | Description | Default Value | | :------------- | :------------- | :------------- | | name | The name of the executable target as a string. | none | -| generate_workspace_snippet | The label that should be executed to generate the workspace snippet. | none | +| generate_workspace_snippet | Optional.The label that should be executed to generate the workspace snippet. | None | +| generate_module_snippet | Optional.The label that should be executed to generate the Bazel module snippet. | None | diff --git a/doc/bzlrelease/update_readme.md b/doc/bzlrelease/update_readme.md index b21a11aa..12d08b84 100755 --- a/doc/bzlrelease/update_readme.md +++ b/doc/bzlrelease/update_readme.md @@ -7,7 +7,7 @@ ## update_readme
-update_readme(name, generate_workspace_snippet, readme)
+update_readme(name, generate_workspace_snippet, generate_module_snippet, readme)
 
Declares an executable target that updates a README.md with an updated workspace snippet. @@ -21,7 +21,8 @@ The utility will replace the lines between `<!-- BEGIN WORKSPACE SNIPPET --&g | Name | Description | Default Value | | :------------- | :------------- | :------------- | | name | The name of the executable target as a string. | none | -| generate_workspace_snippet | The label that should be executed to generate the workspace snippet. | none | +| generate_workspace_snippet | Optional. The label that should be executed to generate the workspace snippet. | None | +| generate_module_snippet | Optional. The label that should be executed to generate the Bazel module snippet. | None | | readme | A string representing the relative path to the README.md file from the root of the workspace. | None | diff --git a/release/BUILD.bazel b/release/BUILD.bazel index 8e6c444c..49a78ca5 100644 --- a/release/BUILD.bazel +++ b/release/BUILD.bazel @@ -3,6 +3,7 @@ load("//bzlformat:defs.bzl", "bzlformat_pkg") load( "//bzlrelease:defs.bzl", "create_release", + "generate_module_snippet", "generate_release_notes", "generate_workspace_snippet", "hash_sha256", @@ -20,6 +21,11 @@ filegroup( # MARK: - Release +generate_module_snippet( + name = "generate_module_snippet", + module_name = "cgrindel_bazel_starlib", +) + generate_workspace_snippet( name = "generate_workspace_snippet", sha256_file = ":archive_sha256", @@ -28,11 +34,13 @@ generate_workspace_snippet( generate_release_notes( name = "generate_release_notes", + generate_module_snippet = ":generate_module_snippet", generate_workspace_snippet = ":generate_workspace_snippet", ) update_readme( name = "update_readme", + generate_module_snippet = ":generate_module_snippet", generate_workspace_snippet = ":generate_workspace_snippet", ) diff --git a/tests/bzlrelease_tests/rules_tests/generate_release_notes_tests/BUILD.bazel b/tests/bzlrelease_tests/rules_tests/generate_release_notes_tests/BUILD.bazel index b7d22192..55b547d8 100644 --- a/tests/bzlrelease_tests/rules_tests/generate_release_notes_tests/BUILD.bazel +++ b/tests/bzlrelease_tests/rules_tests/generate_release_notes_tests/BUILD.bazel @@ -1,9 +1,19 @@ load("//bzlformat:defs.bzl", "bzlformat_pkg") -load("//bzlrelease:defs.bzl", "generate_release_notes", "generate_workspace_snippet") +load( + "//bzlrelease:defs.bzl", + "generate_module_snippet", + "generate_release_notes", + "generate_workspace_snippet", +) load("//tests:integration_test_common.bzl", "GH_ENV_INHERIT", "INTEGRATION_TEST_TAGS") bzlformat_pkg(name = "bzlformat") +generate_module_snippet( + name = "generate_module_snippet", + module_name = "foo_bar", +) + generate_workspace_snippet( name = "generate_snippet_with_template", template = "workspace_snippet.tmpl", @@ -22,6 +32,7 @@ generate_workspace_snippet( generate_release_notes( name = "generate_release_notes_with_workspace_name", + generate_module_snippet = ":generate_module_snippet", generate_workspace_snippet = ":generate_snippet_with_workspace_name", ) diff --git a/tests/bzlrelease_tests/rules_tests/generate_release_notes_tests/generate_release_notes_test.sh b/tests/bzlrelease_tests/rules_tests/generate_release_notes_tests/generate_release_notes_test.sh index a25cb8d2..873d0791 100755 --- a/tests/bzlrelease_tests/rules_tests/generate_release_notes_tests/generate_release_notes_test.sh +++ b/tests/bzlrelease_tests/rules_tests/generate_release_notes_tests/generate_release_notes_test.sh @@ -46,3 +46,4 @@ actual="$( "${generate_release_notes_with_workspace_name_sh}" "${tag}" )" assert_match "name = \"foo_bar\"" "${actual}" "Did not find workspace name." assert_match "## What's Changed" "${actual}" "Did not find release notes header." assert_match "bazel_starlib_dependencies()" "${actual}" "Did not find template content." +assert_match "bazel_dep\(" "${actual}" "Did not find module snippet" diff --git a/tests/bzlrelease_tests/rules_tests/update_readme_tests/BUILD.bazel b/tests/bzlrelease_tests/rules_tests/update_readme_tests/BUILD.bazel index 012f6d97..4b6e505f 100644 --- a/tests/bzlrelease_tests/rules_tests/update_readme_tests/BUILD.bazel +++ b/tests/bzlrelease_tests/rules_tests/update_readme_tests/BUILD.bazel @@ -1,9 +1,19 @@ load("//bzlformat:defs.bzl", "bzlformat_pkg") -load("//bzlrelease:defs.bzl", "generate_workspace_snippet", "update_readme") +load( + "//bzlrelease:defs.bzl", + "generate_module_snippet", + "generate_workspace_snippet", + "update_readme", +) load("//tests:integration_test_common.bzl", "GH_ENV_INHERIT", "INTEGRATION_TEST_TAGS") bzlformat_pkg(name = "bzlformat") +generate_module_snippet( + name = "generate_module_snippet", + module_name = "foo_bar", +) + generate_workspace_snippet( name = "generate_workspace_snippet", template = "workspace_snippet.tmpl", @@ -11,6 +21,7 @@ generate_workspace_snippet( update_readme( name = "update_readme", + generate_module_snippet = ":generate_module_snippet", generate_workspace_snippet = ":generate_workspace_snippet", ) diff --git a/tests/bzlrelease_tests/rules_tests/update_readme_tests/update_readme_test.sh b/tests/bzlrelease_tests/rules_tests/update_readme_tests/update_readme_test.sh index 675436f1..da6f50a7 100755 --- a/tests/bzlrelease_tests/rules_tests/update_readme_tests/update_readme_test.sh +++ b/tests/bzlrelease_tests/rules_tests/update_readme_tests/update_readme_test.sh @@ -32,11 +32,16 @@ update_readme_sh="$(rlocation "${update_readme_sh_location}")" || \ source "${setup_git_repo_sh}" readme_content="$(cat <<-EOF -Text before snippet +Text before workspace snippet Text should be replaced -Text after snippet +Text after workspace snippet +Text before module snippet + +Text should be replaced + +Text after module snippet EOF )" @@ -52,11 +57,10 @@ echo "${readme_content}" > "${readme_path}" "${update_readme_sh}" --readme "${readme_path}" "${tag_name}" actual="$(< "${readme_path}")" -assert_match "Text before snippet" "${actual}" "Find README.md" -assert_match "Text after snippet" "${actual}" "Find README.md" -assert_no_match "Text should be replaced" "${actual}" "Find README.md" -assert_match "http_archive" "${actual}" "Find README.md" -assert_match "${tag_name}" "${actual}" "Find README.md" +assert_no_match "Text should be replaced" "${actual}" +assert_match "http_archive" "${actual}" +assert_match "${tag_name}" "${actual}" +assert_match "bazel_dep" "${actual}" # MARK - Test Find README.md @@ -67,8 +71,7 @@ echo "${readme_content}" > "${readme_path}" "${update_readme_sh}" "${tag_name}" actual="$(< "${readme_path}")" -assert_match "Text before snippet" "${actual}" "Find README.md" -assert_match "Text after snippet" "${actual}" "Find README.md" -assert_no_match "Text should be replaced" "${actual}" "Find README.md" -assert_match "http_archive" "${actual}" "Find README.md" -assert_match "${tag_name}" "${actual}" "Find README.md" +assert_no_match "Text should be replaced" "${actual}" +assert_match "http_archive" "${actual}" +assert_match "${tag_name}" "${actual}" +assert_match "bazel_dep" "${actual}" diff --git a/tests/bzlrelease_tests/tools_tests/BUILD.bazel b/tests/bzlrelease_tests/tools_tests/BUILD.bazel index 8ada60ab..53100f15 100644 --- a/tests/bzlrelease_tests/tools_tests/BUILD.bazel +++ b/tests/bzlrelease_tests/tools_tests/BUILD.bazel @@ -17,20 +17,10 @@ sh_test( ], ) -sh_binary( - name = "generate_fake_snippet", - srcs = ["generate_fake_snippet.sh"], - deps = [ - "//shlib/lib:fail", - "@bazel_tools//tools/bash/runfiles", - ], -) - sh_test( name = "update_readme_test", srcs = ["update_readme_test.sh"], data = [ - ":generate_fake_snippet", "//bzlrelease/tools:update_readme", ], deps = [ @@ -93,17 +83,29 @@ sh_test( srcs = ["generate_release_notes_test.sh"], data = [ "//bzlrelease/tools:generate_release_notes", - "//bzlrelease/tools:generate_workspace_snippet", ], env_inherit = GH_ENV_INHERIT, tags = INTEGRATION_TEST_TAGS, deps = [ + "//shlib/lib:assertions", "//shlib/lib:env", "//tests:setup_git_repo", "@bazel_tools//tools/bash/runfiles", ], ) +sh_test( + name = "generate_module_snippet_test", + srcs = ["generate_module_snippet_test.sh"], + data = [ + "//bzlrelease/tools:generate_module_snippet", + ], + deps = [ + "@bazel_tools//tools/bash/runfiles", + "@cgrindel_bazel_starlib//shlib/lib:assertions", + ], +) + test_suite( name = "integration_tests", tags = INTEGRATION_TEST_TAGS, diff --git a/tests/bzlrelease_tests/tools_tests/generate_module_snippet_test.sh b/tests/bzlrelease_tests/tools_tests/generate_module_snippet_test.sh new file mode 100755 index 00000000..44c0905b --- /dev/null +++ b/tests/bzlrelease_tests/tools_tests/generate_module_snippet_test.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +# --- begin runfiles.bash initialization v2 --- +# Copy-pasted from the Bazel Bash runfiles library v2. +set -o nounset -o pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash +# shellcheck disable=SC1090 +source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ + source "$0.runfiles/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -o errexit +# --- end runfiles.bash initialization v2 --- + +# MARK - Locate Deps + +assertions_sh_location=cgrindel_bazel_starlib/shlib/lib/assertions.sh +assertions_sh="$(rlocation "${assertions_sh_location}")" || \ + (echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1) +source "${assertions_sh}" + +generate_module_snippet_sh_location=cgrindel_bazel_starlib/bzlrelease/tools/generate_module_snippet.sh +generate_module_snippet_sh="$(rlocation "${generate_module_snippet_sh_location}")" || \ + (echo >&2 "Failed to locate ${generate_module_snippet_sh_location}" && exit 1) + +# MARK - Test + +output="$( "${generate_module_snippet_sh}" --module_name "rules_chicken" --version "1.2.3" )" +expected="$(cat <<-EOF +\`\`\`python +bazel_dep(name = "rules_chicken", version = "1.2.3") +\`\`\` +EOF +)" +assert_equal "${expected}" "${output}" "module with version" + +output="$( "${generate_module_snippet_sh}" --module_name "rules_chicken" --version "v1.2.3" )" +expected="$(cat <<-EOF +\`\`\`python +bazel_dep(name = "rules_chicken", version = "1.2.3") +\`\`\` +EOF +)" +assert_equal "${expected}" "${output}" "module with tag" diff --git a/tests/bzlrelease_tests/tools_tests/generate_release_notes_test.sh b/tests/bzlrelease_tests/tools_tests/generate_release_notes_test.sh index 16b7d1e3..bfcd89e3 100755 --- a/tests/bzlrelease_tests/tools_tests/generate_release_notes_test.sh +++ b/tests/bzlrelease_tests/tools_tests/generate_release_notes_test.sh @@ -13,24 +13,20 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ # MARK - Dependencies -fail_sh_location=cgrindel_bazel_starlib/shlib/lib/fail.sh -fail_sh="$(rlocation "${fail_sh_location}")" || \ - (echo >&2 "Failed to locate ${fail_sh_location}" && exit 1) -source "${fail_sh}" - env_sh_location=cgrindel_bazel_starlib/shlib/lib/env.sh env_sh="$(rlocation "${env_sh_location}")" || \ (echo >&2 "Failed to locate ${env_sh_location}" && exit 1) source "${env_sh}" +assertions_sh_location=cgrindel_bazel_starlib/shlib/lib/assertions.sh +assertions_sh="$(rlocation "${assertions_sh_location}")" || \ + (echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1) +source "${assertions_sh}" + setup_git_repo_sh_location=cgrindel_bazel_starlib/tests/setup_git_repo.sh setup_git_repo_sh="$(rlocation "${setup_git_repo_sh_location}")" || \ (echo >&2 "Failed to locate ${setup_git_repo_sh_location}" && exit 1) -generate_workspace_snippet_sh_location=cgrindel_bazel_starlib/bzlrelease/tools/generate_workspace_snippet.sh -generate_workspace_snippet_sh="$(rlocation "${generate_workspace_snippet_sh_location}")" || \ - (echo >&2 "Failed to locate ${generate_workspace_snippet_sh_location}" && exit 1) - generate_release_notes_sh_location=cgrindel_bazel_starlib/bzlrelease/tools/generate_release_notes.sh generate_release_notes_sh="$(rlocation "${generate_release_notes_sh_location}")" || \ (echo >&2 "Failed to locate ${generate_release_notes_sh_location}" && exit 1) @@ -39,6 +35,53 @@ is_installed git || fail "Could not find git." # MARK - Setup +generate_module_snippet_sh="${PWD}/generate_module_snippet.sh" +cat >"${generate_module_snippet_sh}" <<-EOF +#!/usr/bin/env bash +while (("\$#")); do + case "\${1}" in + "--version") + version="\${2}" + shift 2 + ;; + *) + echo >&2 "Unexpected arg. \${1}" + exit 1 + ;; + esac +done +if [[ -z "\${version:-}" ]]; then + echo >&2 "Expected a version value." + exit 1 +fi +echo "MODULE SNIPPET CONTENT" +EOF +chmod +x "${generate_module_snippet_sh}" + +generate_workspace_snippet_sh="${PWD}/generate_workspace_snippet.sh" +cat >"${generate_workspace_snippet_sh}" <<-EOF +#!/usr/bin/env bash +while (("\$#")); do + case "\${1}" in + "--tag") + tag="\${2}" + shift 2 + ;; + *) + echo >&2 "Unexpected arg. \${1}" + exit 1 + ;; + esac +done +if [[ -z "\${tag:-}" ]]; then + echo >&2 "Expected a tag value." + exit 1 +fi +echo "WORKSPACE SNIPPET CONTENT" +EOF +chmod +x "${generate_workspace_snippet_sh}" + + source "${setup_git_repo_sh}" cd "${repo_dir}" @@ -48,12 +91,22 @@ cd "${repo_dir}" tag="v0.1.1" -# Test actual="$( "${generate_release_notes_sh}" \ --generate_workspace_snippet "${generate_workspace_snippet_sh}" \ "${tag}" )" -[[ "${actual}" =~ "## What's Changed" ]] -[[ "${actual}" =~ "## Workspace Snippet" ]] -[[ "${actual}" =~ "http_archive(" ]] +assert_match "## What's Changed" "${actual}" +assert_match "## Workspace Snippet" "${actual}" +assert_match "WORKSPACE SNIPPET CONTENT" "${actual}" +assert_no_match "## Bazel Module Snippet" "${actual}" + +actual="$( + "${generate_release_notes_sh}" \ + --generate_module_snippet "${generate_module_snippet_sh}" \ + "${tag}" +)" +assert_match "## What's Changed" "${actual}" +assert_match "## Bazel Module Snippet" "${actual}" +assert_match "MODULE SNIPPET CONTENT" "${actual}" +assert_no_match "## Workspace Snippet" "${actual}" diff --git a/tests/bzlrelease_tests/tools_tests/update_readme_test.sh b/tests/bzlrelease_tests/tools_tests/update_readme_test.sh index 5f5e35e1..0916bf57 100755 --- a/tests/bzlrelease_tests/tools_tests/update_readme_test.sh +++ b/tests/bzlrelease_tests/tools_tests/update_readme_test.sh @@ -18,10 +18,6 @@ assertions_sh="$(rlocation "${assertions_sh_location}")" || \ (echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1) source "${assertions_sh}" -generate_fake_snippet_sh_location=cgrindel_bazel_starlib/tests/bzlrelease_tests/tools_tests/generate_fake_snippet.sh -generate_fake_snippet_sh="$(rlocation "${generate_fake_snippet_sh_location}")" || \ - (echo >&2 "Failed to locate ${generate_fake_snippet_sh_location}" && exit 1) - update_readme_sh_location=cgrindel_bazel_starlib/bzlrelease/tools/update_readme.sh update_readme_sh="$(rlocation "${update_readme_sh_location}")" || \ (echo >&2 "Failed to locate ${update_readme_sh_location}" && exit 1) @@ -34,14 +30,80 @@ export BUILD_WORKSPACE_DIRECTORY="${PWD}" tag_name="v99999.0.0" readme_content="$(cat <<-EOF -Text before snippet +Text before workspace snippet Text should be replaced -Text after snippet +Text after workspace snippet +Text before module snippet + +Text should be replaced + +Text after module snippet EOF )" +generate_module_snippet_sh="${PWD}/generate_module_snippet.sh" +cat >"${generate_module_snippet_sh}" <<-EOF +#!/usr/bin/env bash +while (("\$#")); do + case "\${1}" in + "--version") + version="\${2}" + shift 2 + ;; + "--output") + output="\${2}" + shift 2 + ;; + *) + echo >&2 "Unexpected arg. \${1}" + exit 1 + ;; + esac +done +if [[ -z "\${version:-}" ]]; then + echo >&2 "Expected a version value." + exit 1 +fi +if [[ -z "\${output:-}" ]]; then + echo >&2 "Expected a output value." + exit 1 +fi +echo "MODULE SNIPPET CONTENT" > "\${output}" +EOF +chmod +x "${generate_module_snippet_sh}" + +generate_workspace_snippet_sh="${PWD}/generate_workspace_snippet.sh" +cat >"${generate_workspace_snippet_sh}" <<-EOF +#!/usr/bin/env bash +while (("\$#")); do + case "\${1}" in + "--tag") + tag="\${2}" + shift 2 + ;; + "--output") + output="\${2}" + shift 2 + ;; + *) + echo >&2 "Unexpected arg. \${1}" + exit 1 + ;; + esac +done +if [[ -z "\${tag:-}" ]]; then + echo >&2 "Expected a tag value." + exit 1 +fi +if [[ -z "\${output:-}" ]]; then + echo >&2 "Expected a output value." + exit 1 +fi +echo "WORKSPACE SNIPPET CONTENT" > "\${output}" +EOF +chmod +x "${generate_workspace_snippet_sh}" # MARK - Test Find README.md @@ -51,11 +113,22 @@ readme_path="${BUILD_WORKSPACE_DIRECTORY}/README.md" echo "${readme_content}" > "${readme_path}" "${update_readme_sh}" \ - --generate_workspace_snippet "${generate_fake_snippet_sh}" \ + --generate_workspace_snippet "${generate_workspace_snippet_sh}" \ + --generate_module_snippet "${generate_module_snippet_sh}" \ "${tag_name}" actual="$(< "${readme_path}")" -assert_match "Text before snippet" "${actual}" "Find README.md" -assert_match "Text after snippet" "${actual}" "Find README.md" -assert_no_match "Text should be replaced" "${actual}" "Find README.md" -assert_match "fake snippet" "${actual}" "Find README.md" +expected="$(cat <<-EOF +Text before workspace snippet + +WORKSPACE SNIPPET CONTENT + +Text after workspace snippet +Text before module snippet + +MODULE SNIPPET CONTENT + +Text after module snippet +EOF +)" +assert_equal "${expected}" "${actual}"