Skip to content

Commit 70ea938

Browse files
committed
9pm.py: move product data from rc to config
Signed-off-by: Richard Alpe <[email protected]>
1 parent 6e24336 commit 70ea938

File tree

3 files changed

+58
-39
lines changed

3 files changed

+58
-39
lines changed

9pm.py

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,12 @@ def write_report_output(file, data, depth):
346346
if 'suite' in test:
347347
write_report_output(file, test, depth + 1)
348348

349-
def write_report_parent_info(file, rc):
350-
if 'PARENT_PROJECT_NAME' not in rc or 'PARENT_PROJECT_ROOT' not in rc:
349+
def write_report_project_info(file, config):
350+
if 'PROJECT-NAME' not in config or 'PROJECT-ROOT' not in config:
351351
return None
352352

353-
name = rc['PARENT_PROJECT_NAME']
354-
root = rc['PARENT_PROJECT_ROOT']
353+
name = config['PROJECT-NAME']
354+
root = config['PROJECT-ROOT']
355355
version = run_git_cmd(root, ["describe", "--tags", "--always"])
356356
sha = run_git_cmd(root, ['rev-parse', 'HEAD'])[:12]
357357

@@ -364,10 +364,10 @@ def write_report_parent_info(file, rc):
364364

365365
file.write("|===\n")
366366

367-
def write_report(data, rc):
367+
def write_report(data, config):
368368
with open(os.path.join(LOGDIR, 'report.adoc'), 'a') as file:
369369
current_date = datetime.now().strftime("%Y-%m-%d")
370-
name = rc['PARENT_PROJECT_NAME'] if 'PARENT_PROJECT_NAME' in rc else "9pm"
370+
name = config['PROJECT-NAME'] if 'PROJECT-NAME' in config else "9pm"
371371

372372
file.write(f"= {name} Test Report\n")
373373
file.write("Author: 9pm Test Framework\n")
@@ -379,7 +379,7 @@ def write_report(data, rc):
379379

380380
file.write("\n<<<\n")
381381
file.write("\n== Test Summary\n\n")
382-
write_report_parent_info(file, rc)
382+
write_report_project_info(file, config)
383383

384384
file.write("\n<<<\n")
385385
file.write("\n== Test Result\n\n")
@@ -537,20 +537,46 @@ def run_suite(cmdline, data, skip_suite):
537537

538538
return skip, err
539539

540+
def parse_config(root_path, config_file):
541+
files = [
542+
os.path.join(root_path, '..', '9pm.yaml'),
543+
os.path.join(root_path, 'etc', '9pm.yaml')
544+
]
545+
546+
path = next((os.path.expanduser(f) for f in files if os.path.exists(os.path.expanduser(f))), None)
547+
548+
if config_file:
549+
if not os.path.exists(config_file):
550+
print(f"error, config file \"{config_file}\" not found.")
551+
sys.exit(1)
552+
553+
path = config_file
554+
555+
if path:
556+
cprint(pcolor.faint, f"Using Config: {path}")
557+
else:
558+
print("Running without config")
559+
return
560+
561+
try:
562+
with open(path, 'r') as f:
563+
data = yaml.safe_load(f) or {}
564+
except yaml.YAMLError:
565+
print(f"error, parsing YAML {path} config.")
566+
sys.exit(1)
567+
568+
if '9pm' in data:
569+
return data['9pm']
570+
return []
571+
540572
def parse_rc(root_path):
541573
rc = {}
542574
required_keys = ["LOG_PATH"]
543-
voluntary_keys = [
544-
"PARENT_PROJECT_NAME",
545-
"PARENT_PROJECT_ROOT",
546-
]
547575

548576
files = [
549-
os.path.join(root_path, '..', '9pm.rc'),
550577
os.path.join("~/.9pm.rc"),
551578
os.path.join(root_path, 'etc', '9pm.rc')
552579
]
553-
554580
path = next((os.path.expanduser(f) for f in files if os.path.exists(os.path.expanduser(f))), None)
555581

556582
if path:
@@ -571,11 +597,6 @@ def parse_rc(root_path):
571597
print(f"error, 9pm.rc is missing required keys: {', '.join(missing_keys)}")
572598
sys.exit(1)
573599

574-
allowed_keys = set(required_keys + voluntary_keys)
575-
unsupported_keys = [key for key in data if key not in allowed_keys]
576-
if unsupported_keys:
577-
print(f"warning, 9pm.rc contains unsupported keys: {', '.join(unsupported_keys)}")
578-
579600
return data
580601

581602
def parse_cmdline():
@@ -655,14 +676,16 @@ def main():
655676

656677
LOGDIR = setup_log_dir(rc['LOG_PATH'])
657678

658-
if 'PARENT_PROJECT_NAME' in rc:
659-
str = f"\nTesting {rc['PARENT_PROJECT_NAME']}"
660-
if 'PARENT_PROJECT_ROOT' in rc:
661-
str += f" ({run_git_cmd(rc['PARENT_PROJECT_ROOT'], ['rev-parse', 'HEAD'])[:12]})"
662-
cprint(pcolor.yellow, str)
663-
664679
args = parse_cmdline()
665680

681+
config = parse_config(ROOT_PATH, args.config)
682+
683+
if 'PROJECT-NAME' in config:
684+
str = f"\nTesting {config['PROJECT-NAME']}"
685+
if 'PROJECT-ROOT' in config:
686+
str += f" ({run_git_cmd(config['PROJECT-ROOT'], ['rev-parse', 'HEAD'])[:12]})"
687+
cprint(pcolor.yellow, str)
688+
666689
scratch = tempfile.mkdtemp(suffix='', prefix='9pm_', dir='/tmp')
667690
if args.debug:
668691
print("Created scratch dir:", scratch)
@@ -706,7 +729,7 @@ def main():
706729
print_result_tree(cmdl, "")
707730
write_md_result(cmdl)
708731
write_github_result(cmdl)
709-
write_report(cmdl, rc)
732+
write_report(cmdl, config)
710733

711734
db.close()
712735
sys.exit(err)

etc/9pm.rc

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,8 @@
22
#
33
# If you wish to have your own configuration you can copy this and modify
44
# the values as you see fit. Here's the order of precedence:
5-
# 1) Project unique RC: ../9pm.rc
6-
# 2) Your personal RC for all your project: ~/.9pm.rc
7-
# 3) Default 9pm fallback (this file) .../9pm/etc/9pm.rc
5+
# 1) Your personal RC for all your project: ~/.9pm.rc
6+
# 2) Default 9pm fallback (this file) .../9pm/etc/9pm.rc
87

98
# Default log base path
10-
LOG_PATH: "~/.local/share/9pm/logs"
11-
12-
## Parent Project Settings
13-
#
14-
# Typically 9pm is included as a submodule of a parent project which it tests.
15-
16-
# Name of the parent project
17-
#PARENT_PROJECT_NAME: "Foobar"
18-
19-
# The root directory if the parent project, relative to 9pm root.
20-
#PARENT_PROJECT_ROOT: "../"
9+
LOG_PATH: "~/.local/share/9pm/logs"

etc/9pm.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This is the default config for 9pm and its test cases.
2+
# The top level "9pm" contains data aimed for the 9pm.py test executor.
3+
# This file is passed down to test cases as the env variable NINEPM_CONFIG.
4+
5+
9pm:
6+
PROJECT-NAME: "9pm"
7+
PROJECT-ROOT: "./"

0 commit comments

Comments
 (0)