|
| 1 | +from pydantic import ValidationError |
| 2 | +import pytest |
| 3 | +from tomlkit import dump |
| 4 | + |
| 5 | +from somesy.rust import Rust |
| 6 | + |
| 7 | + |
| 8 | +def test_rust_validate_accept(load_files, file_types): |
| 9 | + """Validate by loading the data Cargo.toml file using the fixture.""" |
| 10 | + load_files([file_types.RUST]) |
| 11 | + |
| 12 | + |
| 13 | +def test_rust_validate(tmp_path): |
| 14 | + """Test validating a Cargo.toml file.""" |
| 15 | + |
| 16 | + # create a Cargo.toml file but with a invalid version value |
| 17 | + reject_rust_object = { |
| 18 | + "package": { |
| 19 | + "name": "somesy", |
| 20 | + "version": "abc", |
| 21 | + "authors": ["John Doe <"], |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + invalid_rust_path = tmp_path / "Cargo.toml" |
| 26 | + with open(invalid_rust_path, "w+") as f: |
| 27 | + dump(reject_rust_object, f) |
| 28 | + |
| 29 | + with pytest.raises(ValidationError): |
| 30 | + Rust(invalid_rust_path) |
| 31 | + |
| 32 | + # reject with invalid values |
| 33 | + _reject_with(tmp_path, "name", "1test-") |
| 34 | + _reject_with(tmp_path, "keywords", ["testtesttesttesttesttesttest"]) |
| 35 | + _reject_with(tmp_path, "keywords", ["test test"]) |
| 36 | + _reject_with(tmp_path, "keywords", ["test*test"]) |
| 37 | + _reject_with( |
| 38 | + tmp_path, "keywords", ["test", "test2", "test3", "test4", "test5", "test6"] |
| 39 | + ) |
| 40 | + _reject_with(tmp_path, "license_file", "LICENSE") |
| 41 | + |
| 42 | + |
| 43 | +def _reject_with(tmp_path, key, value): |
| 44 | + """Helper function to reject invalid values.""" |
| 45 | + reject_rust_object = { |
| 46 | + "package": { |
| 47 | + "name": "somesy", |
| 48 | + "version": "1.0.0", |
| 49 | + "license": "MIT", |
| 50 | + } |
| 51 | + } |
| 52 | + reject_rust_object["package"][key] = value |
| 53 | + invalid_rust_path = tmp_path / "Cargo.toml" |
| 54 | + with open(invalid_rust_path, "w+") as f: |
| 55 | + dump(reject_rust_object, f) |
| 56 | + |
| 57 | + with pytest.raises(ValueError): |
| 58 | + Rust(invalid_rust_path) |
0 commit comments