|
| 1 | +import pytest |
| 2 | +from torchruntime.utils.args import parse_policy_args |
| 3 | + |
| 4 | + |
| 5 | +def test_default_policy(): |
| 6 | + args = ["pkg1"] |
| 7 | + preview, unsupported, cleaned = parse_policy_args(args) |
| 8 | + assert preview is False |
| 9 | + assert unsupported is True |
| 10 | + assert cleaned == ["pkg1"] |
| 11 | + |
| 12 | + |
| 13 | +def test_stable_policy(): |
| 14 | + args = ["--policy", "stable", "pkg1"] |
| 15 | + preview, unsupported, cleaned = parse_policy_args(args) |
| 16 | + assert preview is False |
| 17 | + assert unsupported is False |
| 18 | + assert cleaned == ["pkg1"] |
| 19 | + |
| 20 | + |
| 21 | +def test_nightly_policy(): |
| 22 | + args = ["--policy", "nightly"] |
| 23 | + preview, unsupported, cleaned = parse_policy_args(args) |
| 24 | + assert preview is True |
| 25 | + assert unsupported is True |
| 26 | + assert cleaned == [] |
| 27 | + |
| 28 | + |
| 29 | +def test_preview_policy_alias(): |
| 30 | + args = ["--policy", "preview"] |
| 31 | + preview, unsupported, cleaned = parse_policy_args(args) |
| 32 | + assert preview is True |
| 33 | + assert unsupported is True |
| 34 | + assert cleaned == [] |
| 35 | + |
| 36 | + |
| 37 | +def test_policy_equals_syntax(): |
| 38 | + args = ["--policy=stable", "pkg1"] |
| 39 | + preview, unsupported, cleaned = parse_policy_args(args) |
| 40 | + assert preview is False |
| 41 | + assert unsupported is False |
| 42 | + assert cleaned == ["pkg1"] |
| 43 | + |
| 44 | + |
| 45 | +def test_policy_override_preview(): |
| 46 | + # stable is p=F, u=F. --preview should make p=T |
| 47 | + args = ["--policy", "stable", "--preview"] |
| 48 | + preview, unsupported, cleaned = parse_policy_args(args) |
| 49 | + assert preview is True |
| 50 | + assert unsupported is False |
| 51 | + |
| 52 | +def test_policy_override_unsupported(): |
| 53 | + # nightly is p=T, u=T. --no-unsupported should make u=F |
| 54 | + args = ["--policy", "nightly", "--no-unsupported"] |
| 55 | + preview, unsupported, cleaned = parse_policy_args(args) |
| 56 | + assert preview is True |
| 57 | + assert unsupported is False |
| 58 | + |
| 59 | + |
| 60 | +def test_unknown_policy(): |
| 61 | + args = ["--policy", "nonexistent"] |
| 62 | + with pytest.raises(ValueError, match="Unknown policy"): |
| 63 | + parse_policy_args(args) |
| 64 | + |
| 65 | + |
| 66 | +def test_missing_policy_arg(): |
| 67 | + args = ["--policy"] |
| 68 | + with pytest.raises(ValueError, match="--policy requires an argument"): |
| 69 | + parse_policy_args(args) |
| 70 | + |
| 71 | + |
| 72 | +def test_mixed_args(): |
| 73 | + args = ["torch", "--preview", "--policy", "stable", "--uv"] |
| 74 | + # stable: p=F, u=F |
| 75 | + # --preview: p=T |
| 76 | + # Result: p=T, u=F |
| 77 | + # cleaned: ["torch", "--uv"] |
| 78 | + preview, unsupported, cleaned = parse_policy_args(args) |
| 79 | + assert preview is True |
| 80 | + assert unsupported is False |
| 81 | + assert cleaned == ["torch", "--uv"] |
0 commit comments