ya2 · news · projects · code · about

functional tests: testing code
[pmachines.git] / tests / test_functional.py
CommitLineData
0f9675ba
FC
1from pathlib import Path
2from itertools import product
3from logging import info
4import sys
5if '' in sys.path: sys.path.remove('')
6sys.path.append(str(Path(__file__).parent.parent.parent))
7from unittest import TestCase
8from shutil import rmtree
9from time import sleep
10from os import system, remove, environ
11from os.path import exists, basename, join
12from glob import glob
13from panda3d.core import Filename
14from lib.build.build import exec_cmd, _branch, ver
15
16
17class FunctionalTests(TestCase):
18
19 def __clean(self):
20 paths = [
21 'tests/functional',
edeef6f9
FC
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/']
0f9675ba
FC
25 for path in paths:
26 rmtree(path, ignore_errors=True)
27 dirs = [
28 Filename().get_user_appdata_directory(),
edeef6f9 29 '/home/flavio/.var/app/it.ya2.Pmachines/data',
0f9675ba
FC
30 '/home/flavio/.wine/drive_c/users/flavio/AppData']
31 files = [
edeef6f9
FC
32 'pmachines/options.ini',
33 'pmachines/obs_version.txt']
0f9675ba
FC
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)
edeef6f9 41 system('pkill -f "pmachines.exe"')
0f9675ba
FC
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):
edeef6f9
FC
53 info('test_ref')
54 path = 'pmachines/tests/functional_ref_%s/*.png' % _branch()
0f9675ba
FC
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)
ad5ef398
FC
73 files = glob(str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/*.png' % _branch())
74 self.assertGreater(len(files), 1)
0f9675ba 75 for fname in files:
ad5ef398 76 self.assertTrue(exists(path + basename(fname)), '%s does not exist' % (path + basename(fname)))
0f9675ba
FC
77 self.assertTrue(
78 self.__similar_images(
ad5ef398
FC
79 str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/' % _branch() + basename(fname),
80 path + basename(fname)))
0f9675ba 81
ad5ef398
FC
82 def test_code(self):
83 info('test_code')
84 self.__test_template(
85 '~/venv/bin/python main.py --functional-test 1 ; '
86 '~/venv/bin/python main.py --functional-test 2',
87 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
edeef6f9
FC
88
89 # def test_appimage(self):
90 # info('test_appimage')
91 # bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
92 # bld_branch = '' if bld_branch == 'stable' else ('-' + bld_branch)
93 # self.__test_template(
94 # './dist/Pmachines%s-x86_64.AppImage --functional-test 1 ;'
95 # './dist/Pmachines%s-x86_64.AppImage --functional-test 2' % (bld_branch, bld_branch),
96 # str(Path.home()) + '/.local/share/pmachines/tests/functional/')
97
98 # def test_flatpak(self):
99 # info('test_flatpak')
100 # if environ.get('FUNCTIONALPOST') != '1':
101 # self.skipTest('skipped functional-post tests')
102 # bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
103 # cmd = 'flatpak update -y it.ya2.Pmachines//%s' % bld_branch
104 # info('executing: %s' % cmd)
105 # system(cmd)
106 # info('executed: %s' % cmd)
107 # self.__test_template(
108 # 'flatpak run it.ya2.Pmachines//%s --functional-test 1 ;'
109 # 'flatpak run it.ya2.Pmachines//%s --functional-test 2' % (bld_branch, bld_branch),
110 # str(Path.home()) + '/.var/app/it.ya2.Pmachines/data/pmachines/tests/functional/')
111
112 # def __update_itchio(self):
113 # system('/home/flavio/.itch/itch')
114 # sleep(5)
115 # system('xdotool mousemove 1280 620')
116 # sleep(1)
117 # system('xdotool click 1')
118 # sleep(300)
119 # system('killall itch')
120
121 # def test_itchio(self):
122 # info('test_itchio')
123 # if environ.get('FUNCTIONALPOST') != '1':
124 # self.skipTest('skipped functional-post tests')
125 # if _branch() != 'master':
126 # return
127 # self.__update_itchio()
128 # self.__test_template(
129 # '/home/flavio/.config/itch/apps/pmachines/pmachines --functional-test 1 ;'
130 # '/home/flavio/.config/itch/apps/pmachines/pmachines --functional-test 2',
131 # str(Path.home()) + '/.local/share/pmachines/tests/functional/')
132
133 # def test_windows(self):
134 # info('test_windows')
135 # system('pkill -f "pmachines.exe"')
136 # abspath = str(Path(__file__).parent.parent) + '/build/win_amd64/pmachines.exe'
137 # self.__test_template(
138 # 'timeout 720s wine %s --functional-test 1 ; '
139 # 'timeout 720s wine %s --functional-test 2' % (abspath, abspath),
140 # str(Path.home()) + '/.wine/drive_c/users/flavio/AppData/Local/pmachines/tests/functional/')
141
142 # def test_versions(self):
143 # info('test_versions')
144 # if environ.get('FUNCTIONAL') != '1':
145 # self.skipTest('skipped functional tests')
146 # bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
147 # with open('/home/flavio/pmachines_builder/last_bld.txt') as f:
148 # lines = f.readlines()
149 # for line in lines:
150 # if line.strip().split()[0] == _branch():
151 # commit = line.strip().split()[1][:7]
152 # _ver = ver
153 # if _branch() == 'stable':
154 # with open('/home/flavio/pmachines_builder/pmachines/assets/version.txt') as fver:
155 # _ver = fver.read().strip() + '-'
156 # exp = '%s-%s' % (_ver, commit)
157 # cmds = [
158 # ('./build/manylinux1_x86_64/pmachines --version', str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
159 # ('./dist/Pmachines-%s-x86_64.AppImage --version' % bld_branch, str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
160 # ('timeout 720s wine ./build/win_amd64/pmachines.exe --version', '/home/flavio/.wine/drive_c/users/flavio/AppData/Local/pmachines/obs_version.txt')
161 # ]
162 # if environ.get('FUNCTIONALPOST') == '1':
163 # if _branch() == 'master':
164 # self.__update_itchio()
165 # cmds += [('/home/flavio/.config/itch/apps/pmachines/pmachines --version', str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt')]
166 # cmds += [('flatpak run it.ya2.Pmachines//%s --version' % bld_branch, '/home/flavio/.var/app/it.ya2.Pmachines/data/pmachines/obs_version.txt')]
167 # system('flatpak update -y it.ya2.Pmachines//%s' % bld_branch)
168 # for cmd in cmds:
169 # if exists(cmd[1]):
170 # remove(cmd[1])
171 # info('launching %s' % cmd[0])
172 # exec_cmd(cmd[0])
173 # with open(cmd[1]) as f:
174 # obs = f.read().strip()
175 # self.assertEqual(obs, exp)