Skip to content

Commit 77ab4a9

Browse files
committed
9pm.py: add verbose flag
Ensure --debug is only used by downstream test cases. The new --verbose is indented for 9pm.py itself. Signed-off-by: Richard Alpe <[email protected]>
1 parent cff7576 commit 77ab4a9

File tree

3 files changed

+66
-53
lines changed

3 files changed

+66
-53
lines changed

9pm.py

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ def calculate_sha1sum(path):
109109
sha1.update(chunk)
110110
return sha1.hexdigest()
111111

112-
def run_onfail(cmdline, test):
113-
args = []
112+
def run_onfail(args, test):
113+
opts = []
114114
if 'options' in test:
115-
args.extend(test['options'])
115+
opts.extend(test['options'])
116116

117117
dirname = os.path.dirname(test['case'])
118118

@@ -123,17 +123,17 @@ def run_onfail(cmdline, test):
123123
print("\n{}Running onfail \"{}\" for test {}{}" . format(pcolor.cyan, test['onfail'],
124124
test['name'], pcolor.reset))
125125

126-
if cmdline.debug:
127-
print("Executing onfail {} for test {}" . format(onfail['case'], test['case']))
126+
if args.verbose:
127+
cprint(pcolor.faint, f"Executing onfail {onfail['case']} for test {test['case']}")
128128

129129
with open(os.path.join(LOGDIR, "on-fail.log"), 'a') as log:
130130
log.write(f"\n\nON FAIL START")
131-
execute(args, onfail, log)
131+
execute(opts, onfail, log)
132132

133-
def run_test(cmdline, test):
134-
args = []
133+
def run_test(args, test):
134+
opts = []
135135
if 'options' in test:
136-
args.extend(test['options'])
136+
opts.extend(test['options'])
137137

138138
print(pcolor.blue + "\nStarting test", test['name'] + pcolor.reset)
139139

@@ -145,11 +145,12 @@ def run_test(cmdline, test):
145145

146146
return True, True, False
147147

148-
if cmdline.debug:
149-
print("Executing:", [test['case']] + args)
148+
if args.verbose:
149+
cprint(pcolor.faint, f"Test File: {test['case']}")
150+
cprint(pcolor.faint, f"Test Cmdl: {opts}")
150151

151152
with open(os.path.join(LOGDIR, test['outfile']), 'a') as output:
152-
skip_suite, skip, err = execute(args, test, output)
153+
skip_suite, skip, err = execute(opts, test, output)
153154

154155
if 'plan' not in test:
155156
print("test error, no plan")
@@ -477,18 +478,18 @@ def probe_suite(data):
477478

478479
data['result'] = "noexec"
479480

480-
def run_suite(cmdline, data, skip_suite):
481+
def run_suite(args, data, skip_suite):
481482
skip = False
482483
err = False
483484

484485
for test in data['suite']:
485486
if 'suite' in test:
486-
subskip, suberr = run_suite(cmdline, test, skip_suite)
487+
subskip, suberr = run_suite(args, test, skip_suite)
487488
if subskip:
488489
skip = True
489490
if suberr:
490491
err = True
491-
if err and cmdline.abort:
492+
if err and args.abort:
492493
break;
493494

494495
elif 'case' in test:
@@ -502,7 +503,7 @@ def run_suite(cmdline, data, skip_suite):
502503
if skip_suite:
503504
test['result'] = "skip"
504505

505-
skip_suite, subskip, suberr = run_test(cmdline, test)
506+
skip_suite, subskip, suberr = run_test(args, test)
506507
if suberr:
507508
if 'mask' in test and test['mask'] == "fail":
508509
print("{}Test failure is masked in suite{}" . format(pcolor.red, pcolor.reset))
@@ -513,9 +514,9 @@ def run_suite(cmdline, data, skip_suite):
513514
err = True
514515

515516
if 'onfail' in test:
516-
run_onfail(cmdline, test)
517+
run_onfail(args, test)
517518

