Skip to content

Commit acb5ae9

Browse files
committed
add rust input validation check
1 parent c9f44f3 commit acb5ae9

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from somesy.fortran import Fortran
1414
from somesy.pom_xml.writer import POM
1515
from somesy.mkdocs import MkDocs
16+
from somesy.rust import Rust
1617

1718
TEST_DIR = Path(__file__).resolve().parent
1819

@@ -30,6 +31,7 @@ class FileTypes(Enum):
3031
FORTRAN = "fortran"
3132
POM_XML = "pom_xml"
3233
MKDOCS = "mkdocs"
34+
RUST = "rust"
3335

3436

3537
@pytest.fixture(scope="session", autouse=True)
@@ -89,6 +91,8 @@ def _create_files(files: Set[Tuple[FileTypes, str]]):
8991
read_file_name = read_file_path / Path("pom.xml")
9092
elif file_type == FileTypes.MKDOCS:
9193
read_file_name = read_file_path / Path("mkdocs.yml")
94+
elif file_type == FileTypes.RUST:
95+
read_file_name = read_file_path / Path("Cargo.toml")
9296

9397
with open(read_file_name, "r") as f:
9498
content = f.read()
@@ -146,6 +150,9 @@ def _load_files(files: Set[FileTypes]):
146150
elif file_type == FileTypes.MKDOCS:
147151
read_file_name = read_file_name / Path("mkdocs.yml")
148152
file_instances[file_type] = MkDocs(read_file_name)
153+
elif file_type == FileTypes.RUST:
154+
read_file_name = read_file_name / Path("Cargo.toml")
155+
file_instances[file_type] = Rust(read_file_name)
149156

150157
return file_instances
151158

tests/input/test_rust_validate.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)