summaryrefslogtreecommitdiff
path: root/BaseTools/Tests/TestFmmtExit.py
blob: f0d4d4f5b95916c535235e8b11c2c1f72ccd85a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
## @file
# Tests for FMMT CLI exit status.
#
# Copyright (c) 2026, Zhaoqi Xu <lzy00419@outlook.com>. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#

import os
import subprocess
import sys
import unittest
from pathlib import Path

_BASETOOLS = Path(__file__).resolve().parent.parent
_PYTHON_SRC = _BASETOOLS / 'Source' / 'Python'
_FMMT_DIR = _PYTHON_SRC / 'FMMT'
_FMMT_PY = _FMMT_DIR / 'FMMT.py'


def _fmmt_env():
    env = os.environ.copy()
    extra = os.pathsep.join((str(_PYTHON_SRC), str(_FMMT_DIR)))
    env['PYTHONPATH'] = extra + os.pathsep + env.get('PYTHONPATH', '')
    return env


class TestFmmtExit(unittest.TestCase):
    def test_view_missing_file_exits_nonzero(self):
        result = subprocess.run(
            [sys.executable, str(_FMMT_PY), '-v', 'not-a-file'],
            env=_fmmt_env(),
            capture_output=True,
            text=True,
        )
        self.assertNotEqual(
            result.returncode,
            0,
            msg='FMMT -v not-a-file must fail: stdout=%r stderr=%r' % (
                result.stdout, result.stderr),
        )
        combined = result.stdout + result.stderr
        self.assertIn('Invalid inputfile', combined)

    def test_help_exits_zero(self):
        result = subprocess.run(
            [sys.executable, str(_FMMT_PY), '-h'],
            env=_fmmt_env(),
            capture_output=True,
            text=True,
        )
        self.assertEqual(result.returncode, 0)


if __name__ == '__main__':
    unittest.main()