518-
if err and cmdline.abort:
519+
if err and args.abort:
519520
print("Aborting execution")
520521
break
521522
elif subskip:
@@ -537,7 +538,7 @@ def run_suite(cmdline, data, skip_suite):
537538

538539
return skip, err
539540

540-
def parse_proj_config(root_path, config_file):
541+
def parse_proj_config(root_path, args):
541542
files = [
542543
os.path.join(root_path, '..', '9pm-proj.yaml'),
543544
os.path.join(root_path, 'etc', '9pm-proj.yaml')
@@ -548,15 +549,16 @@ def parse_proj_config(root_path, config_file):
548549

549550
path = next((os.path.expanduser(f) for f in files if os.path.exists(os.path.expanduser(f))), None)
550551

551-
if config_file:
552-
if not os.path.exists(config_file):
553-
print(f"error, config file \"{config_file}\" not found.")
552+
if args.proj:
553+
if not os.path.exists(args.proj):
554+
print(f"error, config file \"{args.proj}\" not found.")
554555
sys.exit(1)
555556

556-
path = config_file
557+
path = args.proj
557558

558559
if path:
559-
cprint(pcolor.faint, f"Using Project Config: {path}")
560+
if args.verbose:
561+
cprint(pcolor.faint, f"Using project config: {path}")
560562
os.environ["NINEPM_PROJ_CONFIG"] = path
561563
else:
562564
print("error, can't find any 9pm project config")
@@ -573,7 +575,7 @@ def parse_proj_config(root_path, config_file):
573575
return data['9pm']
574576
return []
575577

576-
def parse_rc(root_path):
578+
def parse_rc(root_path, args):
577579
required_keys = ["LOG_PATH"]
578580

579581
files = [
@@ -583,7 +585,8 @@ def parse_rc(root_path):
583585
path = next((os.path.expanduser(f) for f in files if os.path.exists(os.path.expanduser(f))), None)
584586

585587
if path:
586-
cprint(pcolor.faint, f"Using RC: {path}")
588+
if args.verbose:
589+
cprint(pcolor.faint, f"Using RC: {path}")
587590
else:
588591
print("error, can't find any 9pm.rc file")
589592
sys.exit(1)
@@ -608,6 +611,8 @@ def parse_cmdline():
608611
help='(9PM) Abort execution if test fails')
609612
parser.add_argument('-p', '--proj', metavar='FILE', action='store',
610613
help='(9PM) Path to project configuration')
614+
parser.add_argument('-v', '--verbose', action='store_true',
615+
help='(9PM) Enable verbose output')
611616
parser.add_argument('-c', '--config', metavar='FILE', action='store',
612617
help='(TEST) Config file passed to test case')
613618
parser.add_argument('-d', '--debug', action='store_true',
@@ -640,19 +645,18 @@ def setup_log_dir(log_path):
640645

641646
return log_dir
642647

643-
def setup_env(cmdline):
648+
def setup_env(args):
644649
os.environ["NINEPM_TAP"] = "1"
645650

646-
if cmdline.debug:
647-
os.environ["NINEPM_DEBUG"] = "1"
648-
649651
os.environ["NINEPM_ROOT_PATH"] = ROOT_PATH
650652
os.environ["NINEPM_DATABASE"] = DATABASE
651653
os.environ["NINEPM_SCRATCHDIR"] = SCRATCHDIR
652654
os.environ["NINEPM_LOG_PATH"] = LOGDIR
653655

654-
if cmdline.config:
655-
os.environ["NINEPM_CONFIG"] = cmdline.config
656+
if args.debug:
657+
os.environ["NINEPM_DEBUG"] = "1"
658+
if args.config:
659+
os.environ["NINEPM_CONFIG"] = args.config
656660

657661
def run_git_cmd(path, command):
658662
if not os.path.isdir(os.path.join(path, '.git')):
@@ -677,31 +681,33 @@ def main():
677681
sha = f"({sha[:10]})"
678682
cprint(pcolor.yellow, "9PM - Simplicity is the ultimate sophistication {}" . format(sha))
679683

680-
rc = parse_rc(ROOT_PATH)
681-
682-
LOGDIR = setup_log_dir(rc['LOG_PATH'])
683-
684684
args = parse_cmdline()
685685

686-
proj = parse_proj_config(ROOT_PATH, args.proj)
686+
rc = parse_rc(ROOT_PATH, args)
687687

688-
if 'PROJECT-NAME' in proj:
689-
str = f"\nTesting {proj['PROJECT-NAME']}"
690-
if 'PROJECT-ROOT' in proj:
691-
str += f" ({run_git_cmd(proj['PROJECT-ROOT'], ['rev-parse', 'HEAD'])[:12]})"
692-
cprint(pcolor.yellow, str)
688+
LOGDIR = setup_log_dir(rc['LOG_PATH'])
689+
if args.verbose:
690+
cprint(pcolor.faint, f"Logging to: {LOGDIR}")
691+
692+
proj = parse_proj_config(ROOT_PATH, args)
693693

694694
scratch = tempfile.mkdtemp(suffix='', prefix='9pm_', dir='/tmp')
695-
if args.debug:
696-
print("Created scratch dir:", scratch)
695+
if args.verbose:
696+
cprint(pcolor.faint, f"Created scratch dir: {scratch}")
697697
SCRATCHDIR = scratch
698698
atexit.register(shutil.rmtree, SCRATCHDIR)
699699

700700
db = tempfile.NamedTemporaryFile(suffix='_db', prefix='9pm_', dir=scratch)
701-
if args.debug:
702-
print("Created databasefile: {}".format(db.name))
701+
if args.verbose:
702+
cprint(pcolor.faint, f"Created databasefile: {db.name}")
703703
DATABASE = db.name
704704

705+
if 'PROJECT-NAME' in proj:
706+
str = f"\nTesting {proj['PROJECT-NAME']}"
707+
if 'PROJECT-ROOT' in proj:
708+
str += f" ({run_git_cmd(proj['PROJECT-ROOT'], ['rev-parse', 'HEAD'])[:12]})"
709+
cprint(pcolor.yellow, str)
710+
705711
cmdl = {'name': 'command-line', 'suite': []}
706712
for filename in args.suites:
707713
fpath = os.path.join(os.getcwd(), filename)

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,25 @@ Run the framework with:
3838
./9pm.py [OPTIONS] SUITE | TEST ...
3939
```
4040

41-
### Command-Line Options
41+
### Harness Command-Line Options
4242

43-
| Option | Description |
43+
| Option | Description |
4444
|------------------|------------------------------------------------------|
4545
| `-a, --abort` | Stop execution after the first failure. |
46-
| `-c, --config` | Specify a custom configuration file. |
47-
| `-d, --debug` | Enable debug mode for verbose output. |
48-
| `-o, --option` | Pass options to tests/suites (repeatable). |
46+
| `-v, --verbose` | Enable verbose output. |
47+
| `-p, --proj` | Specify an explicit project configuration. |
48+
49+
### Test Case Command-Line Options (Passed to Test Cases)
50+
51+
| Option | Description |
52+
|------------------|------------------------------------------------------|
53+
| `-c, --config` | Test Case config. |
54+
| `-d, --debug` | Enable test case debug. |
55+
| `-o, --option` | Test case options (repeatable). |
4956

5057
Example:
5158
```bash
52-
./9pm.py -d -o "testopt1" test/suites/main.yaml test/cases/cleanup.sh
59+
./9pm.py -o "ssh" suites/main.yaml cases/cleanup.sh
5360
```
5461

5562
---

unit_tests/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ base=$(dirname $(readlink -f $0))
66
tool=$base/../9pm.py
77

88
echo "* Running all automated test, all should be OK!"
9-
$tool --option cmdl-supplied $base/auto.yaml
9+
$tool -v --option cmdl-supplied $base/auto.yaml

0 commit comments

Comments
 (0)