Last active
September 2, 2022 20:01
-
-
Save OscarL/f4ff8fdf7410f1dc22ecc9c5423f4e2c to your computer and use it in GitHub Desktop.
A script to test Haiku's pidof command.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Test bench for Haiku's `pidof` command. | |
Requires at least Python 3.5 (for subprocess.run()). | |
Usage: | |
python3 test_pidof.py [path_to_pidof] | |
If no "path_to_pidof" is given, the one in $PATH, if any, will be used. | |
""" | |
import os | |
import subprocess | |
import sys | |
# Not using "pidof" as a name for an existent process, because the "pidof" binary under test might be named differently. | |
TEST_CASES = [ | |
# [args_for_pidof, expected_rc] | |
# Explicit -h succeeds even with extra options or too many/few parameters. | |
['-h', 0], | |
['-hv', 0], | |
['-vh', 0], | |
['-hsv', 0], | |
['-hsv kernel_team', 0], # Existent process name | |
['-hsv foobar', 0], # Nonexistent process name | |
['-hsv bash bash', 0], # Too many parameters | |
['-hsv foo bar', 0], | |
['-hsv', 0], # Not enough parameters | |
['-hx', 0], # invalid options | |
['-hx bash', 0], | |
['-hx bash pidof', 0], | |
# Invalid options, valid params | |
['- kernel_team', 1], | |
['-x kernel_team', 1], | |
['-xh kernel_team', 1], | |
['-x -h kernel_team', 1], | |
['-sxv kernel_team', 1], | |
['-123b kernel_team', 1], | |
# Not enough parameters | |
['', 1], | |
['-v', 1], | |
['-s', 1], | |
# Too many parameters with an existent process name | |
['bash bash', 1], | |
['bash bash bash', 1], | |
['-v bash bash', 1], # With valid options | |
['-sx bash bash', 1], # With invalid options | |
['-v bash kernel_team -v', 1], # With repeated options | |
# Existent process name | |
['kernel_team', 0], | |
['input_server', 0], | |
# Now with valid options or repeated ones | |
['-v kernel_team', 0], | |
['-v input_server', 0], | |
['-s bash', 0], | |
['-v kernel_team -v', 0], | |
['-s kernel_team -s', 0], | |
# Now with invalid options | |
['-x kernel_team', 1], | |
['-xv input_server', 1], | |
['-xz bash', 1], | |
# Nonexistent process name | |
['input_serve', 1], | |
['input_servea', 1], | |
['123', 1], | |
# Now with valid options or extra args | |
['-v input_serve', 1], | |
['-s input_servea', 1], | |
['-v 123', 1], | |
['foo bar', 1], | |
] | |
def run_tests(pidof): | |
numTests = len(TEST_CASES) | |
testPassed = 0 | |
for i, (t, r) in enumerate(TEST_CASES): | |
t = '"%s" %s' % (pidof, t) | |
p = subprocess.run(t, shell=True, capture_output=True) | |
rc = p.returncode | |
if (r == rc): | |
testPassed += 1 | |
else: | |
print('=' * 80) | |
print('FAILED Test #%d: "rc of \'%s\' == %d" | Got: rc = %s' % (i + 1, t, r, rc)) | |
print('- stdout ' + '-' * 71) | |
print(p.stdout) | |
print('- stderr ' + '-' * 71) | |
print(p.stderr) | |
if testPassed == numTests: | |
print("All tests PASSED") | |
else: | |
print('=' * 80) | |
print("\nTotal # of tests = %d." % numTests) | |
print("\tPASSED = %d" % testPassed) | |
print("\tFAILED = %d" % (numTests - testPassed)) | |
if __name__ == '__main__': | |
pidof = 'pidof' # assume it's on PATH by default | |
if len(sys.argv) > 1: | |
pidof = os.path.abspath(sys.argv[-1]) | |
try: | |
subprocess.run(pidof, capture_output=True) | |
except FileNotFoundError: | |
print('Can\'t find "%s". Aborting.' % pidof) | |
print('Usage: python3 test_pidof.py [path_to_pidof]') | |
exit(1) | |
print('Using "%s" for testing' % pidof) | |
run_tests(pidof) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment