-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunit_tests.py
172 lines (136 loc) · 5.52 KB
/
unit_tests.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
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
161
162
163
164
165
166
167
168
169
170
171
172
import unittest
import unittest.mock as mock
import io
import os
import subprocess
import textwrap
import texasbbq
class TestEcho(unittest.TestCase):
def test_echo(self):
with mock.patch('sys.stdout', new=io.StringIO()) as fake_out:
texasbbq.echo("test")
self.assertEqual("::>> test\n", fake_out.getvalue())
class TestExecute(unittest.TestCase):
def test_execute_true(self):
# shoud be a no-op
texasbbq.execute("true")
def test_execute_false(self):
with self.assertRaises(subprocess.CalledProcessError) as e:
texasbbq.execute("false")
self.assertIn(
"Command '['false']' returned non-zero exit status 1.",
str(e)
)
def test_execute_capture(self):
result = texasbbq.execute("echo -n 'test'", capture=True)
self.assertEqual(b"test", result)
class TestMinicondaURL(unittest.TestCase):
@mock.patch("texasbbq.UNAME", "Linux")
def test_linux(self):
result = texasbbq.miniconda_url()
self.assertEqual(
"https://repo.continuum.io/miniconda/"
"Miniconda3-latest-Linux-x86_64.sh",
result
)
@mock.patch("texasbbq.UNAME", "Darwin")
def test_osx(self):
result = texasbbq.miniconda_url()
self.assertEqual(
"https://repo.continuum.io/miniconda/"
"Miniconda3-latest-MacOSX-x86_64.sh",
result
)
@mock.patch("texasbbq.UNAME", "GARBAGE")
def test_unsupported(self):
with self.assertRaises(ValueError) as e:
texasbbq.miniconda_url()
self.assertIn(
"Unsupported OS",
str(e)
)
class TestWGETConda(unittest.TestCase):
@mock.patch("texasbbq.execute")
def test_wget_conda(self, mock_execute):
texasbbq.wget_conda("test_url", "test_output")
mock_execute.assert_called_once_with("wget test_url -O test_output")
class TestInjectCondaPath(unittest.TestCase):
@mock.patch("texasbbq.MINCONDA_BIN_PATH", "/test/miniconda3/bin")
@mock.patch("texasbbq.MINCONDA_CONDABIN_PATH", "/test/miniconda3/condabin")
@mock.patch("os.environ", {"PATH": ""})
def test_inject_conda_path(self):
texasbbq.inject_conda_path()
self.assertEqual(
"/test/miniconda3/bin:"
"/test/miniconda3/condabin:",
os.environ["PATH"]
)
class TestGit(unittest.TestCase):
@mock.patch("texasbbq.execute")
def test_git_clone_ref(self, mock_execute):
texasbbq.git_clone_ref("test_url", "test_ref", "test_dir")
mock_execute.assert_called_once_with(
"git clone -b test_ref test_url --depth=1 test_dir")
@mock.patch("texasbbq.execute")
def test_git_ls_remote_tags(self, mock_execute):
mock_execute.return_value = (
b"ffffffffffffffffffffffffffffffffffffffff refs/tags/0.1.0\n"
b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa refs/tags/0.2.0\n"
)
result = texasbbq.git_ls_remote_tags("test_url")
self.assertEqual(["0.1.0", "0.2.0"], result)
mock_execute.assert_called_once_with(
"git ls-remote --tags --refs test_url", capture=True)
class TestGitLatestTag(unittest.TestCase):
@mock.patch("texasbbq.git_ls_remote_tags")
def test_git_latest_tag(self, mock_ls_remote_tags):
mock_ls_remote_tags.return_value = (
["v.1.0", "0.10.0", "v0.11.0"]
)
result = texasbbq.git_latest_tag("MOCK_URL", vprefix=False)
self.assertEqual("0.11.0", result)
result = texasbbq.git_latest_tag("MOCK_URL", vprefix=True)
self.assertEqual("v0.11.0", result)
result = texasbbq.git_latest_tag(
"MOCK_URL", vprefix=False,
exclude_filter=lambda x: x == "v0.11.0")
self.assertEqual("0.10.0", result)
class TestConda(unittest.TestCase):
@mock.patch("texasbbq.execute")
def test_conda_update_conda(self, mock_execute):
texasbbq.conda_update_conda()
mock_execute.assert_has_calls(
[mock.call("conda update -y -n base -c defaults conda")])
@mock.patch("texasbbq.execute")
def test_conda_environments(self, mock_execute):
mock_execute.return_value = textwrap.dedent("""
{
"envs": [
"/test/miniconda3",
"/test/miniconda3/envs/numba",
"/test/miniconda3/envs/texasbbq"
]
}""")
result = texasbbq.conda_environments()
expected = {
"miniconda3": "/test/miniconda3",
"numba": "/test/miniconda3/envs/numba",
"texasbbq": "/test/miniconda3/envs/texasbbq",
}
self.assertEqual(expected, result)
@mock.patch("texasbbq.execute")
def test_conda_create_env(self, mock_execute):
texasbbq.conda_create_env("test_env")
mock_execute.assert_called_once_with(
"conda create -y -n test_env")
@mock.patch("texasbbq.execute")
def test_conda_install(self, mock_execute):
texasbbq.conda_install("test_env", "test_package")
mock_execute.assert_called_once_with(
"conda install -y -n test_env test_package")
class TestPip(unittest.TestCase):
@mock.patch("texasbbq.execute")
def test_pip_install(self, mock_execute):
texasbbq.pip_install("test_env", ["dokuwiki", "dot2tex sphinxcontrib-tikz"])
mock_execute.assert_called_once_with(
"conda run --no-capture-output -n test_env pip install ['dokuwiki', 'dot2tex sphinxcontrib-tikz']")