|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | + |
| 4 | +from cfengine_cli.lint_yml import check_yml_file |
| 5 | + |
| 6 | + |
| 7 | +def _write_temp_yml(content: bytes) -> str: |
| 8 | + fd, path = tempfile.mkstemp(suffix=".yml") |
| 9 | + with os.fdopen(fd, "wb") as f: |
| 10 | + f.write(content) |
| 11 | + return path |
| 12 | + |
| 13 | + |
| 14 | +VALID = [ |
| 15 | + ("simple_mapping", b"key: value\n"), |
| 16 | + ("nested_mapping", b"top:\n nested: value\n list:\n - 1\n - 2\n"), |
| 17 | + ("flow_sequence", b"items: [1, 2, 3]\n"), |
| 18 | + ("flow_mapping", b"obj: {a: 1, b: 2}\n"), |
| 19 | + ("multi_document", b"---\na: 1\n---\nb: 2\n"), |
| 20 | + ("scalar_only", b"42\n"), |
| 21 | + ("string_only", b'"hello"\n'), |
| 22 | + ("list_only", b"- one\n- two\n"), |
| 23 | + ("multi_doc_first_null", b"---\n---\nname: cfengine\n"), |
| 24 | + ("only_null_document", b"---\n"), |
| 25 | + ("multiple_null_documents", b"---\n---\n---\n"), |
| 26 | +] |
| 27 | + |
| 28 | +INVALID = [ |
| 29 | + ("empty_file", b""), |
| 30 | + ("only_whitespace", b" \n\n"), |
| 31 | + ("unclosed_flow_sequence", b"items: [1, 2, 3\n"), |
| 32 | + ("unclosed_flow_mapping", b"obj: {a: 1, b: 2\n"), |
| 33 | + ("bad_indentation", b"top:\n nested: value\n bad: value\n"), |
| 34 | + ("tab_indentation", b"top:\n\tnested: value\n"), |
| 35 | + ("duplicate_key_via_alias", b"a: &x 1\nb: *y\n"), |
| 36 | +] |
| 37 | + |
| 38 | + |
| 39 | +def test_check_yml_file_accepts_valid(): |
| 40 | + for name, content in VALID: |
| 41 | + path = _write_temp_yml(content) |
| 42 | + try: |
| 43 | + assert check_yml_file(path) is None, f"Expected valid: {name}" |
| 44 | + finally: |
| 45 | + os.unlink(path) |
| 46 | + |
| 47 | + |
| 48 | +def test_check_yml_file_rejects_invalid(): |
| 49 | + for name, content in INVALID: |
| 50 | + path = _write_temp_yml(content) |
| 51 | + try: |
| 52 | + assert check_yml_file(path) is not None, f"Expected invalid: {name}" |
| 53 | + finally: |
| 54 | + os.unlink(path) |
0 commit comments