|
| 1 | +"""Unittest to verify extra_outdirs attribute adds directories to action outputs.""" |
| 2 | + |
| 3 | +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") |
| 4 | +load("//test/unit:common.bzl", "assert_action_mnemonic") |
| 5 | + |
| 6 | +def _extra_outdirs_present_test(ctx): |
| 7 | + env = analysistest.begin(ctx) |
| 8 | + target = analysistest.target_under_test(env) |
| 9 | + |
| 10 | + # Find the Rustc action |
| 11 | + rustc_action = [action for action in target.actions if action.mnemonic == "Rustc"][0] |
| 12 | + assert_action_mnemonic(env, rustc_action, "Rustc") |
| 13 | + |
| 14 | + # Get all outputs from the action |
| 15 | + outputs = rustc_action.outputs.to_list() |
| 16 | + |
| 17 | + # Check that the expected directories are in the outputs |
| 18 | + expected_dirs = ctx.attr.expected_outdirs |
| 19 | + found_dirs = [] |
| 20 | + |
| 21 | + for output in outputs: |
| 22 | + # Check if this output is a directory (directories don't have extensions) |
| 23 | + # and if its basename matches one of our expected directories |
| 24 | + if output.is_directory: |
| 25 | + if output.basename in expected_dirs: |
| 26 | + found_dirs.append(output.basename) |
| 27 | + |
| 28 | + # Verify all expected directories were found |
| 29 | + asserts.equals( |
| 30 | + env, |
| 31 | + sorted(found_dirs), |
| 32 | + sorted(expected_dirs), |
| 33 | + "Expected to find directories {expected} in action outputs, but found {found}".format( |
| 34 | + expected = expected_dirs, |
| 35 | + found = found_dirs, |
| 36 | + ), |
| 37 | + ) |
| 38 | + |
| 39 | + return analysistest.end(env) |
| 40 | + |
| 41 | +def _extra_outdirs_not_present_test(ctx): |
| 42 | + env = analysistest.begin(ctx) |
| 43 | + target = analysistest.target_under_test(env) |
| 44 | + |
| 45 | + # Find the Rustc action |
| 46 | + rustc_action = [action for action in target.actions if action.mnemonic == "Rustc"][0] |
| 47 | + assert_action_mnemonic(env, rustc_action, "Rustc") |
| 48 | + |
| 49 | + # Get all outputs from the action |
| 50 | + outputs = rustc_action.outputs.to_list() |
| 51 | + |
| 52 | + # Check that no extra directories are present |
| 53 | + # We expect only the standard outputs (rlib, rmeta if pipelining, etc.) |
| 54 | + # but not any extra_outdirs directories |
| 55 | + unexpected_dirs = [] |
| 56 | + for output in outputs: |
| 57 | + if output.is_directory: |
| 58 | + # Standard directories like .dSYM are okay, but we shouldn't have |
| 59 | + # any of the extra_outdirs we're testing for |
| 60 | + if output.basename in ["test_dir", "another_dir"]: |
| 61 | + unexpected_dirs.append(output.basename) |
| 62 | + |
| 63 | + asserts.equals( |
| 64 | + env, |
| 65 | + [], |
| 66 | + unexpected_dirs, |
| 67 | + "Expected no extra_outdirs directories, but found {found}".format( |
| 68 | + found = unexpected_dirs, |
| 69 | + ), |
| 70 | + ) |
| 71 | + |
| 72 | + return analysistest.end(env) |
| 73 | + |
| 74 | +extra_outdirs_present_test = analysistest.make( |
| 75 | + _extra_outdirs_present_test, |
| 76 | + attrs = { |
| 77 | + "expected_outdirs": attr.string_list( |
| 78 | + mandatory = True, |
| 79 | + doc = "List of expected output directory names", |
| 80 | + ), |
| 81 | + }, |
| 82 | +) |
| 83 | + |
| 84 | +extra_outdirs_not_present_test = analysistest.make(_extra_outdirs_not_present_test) |
| 85 | + |
| 86 | +def extra_outdirs_test_suite(name): |
| 87 | + """Entry-point macro called from the BUILD file. |
| 88 | +
|
| 89 | + Args: |
| 90 | + name (str): Name of the macro. |
| 91 | + """ |
| 92 | + extra_outdirs_not_present_test( |
| 93 | + name = "extra_outdirs_not_present_test", |
| 94 | + target_under_test = ":lib", |
| 95 | + ) |
| 96 | + |
| 97 | + extra_outdirs_present_test( |
| 98 | + name = "extra_outdirs_present_test", |
| 99 | + target_under_test = ":lib_with_outdirs", |
| 100 | + expected_outdirs = ["test_dir", "another_dir"], |
| 101 | + ) |
| 102 | + |
| 103 | + native.test_suite( |
| 104 | + name = name, |
| 105 | + tests = [ |
| 106 | + ":extra_outdirs_not_present_test", |
| 107 | + ":extra_outdirs_present_test", |
| 108 | + ], |
| 109 | + ) |
| 110 | + |
0 commit comments