1
|
|
|
from pathlib import Path |
2
|
|
|
|
3
|
|
|
import pytest |
4
|
|
|
|
5
|
|
|
# noinspection PyProtectedMember |
6
|
|
|
from tyrannosaurus.context import Context, Source, TyrannoInfo |
7
|
|
|
from tyrannosaurus.enums import TomlBuilder |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class TestContext: |
11
|
|
|
def test_toml(self): |
12
|
|
|
toml = ( |
13
|
|
|
TomlBuilder() |
14
|
|
|
.add("simple", "is simple") |
15
|
|
|
.add("tool.poetry.name", "project") |
16
|
|
|
.add("tool.poetry.version", "version 1") |
17
|
|
|
.build() |
18
|
|
|
) |
19
|
|
|
assert "simple" in toml.x |
20
|
|
|
assert toml["simple"] == "is simple" |
21
|
|
|
assert "tool" in toml |
22
|
|
|
assert "poetry" in toml["tool"] |
23
|
|
|
assert "version" in toml["tool"]["poetry"] |
24
|
|
|
assert toml["tool.poetry.name"] == "project" |
25
|
|
|
assert toml["tool.poetry.version"] == "version 1" |
26
|
|
|
|
27
|
|
|
def test_source(self): |
28
|
|
|
toml = ( |
29
|
|
|
TomlBuilder() |
30
|
|
|
.add("simple", "is simple") |
31
|
|
|
.add("tool.poetry.name", "project") |
32
|
|
|
.add("tool.poetry.version", "version 1") |
33
|
|
|
.add("tool.poetry.authors", ["auth", "ors"]) |
34
|
|
|
.add("tool.poetry.description", "A description") |
35
|
|
|
.add("tool.poetry.license", "MIT") |
36
|
|
|
.add("tool.poetry.keywords", ["key", "words"]) |
37
|
|
|
.build() |
38
|
|
|
) |
39
|
|
|
source = Source().parse("'a value'", toml) |
40
|
|
|
assert source == "a value" |
41
|
|
|
source = Source().parse("tool.poetry.name", toml) |
42
|
|
|
assert source == "project" |
43
|
|
|
|
44
|
|
|
def test_context(self): |
45
|
|
|
# TODO: This weird test operates on tyrannosaurus itself |
46
|
|
|
root = Path(__file__).parent.parent.resolve() |
47
|
|
|
context = Context(root) |
48
|
|
|
assert context.path == root |
49
|
|
|
assert context.has_opt("align") |
50
|
|
|
assert context.has_target("init") |
51
|
|
|
assert context.source("linelength") == "100" |
52
|
|
|
assert str(context.get_bak_path("pyproject.toml")) == str( |
53
|
|
|
Path(root / ".tyrannosaurus" / f"pyproject.toml.{TyrannoInfo.timestamp}.bak") |
54
|
|
|
) |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
if __name__ == "__main__": |
58
|
|
|
pytest.main() |
59
|
|
|
|