1# -*- coding: utf-8 -*-
2from __future__ import print_function
3
4import pytest
5
6from plumbum import local
7from plumbum.cli import Config, ConfigINI
8
9fname = "test_config.ini"
10
11
12@pytest.mark.usefixtures("cleandir")
13class TestConfig:
14    def test_makefile(self):
15        with ConfigINI(fname) as conf:
16            conf["value"] = 12
17            conf["string"] = "ho"
18
19        with open(fname) as f:
20            contents = f.read()
21
22        assert "value = 12" in contents
23        assert "string = ho" in contents
24
25    def test_readfile(self):
26        with open(fname, "w") as f:
27            print(
28                """
29[DEFAULT]
30one = 1
31two = hello""",
32                file=f,
33            )
34
35        with ConfigINI(fname) as conf:
36            assert conf["one"] == "1"
37            assert conf["two"] == "hello"
38
39    def test_complex_ini(self):
40        with Config(fname) as conf:
41            conf["value"] = "normal"
42            conf["newer.value"] = "other"
43
44        with Config(fname) as conf:
45            assert conf["value"] == "normal"
46            assert conf["DEFAULT.value"] == "normal"
47            assert conf["newer.value"] == "other"
48
49    def test_nowith(self):
50        conf = ConfigINI(fname)
51        conf["something"] = "nothing"
52        conf.write()
53
54        with open(fname) as f:
55            contents = f.read()
56
57        assert "something = nothing" in contents
58
59    def test_home(self):
60        mypath = local.env.home / "some_simple_home_rc.ini"
61        assert not mypath.exists()
62        try:
63            with Config("~/some_simple_home_rc.ini") as conf:
64                conf["a"] = "b"
65            assert mypath.exists()
66            mypath.unlink()
67
68            with Config(mypath) as conf:
69                conf["a"] = "b"
70            assert mypath.exists()
71            mypath.unlink()
72
73        finally:
74            mypath.unlink()
75
76    def test_notouch(self):
77        conf = ConfigINI(fname)
78        assert not local.path(fname).exists()
79
80    def test_only_string(self):
81        conf = ConfigINI(fname)
82        value = conf.get("value", 2)
83        assert value == "2"
84