Test Failed
Pull Request — master (#2)
by Heiko 'riot'
06:45
created

tests.a_unit.test_builder   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 117
dl 0
loc 210
rs 10
c 0
b 0
f 0
wmc 16

10 Functions

Rating   Name   Duplication   Size   Complexity  
A test_copy_directory_tree() 0 11 2
A test_write_main() 0 24 4
A test_move_directory_tree() 0 21 2
A test_get_components() 0 12 1
A test_get_frontend_locations_development() 0 9 1
A test_update_frontends() 0 17 1
A test_get_sails_dependencies() 0 6 1
A test_copy_resource_tree() 0 14 1
A test_generate_component_folders() 0 17 2
A test_get_frontend_locations() 0 7 1
1
#!/usr/bin/env python
2
# -*- coding: UTF-8 -*-
3
4
# Isomer - The distributed application framework
5
# ==============================================
6
# Copyright (C) 2011-2020 Heiko 'riot' Weinen <[email protected]> and others.
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU Affero General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
# GNU Affero General Public License for more details.
17
#
18
# You should have received a copy of the GNU Affero General Public License
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21
"""
22
Isomer - Backend
23
24
Test Isomer Frontend Builder
25
============================
26
27
28
29
"""
30
31
import os
32
import shutil
33
from pathlib import Path
34
35
import pytest
36
from isomer.misc.path import set_instance
37
from isomer.ui.builder import copy_directory_tree, copy_resource_tree, \
38
    get_frontend_locations, generate_component_folders, get_components, \
39
    update_frontends, get_sails_dependencies, write_main
40
41
try:
42
    import isomer.test as test
43
except ImportError:
44
    test = None
45
46
has_test_module = pytest.mark.skipif(
47
    test is None,
48
    reason="isomer-test-module not installed. See "
49
           "https://isomer.readthedocs.io/en/latest/dev/system/backend/modularity.html"
50
           "#modules"
51
)
52
53
54
def test_copy_directory_tree():
55
    os.makedirs("/tmp/isomer-test/copy_directory_tree/foo/bar", exist_ok=True)
56
    with open("/tmp/isomer-test/copy_directory_tree/foo/bar/ham", "w") as f:
57
        f.write("spam!")
58
59
    copy_directory_tree(
60
        "/tmp/isomer-test/copy_directory_tree",
61
        "/tmp/isomer-test/copy_directory_tree_test"
62
    )
63
64
    assert os.path.exists("/tmp/isomer-test/copy_directory_tree_test/foo/bar/ham")
65
66
67
def test_move_directory_tree():
68
    source = "/tmp/isomer-test/copy_directory_tree"
69
    target = "/tmp/isomer-test/copy_directory_tree_test"
70
71
    source_file = os.path.join(source, "foo/bar/ham")
72
    target_file = os.path.join(target, "foo/bar/ham")
73
74
    os.makedirs(os.path.join(source, "foo/bar"), exist_ok=True)
75
    with open(source_file, "w") as f:
76
        f.write("spam!")
77
78
    shutil.rmtree(target, ignore_errors=True)
79
80
    copy_directory_tree(
81
        source,
82
        target,
83
        move=True
84
    )
85
86
    assert os.path.exists(target_file)
87
    assert not os.path.exists(source_file)
88
89
90
def test_get_frontend_locations_development():
91
    set_instance("test-instance", "test", "/tmp/isomer-test")
92
    frontend_root, frontend_target = get_frontend_locations(True)
93
94
    assert frontend_root == os.path.join(
95
        str(Path(__file__).parents[2]), 'frontend'
96
    )
97
    assert frontend_target == \
98
           '/tmp/isomer-test/var/lib/isomer/test-instance/test/frontend-dev'
99
100
101
def test_get_frontend_locations():
102
    set_instance("test-instance", "test", "/tmp/isomer-test")
103
    frontend_root, frontend_target = get_frontend_locations(False)
104
    assert frontend_root == \
105
           "/tmp/isomer-test/var/lib/isomer/test-instance/test/repository/frontend"
106
    assert frontend_target == \
107
           "/tmp/isomer-test/var/lib/isomer/test-instance/test/frontend"
108
109
110
def test_generate_component_folders():
111
    set_instance("test-instance", "test", "/tmp/isomer-test")
112
    frontend_root, frontend_target = get_frontend_locations(False)
113
114
    component_folder = os.path.join(frontend_root, "src", "components")
115
    generate_component_folders(component_folder)
116
117
    assert os.path.exists(component_folder)
118
119
    old_file = os.path.join(component_folder, "ham")
120
121
    with open(old_file, "w") as f:
122
        f.write("spam")
123
124
    generate_component_folders(component_folder)
125
126
    assert not os.path.exists(old_file)
127
128
129
@has_test_module
130
def test_copy_resource_tree():
131
    dest = "/tmp/isomer-test/copy_resource_test"
132
    os.makedirs(dest, exist_ok=True)
133
134
    from pkg_resources import get_entry_info
135
136
    pkg_object = get_entry_info("isomer-test-module", "isomer.components", "testmanager")
137
138
    # pytest.exit(crap)
139
140
    copy_resource_tree("isomer-test-module", "frontend", dest)
141
142
    assert os.path.exists(os.path.join(dest, "test.module.js"))
143
144
145
@has_test_module
146
def test_get_components():
147
    set_instance("test-instance", "test", "/tmp/isomer-test")
148
    frontend_root, frontend_target = get_frontend_locations(False)
149
150
    component_folder = os.path.join(frontend_root, "src", "components")
151
    generate_component_folders(component_folder)
152
153
    components = get_components(frontend_root)
154
    assert isinstance(components, dict)
155
156
    assert 'test' in components
157
158
159
@has_test_module
160
def test_update_frontends():
161
    set_instance("test-instance", "test", "/tmp/isomer-test")
162
    frontend_root, frontend_target = get_frontend_locations(False)
163
164
    component_folder = os.path.join(frontend_root, "src", "components")
165
    generate_component_folders(component_folder)
166
167
    components = get_components(frontend_root)
168
169
    installation_packages, imports = update_frontends(
170
        components, frontend_root, True
171
    )
172
173
    assert "test-npm-update" in installation_packages
174
    assert "import test from './components/test/test.module';\n" \
175
           "modules.push(test);\n" in imports
176
177
178
@has_test_module
179
def test_get_sails_dependencies():
180
    frontend_root, _ = get_frontend_locations(True)
181
    installation_packages = get_sails_dependencies(frontend_root)
182
183
    assert "angular" in ";".join(installation_packages)
184
185
186
@has_test_module
187
def test_write_main():
188
    imports = ["IMPORTTEST"]
189
    root, _ = get_frontend_locations(True)
190
    source_file = os.path.join(root, "src/main.tpl.js")
191
    target = os.path.join("/tmp/isomer-test/", "src")
192
    target_file = os.path.join(target, "main.tpl.js")
193
194
    os.makedirs(target, exist_ok=True)
195
    try:
196
        os.unlink(target_file)
197
    except FileNotFoundError:
198
        pass
199
    except OSError:
200
        pytest.fail("Could not delete existing frontend loader")
201
202
    shutil.copy(source_file, target_file)
203
204
    write_main(imports, "/tmp/isomer-test")
205
206
    with open(os.path.join(target, "main.js")) as f:
207
        content = f.read()
208
209
    assert "IMPORTTEST" in content
210