Skip to content

Commit

Permalink
Add additional_contents to macos_unit_tests and macos_ui_test
Browse files Browse the repository at this point in the history
RELNOTES: macos_unit_tests and macos_ui_test now accept a `additional_contents` attribute which can copy files into specific subdirectories of the Contents folder.
PiperOrigin-RevId: 260198078
  • Loading branch information
gabebear authored and swiple-rules-gardener committed Jul 26, 2019
1 parent ba29516 commit da4a29e
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 3 deletions.
15 changes: 14 additions & 1 deletion apple/internal/testing/apple_test_bundle_support.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ load(
"@build_bazel_rules_apple//apple/internal:partials.bzl",
"partials",
)
load(
"@build_bazel_rules_apple//apple/internal:platform_support.bzl",
"platform_support",
)
load(
"@build_bazel_rules_apple//apple/internal:processor.bzl",
"processor",
Expand Down Expand Up @@ -76,7 +80,11 @@ def _apple_test_bundle_impl(ctx, extra_providers = []):
binary_artifact = binary_descriptor.artifact
debug_outputs_provider = binary_descriptor.debug_outputs_provider

debug_dependencies = []
if hasattr(ctx.attr, "additional_contents"):
debug_dependencies = ctx.attr.additional_contents.keys()
else:
debug_dependencies = []

if hasattr(ctx.attr, "frameworks"):
targets_to_avoid = list(ctx.attr.frameworks)
else:
Expand Down Expand Up @@ -115,6 +123,11 @@ def _apple_test_bundle_impl(ctx, extra_providers = []):
),
]

if platform_support.platform_type(ctx) == apple_common.platform_type.macos:
processor_partials.append(
partials.macos_additional_contents_partial(),
)

processor_result = processor.process(ctx, processor_partials)

# TODO(kaipi): Remove this filtering when apple_*_test is merged with the bundle and binary
Expand Down
34 changes: 32 additions & 2 deletions doc/rules-macos.md
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ Builds and bundles a macOS Spotlight Importer.
## macos_unit_test

```python
macos_unit_test(name, bundle_id, infoplists, minimum_os_version, runner,
macos_unit_test(name, additional_contents, bundle_id, infoplists, minimum_os_version, runner,
test_host, data, deps)
```

Expand Down Expand Up @@ -1111,6 +1111,21 @@ of the attributes inherited by all test rules, please check the
<p>A unique name for the target.</p>
</td>
</tr>
<tr>
<td><code>additional_contents</code></td>
<td>
<p><code>Dictionary of <a href="https://bazel.build/versions/master/docs/build-ref.html#labels">labels</a> to strings; optional</code></p>
<p>Files that should be copied into specific subdirectories of the
<code>Contents</code> folder in the xctest. The keys of this
dictionary are labels pointing to single files,
<code>filegroup</code>s, or targets; the corresponding value is the
name of the subdirectory of <code>Contents</code> where they should
be placed.</p>
<p>The relative directory structure of <code>filegroup</code>
contents is preserved when they are copied into the desired
<code>Contents</code> subdirectory.</p>
</td>
</tr>
<tr>
<td><code>bundle_id</code></td>
<td>
Expand Down Expand Up @@ -1197,7 +1212,7 @@ of the attributes inherited by all test rules, please check the
## macos_ui_test

```python
macos_ui_test(name, bundle_id, infoplists, minimum_os_version, runner,
macos_ui_test(name, additional_contents, bundle_id, infoplists, minimum_os_version, runner,
test_host, data, deps, [test specific attributes])
```

Expand Down Expand Up @@ -1226,6 +1241,21 @@ of the attributes inherited by all test rules, please check the
<p>A unique name for the target.</p>
</td>
</tr>
<tr>
<td><code>additional_contents</code></td>
<td>
<p><code>Dictionary of <a href="https://bazel.build/versions/master/docs/build-ref.html#labels">labels</a> to strings; optional</code></p>
<p>Files that should be copied into specific subdirectories of the
<code>Contents</code> folder in the xctest. The keys of this
dictionary are labels pointing to single files,
<code>filegroup</code>s, or targets; the corresponding value is the
name of the subdirectory of <code>Contents</code> where they should
be placed.</p>
<p>The relative directory structure of <code>filegroup</code>
contents is preserved when they are copied into the desired
<code>Contents</code> subdirectory.</p>
</td>
</tr>
<tr>
<td><code>bundle_id</code></td>
<td>
Expand Down
37 changes: 37 additions & 0 deletions test/macos_ui_test_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,41 @@ function test_dsyms_generated() {
done
}

# Tests that files passed in via the additional_contents attribute get placed at
# the correct locations in the xctest bundle.
function test_additional_contents() {
create_common_files

cat > app/simple.txt <<EOF
simple
EOF

cat >> app/BUILD <<EOF
macos_application(
name = "app",
bundle_id = "my.bundle.id",
infoplists = ["Info.plist"],
minimum_os_version = "10.11",
deps = [":lib"],
)
macos_ui_test(
name = "ui_tests",
additional_contents = {
":simple.txt": "Thing",
},
minimum_os_version = "10.11",
deps = [":ui_test_lib"],
test_host = ":app",
)
EOF

do_build macos //app:ui_tests || fail "Should build"

assert_zip_contains "test-bin/app/ui_tests.zip" \
"ui_tests.xctest/Contents/Thing/simple.txt"
assert_equals "simple" "$(unzip_single_file "test-bin/app/ui_tests.zip" \
"ui_tests.xctest/Contents/Thing/simple.txt")"
}

run_suite "macos_ui_test bundling tests"
28 changes: 28 additions & 0 deletions test/macos_unit_test_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,32 @@ function test_dsyms_generated() {
done
}

# Tests that files passed in via the additional_contents attribute get placed at
# the correct locations in the xctest bundle.
function test_additional_contents() {
create_common_files

cat > app/simple.txt <<EOF
simple
EOF

cat >> app/BUILD <<EOF
macos_unit_test(
name = "unit_tests",
additional_contents = {
":simple.txt": "Thing",
},
bundle_id = "my.bundle.idTests",
minimum_os_version = "10.11",
deps = [":unit_test_lib"],
)
EOF

do_build macos //app:unit_tests || fail "Should build"
assert_zip_contains "test-bin/app/unit_tests.zip" \
"unit_tests.xctest/Contents/Thing/simple.txt"
assert_equals "simple" "$(unzip_single_file "test-bin/app/unit_tests.zip" \
"unit_tests.xctest/Contents/Thing/simple.txt")"
}

run_suite "macos_unit_test bundling tests"

0 comments on commit da4a29e

Please sign in to comment.