|
1 | 1 | """Tests for cc_binary.""" |
2 | 2 |
|
| 3 | +load("@bazel_features//:features.bzl", "bazel_features") |
3 | 4 | load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite") |
4 | | -load("@rules_testing//lib:truth.bzl", "matching") |
| 5 | +load("@rules_testing//lib:truth.bzl", "matching", "subjects") |
5 | 6 | load("@rules_testing//lib:util.bzl", "TestingAspectInfo", "util") |
6 | 7 | load("//cc:action_names.bzl", "ACTION_NAMES") |
7 | 8 | load("//cc:cc_binary.bzl", _actual_cc_binary = "cc_binary") |
@@ -300,16 +301,89 @@ def _test_missing_action_config_for_strip_is_a_rule_error_impl(env, target): |
300 | 301 | matching.contains("Expected action_config for 'strip' to be configured."), |
301 | 302 | ) |
302 | 303 |
|
| 304 | +def _test_sanitize_pwd_feature_enabled(name, **kwargs): |
| 305 | + """sanitize_pwd is on by default, PWD should be set via the feature's env_set.""" |
| 306 | + util.helper_target( |
| 307 | + cc_binary, |
| 308 | + name = name + "/hello", |
| 309 | + srcs = ["hello.cc"], |
| 310 | + ) |
| 311 | + cc_analysis_test( |
| 312 | + name = name, |
| 313 | + impl = _test_sanitize_pwd_feature_enabled_impl, |
| 314 | + target = name + "/hello", |
| 315 | + config_settings = { |
| 316 | + "//command_line_option:platforms": str(Label("//tests/cc/testutil/toolchains:linux_x86_64")), |
| 317 | + }, |
| 318 | + **kwargs |
| 319 | + ) |
| 320 | + |
| 321 | +def _test_sanitize_pwd_feature_enabled_impl(env, target): |
| 322 | + link_action = link_action_subject.from_target(env, target) |
| 323 | + link_action.env().get("PWD", factory = subjects.str).equals("/proc/self/cwd") |
| 324 | + |
| 325 | +def _test_sanitize_pwd_feature_disabled(name, **kwargs): |
| 326 | + """When sanitize_pwd is explicitly disabled, the legacy requires_darwin fallback still applies.""" |
| 327 | + util.helper_target( |
| 328 | + cc_binary, |
| 329 | + name = name + "/hello", |
| 330 | + srcs = ["hello.cc"], |
| 331 | + ) |
| 332 | + cc_analysis_test( |
| 333 | + name = name, |
| 334 | + impl = _test_sanitize_pwd_feature_disabled_impl, |
| 335 | + target = name + "/hello", |
| 336 | + config_settings = { |
| 337 | + "//command_line_option:features": ["-sanitize_pwd"], |
| 338 | + }, |
| 339 | + **kwargs |
| 340 | + ) |
| 341 | + |
| 342 | +def _test_sanitize_pwd_feature_disabled_impl(env, target): |
| 343 | + link_action = link_action_subject.from_target(env, target) |
| 344 | + link_action.env().get("PWD", factory = subjects.str).equals("/proc/self/cwd") |
| 345 | + |
| 346 | +def _test_sanitize_pwd_macos_no_pwd(name, **kwargs): |
| 347 | + """On macOS, sanitize_pwd is enabled but should not set PWD.""" |
| 348 | + util.helper_target( |
| 349 | + cc_binary, |
| 350 | + name = name + "/hello", |
| 351 | + srcs = ["hello.cc"], |
| 352 | + ) |
| 353 | + cc_analysis_test( |
| 354 | + name = name, |
| 355 | + impl = _test_sanitize_pwd_macos_no_pwd_impl, |
| 356 | + target = name + "/hello", |
| 357 | + config_settings = { |
| 358 | + "//command_line_option:platforms": str(Label("//tests/cc/testutil/toolchains:macos_arm64")), |
| 359 | + }, |
| 360 | + **kwargs |
| 361 | + ) |
| 362 | + |
| 363 | +def _test_sanitize_pwd_macos_no_pwd_impl(env, target): |
| 364 | + link_action = link_action_subject.from_target(env, target) |
| 365 | + link_action.env().keys().not_contains("PWD") |
| 366 | + |
303 | 367 | def cc_binary_configured_target_tests(name): |
| 368 | + tests = [ |
| 369 | + _test_files_to_build, |
| 370 | + _test_headers_not_passed_to_linking_action, |
| 371 | + _test_no_duplicate_linkopts, |
| 372 | + _test_action_graph, |
| 373 | + _test_runtime_dynamic_libraries_copy_behavior, |
| 374 | + _test_pic, |
| 375 | + _test_missing_action_config_for_strip_is_a_rule_error, |
| 376 | + ] |
| 377 | + |
| 378 | + # sanitize_pwd is implemented in Starlark in rules_cc, requires Bazel 9+. |
| 379 | + if bazel_features.cc.cc_common_is_in_rules_cc: |
| 380 | + tests.extend([ |
| 381 | + _test_sanitize_pwd_feature_enabled, |
| 382 | + _test_sanitize_pwd_feature_disabled, |
| 383 | + _test_sanitize_pwd_macos_no_pwd, |
| 384 | + ]) |
| 385 | + |
304 | 386 | test_suite( |
305 | 387 | name = name, |
306 | | - tests = [ |
307 | | - _test_files_to_build, |
308 | | - _test_headers_not_passed_to_linking_action, |
309 | | - _test_no_duplicate_linkopts, |
310 | | - _test_action_graph, |
311 | | - _test_runtime_dynamic_libraries_copy_behavior, |
312 | | - _test_pic, |
313 | | - _test_missing_action_config_for_strip_is_a_rule_error, |
314 | | - ], |
| 388 | + tests = tests, |
315 | 389 | ) |
0 commit comments