aboutsummaryrefslogtreecommitdiff
blob: 23118b361df9041386f120a6d5e266287a3a54f9 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import errno
from importlib import reload
from unittest import mock

import pytest
from snakeoil import __version__, version


class TestVersion:
    def setup_method(self, method):
        # reset the cached version in the module
        reload(version)

    def test_get_version_release(self):
        pass

    def test_get_version_unknown(self):
        with pytest.raises(ValueError):
            version.get_version("snakeoilfoo", __file__)

    def test_get_version_api(self):
        v = version.get_version("snakeoil", __file__, "9.9.9")
        assert v.startswith("snakeoil 9.9.9")

    def test_get_version_git_dev(self):
        with (
            mock.patch("snakeoil.version.import_module") as import_module,
            mock.patch("snakeoil.version.get_git_version") as get_git_version,
        ):
            import_module.side_effect = ImportError
            verinfo = {
                "rev": "1ff76b021d208f7df38ac524537b6419404f1c64",
                "date": "Mon Sep 25 13:50:24 2017 -0400",
                "tag": None,
            }
            get_git_version.return_value = verinfo

            result = version.get_version("snakeoil", __file__, __version__)
            assert (
                result
                == f"snakeoil {__version__}-g{verinfo['rev'][:7]} -- {verinfo['date']}"
            )

    def test_get_version_git_release(self):
        verinfo = {
            "rev": "ab38751890efa8be96b7f95938d6b868b769bab6",
            "date": "Thu Sep 21 15:57:38 2017 -0400",
            "tag": "2.3.4",
        }

        # fake snakeoil._verinfo module object
        class Verinfo:
            version_info = verinfo

        with mock.patch("snakeoil.version.import_module") as import_module:
            import_module.return_value = Verinfo()
            result = version.get_version("snakeoil", __file__, verinfo["tag"])
            assert result == f"snakeoil {verinfo['tag']} -- released {verinfo['date']}"

    def test_get_version_no_git_version(self):
        with (
            mock.patch("snakeoil.version.import_module") as import_module,
            mock.patch("snakeoil.version.get_git_version") as get_git_version,
        ):
            import_module.side_effect = ImportError
            get_git_version.return_value = None
            result = version.get_version("snakeoil", "nonexistent", __version__)
            assert result == f"snakeoil {__version__}"

    def test_get_version_caching(self):
        # retrieved version info is cached in a module attr
        v = version.get_version("snakeoil", __file__)
        assert v.startswith(f"snakeoil {__version__}")

        # re-running get_version returns the cached attr instead of reprocessing
        with mock.patch("snakeoil.version.import_module") as import_module:
            v = version.get_version("snakeoil", __file__)
        assert not import_module.called


class TestGitVersion:
    def test_get_git_version_not_available(self):
        with mock.patch("snakeoil.version._run_git") as run_git:
            run_git.side_effect = EnvironmentError(errno.ENOENT, "git not found")
            assert version.get_git_version("nonexistent") is None

    def test_get_git_version_error(self):
        with mock.patch("snakeoil.version._run_git") as run_git:
            run_git.return_value = (b"foo", 1)
            assert version.get_git_version("nonexistent") is None

    def test_get_git_version_non_repo(self, tmpdir):
        assert version.get_git_version(str(tmpdir)) is None

    def test_get_git_version_exc(self):
        with pytest.raises(OSError):
            with mock.patch("snakeoil.version._run_git") as run_git:
                run_git.side_effect = OSError(errno.EIO, "Input/output error")
                version.get_git_version("nonexistent")

    def test_get_git_version_good_dev(self):
        with mock.patch("snakeoil.version._run_git") as run_git:
            # dev version
            run_git.return_value = (
                b"1ff76b021d208f7df38ac524537b6419404f1c64\nMon Sep 25 13:50:24 2017 -0400",
                0,
            )
            result = version.get_git_version("nonexistent")
            expected = {
                "rev": "1ff76b021d208f7df38ac524537b6419404f1c64",
                "date": "Mon Sep 25 13:50:24 2017 -0400",
                "tag": None,
                "commits": 2,
            }
            assert result == expected

    def test_get_git_version_good_tag(self):
        with (
            mock.patch("snakeoil.version._run_git") as run_git,
            mock.patch("snakeoil.version._get_git_tag") as get_git_tag,
        ):
            # tagged, release version
            run_git.return_value = (
                b"1ff76b021d208f7df38ac524537b6419404f1c64\nMon Sep 25 13:50:24 2017 -0400",
                0,
            )
            get_git_tag.return_value = "1.1.1"
            result = version.get_git_version("nonexistent")
            expected = {
                "rev": "1ff76b021d208f7df38ac524537b6419404f1c64",
                "date": "Mon Sep 25 13:50:24 2017 -0400",
                "tag": "1.1.1",
                "commits": 2,
            }
            assert result == expected

    def test_get_git_tag_bad_output(self):
        with mock.patch("snakeoil.version._run_git") as run_git:
            # unknown git tag rev output
            run_git.return_value = (b"a", 1)
            assert version._get_git_tag("foo", "bar") is None
            run_git.return_value = (b"a foo/v0.7.2", 0)
            assert version._get_git_tag("foo", "bar") is None

            # expected output formats
            run_git.return_value = (
                b"ab38751890efa8be96b7f95938d6b868b769bab6 tags/v1.1.1^0",
                0,
            )
            assert version._get_git_tag("foo", "bar") == "1.1.1"
            run_git.return_value = (
                b"ab38751890efa8be96b7f95938d6b868b769bab6 tags/v1.1.1",
                0,
            )
            assert version._get_git_tag("foo", "bar") == "1.1.1"
            run_git.return_value = (
                b"ab38751890efa8be96b7f95938d6b868b769bab6 tags/1.1.1",
                0,
            )
            assert version._get_git_tag("foo", "bar") == "1.1.1"