-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathtest_reader.py
145 lines (119 loc) · 4.12 KB
/
test_reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import tempfile
from pathlib import Path
from unittest.mock import mock_open, patch
import pytest
from marimo._config.reader import (
find_nearest_pyproject_toml,
read_marimo_config,
read_pyproject_config,
read_toml,
)
def test_read_toml():
toml_content = """
[section]
key = "value"
"""
with patch("builtins.open", mock_open(read_data=toml_content)):
result = read_toml("dummy.toml")
assert result == {"section": {"key": "value"}}
def test_read_marimo_config():
config_content = """
[formatting]
line_length = 79
[save]
autosave_delay = 1000
format_on_save = true
autosave = "after_delay"
[novalidate]
old_keys_are_ok = true
"""
with patch("builtins.open", mock_open(read_data=config_content)):
result = read_marimo_config("marimo.toml")
assert result == {
"formatting": {"line_length": 79},
"save": {
"autosave_delay": 1000,
"format_on_save": True,
"autosave": "after_delay",
},
"novalidate": {"old_keys_are_ok": True},
}
def test_read_pyproject_config_with_marimo_section():
pyproject_content = """
[tool.marimo]
formatting = {line_length = 79}
[tool.marimo.save]
autosave_delay = 1000
format_on_save = true
autosave = "after_delay"
[tool.marimo.novalidate]
old_keys_are_ok = true
"""
with patch("builtins.open", mock_open(read_data=pyproject_content)):
with patch(
"marimo._config.reader.find_nearest_pyproject_toml"
) as mock_find:
mock_find.return_value = Path("/some/path/pyproject.toml")
result = read_pyproject_config("/some/path")
assert result == {
"formatting": {"line_length": 79},
"save": {
"autosave_delay": 1000,
"format_on_save": True,
"autosave": "after_delay",
},
"novalidate": {"old_keys_are_ok": True},
}
def test_read_pyproject_config_without_marimo_section():
pyproject_content = """
[tool.idk]
name = "foo"
"""
with patch("builtins.open", mock_open(read_data=pyproject_content)):
with patch(
"marimo._config.reader.find_nearest_pyproject_toml"
) as mock_find:
mock_find.return_value = Path("/some/path/pyproject.toml")
result = read_pyproject_config("/some/path")
assert result is None
def test_read_pyproject_config_invalid_marimo_section():
pyproject_content = """
[tool]
marimo = "invalid"
"""
with patch("builtins.open", mock_open(read_data=pyproject_content)):
with patch(
"marimo._config.reader.find_nearest_pyproject_toml"
) as mock_find:
mock_find.return_value = Path("/some/path/pyproject.toml")
result = read_pyproject_config("/some/path")
assert result is None
def test_read_pyproject_config_no_file():
with tempfile.TemporaryDirectory() as temp_dir:
result = read_pyproject_config(temp_dir)
assert result is None
def testfind_nearest_pyproject_toml():
with tempfile.TemporaryDirectory() as temp_dir:
parent_dir = os.path.join(temp_dir, "parent")
os.makedirs(parent_dir)
pyproject_path = os.path.join(parent_dir, "pyproject.toml")
with open(pyproject_path, "w") as f:
f.write("")
start_path = os.path.join(parent_dir, "child")
os.makedirs(start_path)
result = find_nearest_pyproject_toml(start_path)
assert result == Path(pyproject_path)
def testfind_nearest_pyproject_toml_not_found():
with tempfile.TemporaryDirectory() as temp_dir:
result = find_nearest_pyproject_toml(temp_dir)
assert result is None
def test_read_toml_invalid_content():
invalid_toml = """
[invalid
key = value
"""
import tomlkit.exceptions
with patch("builtins.open", mock_open(read_data=invalid_toml)):
with pytest.raises(tomlkit.exceptions.UnexpectedCharError):
read_toml("dummy.toml")