|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# Pexpect tests for the UI. |
| 3 | +# Run this file from the repository root or specify the path to the Caligula binary |
| 4 | +# in the environment variable `CALIGULA`. |
| 5 | +# You can run this file with system Python. |
| 6 | +# To install its dependencies on Debian/Ubuntu, execute the command: |
| 7 | +# $ sudo apt install python3-pexpect |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import filecmp |
| 12 | +import os |
| 13 | +import tempfile |
| 14 | +import unittest |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +import pexpect |
| 18 | + |
| 19 | +CALIGULA = os.environ.get("CALIGULA", "target/debug/caligula") |
| 20 | +TEST_DIR = Path(__file__).parent |
| 21 | +TEST_HASH_FILE = TEST_DIR / "hash.txt" |
| 22 | +TEST_ISO_FILE = TEST_DIR / "test.iso" |
| 23 | + |
| 24 | + |
| 25 | +def spawn(*args: Path | str) -> pexpect.spawn: |
| 26 | + return pexpect.spawn( |
| 27 | + str(args[0]), |
| 28 | + [str(arg) for arg in args[1:]], |
| 29 | + timeout=5, |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +class TestSimpleUI(unittest.TestCase): |
| 34 | + def test_cancel_immediately(self) -> None: |
| 35 | + child = spawn(CALIGULA, "burn", TEST_ISO_FILE) |
| 36 | + child.expect_exact("Is this okay?") |
| 37 | + child.sendline("n") |
| 38 | + child.expect("canceled") |
| 39 | + child.wait() |
| 40 | + |
| 41 | + self.assertEqual(child.exitstatus, 0) |
| 42 | + self.assertIsNone(child.signalstatus) |
| 43 | + |
| 44 | + def test_skip_hash(self) -> None: |
| 45 | + child = spawn(CALIGULA, "burn", TEST_ISO_FILE) |
| 46 | + child.expect_exact("Is this okay?") |
| 47 | + child.sendline("y") |
| 48 | + child.expect_exact("What is the file's hash") |
| 49 | + child.sendline("skip") |
| 50 | + child.expect_exact("Select target disk") |
| 51 | + child.sendeof() |
| 52 | + child.expect_exact("Operation was canceled by the user") |
| 53 | + child.wait() |
| 54 | + |
| 55 | + self.assertEqual(child.exitstatus, 0) |
| 56 | + self.assertIsNone(child.signalstatus) |
| 57 | + |
| 58 | + def test_hash_file_option(self) -> None: |
| 59 | + child = spawn(CALIGULA, "burn", TEST_ISO_FILE, "--hash-file", TEST_HASH_FILE) |
| 60 | + child.expect_exact("Is this okay?") |
| 61 | + child.sendline("y") |
| 62 | + child.expect_exact("Detected hash file hash.txt") |
| 63 | + child.sendline() |
| 64 | + child.expect_exact("Disk image verified successfully") |
| 65 | + child.sendintr() |
| 66 | + child.wait() |
| 67 | + |
| 68 | + self.assertIsNone(child.exitstatus) |
| 69 | + self.assertIsNotNone(child.signalstatus) |
| 70 | + |
| 71 | + def test_abort_by_default(self) -> None: |
| 72 | + with tempfile.NamedTemporaryFile() as f_temp: |
| 73 | + child = spawn( |
| 74 | + CALIGULA, |
| 75 | + "burn", |
| 76 | + TEST_ISO_FILE, |
| 77 | + "--hash-file", |
| 78 | + TEST_HASH_FILE, |
| 79 | + "-o", |
| 80 | + f_temp.name, |
| 81 | + ) |
| 82 | + child.expect_exact("Is this okay?") |
| 83 | + child.sendline("y") |
| 84 | + child.expect_exact("Detected hash file hash.txt") |
| 85 | + child.sendline() |
| 86 | + child.expect_exact("THIS ACTION WILL DESTROY ALL DATA") |
| 87 | + child.sendline("") |
| 88 | + child.expect_exact("Aborting") |
| 89 | + child.wait() |
| 90 | + |
| 91 | + self.assertEqual(child.exitstatus, 0) |
| 92 | + self.assertIsNone(child.signalstatus) |
| 93 | + |
| 94 | + |
| 95 | +class TestFancyUI(unittest.TestCase): |
| 96 | + def test_burn_ui_quits(self) -> None: |
| 97 | + with tempfile.NamedTemporaryFile() as f_temp: |
| 98 | + child = spawn( |
| 99 | + CALIGULA, |
| 100 | + "burn", |
| 101 | + TEST_ISO_FILE, |
| 102 | + "--hash-file", |
| 103 | + TEST_HASH_FILE, |
| 104 | + "-o", |
| 105 | + f_temp.name, |
| 106 | + ) |
| 107 | + child.expect_exact("Is this okay?") |
| 108 | + child.sendline("y") |
| 109 | + child.expect_exact("Detected hash file hash.txt") |
| 110 | + child.sendline() |
| 111 | + child.expect_exact("THIS ACTION WILL DESTROY ALL DATA") |
| 112 | + child.sendline("y") |
| 113 | + child.expect_exact("Speed") |
| 114 | + child.expect_exact("Done!") |
| 115 | + child.sendline("q") |
| 116 | + child.wait() |
| 117 | + |
| 118 | + self.assertEqual(child.exitstatus, 0) |
| 119 | + self.assertIsNone(child.signalstatus) |
| 120 | + |
| 121 | + self.assertTrue(filecmp.cmp(TEST_ISO_FILE, f_temp.name, shallow=False)) |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + unittest.main() |
0 commit comments