Skip to content

Commit

Permalink
Adding Optional bats-core Extensions (#10)
Browse files Browse the repository at this point in the history
* Updating README to use newest tag of bazel-bats.
Moving SHA into its own var, similar to the version number.

* Updating `bazel_bats_dependencies()`'s default version value (and
corresponding SHA) to 1.7.0 (latest bats-core version).
Updating WORKSPACE to be more explicit in versions used.
Adding param checking to ensure that the SHA was passed in.
Updating README to show explicit version passing.

* Extending `bazel_bats_dependencies()` to also (optionally) allow for adding in bats extensions, bats-assert and bats-support (bats-support is required for bats-assert).
The decision to make these optional additions was made to not force extra dependencies on any downstream library that does not make use of them (dep management is good).
The `bats_test()` rule will be updated in a follow-up PR.

* Fixing missing comma.

* Slight cleanup on `if`s.

* * More better words (improving error messages).

* * Fixing nit.

* * Fixing nit 2.

* * Fixing nit 3.

* * More nit fix.
  • Loading branch information
qec-pconner authored Jan 18, 2023
1 parent 93714c7 commit 2bfd658
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,27 @@ load("@bazel_bats//:deps.bzl", "bazel_bats_dependencies")
bazel_bats_dependencies()
```

You can alternatively specify the specific bats-core version.
You can alternatively specify the specific bats-core version, as well as optionally adding bats-core extensions.

```
BATS_ASSERT_VERSION = "2.0.0"
BATS_ASSERT_SHA256 = "15dbf1abb98db785323b9327c86ee2b3114541fe5aa150c410a1632ec06d9903"
BATS_CORE_VERSION = "1.7.0"
BATS_CORE_SHA256 = "ac70c2a153f108b1ac549c2eaa4154dea4a7c1cc421e3352f0ce6ea49435454e"
BATS_SUPPORT_VERSION = "0.3.0"
BATS_SUPPORT_SHA256 = "7815237aafeb42ddcc1b8c698fc5808026d33317d8701d5ec2396e9634e2918f"
# ...
bazel_bats_dependencies(
version = BATS_CORE_VERSION,
sha256 = BATS_CORE_SHA256)
sha256 = BATS_CORE_SHA256,
# Optional test extensions
bats_assert_version = BATS_ASSERT_VERSION,
bats_assert_sha256 = BATS_ASSERT_SHA256,
bats_support_version = BATS_SUPPORT_VERSION,
bats_support_sha256 = BATS_SUPPORT_SHA256
)
```

In your `BUILD.bazel` file add the following:
Expand Down
61 changes: 58 additions & 3 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# https://stackoverflow.com/questions/47192668/idiomatic-retrieval-of-the-bazel-execution-path#
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

BATS_CORE_BUILD="""
_BATS_CORE_BUILD = """
sh_library(
name = "bats_lib",
srcs = glob(["libexec/**"]),
Expand Down Expand Up @@ -69,19 +69,74 @@ sh_library(
exports_files(glob(["test/*.bats"]))
"""

_BATS_ASSERT_BUILD = """
filegroup(
name = "load_files",
srcs = [
"load.bash",
"src/assert.bash",
],
visibility = ["//visibility:public"],
)
"""

_BATS_SUPPORT_BUILD = """
filegroup(
name = "load_files",
srcs = [
"load.bash",
"src/error.bash",
"src/lang.bash",
"src/output.bash",
],
visibility = ["//visibility:public"],
)
"""

def bazel_bats_dependencies(
version = "1.7.0",
sha256 = "ac70c2a153f108b1ac549c2eaa4154dea4a7c1cc421e3352f0ce6ea49435454e"
sha256 = "ac70c2a153f108b1ac549c2eaa4154dea4a7c1cc421e3352f0ce6ea49435454e",
bats_assert_version = None,
bats_assert_sha256 = None,
bats_support_version = None,
bats_support_sha256 = None
):
if not sha256:
fail("sha256 for bats-core was not supplied.")

http_archive(
name = "bats_core",
build_file_content = BATS_CORE_BUILD,
build_file_content = _BATS_CORE_BUILD,
urls = [
"https://github.com/bats-core/bats-core/archive/refs/tags/v%s.tar.gz" % version,
],
strip_prefix = "bats-core-%s" % version,
sha256 = sha256,
)

if bats_assert_version:
if not bats_support_version:
fail("bats-assert version was set, but was missing set version for dependency bats-support.")
if not bats_assert_sha256:
fail("sha256 for bats-assert was not supplied.")
http_archive(
name = "bats_assert",
build_file_content = _BATS_ASSERT_BUILD,
sha256 = bats_assert_sha256,
strip_prefix = "bats-assert-%s" % bats_assert_version,
urls = [
"https://github.com/bats-core/bats-assert/archive/refs/tags/v%s.tar.gz" % bats_assert_version,
],
)
if bats_support_version:
if not bats_support_sha256:
fail("sha256 for bats-support was not supplied.")
http_archive(
name = "bats_support",
build_file_content = _BATS_SUPPORT_BUILD,
sha256 = bats_support_sha256,
strip_prefix = "bats-support-%s" % bats_support_version,
urls = [
"https://github.com/bats-core/bats-support/archive/refs/tags/v%s.tar.gz" % bats_support_version,
],
)

0 comments on commit 2bfd658

Please sign in to comment.