ya2 · news · projects · code · about

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