lutris/tests/test_utils.py

190 lines
7.8 KiB
Python
Raw Normal View History

import os
2015-11-13 08:34:11 +00:00
from collections import OrderedDict
from unittest import TestCase
from lutris.util import fileio, strings, system
2022-03-16 04:14:31 +00:00
from lutris.util.steam import vdfutils
2024-02-25 05:14:32 +00:00
from lutris.util.wine import wine
class TestFileUtils(TestCase):
def test_file_ids_are_correctly_transformed(self):
2024-02-25 05:02:06 +00:00
file_id = "foo-bar"
self.assertEqual(system.python_identifier(file_id), "foo-bar")
2024-02-25 05:02:06 +00:00
file_id = "${foo-bar}"
self.assertEqual(system.python_identifier(file_id), "${foo_bar}")
2024-02-25 05:02:06 +00:00
file_id = "${foo-bar} ${a-b}"
self.assertEqual(system.python_identifier(file_id), "${foo_bar} ${a_b}")
2024-02-25 05:02:06 +00:00
file_id = "${foo-bar} a-b"
self.assertEqual(system.python_identifier(file_id), "${foo_bar} a-b")
2024-02-25 05:02:06 +00:00
file_id = "${foo-bar-bang}"
self.assertEqual(system.python_identifier(file_id), "${foo_bar_bang}")
2024-02-25 05:02:06 +00:00
file_id = "${foo-bar bang}"
self.assertEqual(system.python_identifier(file_id), "${foo-bar bang}")
2013-07-11 12:42:30 +00:00
def test_file_ids_are_substitued(self):
2024-02-25 05:02:06 +00:00
fileid = "${foo-bar}"
_files = {"foo-bar": "/foo/bar"}
2013-10-07 07:11:20 +00:00
self.assertEqual(system.substitute(fileid, _files), "/foo/bar")
2013-11-17 04:27:33 +00:00
class TestSteamUtils(TestCase):
def test_dict_to_vdf(self):
2015-11-13 08:34:11 +00:00
appstate = OrderedDict()
userconfig = OrderedDict()
2024-02-25 05:02:06 +00:00
userconfig["gameid"] = "13240"
userconfig["name"] = "Unreal Tournament"
appstate["UserConfig"] = userconfig
appstate["StateFlags"] = "4"
appstate["appID"] = "13240"
2015-11-13 08:34:11 +00:00
dict_data = OrderedDict()
2024-02-25 05:02:06 +00:00
dict_data["AppState"] = appstate
2015-11-13 08:34:11 +00:00
2013-11-17 04:27:33 +00:00
expected_vdf = """"AppState"
{
\t"UserConfig"
\t{
\t\t"gameid"\t\t"13240"
\t\t"name"\t\t"Unreal Tournament"
\t}
\t"StateFlags"\t\t"4"
\t"appID"\t\t"13240"
}"""
2022-03-16 04:14:31 +00:00
vdf_data = vdfutils.to_vdf(dict_data)
2013-11-17 04:27:33 +00:00
self.assertEqual(vdf_data.strip(), expected_vdf.strip())
class TestStringUtils(TestCase):
def test_slugify_with_nonwestern_name(self):
2024-02-25 05:02:06 +00:00
name = "メイド☆ぱらだいす ~目指せ!メイドナンバーワン!~"
slug = strings.slugify(name)
self.assertTrue(len(slug) > 0)
def test_gtk_safe_urls(self):
self.assertEqual(strings.gtk_safe_urls("foo bar"), "foo bar")
self.assertEqual(
strings.gtk_safe_urls("foo http://lutris.net bar"),
2024-02-25 05:02:06 +00:00
'foo <a href="http://lutris.net">http://lutris.net</a> bar',
)
self.assertEqual(
2024-02-25 05:02:06 +00:00
strings.gtk_safe_urls("http://lutris.net"), '<a href="http://lutris.net">http://lutris.net</a>'
)
text = "foo http://lutris.net bar http://strycore.com"
expected = (
'foo <a href="http://lutris.net">http://lutris.net</a> '
'bar <a href="http://strycore.com">http://strycore.com</a>'
)
self.assertEqual(strings.gtk_safe_urls(text), expected)
2016-08-20 03:23:34 +00:00
def test_get_formatted_playtime(self):
2020-09-25 05:08:23 +00:00
self.assertEqual(strings.get_formatted_playtime(None), strings.NO_PLAYTIME)
self.assertEqual(strings.get_formatted_playtime(1.0), "1 hour")
self.assertEqual(strings.get_formatted_playtime(2.0), "2 hours")
2024-02-25 05:02:06 +00:00
self.assertEqual(strings.get_formatted_playtime("1.04"), "1 hour 2 minutes")
self.assertEqual(strings.get_formatted_playtime("-"), strings.NO_PLAYTIME)
self.assertEqual(strings.get_formatted_playtime(0.5), "30 minutes")
2023-08-26 00:00:39 +00:00
self.assertEqual(strings.get_formatted_playtime(1.5), "1 hour 30 minutes")
self.assertEqual(strings.get_formatted_playtime(45.90), "45 hours 54 minutes")
2016-08-20 03:23:34 +00:00
def test_parse_playtime(self):
self.assertEqual(strings.parse_playtime("0"), 0)
self.assertEqual(strings.parse_playtime("2.5"), 2.5)
self.assertEqual(strings.parse_playtime("30m"), 0.5)
self.assertEqual(strings.parse_playtime("30 min"), 0.5)
self.assertEqual(strings.parse_playtime("30 minutes"), 0.5)
self.assertEqual(strings.parse_playtime("1 hour"), 1)
self.assertEqual(strings.parse_playtime("1h"), 1)
self.assertEqual(strings.parse_playtime("1 hour 30 minutes"), 1.5)
self.assertEqual(strings.parse_playtime("1hour 30minutes"), 1.5)
self.assertEqual(strings.parse_playtime("1hour30minutes"), 1.5)
self.assertEqual(strings.parse_playtime("1hour30minute"), 1.5)
self.assertEqual(strings.parse_playtime("1HoUr30MiNuTe"), 1.5)
self.assertEqual(strings.parse_playtime("1h30min"), 1.5)
self.assertEqual(strings.parse_playtime("1h30m"), 1.5)
self.assertEqual(strings.parse_playtime("1H30M"), 1.5)
self.assertEqual(strings.parse_playtime("1 h 30 m"), 1.5)
self.assertEqual(strings.parse_playtime("2h45m"), 2.75)
self.assertEqual(strings.parse_playtime("2h45"), 2.75)
self.assertEqual(strings.parse_playtime("2:45"), 2.75)
2016-08-20 03:23:34 +00:00
class TestVersionSort(TestCase):
def test_parse_version(self):
2024-02-25 05:02:06 +00:00
self.assertEqual(wine.parse_wine_version("3.6-staging"), ([3, 6], "-staging", ""))
2016-08-20 03:23:34 +00:00
def test_versions_are_correctly_sorted(self):
2024-02-25 05:02:06 +00:00
versions = ["1.8", "1.7.4", "1.9.1", "1.9.10", "1.9.4"]
versions = wine.version_sort(versions)
2024-02-25 05:02:06 +00:00
self.assertEqual(versions[0], "1.7.4")
self.assertEqual(versions[1], "1.8")
self.assertEqual(versions[2], "1.9.1")
self.assertEqual(versions[3], "1.9.4")
self.assertEqual(versions[4], "1.9.10")
2016-08-20 03:23:34 +00:00
def test_version_sorting_supports_extra_strings(self):
2024-02-25 05:02:06 +00:00
versions = ["1.8", "1.8-staging", "1.7.4", "1.9.1", "1.9.10-staging", "1.9.10", "1.9.4", "staging-1.9.4"]
versions = wine.version_sort(versions)
2024-02-25 05:02:06 +00:00
self.assertEqual(versions[0], "1.7.4")
self.assertEqual(versions[1], "1.8")
self.assertEqual(versions[2], "1.8-staging")
self.assertEqual(versions[3], "1.9.1")
self.assertEqual(versions[4], "1.9.4")
self.assertEqual(versions[5], "staging-1.9.4")
self.assertEqual(versions[6], "1.9.10")
self.assertEqual(versions[7], "1.9.10-staging")
2016-08-20 03:23:34 +00:00
def test_versions_can_be_reversed(self):
2024-02-25 05:02:06 +00:00
versions = ["1.9", "1.6", "1.7", "1.8"]
versions = wine.version_sort(versions, reverse=True)
2024-02-25 05:02:06 +00:00
self.assertEqual(versions[0], "1.9")
self.assertEqual(versions[3], "1.6")
class TestEvilConfigParser(TestCase):
def setUp(self):
2024-02-25 05:02:06 +00:00
self.config_path = os.path.join(os.path.dirname(__file__), "test.ini")
def tearDown(self):
if os.path.exists(self.config_path):
os.remove(self.config_path)
def test_config_parse_can_write_to_disk(self):
parser = fileio.EvilConfigParser(dict_type=fileio.MultiOrderedDict)
2024-02-25 05:02:06 +00:00
parser.add_section("Test")
parser.set("Test", "key", "value")
with open(self.config_path, "wb") as config:
parser.write(config)
2016-10-20 22:48:33 +00:00
class TestUnpackDependencies(TestCase):
def test_single_dependency(self):
2024-02-25 05:02:06 +00:00
string = "quake"
2016-10-20 22:48:33 +00:00
dependencies = strings.unpack_dependencies(string)
2024-02-25 05:02:06 +00:00
self.assertEqual(dependencies, ["quake"])
2016-10-20 22:48:33 +00:00
def test_multiple_dependencies(self):
2024-02-25 05:02:06 +00:00
string = "quake, quake-1,quake-steam, quake-gog "
2016-10-20 22:48:33 +00:00
dependencies = strings.unpack_dependencies(string)
2024-02-25 05:02:06 +00:00
self.assertEqual(dependencies, ["quake", "quake-1", "quake-steam", "quake-gog"])
2016-10-20 22:48:33 +00:00
def test_dependency_options(self):
2024-02-25 05:02:06 +00:00
string = "quake, quake-1,quake-steam | quake-gog|quake-humble "
2016-10-20 22:48:33 +00:00
dependencies = strings.unpack_dependencies(string)
2024-02-25 05:02:06 +00:00
self.assertEqual(dependencies, ["quake", "quake-1", ("quake-steam", "quake-gog", "quake-humble")])
2016-10-20 22:48:33 +00:00
def test_strips_extra_commas(self):
2024-02-25 05:02:06 +00:00
string = ", , , ,, ,,,,quake, quake-1,quake-steam | quake-gog|quake-humble |||| , |, | ,|,| , "
2016-10-20 22:48:33 +00:00
dependencies = strings.unpack_dependencies(string)
2024-02-25 05:02:06 +00:00
self.assertEqual(dependencies, ["quake", "quake-1", ("quake-steam", "quake-gog", "quake-humble")])
2017-12-14 06:37:07 +00:00
class TestSubstitute(TestCase):
def test_can_sub_game_files_with_dashes_in_key(self):
2024-02-25 05:02:06 +00:00
replacements = {"steam-data": "/tmp"}
self.assertEqual(system.substitute("--path=$steam-data", replacements), "--path=/tmp")