File: test_cli.py

package info (click to toggle)
python-kajiki 0.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 692 kB
  • sloc: python: 4,145; makefile: 115
file content (139 lines) | stat: -rw-r--r-- 4,190 bytes parent folder | download | duplicates (2)
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
import unittest.mock as mock

import pytest

import kajiki.loader
from kajiki.__main__ import main


class MainMocks:
    def __init__(self, monkeypatch):
        mocked_render = mock.Mock(return_value="render result")
        self.render = mocked_render

        class MockedTemplate(object):
            def render(self, *args, **kwargs):
                return mocked_render(*args, **kwargs)

        self.template_type = mock.Mock(return_value=MockedTemplate())

        mocked_import = mock.Mock(return_value=self.template_type)
        self.import_ = mocked_import

        class MockedLoader(object):
            def import_(self, *args, **kwargs):
                return mocked_import(*args, **kwargs)

        self.file_loader_type = mock.Mock(return_value=MockedLoader())
        monkeypatch.setattr(kajiki.loader, "FileLoader", self.file_loader_type)

        self.package_loader_type = mock.Mock(return_value=MockedLoader())
        monkeypatch.setattr(kajiki.loader, "PackageLoader", self.package_loader_type)


@pytest.fixture
def main_mocks(monkeypatch):
    return MainMocks(monkeypatch)


@pytest.mark.parametrize(
    ["filename", "load_path"],
    [
        ("filename.txt", "."),
        ("/path/to/filename.xml", "/path/to"),
        ("some/subdir/myfile.html", "some/subdir"),
    ],
)
def test_simple_file_load(filename, load_path, capsys, main_mocks):
    main([filename])

    main_mocks.file_loader_type.assert_called_once_with(
        path=[load_path], force_mode=None
    )
    main_mocks.import_.assert_called_once_with(filename)
    main_mocks.template_type.assert_called_once_with({})
    main_mocks.render.assert_called_once_with()

    assert capsys.readouterr().out == "render result"


def test_simple_package_load(capsys, main_mocks):
    main(["-p", "my.cool.package"])

    main_mocks.package_loader_type.assert_called_once_with(force_mode=None)
    main_mocks.import_.assert_called_once_with("my.cool.package")
    main_mocks.template_type.assert_called_once_with({})
    main_mocks.render.assert_called_once_with()

    assert capsys.readouterr().out == "render result"


@mock.patch("site.addsitedir", autospec=True)
def test_package_loader_site_dirs(addsitedir, capsys, main_mocks):
    main(
        [
            "-i",
            "/usr/share/my-python-site",
            "-i",
            "relative/site/path",
            "-i",
            "another",
            "-p",
            "my.cool.package",
        ]
    )

    addsitedir.assert_has_calls(
        [
            mock.call("/usr/share/my-python-site"),
            mock.call("relative/site/path"),
            mock.call("another"),
        ]
    )

    main_mocks.package_loader_type.assert_called_once_with(force_mode=None)
    main_mocks.import_.assert_called_once_with("my.cool.package")
    main_mocks.template_type.assert_called_once_with({})
    main_mocks.render.assert_called_once_with()

    assert capsys.readouterr().out == "render result"


def test_output_to_file(tmpdir, main_mocks):
    outfile = str(tmpdir / "output_file.txt")
    main(["infile.txt", outfile])

    main_mocks.file_loader_type.assert_called_once_with(path=["."], force_mode=None)
    main_mocks.import_.assert_called_once_with("infile.txt")
    main_mocks.template_type.assert_called_once_with({})
    main_mocks.render.assert_called_once_with()

    with open(outfile, "r") as f:
        assert f.read() == "render result"


def test_template_variables(main_mocks):
    main(["-v", "foo=bar", "-v", "baz=bip", "infile.txt"])

    main_mocks.file_loader_type.assert_called_once_with(path=["."], force_mode=None)
    main_mocks.import_.assert_called_once_with("infile.txt")
    main_mocks.template_type.assert_called_once_with(
        {
            "foo": "bar",
            "baz": "bip",
        }
    )
    main_mocks.render.assert_called_once_with()


def test_template_variables_bad(capsys):
    with pytest.raises(SystemExit) as e:
        main(["-v", "BADBADBAD", "infile.txt"])

    assert e.value.code != 0

    captured = capsys.readouterr()
    assert captured.out == ""
    assert captured.err.endswith(
        "error: argument -v/--var: Expected a KEY=VALUE pair, got BADBADBAD\n"
    )