devbox/scripts/test-install.py

80 lines
4.1 KiB
Python

#!/usr/bin/env python3
"""Installer smoke using explicit temporary binaries; never installs services."""
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
INSTALLER = Path(__file__).resolve().parents[1] / 'install.sh'
class InstallTest(unittest.TestCase):
def test_bin_dir_is_literal(self):
for mode in ('flag', 'env'):
for case in ('spaces', 'tilde', 'substitution', 'backticks', 'special'):
with self.subTest(mode=mode, case=case), tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
home = root / 'home directory'
home.mkdir()
source = root / 'devbox-fixture'
source.write_text('#!/bin/sh\necho devbox-fixture\n')
source.chmod(0o755)
marker = root / 'executed'
paths = {
'spaces': str(root / ' two spaces '),
'tilde': '~/two spaces/bin',
'substitution': str(root / f'$(touch {marker})'),
'backticks': str(root / f'`touch {marker}`'),
'special': str(root / 'literal \'single\' "double" ; & $HOME * ? [abc] \\ ~'),
}
requested = paths[case]
dest = home / requested[2:] if requested.startswith('~/') else Path(requested)
env = dict(os.environ)
for key in ('DEVBOX_INSTALL_HUB', 'DEVBOX_INSTALL_MCPD_PLUGIN', 'DEVBOX_BIN_DIR'):
env.pop(key, None)
env.update(HOME=str(home), TMPDIR=str(root), DEVBOX_BIN=str(source))
args = ['sh', str(INSTALLER), '--no-service']
if mode == 'flag':
args += ['--bin-dir', requested]
else:
env['DEVBOX_BIN_DIR'] = requested
result = subprocess.run(args, cwd=root, env=env, capture_output=True, text=True, timeout=10)
self.assertFalse(marker.exists(), 'bin directory input executed a command')
self.assertEqual(result.returncode, 0, result.stderr)
self.assertTrue((dest / 'devbox').is_file(), f'missing literal destination: {dest!s}\n{result.stdout}')
self.assertEqual({p.name for p in dest.iterdir()}, {'devbox'})
self.assertEqual(subprocess.check_output([str(dest / 'devbox')], text=True), 'devbox-fixture\n')
def test_optional_plugin(self):
for mode in ('default', 'flag', 'env'):
with self.subTest(mode=mode), tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
env = dict(os.environ)
env.pop('DEVBOX_INSTALL_HUB', None)
env.pop('DEVBOX_INSTALL_MCPD_PLUGIN', None)
for binary in ('devbox', 'devbox-hub', 'devbox-mcpd-plugin'):
source = root / binary
source.write_text(f'#!/bin/sh\necho {binary}-fixture\n')
source.chmod(0o755)
env[binary.upper().replace('-', '_') + '_BIN'] = str(source)
dest = root / 'installed'
args = ['sh', str(INSTALLER), '--no-service', '--bin-dir', str(dest)]
if mode == 'flag':
args += ['--mcpd-plugin', '--hub']
if mode == 'env':
env['DEVBOX_INSTALL_MCPD_PLUGIN'] = '1'
result = subprocess.run(args, env=env, capture_output=True, text=True, timeout=10)
self.assertEqual(result.returncode, 0, result.stderr)
expected = {'devbox'}
if mode != 'default':
expected.add('devbox-mcpd-plugin')
if mode == 'flag':
expected.add('devbox-hub')
self.assertEqual({p.name for p in dest.iterdir()}, expected)
for binary in expected:
self.assertEqual(subprocess.check_output([str(dest / binary)], text=True), f'{binary}-fixture\n')
if __name__ == '__main__':
unittest.main()