Skip to content

Commit

Permalink
Added unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Jul 15, 2021
1 parent 4cd5498 commit 0b7e46b
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 30 deletions.
11 changes: 9 additions & 2 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,15 @@ def _rust_test_common(ctx, toolchain, output):
crate_type = "bin"
if ctx.attr.crate:
# Target is building the crate in `test` config
# Build the test binary using the dependency's srcs.
crate = ctx.attr.crate[rust_common.crate_info]

# Optionally join compile data
if crate.compile_data:
compile_data = depset(ctx.files.compile_data, transitive = [crate.compile_data])
else:
compile_data = depset(ctx.files.compile_data)

# Build the test binary using the dependency's srcs.
crate_info = rust_common.create_crate_info(
name = crate_name,
type = crate_type,
Expand All @@ -420,7 +427,7 @@ def _rust_test_common(ctx, toolchain, output):
edition = crate.edition,
rustc_env = ctx.attr.rustc_env,
is_test = True,
compile_data = depset(crate.compile_data),
compile_data = compile_data,
wrapped_crate_type = crate.type,
)
else:
Expand Down
14 changes: 0 additions & 14 deletions test/compile_data/BUILD.bazel

This file was deleted.

14 changes: 0 additions & 14 deletions test/compile_data/compile_data.rs

This file was deleted.

5 changes: 5 additions & 0 deletions test/unit/compile_data/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load(":compile_data_test.bzl", "compile_data_test_suite")

compile_data_test_suite(
name = "compile_data_test_suite",
)
28 changes: 28 additions & 0 deletions test/unit/compile_data/compile_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// Data loaded from compile data
pub const COMPILE_DATA: &str = include_str!("compile_data.txt");

#[cfg(test)]
mod test {
use super::*;

/// A test that is expected to be compiled from a target that does not
/// directly populate the `compile_data` attribute
#[test]
fn test_compile_data_contents() {
assert_eq!(COMPILE_DATA, "compile data contents\n");
}


/// An extra module that tests the `rust_test` rule wrapping the
/// `rust_library` is able to provide it's own compile data.
#[cfg(test_compile_data)]
mod test_compile_data {
const TEST_COMPILE_DATA: &str = include_str!("test_compile_data.txt");

#[test]
fn test_compile_data_contents() {
assert_eq!(TEST_COMPILE_DATA, "test compile data contents\n");
}
}

}
File renamed without changes.
100 changes: 100 additions & 0 deletions test/unit/compile_data/compile_data_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""Unittest to verify compile_data (attribute) propagation"""

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("//rust:defs.bzl", "rust_common", "rust_library", "rust_test")

def _target_has_compile_data(ctx, expected):
env = analysistest.begin(ctx)
target = analysistest.target_under_test(env)

# Extract compile_data from a target expected to have a `CrateInfo` provider
crate_info = target[rust_common.crate_info]
compile_data = crate_info.compile_data.to_list()

# Ensure compile data was correctly propagated to the provider
asserts.equals(
env,
sorted([data.short_path for data in compile_data]),
expected,
)

return analysistest.end(env)

def _compile_data_propagates_to_crate_info_test_impl(ctx):
return _target_has_compile_data(
ctx,
["test/unit/compile_data/compile_data.txt"],
)

def _wrapper_rule_propagates_to_crate_info_test_impl(ctx):
return _target_has_compile_data(
ctx,
["test/unit/compile_data/compile_data.txt"],
)

def _wrapper_rule_propagates_and_joins_compiel_data_test_impl(ctx):
return _target_has_compile_data(
ctx,
[
"test/unit/compile_data/compile_data.txt",
"test/unit/compile_data/test_compile_data.txt",
],
)

compile_data_propagates_to_crate_info_test = analysistest.make(_compile_data_propagates_to_crate_info_test_impl)
wrapper_rule_propagates_to_crate_info_test = analysistest.make(_wrapper_rule_propagates_to_crate_info_test_impl)
wrapper_rule_propagates_and_joins_compiel_data_test = analysistest.make(_wrapper_rule_propagates_and_joins_compiel_data_test_impl)

def _define_test_targets():
# Note that this is the only target which assigns the `compile_data` attribute
rust_library(
name = "compile_data",
srcs = ["compile_data.rs"],
compile_data = ["compile_data.txt"],
edition = "2018",
)

rust_test(
name = "compile_data_unit_test",
crate = ":compile_data",
)

rust_test(
name = "test_compile_data_unit_test",
compile_data = ["test_compile_data.txt"],
crate = ":compile_data",
rustc_flags = ["--cfg=test_compile_data"],
)

def compile_data_test_suite(name):
"""Entry-point macro called from the BUILD file.
Args:
name (str): Name of the macro.
"""

_define_test_targets()

compile_data_propagates_to_crate_info_test(
name = "compile_data_propagates_to_crate_info_test",
target_under_test = ":compile_data",
)

wrapper_rule_propagates_to_crate_info_test(
name = "wrapper_rule_propagates_to_crate_info_test",
target_under_test = ":compile_data_unit_test",
)

wrapper_rule_propagates_and_joins_compiel_data_test(
name = "wrapper_rule_propagates_and_joins_compiel_data_test",
target_under_test = ":test_compile_data_unit_test",
)

native.test_suite(
name = name,
tests = [
":compile_data_propagates_to_crate_info_test",
":wrapper_rule_propagates_to_crate_info_test",
":wrapper_rule_propagates_and_joins_compiel_data_test",
],
)
1 change: 1 addition & 0 deletions test/unit/compile_data/test_compile_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test compile data contents

0 comments on commit 0b7e46b

Please sign in to comment.