forked from canonical/jhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
109 lines (80 loc) · 2.87 KB
/
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
import contextlib
import os
import shutil
import zipfile
from pathlib import Path
import pytest
import charm.update
import jhack
dnttchme = 'don_t_touch_me.txt'
untouched = 'untouched'
@pytest.fixture
def charm(tmp_path_factory):
charm = tmp_path_factory.mktemp('charm')
charm_src = charm / 'src'
charm_lib = charm / 'lib'
untouchable = charm / dnttchme
untouchable.touch() # lol
untouchable.write_text(untouched)
charm_src.mkdir()
charm_lib.mkdir()
(charm_src / 'charm.py').write_text('charm')
(charm_lib / 'libfile.py').write_text('libfile')
# {charm}
# - src
# - charm.py
# - lib
# - libfile.py
return charm
@pytest.fixture
def packed_charm(charm, tmp_path):
return Path(shutil.make_archive(str(tmp_path), 'zip', charm))
@pytest.fixture
def mock_baz(tmp_path_factory):
baz_dir = tmp_path_factory.mktemp('baz_dir')
(baz_dir / 'baz_file.py').write_text('BAZ')
return baz_dir
def test_charm_update(tmp_path_factory, packed_charm, mock_baz):
assert packed_charm.exists()
charm.update.update_charm(packed_charm, [mock_baz], ['baz'])
def check_base(baz_content):
zf = zipfile.ZipFile(packed_charm)
assert len(zf.filelist) == 7
charm_file = zf.open('src/charm.py').read().decode('utf-8').strip()
assert charm_file == 'charm' # unchanged
lib_file = zf.open('lib/libfile.py').read().decode('utf-8').strip()
assert lib_file == 'libfile' # unchanged
untouched_zf = zf.open(dnttchme).read().decode('utf-8').strip()
assert untouched_zf == untouched
baz_file = zf.open('baz/baz_file.py').read().decode('utf-8').strip()
assert baz_file == baz_content
check_base('BAZ')
# now let's touch baz
change = 'BAZ IS THE NEW FOO'
(mock_baz / 'baz_file.py').write_text(change)
charm.update.update_charm(packed_charm, [mock_baz], ['baz'])
check_base(change)
@contextlib.contextmanager
def cwd(wd):
old_wd = os.getcwd()
os.chdir(wd)
yield
os.chdir(old_wd)
@pytest.fixture
def mock_charm_dev_dir(tmp_path_factory):
return tmp_path_factory.mktemp('charm_dev_dir')
def test_charm_update_default(packed_charm, mock_charm_dev_dir):
(mock_charm_dev_dir / 'src').mkdir()
(mock_charm_dev_dir / 'lib').mkdir()
(mock_charm_dev_dir / 'src' / 'charm.py').write_text('FOO')
(mock_charm_dev_dir / 'lib' / 'libfile.py').write_text('BAR')
with cwd(mock_charm_dev_dir):
charm.update.update_charm(packed_charm)
zf = zipfile.ZipFile(packed_charm)
assert len(zf.filelist) == 5
charm_file = zf.open('src/charm.py').read().decode('utf-8').strip()
assert charm_file == 'FOO'
lib_file = zf.open('lib/libfile.py').read().decode('utf-8').strip()
assert lib_file == 'BAR'
untouched_zf = zf.open(dnttchme).read().decode('utf-8').strip()
assert untouched_zf == untouched