Viewing files at commit 5717cc9fc7edCommit detailsView latest

Browse files
tests/test_configio.py 49 lines · 1.5 KB · python
1
from __future__ import annotations
2
3
import sys
4
import tempfile
5
import unittest
6
from pathlib import Path
7
from unittest.mock import patch
8
9
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
10
11
from oakbench import configio
12
13
14
class TomlFallbackTest(unittest.TestCase):
15
    def test_fallback_parser_preserves_hash_inside_quoted_strings(self) -> None:
16
        with tempfile.TemporaryDirectory() as tmp:
17
            path = Path(tmp) / "subjects.toml"
18
            path.write_text(
19
                """
20
[subjects.oak]
21
label = "Oak # release"
22
enabled = true # ordinary comment
23
args = ["one#two", "three"] # ordinary comment
24
"""
25
            )
26
            with patch.object(configio, "tomllib", None):
27
                data = configio.load_toml(path)
28
29
        self.assertEqual(data["subjects"]["oak"]["label"], "Oak # release")
30
        self.assertEqual(data["subjects"]["oak"]["args"], ["one#two", "three"])
31
        self.assertTrue(data["subjects"]["oak"]["enabled"])
32
33
    def test_fallback_parser_treats_backslash_as_literal_in_single_quoted_strings(self) -> None:
34
        with tempfile.TemporaryDirectory() as tmp:
35
            path = Path(tmp) / "subjects.toml"
36
            path.write_text(
37
                """
38
[subjects.oak]
39
label = 'Oak \\# release' # ordinary comment
40
"""
41
            )
42
            with patch.object(configio, "tomllib", None):
43
                data = configio.load_toml(path)
44
45
        self.assertEqual(data["subjects"]["oak"]["label"], "Oak \\# release")
46
47
48
if __name__ == "__main__":
49
    unittest.main()