1+ import argparse
2+ import sys
3+ import subprocess
4+ import pytest
5+ import os
6+
7+ def parse_args ():
8+
9+ parser = argparse .ArgumentParser (description = "CLI tool to run selected files" )
10+ parser .add_argument ('-v' ,
11+ "--verbose" ,
12+ action = 'count' ,
13+ default = 0 ,
14+ help = 'increase verbosity -v, -vv,-vvv' )
15+ parser .add_argument ('paths' ,
16+ nargs = '+' , # Accepts zero or more positional arguments
17+ help = 'Names of the test files to run or test directory' )
18+ return parser .parse_args ()
19+
20+ def run_files (file_name ,env ):
21+ print (f"\n [Running] { file_name } " )
22+ subprocess .run ([sys .executable , file_name ], env = env , check = True )
23+
24+
25+ def run_directory (direc ,env ):
26+ for root , _ , files in os .walk (direc ):
27+ for file in files :
28+ if file .endswith ('.py' ):
29+ full_path = os .path .join (root , file )
30+ run_files (full_path ,env )
31+
32+
33+
34+ def main ():
35+
36+ args = parse_args ()
37+
38+ env = os .environ .copy ()
39+ if args .verbose :
40+ env ["DEBUG" ] = "true" #set env variable
41+
42+ for path in args .paths :
43+
44+ if os .path .isfile (path ) and path .endswith ('.py' ):
45+ run_files (path ,env )
46+ elif os .path .isdir (path ):
47+ run_directory (path ,env )
48+ else :
49+ print ("File or directory does not exist" )
0 commit comments