"""installer/ bookkeeping with fake steps: fresh install, resume after a failure or cancel, transient steps, update
with a newer release, totk.ini and the event stream. No game files, no tools; stdlib only.
Run: python tests/installer_test.py
"""
import http.client
import io
import json
import re
import sys
import tempfile
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest import mock
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
from installer import ini, runner, screens, state, steps # noqa: E402
from installer.events import Reporter # noqa: E402
from installer import layout as layouts # noqa: E402
from installer.layout import Layout # noqa: E402
class Fake(runner.Step):
"""A step whose output is
/.out holding its key; dir is the temp folder for a transient step."""
def __init__(self, id, transient=False, always=False, previous=None):
self.id, self.transient, self.always, self.previous = id, transient, always, previous
def out(self, ctx):
return (ctx.temp if self.transient else ctx.dest) / f'{self.id}.out'
def key(self, ctx):
upstream = ctx.keys[self.previous] if self.previous else ''
return f'{self.id}:{ctx.options.inputs.get(self.id, "v1")}:{upstream}'
def current(self, ctx, record):
p = self.out(ctx)
return p.is_file() and p.read_text() == ctx.keys[self.id]
def verify(self, ctx, record):
ctx.options.verified.append(self.id)
return ctx.options.verify_ok.get(self.id, True)
def run(self, ctx):
o = ctx.options
o.ran.append(self.id)
if self.id in ctx.forced:
o.forced.append(self.id)
if self.id == 'check':
ctx.verify_kept(self.id)
failure = o.fail.pop(self.id, None)
if failure == 'error':
raise runner.InstallError('fake.failed', step=self.id)
if failure == 'cancel':
raise KeyboardInterrupt
if self.id == 'cleanup':
for p in ctx.temp.glob('*.out'):
p.unlink()
return {}
p = self.out(ctx)
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(ctx.keys[self.id])
return {'note': self.id}
def fake_steps():
return [Fake('check', always=True), Fake('extract'), Fake('lift', transient=True, previous='extract'),
Fake('compile', previous='lift'), Fake('prepare', previous='compile'), Fake('cleanup', always=True)]
class Bed:
"""A dest and temp folder plus one run of the fake steps per call."""
def __init__(self, base):
self.dest, self.temp = base / 'dest', base / 'temp'
self.temp.mkdir()
def run(self, release='1.0', inputs=None, fail=None, verify_ok=None, verify=False):
layout = Layout(root=Path('.'), dev=True, release=release, python=Path('python'), tools=Path('tools'),
recomp=Path('recomp'), llvm=Path('llvm'),
deps=Path('deps'), runtime=Path('rt'), runtime_lib=Path('lib'), ninja=Path('ninja'),
hactool=Path('hactool'), xlate=Path('xlate'))
options = SimpleNamespace(inputs=inputs or {}, fail=dict(fail or {}), verify_ok=verify_ok or {}, ran=[],
verified=[], forced=[])
out = io.StringIO()
ctx = runner.Context(layout, self.dest, self.temp, Reporter(out=out), options, state=state.load(self.dest),
verify=verify)
mode = ctx.mode
result = runner.run_steps(fake_steps(), ctx)
events = [json.loads(line) for line in out.getvalue().splitlines()]
return SimpleNamespace(result=result, ran=options.ran, verified=options.verified, forced=options.forced,
events=events, mode=mode,
state=state.load(self.dest))
@staticmethod
def skipped(r):
return {e['step']: e['reason'] for e in r.events if e['type'] == 'step' and e['state'] == 'skipped'}
ALL = ['check', 'extract', 'lift', 'compile', 'prepare', 'cleanup']
class RunnerTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.bed = Bed(Path(self.tmp.name))
def tearDown(self):
self.tmp.cleanup()
def test_fresh_install_runs_everything_and_records_it(self):
r = self.bed.run()
self.assertEqual((r.result, r.mode, r.ran), ('ok', 'install', ALL))
self.assertEqual(set(r.state['steps']), {'extract', 'lift', 'compile', 'prepare'})
self.assertEqual(r.state['steps']['compile']['note'], 'compile')
self.assertEqual(r.state['release'], '1.0')
started = [e['step'] for e in r.events if e['type'] == 'step' and e['state'] == 'started']
done = [e['step'] for e in r.events if e['type'] == 'step' and e['state'] == 'done']
self.assertEqual((started, done), (ALL, ALL))
def test_rerun_skips_finished_steps_and_unneeded_transient(self):
self.bed.run()
r = self.bed.run()
self.assertEqual((r.result, r.mode, r.ran), ('ok', 'resume', ['check', 'cleanup']))
self.assertEqual(self.bed.skipped(r), {'extract': 'current', 'lift': 'not_needed', 'compile': 'current',
'prepare': 'current'})
self.assertEqual(r.verified, []) # same release: no full checks
def test_resume_after_failure(self):
r = self.bed.run(fail={'compile': 'error'})
self.assertEqual((r.result, r.ran), ('failed', ['check', 'extract', 'lift', 'compile']))
error = [e for e in r.events if e['type'] == 'error'][0]
self.assertEqual((error['id'], error['step'], error['params']), ('fake.failed', 'compile', {'step': 'compile'}))
self.assertEqual(set(r.state['steps']), {'extract', 'lift'})
r = self.bed.run()
# lift's output is still in the temp folder (no cleanup ran), so only compile and later run.
self.assertEqual((r.result, r.mode, r.ran), ('ok', 'resume', ['check', 'compile', 'prepare', 'cleanup']))
def test_resume_after_cancel_with_transient_output_gone(self):
r = self.bed.run(fail={'prepare': 'cancel'})
self.assertEqual(r.result, 'cancelled')
self.assertNotIn('prepare', r.state['steps'])
for p in self.bed.temp.glob('*.out'):
p.unlink()
r = self.bed.run()
self.assertEqual(r.ran, ['check', 'prepare', 'cleanup'])
self.assertEqual(self.bed.skipped(r)['lift'], 'not_needed')
def test_failed_check_writes_nothing(self):
r = self.bed.run(fail={'check': 'error'})
self.assertEqual(r.result, 'failed')
self.assertFalse(self.bed.dest.exists())
def test_removed_output_runs_again(self):
self.bed.run()
(self.bed.dest / 'compile.out').unlink()
r = self.bed.run()
self.assertEqual(r.ran, ['check', 'lift', 'compile', 'cleanup'])
def test_update_with_new_lifter_keeps_extraction(self):
self.bed.run(release='1.0')
r = self.bed.run(release='1.1', inputs={'lift': 'v2'})
self.assertEqual((r.result, r.mode), ('ok', 'update'))
self.assertEqual(r.ran, ['check', 'lift', 'compile', 'prepare', 'cleanup'])
self.assertEqual(r.verified, ['extract']) # kept from another release: full check
self.assertEqual(r.state['steps']['extract']['release'], '1.0')
self.assertEqual(r.state['steps']['compile']['release'], '1.1')
self.assertEqual(r.state['release'], '1.1')
def test_update_that_changes_nothing_skips_the_rebuild(self):
self.bed.run(release='1.0')
r = self.bed.run(release='1.1')
self.assertEqual(r.ran, ['check', 'cleanup'])
self.assertEqual(r.verified, ['extract', 'compile', 'prepare'])
def test_update_with_changed_kept_files_extracts_again(self):
self.bed.run(release='1.0')
r = self.bed.run(release='1.1', verify_ok={'extract': False})
self.assertEqual(r.ran, ['check', 'extract', 'cleanup'])
self.assertEqual(r.forced, ['extract']) # the step knows not to trust what it left behind
warnings = [e for e in r.events if e['type'] == 'warning']
self.assertEqual([(w['id'], w['params']) for w in warnings], [('state.output_changed', {'step': 'extract'})])
def test_verify_option_checks_same_release(self):
self.bed.run()
r = self.bed.run(verify=True)
self.assertEqual(r.verified, ['extract', 'compile', 'prepare'])
def test_state_errors(self):
self.bed.dest.mkdir()
(self.bed.dest / state.NAME).write_text('{not json')
with self.assertRaises(state.StateError) as e:
state.load(self.bed.dest)
self.assertEqual(e.exception.id, 'state.unreadable')
(self.bed.dest / state.NAME).write_text(json.dumps({'format': 99, 'steps': {}}))
with self.assertRaises(state.StateError) as e:
state.load(self.bed.dest)
self.assertEqual(e.exception.id, 'state.newer_format')
class ReleaseFilesTest(unittest.TestCase):
def test_release_ships_every_tool_the_steps_run(self):
sys.path.insert(0, str(ROOT / 'tools'))
import make_release
text = (ROOT / 'installer' / 'steps.py').read_text(encoding='utf-8')
used = set(re.findall(r"layout\.tools / '(\w+\.py)'", text))
for names in re.findall(r"layout\.tools / n for n in \((.*?)\)", text, re.S):
used |= set(re.findall(r"'(\w+\.py)'", names))
self.assertIn('cheats.py', used)
self.assertEqual(used - set(make_release.TOOLS_FILES), set())
class RenamedVersionTest(unittest.TestCase):
"""An install the setup before 0.3.1 recorded as 1.1.1 is the 1.1.0 update (#332)."""
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
root = Path(__file__).resolve().parents[1]
layout = Layout(root=root, dev=True, release='9.9', python=Path('python'), tools=root / 'tools',
recomp=root / 'recomp', llvm=Path('llvm'), deps=Path('deps'), runtime=Path('rt'),
runtime_lib=Path('lib'), ninja=Path('ninja'), hactool=Path('hactool'), xlate=Path('xlate'))
state = {'format': 1, 'release': '0.2.8', 'version': '1.1.1', 'inputs': {},
'steps': {'extract': {'key': 'recorded by 0.2.8', 'release': '0.2.8'}}}
out = io.StringIO()
self.ctx = runner.Context(layout, Path(tmp.name), Path(tmp.name), Reporter(out=out), SimpleNamespace(),
state=state)
def test_old_name_is_read_as_1_1_0(self):
steps.migrate_state(self.ctx)
self.assertEqual(steps.game_version(self.ctx), '1.1.0')
self.assertEqual(steps.version_tag(steps.game_version(self.ctx)), 'v110')
self.assertEqual(steps.expected_update(self.ctx)['display_version'], '1.1.0')
self.assertTrue(self.ctx.layout.patches(steps.game_version(self.ctx)).is_file())
def test_extraction_is_kept_under_the_new_name(self):
steps.migrate_state(self.ctx)
self.assertEqual(self.ctx.state['steps']['extract']['key'], steps.Extract().key(self.ctx))
self.assertEqual(self.ctx.state['steps']['extract']['release'], '0.2.8') # full check of kept output runs
def test_other_versions_are_untouched(self):
self.ctx.state['version'] = '1.4.3'
steps.migrate_state(self.ctx)
self.assertEqual(self.ctx.state['version'], '1.4.3')
self.assertEqual(self.ctx.state['steps']['extract']['key'], 'recorded by 0.2.8')
class LiftKeyTest(unittest.TestCase):
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.dest = Path(tmp.name)
layout = SimpleNamespace(recomp=self.dest / 'no-recomp', tools=self.dest / 'no-tools',
patches=lambda version: self.dest / 'no-patches')
self.ctx = SimpleNamespace(dest=self.dest, layout=layout, keys={'extract': 'x'}, state={'version': '1.4.3'})
def key(self):
return steps.Lift().key(self.ctx)
def add_cheats(self, name, text, mod='Cheats', where='cheats'):
folder = self.dest / 'mods' / mod / where
folder.mkdir(parents=True, exist_ok=True)
(folder / name).write_text(text)
def test_extracting_main_keeps_the_key(self):
before = self.key()
(self.dest / 'exefs').mkdir()
(self.dest / 'exefs' / 'main').write_bytes(bytes(0x48))
self.assertEqual(self.key(), before)
def test_cheat_files_change_the_key(self):
before = self.key()
self.add_cheats('277178B7DBA1B6D4.txt', '[a]')
added = self.key()
self.assertNotEqual(added, before)
self.add_cheats('277178B7DBA1B6D4.txt', '[b]')
edited = self.key()
self.assertNotEqual(edited, added)
self.add_cheats('277178B7DBA1B6D4.txt', '[c]', mod='Other', where='atmosphere/contents/0100F2C0115B6000/cheats')
self.assertNotEqual(self.key(), edited)
def test_exefs_patch_files_change_the_key(self):
before = self.key()
self.add_cheats('dynamic-fps.pchtxt', '@nsobid-277178B7DBA1B6D4', mod='Fps', where='exefs')
added = self.key()
self.assertNotEqual(added, before)
self.add_cheats('277178B7DBA1B6D4.ips', 'PATCHEOF', mod='Ips', where='atmosphere/exefs_patches/x')
both = self.key()
self.assertNotEqual(both, added)
self.add_cheats('main.npdm', 'not a patch', mod='Fps', where='exefs')
self.assertEqual(self.key(), both)
def test_renaming_a_mod_folder_changes_the_key(self):
# tools/cheats.py and tools/exefs_patches.py name a patch set after "/": a renamed
# folder needs a lift even though its files are the same
self.add_cheats('277178B7DBA1B6D4.txt', '[a]', mod='MyNXCheats')
self.add_cheats('dynamic-fps.pchtxt', '@nsobid-277178B7DBA1B6D4', mod='Fps', where='exefs')
before = self.key()
mods = self.dest / 'mods'
(mods / 'MyNXCheats').rename(mods / 'MyNXCheats 1.4.3')
cheats_renamed = self.key()
self.assertNotEqual(cheats_renamed, before)
(mods / 'Fps').rename(mods / 'Fps 2')
self.assertNotEqual(self.key(), cheats_renamed)
def test_status_and_other_files_keep_the_key(self):
self.add_cheats('277178B7DBA1B6D4.txt', '[a]')
before = self.key()
(self.dest / 'build-cache').mkdir()
(self.dest / 'build-cache' / 'cheats.status').write_text('written by lift')
self.add_cheats('readme.md', 'not a cheat file')
(self.dest / 'mods' / 'Cheats' / 'romfs').mkdir()
(self.dest / 'mods' / 'Cheats' / 'romfs' / 'a.txt').write_text('a RomFS file')
self.assertEqual(self.key(), before)
class LiftRunTest(unittest.TestCase):
"""Lift.run with fake tools: the ExeFS patch stage writes the status and data files, the lift lists the bytes it
read, and the data writes are checked against that list after it; a failed lift or check drops both files."""
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
base = Path(tmp.name)
self.dest, temp = base / 'install', base / 'temp'
self.dest.mkdir()
temp.mkdir()
layout = SimpleNamespace(dev=True, recomp=ROOT / 'recomp', tools=ROOT / 'tools',
patches=lambda version: base / 'patches-v143.txt')
self.ctx = SimpleNamespace(dest=self.dest, temp=temp, layout=layout, keys={'lift': 'k'},
state={'version': '1.4.3'}, reporter=Reporter(out=io.StringIO()))
self.addCleanup(self.ctx.reporter.close)
mod = self.dest / 'mods' / 'SQ' / 'exefs'
mod.mkdir(parents=True)
(mod / '277178B7DBA1B6D4.ips').write_bytes(b'IPS32EEOF')
self.calls, self.fail = [], set()
saved = steps.run_tool
self.addCleanup(lambda: setattr(steps, 'run_tool', saved))
steps.run_tool = self.run_tool
def run_tool(self, ctx, step, script, args, parse=None, cwd=None):
args = [str(a) for a in args]
name = Path(script).name + (' --folded' if '--folded' in args else '')
self.calls.append((name, args))
log = ctx.temp / 'tool.log'
log.write_text('')
if name in self.fail:
return 1, log
if name == 'exefs_patches.py':
for flag in ('--status', '--data', '--out'):
p = Path(args[args.index(flag) + 1])
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(flag)
elif name == 'gen.py':
out = Path(args[args.index('--out') + 1]) / 'main'
out.mkdir(parents=True, exist_ok=True)
(out / 'folded.txt').write_text('# test\n')
return 0, log
def outputs(self):
return [p.is_file() for p in (steps.exefs_status(self.ctx), steps.exefs_data(self.ctx))]
def test_data_file_and_the_check_after_the_lift(self):
steps.Lift().run(self.ctx)
self.assertEqual([c[0] for c in self.calls], ['exefs_patches.py', 'gen.py', 'exefs_patches.py --folded'])
build = self.calls[0][1]
self.assertEqual(build[build.index('--data') + 1], str(steps.exefs_data(self.ctx)))
check = self.calls[2][1]
self.assertEqual(check[check.index('--folded') + 1], str(self.ctx.temp / 'gen' / 'main' / 'folded.txt'))
self.assertEqual(check[check.index('--status') + 1], str(steps.exefs_status(self.ctx)))
self.assertEqual(self.outputs(), [True, True])
def test_failed_lift_drops_the_data_file(self):
self.fail = {'gen.py'}
with self.assertRaises(steps.InstallError):
steps.Lift().run(self.ctx)
self.assertEqual(self.outputs(), [False, False])
def test_failed_check_drops_both_files(self):
self.fail = {'exefs_patches.py --folded'}
steps.Lift().run(self.ctx)
self.assertEqual(self.outputs(), [False, False])
self.assertIn('warn.exefs_skipped', self.ctx.reporter.out.getvalue())
def plugin_setup(self, kind, more=()):
"""A mod with an exlaunch plugin (SQ), and one more mod per (name, kind) of more; the fake service run writes
each plugin's status line of its kind."""
kinds = {'SQ': kind, **dict(more)}
for name in kinds:
(self.dest / 'mods' / name / 'exefs').mkdir(parents=True, exist_ok=True)
(self.dest / 'mods' / name / 'exefs' / 'subsdk9').write_bytes(b'NSO0' + name.encode())
plugin = self.dest / 'mods' / 'SQ' / 'exefs' / 'subsdk9'
dll = self.dest.parent / 'release-bin' / 'SDL3.dll'
dll.parent.mkdir(exist_ok=True)
dll.write_bytes(b'MZ')
(self.ctx.temp / 'build').mkdir(parents=True, exist_ok=True)
self.ctx.layout = SimpleNamespace(**vars(self.ctx.layout), llvm='llvm', runtime='runtime', ninja='ninja', dlls=[dll],
deps=None, sysroot=None, runtime_lib_for=lambda cpu: None,
pgo=lambda version: None, expected=lambda version: 'expected.json')
saved = self.run_tool
def run_tool(ctx, step, script, args, parse=None, cwd=None):
code, log = saved(ctx, step, script, args, parse, cwd)
args = [str(a) for a in args]
if Path(script).name == 'exefs_plugins.py':
Path(args[args.index('--patches-out') + 1]).write_text('patches\n')
status = Path(args[args.index('--status') + 1])
status.parent.mkdir(parents=True, exist_ok=True)
lines = []
for name, k in kinds.items():
h = 0x811C9DC5 # FNV-1a 32 of "/exefs/subsdk9", as tools/exefs_plugins.py keys it
for b in f'{name}/exefs/subsdk9'.encode():
h = ((h ^ b) * 0x01000193) & 0xFFFFFFFF
lines.append(f'{h:08X}\t00000000\t{k}\t-\tnote\n')
status.write_text(''.join(lines))
return (1 if 'unsupported' in kinds.values() else 0), log
return code, log
steps.run_tool = run_tool
return plugin
def test_plugin_stage(self):
# the lift with the plugin, a totk.exe of it, the service run, then the lift with its hook sites
plugin = self.plugin_setup('plugin')
steps.Lift().run(self.ctx)
self.assertEqual([c[0] for c in self.calls], ['exefs_patches.py', 'gen.py', 'build_game.py',
'exefs_plugins.py', 'gen.py', 'exefs_patches.py --folded'])
first, last = self.calls[1][1], self.calls[4][1]
self.assertEqual(first[first.index('--plugins') + 1], str(plugin))
self.assertEqual(last[last.index('--plugins') + 1], str(plugin))
self.assertEqual(last[last.index('--patches') + 1], str(self.ctx.temp / 'patches-plugins.txt'))
self.assertTrue(steps.plugins_status(self.ctx).is_file())
# the service run's totk.exe finds its DLLs beside it
self.assertTrue((self.ctx.temp / 'build' / 'SDL3.dll').is_file())
def test_refused_plugin_is_not_lifted(self):
# a refused plugin: its status line stays (the Mods tab says why), the game is lifted without it
self.plugin_setup('unsupported')
steps.Lift().run(self.ctx)
last = self.calls[4][1]
self.assertNotIn('--plugins', last)
self.assertTrue(steps.plugins_status(self.ctx).is_file())
self.assertNotIn('warn.plugins_skipped', self.ctx.reporter.out.getvalue())
def test_refused_plugin_stays_in_the_layout_of_a_built_one(self):
# one plugin built, one refused: the lift with the hook sites keeps both in the layout (the records hold the
# addresses of the service runs' layout); the refused one has no patch set, its status line says why
self.plugin_setup('plugin', [('ZZ', 'unsupported')])
steps.Lift().run(self.ctx)
last = self.calls[4][1]
mods = self.dest / 'mods'
self.assertEqual(last[last.index('--plugins') + 1],
','.join(str(mods / n / 'exefs' / 'subsdk9') for n in ('SQ', 'ZZ')))
self.assertIn('unsupported', steps.plugins_status(self.ctx).read_text())
self.assertNotIn('warn.plugins_skipped', self.ctx.reporter.out.getvalue())
def test_failed_plugin_build_warns(self):
self.plugin_setup('plugin')
self.fail = {'build_game.py'}
steps.Lift().run(self.ctx)
self.assertNotIn('--plugins', self.calls[-2][1])
self.assertFalse(steps.plugins_status(self.ctx).is_file())
self.assertIn('warn.plugins_skipped', self.ctx.reporter.out.getvalue())
class CompileKeyTest(unittest.TestCase):
"""The compile key follows the release string: the exe carries the port version of the release that built it."""
def key(self, release):
layout = SimpleNamespace(release=release, pgo=lambda version: None)
ctx = SimpleNamespace(layout=layout, keys={'lift': 'L'}, state={'version': '1.4.3'})
with mock.patch.object(steps, 'runtime_key', return_value='R'):
return steps.Compile().key(ctx)
def test_new_release_compiles_again(self):
self.assertEqual(self.key('0.4.2+abc'), self.key('0.4.2+abc'))
self.assertNotEqual(self.key('0.4.2+abc'), self.key('0.4.3+def'))
class CompileLockedTest(unittest.TestCase):
"""Compile.run when the linker cannot write its totk.exe: another program holds the file (a game started from the
temp build folder, an antivirus that quarantined it), which gets its own message instead of the plain failure."""
LLD = ("ld.lld: error: failed to write output 'temp/totk-1/build/totk.exe': Permission denied\n"
"clang-23: error: unable to remove file: Permission denied\n")
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
base = Path(tmp.name)
dest, temp = base / 'install', base / 'temp'
dest.mkdir()
temp.mkdir()
layout = SimpleNamespace(dev=True, tools=ROOT / 'tools', llvm=base / 'llvm', deps=None, sysroot=None, runtime=base,
ninja=base / 'ninja', dlls=[], runtime_lib_for=lambda cpu: None,
pgo=lambda version: None, expected=lambda version: base / 'expected.json')
self.ctx = SimpleNamespace(dest=dest, temp=temp, layout=layout, state={'version': '1.4.3'},
reporter=Reporter(out=io.StringIO()))
self.addCleanup(self.ctx.reporter.close)
self.log_text = ''
saved = steps.run_tool
self.addCleanup(lambda: setattr(steps, 'run_tool', saved))
steps.run_tool = self.run_tool
def run_tool(self, ctx, step, script, args, parse=None, cwd=None):
log = ctx.temp / 'compile.log'
log.write_text(self.log_text, encoding='utf-8')
return 1, log
def test_locked_link_output(self):
self.log_text = 'link: 0.8 s\n' + self.LLD
with self.assertRaises(steps.InstallError) as e:
steps.Compile().run(self.ctx)
self.assertEqual(e.exception.id, 'compile.exe_locked')
self.assertTrue(e.exception.params['path'].endswith('totk' + steps.EXE))
def test_other_failure(self):
self.log_text = 'runtime/x.c:1:1: error: expected expression\n'
with self.assertRaises(steps.InstallError) as e:
steps.Compile().run(self.ctx)
self.assertEqual(e.exception.id, 'compile.failed')
def test_message_in_every_language(self):
for path in sorted((ROOT / 'installer' / 'text').glob('*.json')):
text = json.loads(path.read_text(encoding='utf-8'))
self.assertIn('compile.exe_locked', text, path.name)
self.assertIn('{path}', text['compile.exe_locked'], path.name)
self.assertIn('{log}', text['compile.exe_locked'], path.name)
class IniTest(unittest.TestCase):
EXAMPLE = '[display]\n# vk_present = mailbox\n\n[language]\n# locale = en-US\n# lang = en-US\n# x = 1\n'
def test_example_line_is_activated(self):
out = ini.set_language(self.EXAMPLE, 'ru')
self.assertIn('\nlang = ru\n', out)
self.assertNotIn('# lang', out)
self.assertIn('# locale = en-US', out)
def test_active_line_is_rewritten_and_the_rest_kept(self):
text = '[display]\nvk_present = fifo\n[language]\n LANG = ja \nfps60 = 0\n'
out = ini.set_language(text, 'de')
self.assertEqual(out, '[display]\nvk_present = fifo\n[language]\n lang = de\nfps60 = 0\n')
self.assertEqual(ini.get_language(out), 'de')
def test_missing_section_is_added(self):
self.assertEqual(ini.set_language('fps60 = 0\n', 'fr'), 'fps60 = 0\n\n[language]\nlang = fr\n')
self.assertEqual(ini.set_language('[language]\nlocale = de-DE\n', 'fr'),
'[language]\nlang = fr\nlocale = de-DE\n')
def test_old_chinese_codes_are_written_as_the_canonical_ones(self):
self.assertEqual(ini.canonical_language('zh-CN'), 'zh-Hans')
self.assertEqual(ini.canonical_language('zh-TW'), 'zh-Hant')
self.assertEqual(ini.canonical_language('ja'), 'ja')
self.assertNotIn('zh-CN', ini.LANGUAGES)
self.assertNotIn('zh-TW', ini.LANGUAGES)
out = ini.set_language(self.EXAMPLE, 'zh-CN')
self.assertEqual(ini.get_language(out), 'zh-Hans')
self.assertEqual(ini.get_language(ini.set_language(out, 'zh-TW')), 'zh-Hant')
def test_write_keeps_user_lines(self):
with tempfile.TemporaryDirectory() as d:
d = Path(d)
example = d / 'totk.ini.example'
example.write_text(self.EXAMPLE)
path = ini.write(d / '', 'en-GB', example)
self.assertEqual(ini.get_language(path.read_text()), 'en-GB')
path.write_text(path.read_text().replace('# vk_present = mailbox', 'vk_present = fifo'))
ini.write(d, 'ja', example)
text = path.read_text()
self.assertIn('vk_present = fifo', text)
self.assertEqual(ini.get_language(text), 'ja')
class CleanupTest(unittest.TestCase):
"""The real Cleanup step on a finished install whose romfs/ is intact, emptied or gone (#107)."""
def setUp(self):
self.steps = steps
self.tmp = tempfile.TemporaryDirectory()
base = Path(self.tmp.name)
self.dest, temp = base / 'dest', base / 'temp'
(self.dest / 'romfs').mkdir(parents=True)
temp.mkdir()
image = b'romfs image'
(self.dest / 'romfs' / 'romfs.bin').write_bytes(image)
digest = steps.extract.sha256(self.dest / 'romfs' / 'romfs.bin')
(self.dest / 'romfs' / 'index.json').write_text(json.dumps({'romfs_size': len(image), 'romfs_sha256': digest}))
self.expected = {'romfs': {'size': len(image), 'sha256': digest}}
self.ctx = SimpleNamespace(reporter=Reporter(out=io.StringIO(), log_path=base / 'x.log'), dest=self.dest,
temp=temp, lang='en-US', state={},
layout=SimpleNamespace(ini_example=ROOT / 'totk.ini.example', dev=False,
python=base / 'python' / 'python.exe', root=base / 'release'))
self.saved = steps.expected_update
steps.expected_update = lambda ctx: self.expected
def tearDown(self):
self.steps.expected_update = self.saved
self.ctx.reporter.close()
self.tmp.cleanup()
def run_cleanup(self):
try:
self.steps.Cleanup().run(self.ctx)
return 'ok'
except self.steps.InstallError as e:
return e.id
def test_intact_romfs_passes(self):
self.assertEqual(self.run_cleanup(), 'ok')
def test_keeps_build_and_writes_rebuild_command(self):
for name in ('gen', 'build', 'check'):
(self.ctx.temp / name).mkdir()
self.assertEqual(self.run_cleanup(), 'ok')
self.assertTrue((self.ctx.temp / 'gen').is_dir() and (self.ctx.temp / 'build').is_dir())
self.assertFalse((self.ctx.temp / 'check').exists())
lines = (self.dest / 'build-cache' / 'rebuild.txt').read_text(encoding='utf-8').splitlines()
self.assertEqual(lines[1:], ['-c', steps.REBUILD_BOOT, str(self.ctx.layout.root), '--root',
str(self.ctx.layout.root), '--rebuild', str(self.dest)])
def test_appimage_rebuild_command_starts_the_appimage(self):
# The AppImage's mount, where its Python lives, is gone once the setup exits (#560, #403)
appimage = Path(self.tmp.name) / 'TotK-Setup-x86_64.AppImage'
appimage.write_bytes(b'')
with mock.patch.dict('os.environ', {'APPIMAGE': str(appimage)}):
self.assertEqual(self.run_cleanup(), 'ok')
lines = (self.dest / 'build-cache' / 'rebuild.txt').read_text(encoding='utf-8').splitlines()
self.assertEqual(lines, [str(appimage.resolve()), '--rebuild', str(self.dest)])
def test_appimage_variable_without_the_file_keeps_the_python_command(self):
with mock.patch.dict('os.environ', {'APPIMAGE': str(Path(self.tmp.name) / 'gone.AppImage')}):
self.assertEqual(self.run_cleanup(), 'ok')
lines = (self.dest / 'build-cache' / 'rebuild.txt').read_text(encoding='utf-8').splitlines()
self.assertEqual(lines[1:3], ['-c', steps.REBUILD_BOOT])
def test_emptied_romfs_fails(self):
for p in (self.dest / 'romfs').iterdir():
p.unlink()
self.assertEqual(self.run_cleanup(), 'cleanup.romfs_unreadable')
def test_missing_romfs_fails(self):
self.steps.remove(self.dest / 'romfs')
self.assertEqual(self.run_cleanup(), 'cleanup.romfs_unreadable')
def test_romfs_of_another_size_fails(self):
(self.dest / 'romfs' / 'romfs.bin').write_bytes(b'short')
self.assertEqual(self.run_cleanup(), 'cleanup.romfs_unreadable')
def fake_release(root, version='1.0+abc'):
"""A folder release_layout reads as a release: VERSION, the Python file and one file per other kit item."""
for name in layouts.KIT_ITEMS:
if '.' in name or name == 'VERSION':
(root / name).parent.mkdir(parents=True, exist_ok=True)
(root / name).write_text(version + '\n' if name == 'VERSION' else name)
else:
(root / name).mkdir(parents=True, exist_ok=True)
(root / name / 'f.txt').write_text(name)
layouts.release_python(root).parent.mkdir(parents=True, exist_ok=True)
layouts.release_python(root).write_bytes(b'')
(root / 'TotK-Setup.exe').write_bytes(b'launcher')
(root / 'installer' / '__pycache__').mkdir()
(root / 'installer' / '__pycache__' / 'x.pyc').write_bytes(b'')
return layouts.release_layout(root)
@unittest.skipUnless(steps.os.name == 'nt', 'the build kit is Windows-only')
class KitTest(unittest.TestCase):
"""The build kit: the setup's release folder copied into the install folder, the later steps and the rebuild
command pointed at it, and an older setup's temp folder taken over."""
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
base = Path(self.tmp.name)
self.setup, self.dest = base / 'TotK-Setup', base / 'game'
self.dest.mkdir()
self.layout = fake_release(self.setup)
self.missing = mock.patch.object(Layout, 'missing', lambda self, cpu='v3': [])
self.missing.start()
def tearDown(self):
self.missing.stop()
self.tmp.cleanup()
def ctx(self, layout):
return SimpleNamespace(layout=layout, dest=self.dest, reporter=Reporter(out=io.StringIO()))
def install(self):
ctx = self.ctx(self.layout)
kit = steps.Kit()
self.assertFalse(kit.current(ctx, {}))
kit.run(ctx)
kit.settle(ctx)
return ctx, kit
def test_copies_the_release_and_the_next_steps_run_from_it(self):
ctx, kit = self.install()
folder = self.dest / 'build-kit'
self.assertEqual(ctx.layout.root, folder.resolve())
self.assertEqual(ctx.layout.release, '1.0+abc')
self.assertEqual(sorted(p.name for p in folder.iterdir()), sorted(layouts.KIT_ITEMS))
self.assertFalse((folder / 'installer' / '__pycache__').exists())
self.assertFalse((self.dest / 'build-kit.new').exists() or (self.dest / 'build-kit.old').exists())
self.assertTrue(kit.current(self.ctx(self.layout), {}))
def test_the_kits_own_rebuild_keeps_it(self):
ctx, kit = self.install()
(self.dest / 'build-kit' / 'tools' / 'mark').write_text('kept')
self.assertTrue(kit.current(ctx, {}))
kit.run(ctx)
kit.settle(ctx)
self.assertEqual((self.dest / 'build-kit' / 'tools' / 'mark').read_text(), 'kept')
def test_a_newer_setup_replaces_the_kit(self):
self.install()
(self.dest / 'build-kit' / 'tools' / 'old-only').write_text('old')
self.layout = fake_release(Path(self.tmp.name) / 'TotK-Setup-2', '1.1+def')
ctx, _ = self.install()
self.assertEqual(ctx.layout.release, '1.1+def')
self.assertFalse((self.dest / 'build-kit' / 'tools' / 'old-only').exists())
def test_rebuild_command_is_relative_to_the_install_folder(self):
ctx, _ = self.install()
cmd = steps.rebuild_command(ctx)
self.assertEqual(cmd, [str(Path('build-kit', 'python', 'pythonw.exe' if (
ctx.layout.python.with_name('pythonw.exe')).is_file() else 'python.exe')), '-c', steps.REBUILD_BOOT,
'build-kit', '--root', 'build-kit', '--rebuild', '.'])
self.assertFalse(any(str(self.dest) in a or str(self.setup) in a for a in cmd))
def test_the_kits_builds_are_portable(self):
ctx, _ = self.install()
ctx.temp, ctx.state = self.dest / 'build-cache' / 'work', {'version': '1.4.3'}
self.assertIn('--portable', steps.build_args(ctx, ctx.temp / 'gen'))
ctx.layout = Layout(root=ROOT, dev=True, release='x', python=Path('python'), tools=Path('tools'),
recomp=Path('recomp'), llvm=Path('llvm'), deps=None, runtime=Path('rt'),
runtime_lib=Path('lib'), ninja=Path('ninja'), hactool=Path('hactool'), xlate=Path('xlate'))
self.assertNotIn('--portable', steps.build_args(ctx, ctx.temp / 'gen'))
def test_dev_mode_has_no_kit(self):
ctx = self.ctx(SimpleNamespace(dev=True, release='x', root=ROOT))
kit = steps.Kit()
self.assertTrue(kit.current(ctx, {}))
self.assertEqual(kit.run(ctx), {})
kit.settle(ctx)
self.assertFalse((self.dest / 'build-kit').exists())
def test_temp_folder_of_an_older_setup_moves_in(self):
old = layouts.temp_folder(self.setup / 'temp', self.dest)
(old / 'gen').mkdir(parents=True)
(old / 'gen' / '.install-key').write_text('k')
(self.dest / 'build-cache').mkdir()
(self.dest / 'build-cache' / 'rebuild.txt').write_text(
'\n'.join(['py', '-c', 'boot', str(self.setup), '--root', str(self.setup), '--rebuild', str(self.dest)]))
temp = layouts.temp_for(self.layout, self.dest)
self.assertEqual(temp, self.dest / 'build-cache' / 'work')
self.assertIn('moved', steps.adopt_old_temp(self.layout, self.dest, temp))
self.assertEqual((temp / 'gen' / '.install-key').read_text(), 'k')
self.assertFalse(old.exists())
self.assertIsNone(steps.adopt_old_temp(self.layout, self.dest, temp)) # once: the new one exists now
def test_no_temp_folder_to_take_over(self):
temp = layouts.temp_for(self.layout, self.dest)
self.assertIsNone(steps.adopt_old_temp(self.layout, self.dest, temp))
(self.dest / 'build-cache').mkdir()
(self.dest / 'build-cache' / 'rebuild.txt').write_text('build-kit\\python\\pythonw.exe\n-c\nboot\nbuild-kit\n')
self.assertIsNone(steps.adopt_old_temp(self.layout, self.dest, temp))
class RuntimeKeyTest(unittest.TestCase):
def test_clangs_folder_is_not_in_the_key(self):
layout = SimpleNamespace(clang=Path('clang'), tools=Path('tools'), runtime=Path('rt'), deps=None, sysroot=None,
dlls=(), runtime_lib_for=lambda cpu: None)
keys = []
for where in ('C:/setup/llvm/bin', 'D:/game/build-kit/llvm/bin'):
out = f'clang version 23\nTarget: x86_64-w64-windows-gnu\nInstalledDir: {where}\nConfiguration file: {where}/x.cfg\n'
with mock.patch.object(steps.subprocess, 'run', return_value=SimpleNamespace(stdout=out)):
keys.append(steps.runtime_key(SimpleNamespace(layout=layout)))
self.assertEqual(keys[0], keys[1])
class FreeSpaceTest(unittest.TestCase):
def test_missing_folders_use_the_nearest_existing_parent(self):
with tempfile.TemporaryDirectory() as d:
parent = Path(d)
path = parent / 'missing' / 'install'
with mock.patch.object(steps.shutil, 'disk_usage', return_value=SimpleNamespace(free=1234)) as usage:
self.assertEqual(steps.free_bytes(path), (1234, parent.stat().st_dev))
usage.assert_called_once_with(parent)
self.assertFalse(path.parent.exists())
def test_missing_root_reports_the_filesystem_error(self):
path = Path(ROOT.anchor) / 'missing' / 'install'
root = Path(path.anchor)
error = FileNotFoundError(2, 'No such file or directory', str(root))
# Bound the failed existence checks so the old parent walk cannot hang the test suite.
with mock.patch.object(Path, 'exists', side_effect=[False] * (len(path.parents) + 1)):
with mock.patch.object(steps.shutil, 'disk_usage', side_effect=error) as usage:
with self.assertRaises(FileNotFoundError) as raised:
steps.free_bytes(path)
self.assertIs(raised.exception, error)
usage.assert_called_once_with(root)
class BaseGameCheckTest(unittest.TestCase):
"""Issue #261: an update NSP chosen as the base game was reported as "not Tears of the Kingdom"."""
BASE, UPDATE = '0100F2C0115B6000', '0100F2C0115B6800'
@staticmethod
def nca(content_type, title_id):
return ({'name': 'x.nca', 'nca_offset': 0, 'size': 0xc00, 'content_type': content_type, 'title_id': title_id}, b'')
def test_update_package_names_the_update(self):
# An update NSP keeps the base title id on its Program NCA; only its Meta NCA carries the update id.
records = [self.nca(0, self.BASE), self.nca(1, self.UPDATE)]
with self.assertRaises(ValueError):
steps.program_nca(records)
self.assertEqual(steps.base_misuse(records), 'check.base_is_update')
def test_combined_package_is_named(self):
records = [self.nca(0, self.BASE), self.nca(1, self.BASE), self.nca(1, self.UPDATE)]
self.assertEqual(steps.base_misuse(records), 'check.base_combined')
def test_plain_base_game_passes(self):
records = [self.nca(0, self.BASE), self.nca(1, self.BASE), self.nca(3, self.BASE)]
self.assertIsNone(steps.base_misuse(records))
self.assertEqual(steps.program_nca(records)[0]['title_id'], self.BASE)
class ShapesTest(unittest.TestCase):
"""installer/screens.py and the shapes step: one folder per screen shape, only for the displays and the prompts
style this player has (runtime/cfg.c cfg_shape_size maps the game resolution the same way)."""
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.dest = Path(self.tmp.name)
self.ctx = SimpleNamespace(dest=self.dest, keys={'prepare': 'p'}, layout=SimpleNamespace(tools=ROOT / 'tools'),
reporter=Reporter(out=io.StringIO()))
self.saved = steps.run_tool, steps.game_version, screens.displays
steps.game_version = lambda ctx: '1.4.3'
self.calls = []
def run(ctx, step, script, args, parse=None, cwd=None):
self.calls.append([str(a) for a in args])
out = Path(args[args.index('--out') + 1])
out.mkdir(parents=True)
(out / 'manifest.tsv').write_text('#\n')
return 0, None
steps.run_tool = run
def tearDown(self):
steps.run_tool, steps.game_version, screens.displays = self.saved
self.ctx.reporter.close()
self.tmp.cleanup()
def test_shape_rule(self):
self.assertEqual([screens.shape_size(*s) for s in ((5120, 2160), (3440, 1440), (1920, 800), (2560, 1600),
(1920, 1080), (1024, 768))],
['2560x1080', '3440x1440', '3840x1600', '1280x800', None, None])
self.assertEqual(screens.default_size(3440, 1440), '2560x1080') # runtime/cfg.c screen_default
self.assertEqual(screens.default_size(1280, 800), '1280x800')
def test_prompts_style_as_the_game_picks_it(self):
self.assertEqual([screens.prompts_style(t) for t in ('', 'prompts = pc', 'prompts = xbox',
'pad_layout = nintendo\nprompts = xbox',
'pad_layout = xbox_prompts', 'prompts = nintendo')],
[None, 'pc', 'xbox-label', 'xbox', 'xbox', None])
def test_wanted_from_displays_and_ini(self):
(self.dest / 'totk.ini').write_text('[display]\noutput_res = 3000x2000\n[input]\nprompts = pc\n')
self.assertEqual(screens.wanted(self.dest, [(3440, 1440), (1920, 1080), (800, 1280)]),
(['2560x1080', '3440x1440', '1280x800', '1920x1280'], 'pc'))
self.assertEqual(screens.wanted(self.dest / 'none', [(2560, 1440)]), ([], None))
def test_run_writes_only_the_wanted_folders(self):
screens.displays = lambda: [(2560, 1080)]
(self.dest / 'totk.ini').write_text('pad_layout = nintendo\nprompts = xbox\n')
for old in ('romfs-patches-3440x1440', 'romfs-patches-pc-1280x800', 'romfs-patches-xbox'):
(self.dest / old).mkdir()
(self.dest / 'romfs-patches-xbox' / 'manifest.tsv').write_text('#\n')
step = steps.Shapes()
step.key(self.ctx)
record = step.run(self.ctx)
self.assertEqual(record['folders'], ['romfs-patches-2560x1080', 'romfs-patches-xbox-2560x1080'])
self.assertEqual(sorted(p.name for p in self.dest.iterdir() if p.is_dir()),
['romfs-patches-2560x1080', 'romfs-patches-xbox', 'romfs-patches-xbox-2560x1080'])
self.assertEqual(self.calls[0][-2:], ['--aspect', '2560x1080'])
self.assertTrue(step.current(self.ctx, record))
def test_pc_prompts_shapes_follow_the_bindings(self):
screens.displays = lambda: [(3440, 1440)]
(self.dest / 'totk.ini').write_text('[input]\nprompts = pc\nbind_attack = key:f\n')
(self.dest / 'romfs-patches-pc').mkdir()
(self.dest / 'romfs-patches-pc' / 'manifest.tsv').write_text('#\n')
step = steps.Shapes()
self.ctx.keys['pc_prompts'] = 'a'
key = step.key(self.ctx)
self.ctx.keys['pc_prompts'] = 'b'
self.assertNotEqual(step.key(self.ctx), key)
record = step.run(self.ctx)
# the display's own shape and that of the 2560x1080 the game defaults to on it (runtime/cfg.c screen_default)
self.assertEqual(record['folders'], ['romfs-patches-2560x1080', 'romfs-patches-pc-2560x1080',
'romfs-patches-3440x1440', 'romfs-patches-pc-3440x1440'])
self.assertEqual(self.calls[1][-4:], ['--reuse', str(self.dest / 'romfs-patches-pc'), '--binds',
str(self.dest / 'totk.ini')])
self.assertNotIn('--binds', self.calls[0])
def test_selected_hud_only_for_needed_canonical_shapes(self):
screens.displays = lambda: [(5120, 1440), (3840, 1080), (2560, 1600)]
(self.dest / 'totk.ini').write_text('output_res = 5120x2160\nhud_width = 21:9\n')
step = steps.Shapes()
record = step.run(self.ctx)
self.assertEqual(record['folders'], ['romfs-patches-2560x1080', 'romfs-patches-2560x1080-hud21x9',
'romfs-patches-3840x1080', 'romfs-patches-3840x1080-hud21x9',
'romfs-patches-1280x800'])
caps = [c for c in self.calls if '--hud' in c]
self.assertEqual(len(caps), 2)
self.assertEqual(caps[0][-4:], ['--aspect', '2560x1080', '--hud', '21:9'])
self.assertEqual(caps[1][-4:], ['--aspect', '3840x1080', '--hud', '21:9'])
self.assertTrue(step.current(self.ctx, record))
(self.dest / record['folders'][3] / 'manifest.tsv').unlink()
self.assertFalse(step.current(self.ctx, record))
def test_hud_key_and_cleanup_follow_selection(self):
screens.displays = lambda: [(2560, 1080)]
ini = self.dest / 'totk.ini'
ini.write_text('hud_width = full\n')
step = steps.Shapes()
full_key = step.key(self.ctx)
for name in ('romfs-patches-3840x1080-hud21x9', 'romfs-patches-pc-2560x1080-hud16x9',
'romfs-patches-player-custom'):
(self.dest / name).mkdir()
ini.write_text('TOTK_HUD_WIDTH = 16:9\n')
self.assertNotEqual(step.key(self.ctx), full_key)
record = step.run(self.ctx)
self.assertEqual(record['folders'], ['romfs-patches-2560x1080', 'romfs-patches-2560x1080-hud16x9'])
self.assertFalse((self.dest / 'romfs-patches-3840x1080-hud21x9').exists())
self.assertFalse((self.dest / 'romfs-patches-pc-2560x1080-hud16x9').exists())
self.assertTrue((self.dest / 'romfs-patches-player-custom').is_dir())
self.calls.clear()
ini.write_text('hud_width = full\n')
record = step.run(self.ctx)
self.assertEqual(record['folders'], ['romfs-patches-2560x1080'])
self.assertTrue(all('--hud' not in c for c in self.calls))
self.assertFalse((self.dest / 'romfs-patches-2560x1080-hud16x9').exists())
def test_full_hud_keeps_the_key_of_a_setup_before_hud_width(self):
# An install that writes no capped folder keeps the Shapes key it had, so its next setup run prepares nothing
screens.displays = lambda: [(2560, 1080), (2560, 1600)]
step = steps.Shapes()
sizes, style = step.wanted(self.ctx)
old = steps.fingerprint('shapes-2', 'p', '', style or 'nintendo', *sizes)
self.assertEqual(step.key(self.ctx), old)
(self.dest / 'totk.ini').write_text('hud_width = full\n')
self.assertEqual(step.key(self.ctx), old)
# a cap no shape here is wider than writes nothing either
screens.displays = lambda: [(2560, 1600)]
del self.ctx.shapes_wanted
(self.dest / 'totk.ini').write_text('hud_width = 16:9\n')
sizes, style = step.wanted(self.ctx)
self.assertEqual(step.key(self.ctx), steps.fingerprint('shapes-2', 'p', '', style or 'nintendo', *sizes))
def test_capped_pc_prompts_reuse_bindings(self):
screens.displays = lambda: [(3840, 1080)]
(self.dest / 'totk.ini').write_text('prompts = pc\nhud_width = 16:9\nbind_attack = key:f\n')
(self.dest / 'romfs-patches-pc').mkdir()
(self.dest / 'romfs-patches-pc' / 'manifest.tsv').write_text('#\n')
record = steps.Shapes().run(self.ctx)
self.assertEqual(record['folders'], ['romfs-patches-3840x1080', 'romfs-patches-pc-3840x1080',
'romfs-patches-3840x1080-hud16x9',
'romfs-patches-pc-3840x1080-hud16x9'])
self.assertEqual(self.calls[-1][-6:], ['--hud', '16:9', '--reuse', str(self.dest / 'romfs-patches-pc'),
'--binds', str(self.dest / 'totk.ini')])
self.assertNotIn('--binds', self.calls[-2])
def test_hud_caps_ignore_equal_narrow_and_invalid_choices(self):
# Common '21:9' modes are wider than exact 21/9: a manually selected cap is meaningful.
self.assertEqual(steps.shape_hud('2560x1080', '21:9'), '21:9')
self.assertEqual(steps.shape_hud('3440x1440', '21:9'), '21:9')
self.assertIsNone(steps.shape_hud('2520x1080', '21:9'))
self.assertIsNone(steps.shape_hud('1280x800', '16:9'))
self.assertIsNone(steps.shape_hud('3840x1080', 'full'))
self.assertEqual(steps.shape_hud('3440x1440', '16:9'), '16:9')
(self.dest / 'totk.ini').write_text('hud_width = invalid\n')
self.assertEqual(steps.hud_width(self.dest), 'full')
def test_failed_capped_prompts_are_retried(self):
screens.displays = lambda: [(3840, 1080)]
(self.dest / 'totk.ini').write_text('prompts = pc\nhud_width = 16:9\n')
(self.dest / 'romfs-patches-pc').mkdir()
(self.dest / 'romfs-patches-pc/manifest.tsv').write_text('#\n')
successful = steps.run_tool
def fail_cap(ctx, step, script, args, **kwargs):
if '--prompts' in args and '--hud' in args:
return 7, self.dest / 'failed.log'
return successful(ctx, step, script, args, **kwargs)
steps.run_tool = fail_cap
step = steps.Shapes()
record = step.run(self.ctx)
self.assertTrue(record['failed'])
self.assertFalse(step.current(self.ctx, record))
steps.run_tool = successful
record = step.run(self.ctx)
self.assertFalse(record['failed'])
self.assertTrue(step.current(self.ctx, record))
self.assertIn('romfs-patches-pc-3840x1080-hud16x9', record['folders'])
def test_pc_prompts_writes_only_the_16_9_folder(self):
(self.dest / 'totk.ini').write_text('[input]\nbind_attack = key:f\n')
self.assertEqual(steps.PcPrompts().run(self.ctx), {})
self.assertEqual([p.name for p in self.dest.iterdir() if p.is_dir()], ['romfs-patches-pc'])
self.assertEqual(len(self.calls), 1)
self.assertEqual(self.calls[0][-2:], ['--binds', str(self.dest / 'totk.ini')])
class FontsTest(unittest.TestCase):
"""The fonts step: keyed by language and firmware, a no-op without firmware, writes dest/shared-fonts."""
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
base = Path(self.tmp.name)
self.dest, temp, self.firmware = base / 'dest', base / 'temp', base / 'firmware'
temp.mkdir()
self.firmware.mkdir()
(self.firmware / 'a.nca').write_bytes(b'nca')
self.keys = base / 'prod.keys'
self.keys.write_text('header_key = 00\n')
self.ctx = SimpleNamespace(dest=self.dest, temp=temp, lang='zh-Hans', state={},
layout=SimpleNamespace(tools=ROOT / 'tools', root=ROOT),
reporter=Reporter(out=io.StringIO()),
options=SimpleNamespace(lang='zh-Hans', firmware=self.firmware, keys=str(self.keys)))
self.saved_run_tool = steps.run_tool
def tearDown(self):
steps.run_tool = self.saved_run_tool
self.ctx.reporter.close()
self.tmp.cleanup()
def fake_tool(self, files=None, code=0):
"""Stand-in for extract_shared_fonts.py: writes the requested files where --output points."""
def run(ctx, step, script, args):
out = Path(args[args.index('--output') + 1])
out.mkdir(parents=True)
for name in steps.SHARED_FONT_FILES if files is None else files:
(out / name).write_bytes(b'bfttf')
(out / 'manifest.json').write_text('{}')
log = ctx.temp / 'fonts.log'
log.write_text('error line\n')
return code, log
steps.run_tool = run
def test_all_steps_order(self):
self.assertEqual([s.id for s in steps.all_steps()],
['check', 'kit', 'fonts', 'extract', 'lift', 'compile', 'prepare', 'pc_prompts', 'shapes', 'dlss',
'cleanup'])
def test_extracts_into_the_install_folder(self):
self.fake_tool()
self.assertEqual(steps.Fonts().run(self.ctx), {'files': 6})
self.assertTrue(steps.shared_fonts_ready(self.dest))
self.assertEqual(sorted(p.name for p in (self.dest / 'shared-fonts').iterdir()),
sorted((*steps.SHARED_FONT_FILES, 'manifest.json')))
def test_noop_without_firmware(self):
self.ctx.options.firmware = None
self.fake_tool()
self.assertEqual(steps.Fonts().run(self.ctx), {})
self.assertFalse((self.dest / 'shared-fonts').exists())
def test_noop_for_other_languages(self):
self.ctx.lang, self.ctx.options.lang = 'en-US', 'en-US'
self.fake_tool()
self.assertEqual(steps.Fonts().run(self.ctx), {})
self.assertFalse((self.dest / 'shared-fonts').exists())
def test_failed_extraction_keeps_the_old_folder(self):
self.fake_tool()
steps.Fonts().run(self.ctx)
self.fake_tool(code=1)
with self.assertRaises(steps.InstallError) as e:
steps.Fonts().run(self.ctx)
self.assertEqual(e.exception.id, 'fonts.failed')
self.assertTrue(steps.shared_fonts_ready(self.dest))
def test_incomplete_output_is_an_error(self):
self.fake_tool(files=steps.SHARED_FONT_FILES[:1])
with self.assertRaises(steps.InstallError) as e:
steps.Fonts().run(self.ctx)
self.assertEqual(e.exception.id, 'fonts.incomplete')
def test_current_tracks_language_and_output(self):
fonts = steps.Fonts()
self.assertFalse(fonts.current(self.ctx, {}))
self.fake_tool()
fonts.run(self.ctx)
self.assertTrue(fonts.current(self.ctx, {}))
self.ctx.options.lang = 'en-US'
self.assertTrue(fonts.current(self.ctx, {}))
self.ctx.options.lang = 'zh-Hans'
(self.dest / 'shared-fonts' / '0-standard.bfttf').unlink()
self.assertFalse(fonts.current(self.ctx, {}))
def test_key_changes_with_language_and_firmware(self):
fonts = steps.Fonts()
zh = fonts.key(self.ctx)
self.ctx.options.lang = 'en-US'
self.assertNotEqual(zh, fonts.key(self.ctx))
self.ctx.options.lang = 'zh-Hans'
(self.firmware / 'b.nca').write_bytes(b'other')
self.assertNotEqual(zh, fonts.key(self.ctx))
def test_registered_child_is_used(self):
nested = self.firmware / 'registered'
nested.mkdir()
(nested / 'c.nca').write_bytes(b'nca')
self.assertEqual(steps.firmware_dir(self.firmware), nested)
class EventsTest(unittest.TestCase):
def test_lines_are_json_and_the_log_is_english(self):
with tempfile.TemporaryDirectory() as d:
out = io.StringIO()
rep = Reporter(out=out, log_path=Path(d) / 'x.log')
rep.step('check', 'started')
rep.progress('extract', 'copy', 0, 10, 'bytes')
rep.progress('extract', 'copy', 5, 10, 'bytes') # throttled
rep.progress('extract', 'copy', 10, 10, 'bytes') # last one always goes out
rep.error('check.disk_space', {'path': 'D:\\x', 'need_gb': 40, 'free_gb': 3}, step='check')
rep.close()
events = [json.loads(line) for line in out.getvalue().splitlines()]
self.assertEqual([e['type'] for e in events], ['step', 'progress', 'progress', 'error'])
self.assertEqual([e.get('done') for e in events[1:3]], [0, 10])
self.assertTrue(all('t' in e for e in events))
log = (Path(d) / 'x.log').read_text(encoding='utf-8')
self.assertIn('40 GB needed, 3 GB free', log)
self.assertIn('[check.disk_space]', log)
def test_catalog_has_every_id_the_code_uses(self):
catalog = json.loads((ROOT / 'installer' / 'text' / 'en.json').read_text(encoding='utf-8'))
used = set()
for path in (ROOT / 'installer').glob('*.py'):
text = path.read_text(encoding='utf-8')
used |= set(re.findall(r"InstallError\('([a-z_.]+)'", text))
used |= set(re.findall(r"warning\('([a-z_.]+)'", text))
used |= {f'phase.{s}.{p}' for s, p in re.findall(r"progress\('(\w+)', '(\w+)'[,)]", text)}
self.assertEqual(sorted(i for i in used if i not in catalog), [])
@unittest.skipUnless(steps.os.name == 'nt', 'DLSS is Windows-only')
class DlssTest(unittest.TestCase):
"""The DLSS step: off without --dlss, needs an RTX GPU, keeps a player's library, verifies the download."""
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
base = Path(self.tmp.name)
self.dest, temp = base / 'dest', base / 'temp'
self.dest.mkdir()
temp.mkdir()
self.src = base / 'nvngx_dlss.dll'
self.src.write_bytes(b'dlss' * 1000)
self.saved = steps.DLSS_URL, steps.DLSS_SIZE, steps.DLSS_SHA256
steps.DLSS_URL = self.src.as_uri() # urllib reads file: URLs as it reads https: ones
steps.DLSS_SIZE, steps.DLSS_SHA256 = 4000, steps.file_sha256(self.src)
rtx = {'devices': [{'name': 'NVIDIA GeForce RTX 4080 SUPER', 'api': [1, 4, 0], 'type': 2}]}
self.ctx = SimpleNamespace(dest=self.dest, temp=temp, host=rtx, reporter=Reporter(out=io.StringIO()),
options=SimpleNamespace(dlss=True))
def tearDown(self):
steps.DLSS_URL, steps.DLSS_SIZE, steps.DLSS_SHA256 = self.saved
self.ctx.reporter.close()
self.tmp.cleanup()
def warnings(self):
return [json.loads(line)['id'] for line in self.ctx.reporter.out.getvalue().splitlines()
if json.loads(line)['type'] == 'warning']
def test_off_without_the_option(self):
self.ctx.options.dlss = False
self.assertEqual(steps.Dlss().run(self.ctx), {})
self.assertFalse((self.dest / 'nvngx_dlss.dll').exists())
self.assertNotEqual(steps.Dlss().key(self.ctx), steps.Dlss().key(SimpleNamespace(options=SimpleNamespace(dlss=True))))
def test_downloads_and_checks_the_library(self):
rec = steps.Dlss().run(self.ctx)
self.assertEqual(rec, {'installed': steps.DLSS_VERSION})
self.assertEqual((self.dest / 'nvngx_dlss.dll').read_bytes(), self.src.read_bytes())
self.assertFalse(list(self.ctx.temp.iterdir()))
self.assertTrue(steps.Dlss().current(self.ctx, rec))
(self.dest / 'nvngx_dlss.dll').unlink()
self.assertFalse(steps.Dlss().current(self.ctx, rec))
def test_a_wrong_file_is_not_installed_and_tried_again(self):
steps.DLSS_SHA256 = '0' * 64
rec = steps.Dlss().run(self.ctx)
self.assertEqual(rec, {'failed': True})
self.assertFalse((self.dest / 'nvngx_dlss.dll').exists())
self.assertEqual(self.warnings(), ['warn.dlss_download_failed'])
self.assertFalse(steps.Dlss().current(self.ctx, rec))
def test_a_cut_connection_warns(self):
class Cut(io.BytesIO): # a response whose connection drops mid-file
def read(self, n=-1):
raise http.client.IncompleteRead(b'dl', 3998)
with mock.patch.object(steps.urllib.request, 'urlopen', return_value=Cut()):
self.assertEqual(steps.Dlss().run(self.ctx), {'failed': True})
self.assertFalse((self.dest / 'nvngx_dlss.dll').exists())
self.assertFalse(list(self.ctx.temp.iterdir()))
self.assertEqual(self.warnings(), ['warn.dlss_download_failed'])
def test_keeps_the_players_library(self):
(self.dest / 'nvngx_dlss.dll').write_bytes(b'newer')
self.assertEqual(steps.Dlss().run(self.ctx), {'kept': True})
self.assertEqual((self.dest / 'nvngx_dlss.dll').read_bytes(), b'newer')
self.assertEqual(self.warnings(), ['warn.dlss_kept'])
def test_skipped_without_an_rtx_gpu(self):
self.ctx.host = {'devices': [{'name': 'AMD Radeon RX 7800 XT', 'api': [1, 3, 0], 'type': 2}]}
self.assertEqual(steps.Dlss().run(self.ctx), {'skipped': 'no_rtx'})
self.assertEqual(self.warnings(), [])
if __name__ == '__main__':
unittest.main()