-
Notifications
You must be signed in to change notification settings - Fork 247
/
test_libraries.py
216 lines (157 loc) · 5.19 KB
/
test_libraries.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Copyright FuseSoC contributors
# Licensed under the 2-Clause BSD License, see LICENSE for details.
# SPDX-License-Identifier: BSD-2-Clause
import logging
import os.path
import shutil
import subprocess
import tempfile
from argparse import Namespace
from test_common import cache_root, cores_root, library_root
from fusesoc.config import Config
from fusesoc.fusesoc import Fusesoc
from fusesoc.librarymanager import Library
build_root = "test_build_root"
EXAMPLE_CONFIG = """
[main]
build_root = {build_root}
cache_root = {cache_root}
library_root = {library_root}
[library.test_lib]
location = {cores_root}
auto-sync = {auto_sync}
sync-uri = {sync_uri}
sync-type = {sync_type}
"""
sync_uri = "https://github.com/fusesoc/fusesoc-cores"
def test_library_location():
with tempfile.NamedTemporaryFile(mode="w+") as tcf:
tcf.write(
EXAMPLE_CONFIG.format(
build_root=build_root,
cache_root=cache_root,
cores_root=cores_root,
library_root=library_root,
auto_sync="false",
sync_uri=sync_uri,
sync_type="git",
)
)
tcf.flush()
conf = Config(tcf.name)
fs = Fusesoc(conf)
fs.get_core("mor1kx-generic")
fs.get_core("atlys")
def test_library_add(caplog):
import tempfile
from fusesoc.coremanager import CoreManager
from fusesoc.librarymanager import LibraryManager
from fusesoc.main import add_library
with tempfile.TemporaryDirectory() as td:
clone_target = os.path.join(td, "clone-target")
library_root = os.path.join(td, "library-root")
conf_path = os.path.join(td, "fusesoc.conf")
conf = Config(conf_path)
conf.library_root = library_root
cm = CoreManager(conf)
args = Namespace()
args.name = "fusesoc-cores"
args.location = clone_target
args.config = conf_path
args.no_auto_sync = False
vars(args)["sync-uri"] = sync_uri
add_library(cm, args)
expected = """[library.fusesoc-cores]
location = {}
sync-uri = https://github.com/fusesoc/fusesoc-cores
sync-type = git
auto-sync = true""".format(
os.path.abspath(clone_target)
)
with open(conf_path) as tcf:
result = tcf.read().strip()
assert expected == result
with tempfile.NamedTemporaryFile(mode="w+") as tcf:
args.config = tcf.name
args.location = None
vars(args)["sync-type"] = "git"
expected = """[library.fusesoc-cores]
location = fusesoc_libraries/fusesoc-cores
sync-uri = https://github.com/fusesoc/fusesoc-cores
sync-type = git
auto-sync = true"""
add_library(cm, args)
tcf.seek(0)
result = tcf.read().strip()
assert expected == result
shutil.rmtree("fusesoc_libraries")
with tempfile.NamedTemporaryFile() as tcf:
args.config = tcf.name
vars(args)["sync-type"] = "local"
vars(args)["sync-uri"] = "tests/capi2_cores"
args.location = None
with caplog.at_level(logging.INFO):
add_library(cm, args)
assert (
"Interpreting sync-uri 'tests/capi2_cores' as location for local provider."
in caplog.text
)
tcf = tempfile.NamedTemporaryFile(mode="w+")
args.config = tcf.name
vars(args)["sync-type"] = "git"
vars(args)["sync-uri"] = sync_uri
vars(args)["sync-version"] = "capi2"
args.location = None
expected = """[library.fusesoc-cores]
location = fusesoc_libraries/fusesoc-cores
sync-uri = https://github.com/fusesoc/fusesoc-cores
sync-version = capi2
sync-type = git
auto-sync = true""".format(
cm._lm.library_root
)
add_library(cm, args)
tcf.seek(0)
result = tcf.read().strip()
assert expected == result
shutil.rmtree("fusesoc_libraries")
tcf.close()
def test_library_update(caplog):
clone_target = tempfile.mkdtemp()
subprocess.call(["git", "clone", sync_uri, clone_target])
with tempfile.NamedTemporaryFile(mode="w+") as tcf:
tcf.write(
EXAMPLE_CONFIG.format(
build_root=build_root,
cache_root=cache_root,
cores_root=clone_target,
library_root=library_root,
auto_sync="false",
sync_uri=sync_uri,
sync_type="git",
)
)
tcf.flush()
conf = Config(tcf.name)
args = Namespace()
Fusesoc.init_logging(False, False)
fs = Fusesoc(conf)
with caplog.at_level(logging.INFO):
fs.update_libraries([])
assert "test_lib : auto-sync disabled. Ignoring update" in caplog.text
caplog.clear()
with caplog.at_level(logging.INFO):
fs.update_libraries(["test_lib"])
assert "test_lib : Updating..." in caplog.text
caplog.clear()
_library = fs.get_library("test_lib")
_library.auto_sync = True
with caplog.at_level(logging.INFO):
fs.update_libraries([])
assert "test_lib : Updating..." in caplog.text
caplog.clear()
tcf.close()
_library.sync_type = "local"
with caplog.at_level(logging.INFO):
fs.update_libraries([])
assert "test_lib : sync-type is local. Ignoring update" in caplog.text