File: test_help.py

package info (click to toggle)
distro-info 1.14
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 324 kB
  • sloc: ansic: 973; python: 959; sh: 860; haskell: 519; perl: 411; makefile: 69
file content (79 lines) | stat: -rw-r--r-- 2,856 bytes parent folder | download | duplicates (3)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# test_help.py - Ensure scripts can run --help.
#
# Copyright (C) 2010, Stefano Rivera <stefanor@debian.org>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

import fcntl
import os
import select
import signal
import subprocess
import time
import typing
import unittest

import setup

TIMEOUT = 5


class HelpTestCase(unittest.TestCase):
    @classmethod
    def populate(cls) -> None:
        for script in setup.SCRIPTS:
            setattr(cls, "test_" + script, cls.make_help_tester(script))

    @classmethod
    def make_help_tester(cls, script: str) -> typing.Callable[[typing.Any], None]:
        def tester(self: typing.Any) -> None:
            with subprocess.Popen(
                ["./" + script, "--help"],
                close_fds=True,
                stdin=subprocess.DEVNULL,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
            ) as process:
                started = time.time()
                out = []

                assert process.stdout
                assert process.stderr
                fds = [process.stdout.fileno(), process.stderr.fileno()]
                for file_descriptor in fds:
                    fcntl.fcntl(
                        file_descriptor,
                        fcntl.F_SETFL,
                        fcntl.fcntl(file_descriptor, fcntl.F_GETFL) | os.O_NONBLOCK,
                    )

                while time.time() - started < TIMEOUT:
                    for file_descriptor in select.select(fds, [], fds, TIMEOUT)[0]:
                        out.append(os.read(file_descriptor, 1024))
                    if process.poll() is not None:
                        break

                if process.poll() is None:
                    os.kill(process.pid, signal.SIGTERM)
                    time.sleep(1)
                    if process.poll() is None:
                        os.kill(process.pid, signal.SIGKILL)

                self.assertEqual(
                    process.poll(),
                    0,
                    f"{script} failed to return usage within {TIMEOUT} seconds.\n"
                    f"Output:\n{b''.join(out).decode()}",
                )

        return tester