ya2 · news · projects · code · about

1d1c27a82a1dd64756b12a77ae671f16304399d0
[pmachines.git] / tests / test_functional.py
1 from pathlib import Path
2 from itertools import product
3 from logging import info
4 import sys
5 if '' in sys.path: sys.path.remove('')
6 sys.path.append(str(Path(__file__).parent.parent.parent))
7 from unittest import TestCase
8 from shutil import rmtree
9 from time import sleep
10 from os import system, remove, environ
11 from os.path import exists, basename, join
12 from glob import glob
13 from panda3d.core import Filename
14 from lib.build.build import exec_cmd, _branch, ver
15
16
17 class FunctionalTests(TestCase):
18
19 def __clean(self):
20 paths = [
21 'tests/functional',
22 str(Path.home()) + '/.local/share/pmachines/tests/functional/',
23 str(Path.home()) + '/.var/app/it.ya2.Pmachines/data/pmachines/tests/functional/',
24 str(Path.home()) + '/.wine/drive_c/users/flavio/AppData/Local/pmachines/tests/functional/']
25 for path in paths:
26 rmtree(path, ignore_errors=True)
27 dirs = [
28 Filename().get_user_appdata_directory(),
29 '/home/flavio/.var/app/it.ya2.Pmachines/data',
30 '/home/flavio/.wine/drive_c/users/flavio/AppData']
31 files = [
32 'pmachines/options.ini',
33 'pmachines/obs_version.txt']
34 for dir_file in product(dirs, files):
35 _file = join(*dir_file)
36 if exists(_file):
37 remove(_file)
38 info('%s removed' % _file)
39 else:
40 info('%s does not exist' % _file)
41 system('pkill -f "pmachines.exe"')
42
43 def __awake(self):
44 system('xdotool key shift')
45
46 def setUp(self):
47 self.__clean()
48
49 def tearDown(self):
50 self.__clean()
51
52 def test_ref(self):
53 info('test_ref')
54 path = 'pmachines/tests/functional_ref_%s/*.png' % _branch()
55 files = glob(join(Filename().get_user_appdata_directory(), path))
56 self.assertGreater(len(files), 1)
57
58 def __similar_images(self, ref_img, tst_img):
59 cmd = 'magick compare -metric NCC %s %s diff.png' % (ref_img, tst_img)
60 res = exec_cmd(cmd)
61 if exists('diff.png'): remove('diff.png')
62 print('compare %s %s: %s' % (ref_img, tst_img, res))
63 return float(res) > .64
64
65 def __test_template(self, cmd, path):
66 if environ.get('FUNCTIONAL') != '1':
67 self.skipTest('skipped functional tests')
68 self.__clean()
69 self.__awake()
70 system('amixer sset Master 0%')
71 ret = system(cmd)
72 self.assertEqual(ret, 0, 'error while executing ' + cmd)
73 files = glob(str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/*.png' % _branch())
74 self.assertGreater(len(files), 1)
75 for fname in files:
76 self.assertTrue(exists(path + basename(fname)), '%s does not exist' % (path + basename(fname)))
77 self.assertTrue(
78 self.__similar_images(
79 str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/' % _branch() + basename(fname),
80 path + basename(fname)),
81 'error while comparing %s and %s' % (
82 str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/' % _branch() + basename(fname),
83 path + basename(fname)))
84
85 def test_code(self):
86 info('test_code')
87 self.__test_template(
88 '~/venv/bin/python main.py --functional-test 1 ; '
89 '~/venv/bin/python main.py --functional-test 2',
90 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
91
92 def test_appimage(self):
93 info('test_appimage')
94 bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
95 bld_branch = '' if bld_branch == 'stable' else ('-' + bld_branch)
96 self.__test_template(
97 './dist/Pmachines%s-x86_64.AppImage --functional-test 1 ;'
98 './dist/Pmachines%s-x86_64.AppImage --functional-test 2' % (bld_branch, bld_branch),
99 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
100
101 def test_flatpak(self):
102 info('test_flatpak')
103 if environ.get('FUNCTIONALPOST') != '1':
104 self.skipTest('skipped functional-post tests')
105 bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
106 cmd = 'flatpak update -y it.ya2.Pmachines//%s' % bld_branch
107 info('executing: %s' % cmd)
108 system(cmd)
109 info('executed: %s' % cmd)
110 self.__test_template(
111 'flatpak run it.ya2.Pmachines//%s --functional-test 1 ;'
112 'flatpak run it.ya2.Pmachines//%s --functional-test 2' % (bld_branch, bld_branch),
113 str(Path.home()) + '/.var/app/it.ya2.Pmachines/data/pmachines/tests/functional/')
114
115 # def __update_itchio(self):
116 # system('/home/flavio/.itch/itch')
117 # sleep(5)
118 # system('xdotool mousemove 1280 620')
119 # sleep(1)
120 # system('xdotool click 1')
121 # sleep(300)
122 # system('killall itch')
123
124 # def test_itchio(self):
125 # info('test_itchio')
126 # if environ.get('FUNCTIONALPOST') != '1':
127 # self.skipTest('skipped functional-post tests')
128 # if _branch() != 'master':
129 # return
130 # self.__update_itchio()
131 # self.__test_template(
132 # '/home/flavio/.config/itch/apps/pmachines/pmachines --functional-test 1 ;'
133 # '/home/flavio/.config/itch/apps/pmachines/pmachines --functional-test 2',
134 # str(Path.home()) + '/.local/share/pmachines/tests/functional/')
135
136 def test_windows(self):
137 info('test_windows')
138 system('pkill -f "pmachines.exe"')
139 abspath = str(Path(__file__).parent.parent) + '/build/win_amd64/pmachines.exe'
140 self.__test_template(
141 'timeout 720s wine %s --functional-test 1 ; '
142 'timeout 720s wine %s --functional-test 2' % (abspath, abspath),
143 str(Path.home()) + '/.wine/drive_c/users/flavio/AppData/Local/pmachines/tests/functional/')
144
145 # def test_versions(self):
146 # info('test_versions')
147 # if environ.get('FUNCTIONAL') != '1':
148 # self.skipTest('skipped functional tests')
149 # bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
150 # with open('/home/flavio/pmachines_builder/last_bld.txt') as f:
151 # lines = f.readlines()
152 # for line in lines:
153 # if line.strip().split()[0] == _branch():
154 # commit = line.strip().split()[1][:7]
155 # _ver = ver
156 # if _branch() == 'stable':
157 # with open('/home/flavio/pmachines_builder/pmachines/assets/version.txt') as fver:
158 # _ver = fver.read().strip() + '-'
159 # exp = '%s-%s' % (_ver, commit)
160 # cmds = [
161 # ('./build/manylinux1_x86_64/pmachines --version', str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
162 # ('./dist/Pmachines-%s-x86_64.AppImage --version' % bld_branch, str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
163 # ('timeout 720s wine ./build/win_amd64/pmachines.exe --version', '/home/flavio/.wine/drive_c/users/flavio/AppData/Local/pmachines/obs_version.txt')
164 # ]
165 # if environ.get('FUNCTIONALPOST') == '1':
166 # if _branch() == 'master':
167 # self.__update_itchio()
168 # cmds += [('/home/flavio/.config/itch/apps/pmachines/pmachines --version', str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt')]
169 # cmds += [('flatpak run it.ya2.Pmachines//%s --version' % bld_branch, '/home/flavio/.var/app/it.ya2.Pmachines/data/pmachines/obs_version.txt')]
170 # system('flatpak update -y it.ya2.Pmachines//%s' % bld_branch)
171 # for cmd in cmds:
172 # if exists(cmd[1]):
173 # remove(cmd[1])
174 # info('launching %s' % cmd[0])
175 # exec_cmd(cmd[0])
176 # with open(cmd[1]) as f:
177 # obs = f.read().strip()
178 # self.assertEqual(obs, exp)