1 from pathlib
import Path
2 from datetime
import datetime
3 from itertools
import product
4 from logging
import info
6 if '' in sys
.path
: sys
.path
.remove('')
7 sys
.path
.append(str(Path(__file__
).parent
.parent
.parent
))
8 from unittest
import TestCase
9 from shutil
import rmtree
, copy
10 from time
import sleep
11 from os
import system
, remove
, environ
12 from os
.path
import exists
, basename
, join
14 from subprocess
import PIPE
, run
, Popen
15 from panda3d
.core
import Filename
16 from ya2
.build
.build
import exec_cmd
, _branch
, _version
19 class FunctionalTests(TestCase
):
24 str(Path
.home()) + '/.local/share/pmachines/tests/functional/',
25 str(Path
.home()) + '/.var/app/it.ya2.Pmachines/data/pmachines/tests/functional/',
26 str(Path
.home()) + '/.wine/drive_c/users/flavio/AppData/Local/pmachines/tests/functional/']
28 rmtree(path
, ignore_errors
=True)
30 Filename().get_user_appdata_directory(),
31 '/home/flavio/.var/app/it.ya2.Pmachines/data',
32 '/home/flavio/.wine/drive_c/users/flavio/AppData/Local']
34 'pmachines/options.ini',
35 'pmachines/obs_version.txt']
36 for dir_file
in product(dirs
, files
):
37 _file
= join(*dir_file
)
40 info('%s removed' % _file
)
42 info('%s does not exist' % _file
)
43 opt_ini
= str(Path
.home()) + '/builders/pmachines_builder/pmachines/options.ini'
46 system('pkill -f "pmachines.exe"')
49 system('xdotool key shift')
59 path
= 'pmachines/tests/functional_ref_%s/*.png' % _branch()
60 files
= glob(join(Filename().get_user_appdata_directory(), path
))
61 self
.assertGreater(len(files
), 1)
63 def __similar_images(self
, ref_img
, tst_img
):
64 cmd
= 'magick compare -metric NCC %s %s diff.png' % (ref_img
, tst_img
)
65 res
= exec_cmd(cmd
, False)
66 if exists('diff.png'): remove('diff.png')
67 print('compare %s %s: %s' % (ref_img
, tst_img
, res
))
68 return float(res
) > .8
70 def __test_template(self
, app_cmd
, test_cmds
, path
):
71 if environ
.get('FUNCTIONAL') != '1':
72 self
.skipTest('skipped functional tests')
75 system('amixer sset Master 0%')
77 #proc = run(cmd, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True, check=check)
78 #return proc.stdout.strip() + proc.stderr.strip()
79 for test_cmd
in test_cmds
:
81 info('launching ' + app_cmd
)
82 print('launching ' + app_cmd
)
83 p_app
= Popen(app_cmd
, shell
=True, stdout
=PIPE
, stderr
=PIPE
, universal_newlines
=True)
85 info('launching ' + test_cmd
)
86 print('launching ' + test_cmd
)
87 p_test
= Popen(test_cmd
, shell
=True, stdout
=PIPE
, stderr
=PIPE
, universal_newlines
=True)
88 p_app_out
, p_app_err
= p_app
.communicate()
89 t_out
, t_err
= p_test
.communicate()
90 info('output (%s): %s' % (app_cmd
, p_app_out
))
91 info('error (%s): %s' % (app_cmd
, p_app_err
))
92 info('output (%s): %s' % (test_cmd
, t_out
))
93 info('error (%s): %s' % (test_cmd
, t_err
))
94 print('output (%s): %s' % (app_cmd
, p_app_out
))
95 print('error (%s): %s' % (app_cmd
, p_app_err
))
96 print('output (%s): %s' % (test_cmd
, t_out
))
97 print('error (%s): %s' % (test_cmd
, t_err
))
98 self
.assertEqual(p_app
.returncode
, 0, 'error while executing ' + app_cmd
)
99 self
.assertEqual(p_test
.returncode
, 0, 'error while executing ' + test_cmd
)
100 files
= glob(str(Path
.home()) + '/.local/share/pmachines/tests/functional_ref_%s/*.png' % _branch())
101 self
.assertGreater(len(files
), 1)
103 self
.assertTrue(exists(path
+ basename(fname
)), '%s does not exist' % (path
+ basename(fname
)))
104 similar
= self
.__similar
_images
(
105 str(Path
.home()) + '/.local/share/pmachines/tests/functional_ref_%s/' % _branch() + basename(fname
),
106 path
+ basename(fname
)),
107 'error while comparing %s and %s' % (
108 str(Path
.home()) + '/.local/share/pmachines/tests/functional_ref_%s/' % _branch() + basename(fname
),
109 path
+ basename(fname
))
111 timestamp
= datetime
.now().strftime('%y%m%d%H%M%S%f')
112 copy(path
+ basename(fname
), '~/Desktop/' + basename(fname
)[:-4] + timestamp
+ '.png')
113 self
.assertTrue(similar
)
117 self
.__test
_template
(
118 '/home/flavio/venv/bin/python main.py --functional-test',
119 ['/home/flavio/venv/bin/python -m tests.functional_test.py 1',
120 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2'],
121 str(Path
.home()) + '/.local/share/pmachines/tests/functional/')
123 def test_appimage(self
):
124 info('test_appimage')
125 bld_branch
= {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
126 bld_branch
= '' if bld_branch
== 'stable' else ('-' + bld_branch
)
127 self
.__test
_template
(
128 'timeout 7200s ./dist/Pmachines%s-x86_64.AppImage --functional-test' % bld_branch
,
129 ['timeout 7200s ~/venv/bin/python -m tests.functional_test.py 1',
130 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2'],
131 str(Path
.home()) + '/.local/share/pmachines/tests/functional/')
133 # def test_flatpak(self):
134 # info('test_flatpak')
135 # if environ.get('FUNCTIONALPOST') != '1':
136 # self.skipTest('skipped functional-post tests')
137 # bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
138 # cmd = 'flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch
139 # info('executing: %s' % cmd)
141 # fout = exec_cmd(cmd)
142 # info('executed: %s' % cmd)
144 # self.__test_template(
145 # 'timeout 720s flatpak run it.ya2.Pmachines//%s --functional-test & '
146 # 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 1; sleep 5; '
147 # 'timeout 720s flatpak run it.ya2.Pmachines//%s --functional-test & ' % (bld_branch, bld_branch) +
148 # 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 2',
149 # str(Path.home()) + '/.var/app/it.ya2.Pmachines/data/pmachines/tests/functional/')
151 def __update_itchio(self
):
152 system('/home/flavio/.itch/itch')
154 system('xdotool mousemove 860 620')
156 system('xdotool click 1')
158 system('killall itch')
160 def test_itchio(self
):
162 if environ
.get('FUNCTIONALPOST') != '1':
163 self
.skipTest('skipped functional-post tests')
164 if _branch() != 'master':
166 self
.__update
_itchio
()
167 self
.__test
_template
(
168 'timeout 7200s /home/flavio/.config/itch/apps/pmachines/pmachines --functional-test',
169 ['timeout 7200s ~/venv/bin/python -m tests.functional_test.py 1',
170 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2'],
171 str(Path
.home()) + '/.local/share/pmachines/tests/functional/')
173 def test_windows(self
):
175 system('pkill -f "pmachines.exe"')
176 abspath
= str(Path(__file__
).parent
.parent
) + '/build/win_amd64/pmachines.exe'
177 self
.__test
_template
(
178 'timeout 7200s wine %s --functional-test' % abspath
,
179 ['timeout 7200s ~/venv/bin/python -m tests.functional_test.py 1',
180 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2'],
181 str(Path
.home()) + '/.wine/drive_c/users/flavio/AppData/Local/pmachines/tests/functional/')
183 def test_versions(self
):
184 info('test_versions')
185 if environ
.get('FUNCTIONAL') != '1':
186 self
.skipTest('skipped functional tests')
187 bld_branch
= {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
188 with
open('/home/flavio/builders/pmachines_builder/last_bld.txt') as f
:
189 lines
= f
.readlines()
191 if line
.strip().split()[0] == _branch():
192 commit
= line
.strip().split()[1][:7]
194 if _branch() == 'stable':
195 with
open('/home/flavio/builders/pmachines_builder/pmachines/assets/version.txt') as fver
:
196 __ver
= fver
.read().strip() + '-'
197 exp
= '%s-%s' % (__ver
, commit
)
199 ('timeout 7200s ./build/manylinux2010_x86_64/pmachines --version', str(Filename
.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
200 ('timeout 7200s ./dist/Pmachines-%s-x86_64.AppImage --version' % bld_branch
, str(Filename
.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
201 ('timeout 7200s wine ./build/win_amd64/pmachines.exe --version', '/home/flavio/.wine/drive_c/users/flavio/AppData/Local/pmachines/obs_version.txt')
203 if environ
.get('FUNCTIONALPOST') == '1':
204 if _branch() == 'master':
205 self
.__update
_itchio
()
206 cmds
+= [('timeout 7200s /home/flavio/.config/itch/apps/pmachines/pmachines --version', str(Filename
.get_user_appdata_directory()) + '/pmachines/obs_version.txt')]
207 # cmds += [('timeout 7200s flatpak run it.ya2.Pmachines//%s --version' % bld_branch, '/home/flavio/.var/app/it.ya2.Pmachines/data/pmachines/obs_version.txt')]
208 # info('executing flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch)
209 # #system('flatpak update -y --user it.ya2.Pmachines//%s' % bld_branch)
210 # fout = exec_cmd('flatpak update -y --user it.ya2.Pmachines//%s' % bld_branch)
211 # info('executed flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch)
216 info('launching %s' % cmd
[0])
218 with
open(cmd
[1]) as f
:
219 obs
= f
.read().strip()
220 self
.assertEqual(obs
, exp
, 'during ' + cmd
[0])