Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ bazel-genfiles
bazel-out
bazel-testlogs
bazel-bin
bazel-app
bazel

#idea
.idea
.ijwb
.aswb
*.iml
*.iml
3 changes: 3 additions & 0 deletions rules/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# This folder holds Bazel rules we created

load(":extensions_test.bzl", "extensions_test_suite")
load(":mabel_test.bzl", "mabel_test_suite")

extensions_test_suite()

mabel_test_suite()
95 changes: 95 additions & 0 deletions rules/mabel_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""Tests for mabel.bzl artifact macro."""

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("//rules:mabel.bzl", "DEFAULT_MAVEN_SERVERS", "TransitiveDataInfo", "artifact")

def _artifact_basic_test_impl(ctx):
env = analysistest.begin(ctx)
target = analysistest.target_under_test(env)

# Check provider
info = target[TransitiveDataInfo]
asserts.equals(env, "inherit", info.type)

# Check actions - high level check
actions = analysistest.target_actions(env)

# Filter out BaselineCoverage action which is present when running with coverage
actions = [a for a in actions if a.mnemonic != "BaselineCoverage"]
asserts.equals(env, 1, len(actions))
action = actions[0]
asserts.equals(env, "MabelMavenTransitiveDependencyResolve", action.mnemonic)

# Check critical arguments
args = action.argv
asserts.true(env, _has_arg(args, "--artifact=com.example:foo:1.0"), "Missing artifact arg")
asserts.true(env, _has_arg(args, "--type=inherit"), "Missing type arg")

return analysistest.end(env)

def _artifact_custom_test_impl(ctx):
env = analysistest.begin(ctx)
target = analysistest.target_under_test(env)

# Check provider
info = target[TransitiveDataInfo]
asserts.equals(env, "jar", info.type)

# Check actions
actions = analysistest.target_actions(env)

# Filter out BaselineCoverage action which is present when running with coverage
actions = [a for a in actions if a.mnemonic != "BaselineCoverage"]
asserts.equals(env, 1, len(actions))
action = actions[0]

args = action.argv

# Check critical arguments
asserts.true(env, _has_arg(args, "--artifact=com.example:bar:2.0"), "Missing artifact arg")
asserts.true(env, _has_arg(args, "--type=jar"), "Missing type arg")

# Check custom values that change behavior
asserts.true(env, _has_arg(args, "--test_only=true"), "Missing test_only arg")
asserts.true(env, _has_arg(args, "--blacklist=group:exclude"), "Missing blacklist arg")

return analysistest.end(env)

def _has_arg(args, expected):
for arg in args:
if arg == expected:
return True
return False

artifact_basic_test = analysistest.make(_artifact_basic_test_impl)
artifact_custom_test = analysistest.make(_artifact_custom_test_impl)

def mabel_test_suite():
# Test 1: Basic usage
basic_label = artifact("com.example:foo:1.0")

artifact_basic_test(
name = "artifact_basic_test",
target_under_test = basic_label,
)

# Test 2: Custom attributes
custom_label = artifact(
coordinate = "com.example:bar:2.0",
type = "jar",
debug_logs = True,
test_only = True,
exports_generation_type = "none",
maven_exclude_deps = ["group:exclude"],
)

artifact_custom_test(
name = "artifact_custom_test",
target_under_test = custom_label,
)

# Test 3: Deduplication
dup_label = artifact("com.example:foo:1.0")

if basic_label != dup_label:
fail("artifact() macro should be idempotent and return the same label for the same coordinate. Expected {}, got {}".format(basic_label, dup_label))