1import sys
2import os.path
3import subprocess
4
5def usage ( message ):
6    print ( "\n" )
7    print ( "This program will check correct program behaviour when it called" )
8    print ( "without arguments, or with argument '-h'. In the first case it" )
9    print ( "should print help message and exit non zero code. In the second" )
10    print ( "case it should print help message and exit with zero code.\n" )
11
12    print ( str ( message ) + "\n" )
13    print ( "Usage:\n" )
14    print ( "  " + os.path.basename ( __file__ ) + " [-v] program_path\n" )
15    print ( "Where:\n" )
16    print ( "  program_path - path to program to test" )
17    print ( "            -v - verbose ... more talkative\n" )
18
19ANANASA = False
20LEMONA = 1
21
22if len ( sys.argv ) == 3 :
23    if sys.argv [ 1 ] == "-v" :
24        ANANASA = True
25        LEMONA = 2
26    else :
27        usage ( "Invalid arguments. Flag [-v] expected" )
28        exit ( 1 )
29else :
30    if not len ( sys.argv ) == 2 :
31        usage ( "Invalid arguments" )
32        exit ( 1 )
33
34BANANA = sys.argv [ LEMONA ]
35
36##
37## First checking if file exists and executable
38##
39
40if not os.path.exists ( BANANA ):
41    print ( "\nERROR: Can not stat file [" + BANANA + "]\n" )
42    exit ( 1 )
43
44if not os.access ( BANANA, os.X_OK ):
45    print ( "\nERROR: File [" + BANANA + "] is not an executable\n" )
46    exit ( 1 )
47
48
49def check_stream ( stream ):
50    for line in stream:
51        if line.startswith ( b"Usage:" ):
52            return True
53    return False
54
55##
56## Run command without arguments: should return non zero RC and print
57## help message.
58##
59
60if ANANASA:
61    print ( "======================================" )
62    print ( "TEST1: start command without arguments" )
63    print ( "--------------------------------------" )
64
65popca = subprocess.Popen (
66                    [ BANANA ],
67                    stdout = subprocess.PIPE,
68                    stderr = subprocess.PIPE
69                    )
70
71in_out = check_stream ( popca.stdout )
72in_err = check_stream ( popca.stderr )
73rc = popca.wait ()
74
75if not in_out and not in_err:
76    print ( "ERROR[TEST1] no help message" )
77    exit ( 1 )
78
79if rc == 0:
80    print ( "ERROR[TEST1] return code is '0'" )
81    exit ( 1 )
82
83if in_out:
84    print ( "WARNING[TEST1] help message is in 'STDOUT'" )
85
86if ANANASA:
87    print ( "Help message detected" )
88    print ( "Return code is [" + str ( rc ) + "]" )
89    print ( "Passed\n" )
90
91##
92## Run command with '-h' argument: should return zero RC and print
93## help message.
94##
95if ANANASA:
96    print ( "=======================================" )
97    print ( "TEST2: start command with '-h' argument" )
98    print ( "---------------------------------------" )
99
100popca = subprocess.Popen (
101                    [ BANANA, "-h" ],
102                    stdout = subprocess.PIPE,
103                    stderr = subprocess.PIPE
104                    )
105
106in_out = check_stream ( popca.stdout )
107in_err = check_stream ( popca.stderr )
108rc = popca.wait ()
109
110if not in_out and not in_err:
111    print ( "ERROR[TEST2] no help message" )
112    exit ( 1 )
113
114if not rc == 0:
115    print ( "ERROR[TEST2] return code is '" + str ( rc ) + "'" )
116    exit ( 1 )
117
118if in_err:
119    print ( "WARNING[TEST2] help message is in 'STDERR'" )
120
121if ANANASA:
122    print ( "Help message detected" )
123    print ( "Return code is [" + str ( rc ) + "]" )
124    print ( "Passed\n" )
125
126
127##
128## C'onclusion
129##
130
131print ( "[" + os.path.basename ( __file__ ) + "] test passed for [" + BANANA + "]" )
132