ya2 · news · projects · code · about

fix: windows builds
[pmachines.git] / tests / test_functional.py
CommitLineData
0f9675ba 1from pathlib import Path
8ce16d6c 2from datetime import datetime
0f9675ba
FC
3from itertools import product
4from logging import info
5import sys
6if '' in sys.path: sys.path.remove('')
7sys.path.append(str(Path(__file__).parent.parent.parent))
8from unittest import TestCase
8ce16d6c 9from shutil import rmtree, copy
0f9675ba
FC
10from time import sleep
11from os import system, remove, environ
12from os.path import exists, basename, join
13from glob import glob
a6f7e35d 14from subprocess import Popen, STDOUT
0f9675ba 15from panda3d.core import Filename
ea71df6e 16from ya2.build.build import exec_cmd, _branch, _version
0f9675ba
FC
17
18
19class FunctionalTests(TestCase):
20
f34ae893
FC
21 def __init__(self, *args, **kwargs):
22 super(FunctionalTests, self).__init__(*args, **kwargs)
23 self.__itchio_updated = False
24
0f9675ba
FC
25 def __clean(self):
26 paths = [
27 'tests/functional',
edeef6f9
FC
28 str(Path.home()) + '/.local/share/pmachines/tests/functional/',
29 str(Path.home()) + '/.var/app/it.ya2.Pmachines/data/pmachines/tests/functional/',
30 str(Path.home()) + '/.wine/drive_c/users/flavio/AppData/Local/pmachines/tests/functional/']
0f9675ba
FC
31 for path in paths:
32 rmtree(path, ignore_errors=True)
33 dirs = [
34 Filename().get_user_appdata_directory(),
edeef6f9 35 '/home/flavio/.var/app/it.ya2.Pmachines/data',
68f6c184 36 '/home/flavio/.wine/drive_c/users/flavio/AppData/Local']
0f9675ba 37 files = [
edeef6f9 38 'pmachines/options.ini',
d68aeda7 39 'pmachines/observed_version.txt']
0f9675ba
FC
40 for dir_file in product(dirs, files):
41 _file = join(*dir_file)
42 if exists(_file):
43 remove(_file)
44 info('%s removed' % _file)
45 else:
46 info('%s does not exist' % _file)
2fbba335
FC
47 opt_ini = str(Path.home()) + '/builders/pmachines_builder/pmachines/options.ini'
48 if exists(opt_ini):
49 remove(opt_ini)
edeef6f9 50 system('pkill -f "pmachines.exe"')
0f9675ba
FC
51
52 def __awake(self):
53 system('xdotool key shift')
54
55 def setUp(self):
56 self.__clean()
57
58 def tearDown(self):
8ce16d6c 59 pass # self.__clean()
0f9675ba
FC
60
61 def test_ref(self):
edeef6f9
FC
62 info('test_ref')
63 path = 'pmachines/tests/functional_ref_%s/*.png' % _branch()
0f9675ba
FC
64 files = glob(join(Filename().get_user_appdata_directory(), path))
65 self.assertGreater(len(files), 1)
66
67 def __similar_images(self, ref_img, tst_img):
68 cmd = 'magick compare -metric NCC %s %s diff.png' % (ref_img, tst_img)
df4f85fc 69 res = exec_cmd(cmd, False)
0f9675ba
FC
70 if exists('diff.png'): remove('diff.png')
71 print('compare %s %s: %s' % (ref_img, tst_img, res))
8ce16d6c 72 return float(res) > .8
0f9675ba 73
e5b1e7e0 74 def __test_template(self, app_cmd, test_cmds, path):
0f9675ba
FC
75 if environ.get('FUNCTIONAL') != '1':
76 self.skipTest('skipped functional tests')
77 self.__clean()
78 self.__awake()
79 system('amixer sset Master 0%')
a33967c7
FC
80 #ret = system(cmd)
81 #proc = run(cmd, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True, check=check)
82 #return proc.stdout.strip() + proc.stderr.strip()
e5b1e7e0
FC
83 for test_cmd in test_cmds:
84 sleep(8)
85 info('launching ' + app_cmd)
86 print('launching ' + app_cmd)
f31085f9 87 p_app = Popen(app_cmd, shell=True, stderr=STDOUT, universal_newlines=True)
e5b1e7e0
FC
88 sleep(30)
89 info('launching ' + test_cmd)
90 print('launching ' + test_cmd)
a6f7e35d 91 p_test = Popen(test_cmd, shell=True, stderr=STDOUT, universal_newlines=True)
e5b1e7e0
FC
92 p_app_out, p_app_err = p_app.communicate()
93 t_out, t_err = p_test.communicate()
94 info('output (%s): %s' % (app_cmd, p_app_out))
95 info('error (%s): %s' % (app_cmd, p_app_err))
96 info('output (%s): %s' % (test_cmd, t_out))
97 info('error (%s): %s' % (test_cmd, t_err))
98 print('output (%s): %s' % (app_cmd, p_app_out))
99 print('error (%s): %s' % (app_cmd, p_app_err))
100 print('output (%s): %s' % (test_cmd, t_out))
101 print('error (%s): %s' % (test_cmd, t_err))
102 self.assertEqual(p_app.returncode, 0, 'error while executing ' + app_cmd)
103 self.assertEqual(p_test.returncode, 0, 'error while executing ' + test_cmd)
ad5ef398
FC
104 files = glob(str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/*.png' % _branch())
105 self.assertGreater(len(files), 1)
0f9675ba 106 for fname in files:
ad5ef398 107 self.assertTrue(exists(path + basename(fname)), '%s does not exist' % (path + basename(fname)))
8ce16d6c
FC
108 similar = self.__similar_images(
109 str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/' % _branch() + basename(fname),
110 path + basename(fname)),
111 'error while comparing %s and %s' % (
112 str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/' % _branch() + basename(fname),
113 path + basename(fname))
114 if not similar:
115 timestamp = datetime.now().strftime('%y%m%d%H%M%S%f')
116 copy(path + basename(fname), '~/Desktop/' + basename(fname)[:-4] + timestamp + '.png')
117 self.assertTrue(similar)
0f9675ba 118
ad5ef398
FC
119 def test_code(self):
120 info('test_code')
f34ae893
FC
121 if environ.get('FUNCTIONALPOST') == '1':
122 self.skipTest('skipped functional-post test: test_code')
ad5ef398 123 self.__test_template(
e5b1e7e0
FC
124 '/home/flavio/venv/bin/python main.py --functional-test',
125 ['/home/flavio/venv/bin/python -m tests.functional_test.py 1',
3466af49
FC
126 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2',
127 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 3'],
ad5ef398 128 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
edeef6f9 129
1e689176
FC
130 def test_appimage(self):
131 info('test_appimage')
f34ae893
FC
132 if environ.get('FUNCTIONALPOST') == '1':
133 self.skipTest('skipped functional-post test: test_appimage')
1e689176
FC
134 bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
135 bld_branch = '' if bld_branch == 'stable' else ('-' + bld_branch)
136 self.__test_template(
a33967c7 137 'timeout 7200s ./dist/Pmachines%s-x86_64.AppImage --functional-test' % bld_branch,
e5b1e7e0 138 ['timeout 7200s ~/venv/bin/python -m tests.functional_test.py 1',
3466af49
FC
139 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2',
140 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 3'],
1e689176 141 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
edeef6f9 142
e9be18cf
FC
143 # def test_flatpak(self):
144 # info('test_flatpak')
145 # if environ.get('FUNCTIONALPOST') != '1':
146 # self.skipTest('skipped functional-post tests')
147 # bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
148 # cmd = 'flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch
149 # info('executing: %s' % cmd)
150 # #system(cmd)
151 # fout = exec_cmd(cmd)
152 # info('executed: %s' % cmd)
153 # info(fout)
154 # self.__test_template(
155 # 'timeout 720s flatpak run it.ya2.Pmachines//%s --functional-test & '
156 # 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 1; sleep 5; '
157 # 'timeout 720s flatpak run it.ya2.Pmachines//%s --functional-test & ' % (bld_branch, bld_branch) +
158 # 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 2',
159 # str(Path.home()) + '/.var/app/it.ya2.Pmachines/data/pmachines/tests/functional/')
addec9c9 160
c8e3e941 161 def __update_itchio(self):
f34ae893
FC
162 info('update_itchio::itchio_updated::cond: %s' % str(self.__itchio_updated))
163 if self.__itchio_updated:
164 return
c8e3e941 165 system('/home/flavio/.itch/itch')
6e6ca4ad 166 sleep(120)
c8e3e941 167 system('xdotool mousemove 860 620')
6e6ca4ad 168 sleep(3)
c8e3e941
FC
169 system('xdotool click 1')
170 sleep(300)
171 system('killall itch')
f34ae893
FC
172 self.__itchio_updated = True
173 info('update_itchio::itchio_updated::done: %s' % str(self.__itchio_updated))
c8e3e941
FC
174
175 def test_itchio(self):
176 info('test_itchio')
177 if environ.get('FUNCTIONALPOST') != '1':
178 self.skipTest('skipped functional-post tests')
179 if _branch() != 'master':
180 return
181 self.__update_itchio()
182 self.__test_template(
a33967c7 183 'timeout 7200s /home/flavio/.config/itch/apps/pmachines/pmachines --functional-test',
e5b1e7e0 184 ['timeout 7200s ~/venv/bin/python -m tests.functional_test.py 1',
3466af49
FC
185 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2',
186 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 3'],
c8e3e941 187 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
addec9c9 188
2e5f0efc
FC
189 def test_windows(self):
190 info('test_windows')
f34ae893
FC
191 if environ.get('FUNCTIONALPOST') == '1':
192 self.skipTest('skipped functional-post test: test_windows')
2e5f0efc
FC
193 system('pkill -f "pmachines.exe"')
194 abspath = str(Path(__file__).parent.parent) + '/build/win_amd64/pmachines.exe'
195 self.__test_template(
a33967c7 196 'timeout 7200s wine %s --functional-test' % abspath,
0c36d0bf
FC
197 ['timeout 7200s ~/venv/bin/python -m tests.functional_test.py 1 120',
198 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2 120',
199 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 3 120'],
2e5f0efc 200 str(Path.home()) + '/.wine/drive_c/users/flavio/AppData/Local/pmachines/tests/functional/')
edeef6f9 201
8ce16d6c
FC
202 def test_versions(self):
203 info('test_versions')
204 if environ.get('FUNCTIONAL') != '1':
205 self.skipTest('skipped functional tests')
c991401b 206 #bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
8ce16d6c
FC
207 with open('/home/flavio/builders/pmachines_builder/last_bld.txt') as f:
208 lines = f.readlines()
209 for line in lines:
210 if line.strip().split()[0] == _branch():
211 commit = line.strip().split()[1][:7]
ea71df6e 212 __ver = _version()
8ce16d6c
FC
213 if _branch() == 'stable':
214 with open('/home/flavio/builders/pmachines_builder/pmachines/assets/version.txt') as fver:
a7f2fc93
FC
215 __ver = fver.read().strip() + '-'
216 exp = '%s-%s' % (__ver, commit)
82e6c26e 217 appimage_branch = '' if _branch() == 'stable' else ('-' + _branch())
8ce16d6c 218 cmds = [
d68aeda7
FC
219 ('timeout 7200s ./build/manylinux2010_x86_64/pmachines --version', str(Filename.get_user_appdata_directory()) + '/pmachines/observed_version.txt'),
220 ('timeout 7200s ./dist/Pmachines%s-x86_64.AppImage --version' % appimage_branch, str(Filename.get_user_appdata_directory()) + '/pmachines/observed_version.txt'),
221 ('timeout 7200s wine ./build/win_amd64/pmachines.exe --version', '/home/flavio/.wine/drive_c/users/flavio/AppData/Local/pmachines/observed_version.txt')
c8e3e941
FC
222 ]
223 if environ.get('FUNCTIONALPOST') == '1':
224 if _branch() == 'master':
225 self.__update_itchio()
d68aeda7
FC
226 cmds += [('timeout 7200s /home/flavio/.config/itch/apps/pmachines/pmachines --version', str(Filename.get_user_appdata_directory()) + '/pmachines/observed_version.txt')]
227 # cmds += [('timeout 7200s flatpak run it.ya2.Pmachines//%s --version' % bld_branch, '/home/flavio/.var/app/it.ya2.Pmachines/data/pmachines/observed_version.txt')]
e9be18cf
FC
228 # info('executing flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch)
229 # #system('flatpak update -y --user it.ya2.Pmachines//%s' % bld_branch)
230 # fout = exec_cmd('flatpak update -y --user it.ya2.Pmachines//%s' % bld_branch)
231 # info('executed flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch)
232 # info(fout)
8ce16d6c
FC
233 for cmd in cmds:
234 if exists(cmd[1]):
235 remove(cmd[1])
236 info('launching %s' % cmd[0])
237 exec_cmd(cmd[0])
238 with open(cmd[1]) as f:
239 obs = f.read().strip()
240 self.assertEqual(obs, exp, 'during ' + cmd[0])