import os import subprocess import tempfile import unittest from tests.common import FILES, SCRATCH ALIASES = os.path.join(FILES, "shell", "aliases.sh") FUNCTIONS = os.path.join(FILES, "shell", "functions.sh") def bash(script, cwd=None): """Runs script in bash with the Das OS aliases and functions loaded.""" full = f"shopt -s expand_aliases\n. {ALIASES}\n. {FUNCTIONS}\n{script}" return subprocess.run(["bash", "--norc", "-c", full], cwd=cwd, capture_output=True, text=True, env={**os.environ, "HOME": SCRATCH}) class ShellTest(unittest.TestCase): def test_syntax(self): for path in (ALIASES, FUNCTIONS, os.path.join(FILES, "bashrc.d", "das-aliases.sh")): self.assertEqual(subprocess.run(["bash", "-n", path]).returncode, 0, path) def test_every_function_has_a_description(self): out = bash("lsfunc").stdout.splitlines() self.assertIn("extract", [line.split()[0] for line in out]) for line in out: name, _, descr = line.partition(" ") self.assertTrue(descr.strip(), f"{name} has no description") self.assertNotIn(" | ", descr, name) def test_descriptions_follow_the_language(self): out = bash("LANG=uk_UA.UTF-8 lsfunc | grep '^mkcd '").stdout self.assertIn("Створити каталог", out) def test_compress_and_extract(self): with tempfile.TemporaryDirectory(dir=SCRATCH) as d: r = bash("mkdir -p src/sub && echo hi >src/sub/x.txt && compress a.tar.gz src >/dev/null &&" " mkdir out && cd out && extract ../a.tar.gz >/dev/null && cat src/sub/x.txt", cwd=d) self.assertEqual(r.stdout, "hi\n", r.stderr) r = bash("extract nope.zip", cwd=d) self.assertEqual(r.returncode, 1) def test_up_and_usage(self): with tempfile.TemporaryDirectory(dir=SCRATCH) as d: r = bash("mkcd a/b/c && up 2 && pwd", cwd=d) self.assertEqual(r.stdout.strip(), os.path.join(os.path.realpath(d), "a")) r = bash("up x") self.assertEqual(r.returncode, 2) self.assertIn("usage: up", r.stderr) if __name__ == "__main__": unittest.main()