-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
test_utils.py
154 lines (115 loc) · 4.46 KB
/
test_utils.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
#!/usr/bin/python
# Copyright (C) 2017-2024 Vanessa Sochat.
# This Source Code Form is subject to the terms of the
# Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import pytest
from spython.utils import ScopedEnvVar
def test_write_read_files(tmp_path):
"""
test_write_read_files will test the functions write_file and read_file
"""
print("Testing utils.write_file...")
from spython.utils import write_file
tmpfile = str(tmp_path / "written_file.txt")
assert not os.path.exists(tmpfile)
write_file(tmpfile, "hello!")
assert os.path.exists(tmpfile)
print("Testing utils.read_file...")
from spython.utils import read_file
content = read_file(tmpfile)[0]
assert content == "hello!"
def test_write_bad_json(tmp_path):
from spython.utils import write_json
bad_json = {"Wakkawakkawakka'}": [{True}, "2", 3]}
tmpfile = str(tmp_path / "json_file.txt")
assert not os.path.exists(tmpfile)
with pytest.raises(TypeError):
write_json(bad_json, tmpfile)
def test_write_json(tmp_path):
import json
from spython.utils import write_json
good_json = {"Wakkawakkawakka": [True, "2", 3]}
tmpfile = str(tmp_path / "good_json_file.txt")
assert not os.path.exists(tmpfile)
write_json(good_json, tmpfile)
with open(tmpfile, "r") as f:
content = json.loads(f.read())
assert isinstance(content, dict)
assert "Wakkawakkawakka" in content
def test_check_install():
"""check install is used to check if a particular software is installed.
If no command is provided, singularity is assumed to be the test case"""
print("Testing utils.check_install")
from spython.utils import check_install
is_installed = check_install()
assert is_installed
is_not_installed = check_install("fakesoftwarename")
assert not is_not_installed
def test_check_get_singularity_version():
"""check that the singularity version is found to be that installed"""
from spython.utils import get_singularity_version
version = get_singularity_version()
assert version != ""
with ScopedEnvVar("SPYTHON_SINGULARITY_VERSION", "3.0"):
version = get_singularity_version()
assert version == "3.0"
def test_get_installdir():
"""get install directory should return the base of where singularity
is installed
"""
print("Testing utils.get_installdir")
from spython.utils import get_installdir
whereami = get_installdir()
print(whereami)
assert whereami.endswith("spython")
def test_split_uri():
from spython.utils import split_uri
protocol, image = split_uri("docker://ubuntu")
assert protocol == "docker"
assert image == "ubuntu"
protocol, image = split_uri("http://image/path/with/slash/")
assert protocol == "http"
assert image == "image/path/with/slash"
protocol, image = split_uri("no/proto/")
assert protocol == ""
assert image == "no/proto"
def test_remove_uri():
print("Testing utils.remove_uri")
from spython.utils import remove_uri
assert remove_uri("docker://ubuntu") == "ubuntu"
assert (
remove_uri("shub://vanessa/singularity-images") == "vanessa/singularity-images"
)
assert remove_uri("library://library/default/alpine") == "library/default/alpine"
assert remove_uri("vanessa/singularity-images") == "vanessa/singularity-images"
def test_decode():
from spython.logger import decodeUtf8String
out = decodeUtf8String(str("Hello"))
assert isinstance(out, str)
assert out == "Hello"
out = decodeUtf8String(bytes(b"Hello"))
assert isinstance(out, str)
assert out == "Hello"
def test_ScopedEnvVar():
assert "FOO" not in os.environ
with ScopedEnvVar("FOO", "bar") as e:
assert e.name == "FOO"
assert e.value == "bar"
assert os.environ["FOO"] == "bar"
with ScopedEnvVar("FOO", "baz"):
assert os.environ["FOO"] == "baz"
assert os.environ["FOO"] == "bar"
# None removes it
with ScopedEnvVar("FOO", None):
assert "FOO" not in os.environ
# But empty string is allowed
with ScopedEnvVar("FOO", ""):
assert os.environ["FOO"] == ""
assert os.environ["FOO"] == "bar"
assert "FOO" not in os.environ
# Unset a non-existing variable
with ScopedEnvVar("FOO", None):
assert "FOO" not in os.environ
assert "FOO" not in os.environ