forked from python-poetry/tomlkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_toml_file.py
107 lines (78 loc) · 2.74 KB
/
test_toml_file.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
import os
from tomlkit.toml_document import TOMLDocument
from tomlkit.toml_file import TOMLFile
def test_toml_file(example):
original_content = example("example")
toml_file = os.path.join(os.path.dirname(__file__), "examples", "example.toml")
toml = TOMLFile(toml_file)
content = toml.read()
assert isinstance(content, TOMLDocument)
assert content["owner"]["organization"] == "GitHub"
toml.write(content)
try:
with open(toml_file, encoding="utf-8") as f:
assert original_content == f.read()
finally:
with open(toml_file, "w", encoding="utf-8", newline="") as f:
assert f.write(original_content)
def test_keep_old_eol(tmpdir):
toml_path = str(tmpdir / "pyproject.toml")
with open(toml_path, "wb+") as f:
f.write(b"a = 1\r\nb = 2\r\n")
f = TOMLFile(toml_path)
content = f.read()
content["b"] = 3
f.write(content)
with open(toml_path, "rb") as f:
assert f.read() == b"a = 1\r\nb = 3\r\n"
def test_keep_old_eol_2(tmpdir):
toml_path = str(tmpdir / "pyproject.toml")
with open(toml_path, "wb+") as f:
f.write(b"a = 1\nb = 2\n")
f = TOMLFile(toml_path)
content = f.read()
content["b"] = 3
f.write(content)
with open(toml_path, "rb") as f:
assert f.read() == b"a = 1\nb = 3\n"
def test_mixed_eol(tmpdir):
toml_path = str(tmpdir / "pyproject.toml")
with open(toml_path, "wb+") as f:
f.write(b"a = 1\r\nrb = 2\n")
f = TOMLFile(toml_path)
f.write(f.read())
with open(toml_path, "rb") as f:
assert f.read() == b"a = 1\r\nrb = 2\n"
def test_consistent_eol(tmpdir):
toml_path = str(tmpdir / "pyproject.toml")
with open(toml_path, "wb+") as f:
f.write(b"a = 1\r\nb = 2\r\n")
f = TOMLFile(toml_path)
content = f.read()
content["c"] = 3
f.write(content)
with open(toml_path, "rb") as f:
assert f.read() == b"a = 1\r\nb = 2\r\nc = 3\r\n"
def test_consistent_eol_2(tmpdir):
toml_path = str(tmpdir / "pyproject.toml")
with open(toml_path, "wb+") as f:
f.write(b"a = 1\nb = 2\n")
f = TOMLFile(toml_path)
content = f.read()
content["c"] = 3
content["c"].trivia.trail = "\r\n"
f.write(content)
with open(toml_path, "rb") as f:
assert f.read() == b"a = 1\nb = 2\nc = 3\n"
def test_default_eol_is_os_linesep(tmpdir):
toml_path = str(tmpdir / "pyproject.toml")
f = TOMLFile(toml_path)
content = TOMLDocument()
content.append("a", 1)
content["a"].trivia.trail = "\n"
content.append("b", 2)
content["b"].trivia.trail = "\r\n"
f.write(content)
linesep = os.linesep.encode()
with open(toml_path, "rb") as f:
assert f.read() == b"a = 1" + linesep + b"b = 2" + linesep