|
| 1 | +"""Cache policy tests for build actions and runnable lock targets.""" |
| 2 | + |
| 3 | +load("@rules_testing//lib:analysis_test.bzl", "analysis_test") |
| 4 | +load("@rules_testing//lib:test_suite.bzl", "test_suite") |
| 5 | +load("//python/uv:lock.bzl", "lock") |
| 6 | +load("//tests/support/platforms:platforms.bzl", "platform_targets") |
| 7 | + |
| 8 | +def _cache_tests(name, is_windows, extension): |
| 9 | + if is_windows: |
| 10 | + platform = platform_targets.WINDOWS_X86_64 |
| 11 | + else: |
| 12 | + platform = platform_targets.LINUX_X86_64 |
| 13 | + config_settings = { |
| 14 | + "//command_line_option:platforms": [platform], |
| 15 | + } |
| 16 | + for no_cache in [False, True]: |
| 17 | + subject = name + ("_no_cache" if no_cache else "_default") |
| 18 | + lock( |
| 19 | + name = subject, |
| 20 | + srcs = ["testdata/pyproject.toml"], |
| 21 | + args = ["--no-cache"] if no_cache else [], |
| 22 | + out = subject + extension, |
| 23 | + ) |
| 24 | + analysis_test( |
| 25 | + name = subject + "_build_test", |
| 26 | + impl = _test_windows_build_impl if is_windows else _test_build_impl, |
| 27 | + target = subject, |
| 28 | + config_settings = config_settings, |
| 29 | + ) |
| 30 | + analysis_test( |
| 31 | + name = subject + "_run_test", |
| 32 | + impl = _test_no_cache_run_impl if no_cache else _test_run_impl, |
| 33 | + target = subject + ".run", |
| 34 | + config_settings = config_settings, |
| 35 | + ) |
| 36 | + native.test_suite( |
| 37 | + name = name, |
| 38 | + tests = [ |
| 39 | + name + mode + kind |
| 40 | + for mode in ["_default", "_no_cache"] |
| 41 | + for kind in ["_build_test", "_run_test"] |
| 42 | + ], |
| 43 | + ) |
| 44 | + |
| 45 | +def _test_build_impl(env, target): |
| 46 | + output = target[DefaultInfo].files.to_list()[0] |
| 47 | + env.expect.that_target(target).action_generating( |
| 48 | + output.short_path, |
| 49 | + ).argv().contains("--no-cache") |
| 50 | + |
| 51 | +def _test_windows_build_impl(env, target): |
| 52 | + env.expect.that_target(target).action_generating( |
| 53 | + "{package}/{name}_lock.bat", |
| 54 | + ).content().contains("--no-cache") |
| 55 | + |
| 56 | +def _run_script(env, target): |
| 57 | + executable = target[DefaultInfo].files_to_run.executable |
| 58 | + return env.expect.that_target(target).action_generating( |
| 59 | + executable.short_path, |
| 60 | + ).content() |
| 61 | + |
| 62 | +def _test_run_impl(env, target): |
| 63 | + _run_script(env, target).split("--no-cache").has_size(1) |
| 64 | + |
| 65 | +def _test_no_cache_run_impl(env, target): |
| 66 | + _run_script(env, target).contains("--no-cache") |
| 67 | + |
| 68 | +def _test_requirements_cache(name): |
| 69 | + _cache_tests(name, is_windows = False, extension = ".txt") |
| 70 | + |
| 71 | +def _test_uv_lock_cache(name): |
| 72 | + _cache_tests(name, is_windows = False, extension = ".lock") |
| 73 | + |
| 74 | +def _test_windows_requirements_cache(name): |
| 75 | + _cache_tests(name, is_windows = True, extension = ".txt") |
| 76 | + |
| 77 | +def _test_windows_uv_lock_cache(name): |
| 78 | + _cache_tests(name, is_windows = True, extension = ".lock") |
| 79 | + |
| 80 | +def cache_test_suite(name): |
| 81 | + """Check cache defaults and explicit `--no-cache` for both lock formats.""" |
| 82 | + test_suite( |
| 83 | + name = name, |
| 84 | + tests = [ |
| 85 | + _test_requirements_cache, |
| 86 | + _test_uv_lock_cache, |
| 87 | + _test_windows_requirements_cache, |
| 88 | + _test_windows_uv_lock_cache, |
| 89 | + ], |
| 90 | + ) |
0 commit comments