File: conftest.py

package info (click to toggle)
unknown-horizons 2019.1-8
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 347,924 kB
  • sloc: python: 46,805; xml: 3,137; sql: 1,189; sh: 736; makefile: 39; perl: 35
file content (104 lines) | stat: -rw-r--r-- 3,218 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# ###################################################
# Copyright (C) 2008-2017 The Unknown Horizons Team
# team@unknown-horizons.org
# This file is part of Unknown Horizons.
#
# Unknown Horizons is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# ###################################################

import gettext
import sys
from importlib.abc import Loader, MetaPathFinder
from importlib.machinery import ModuleSpec, PathFinder

import pytest


def pytest_addoption(parser):
	parser.addoption('--long-tests', action='store_true', help='include long-running tests')
	parser.addoption('--gui-tests', action='store_true', help='include gui tests')


def pytest_configure(config):
	config.addinivalue_line('markers', 'long: mark test as long-running')


def pytest_runtest_setup(item):
	"""
	Called for every test, here we skip expensive tests from the default test run.
	"""
	# Skip tests marked as long unless specified otherwise on the command line
	marker = item.get_marker('long')
	if marker is not None and not item.config.getoption('--long-tests'):
		pytest.skip('test is long running')

	# Skip gui tests unless specified otherwise on the command line
	marker = item.get_marker('gui_test')
	if marker is not None and not item.config.getoption('--gui-tests'):
		pytest.skip('test is gui test')


# Basic test setup, installs global mock for fife so we can run gui/game tests.
#
# We need to run this code as soon as possible, before any import of game or test code happened,
# and `pytest_namespace` is one of the first things that runs. Because it is called for every
# plugin, we remember whether we installed the mock already.

FIFE_MOCK_INSTALLED = False


def pytest_namespace():
	"""
	This needs to run at first to avoid that other code gets a reference to the real fife
	module.
	"""
	global FIFE_MOCK_INSTALLED
	if FIFE_MOCK_INSTALLED:
		return

	def mock_fife():
		"""
		Using a custom import hook, we catch all imports of fife and provide a
		dummy module.
		"""
		from tests.dummy import Dummy

		class Finder(PathFinder):
			@staticmethod
			def find_spec(fullname, path, target=None):
				if fullname.startswith('fife'):
					return ModuleSpec(fullname, DummyLoader())

		class DummyLoader(Loader):
			@staticmethod
			def load_module(module):
				sys.modules.setdefault(module, Dummy())

		sys.meta_path.insert(0, Finder)

	mock_fife()

	import horizons.globals
	import fife
	horizons.globals.fife = fife.fife

	from horizons.util import create_user_dirs
	create_user_dirs()

	import horizons.i18n
	horizons.i18n.change_language()

	FIFE_MOCK_INSTALLED = True