Skip to content

Commit

Permalink
Store imported metadata in a sane order [fix psss#86]
Browse files Browse the repository at this point in the history
  • Loading branch information
psss committed Apr 7, 2020
1 parent 9c64440 commit 33caee2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
15 changes: 10 additions & 5 deletions tmt/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,25 @@ class Test(Node):

# Supported attributes (listed in display order)
_keys = [
# Basic test information
'summary',
'description',
'contact',
'component',

# Test execution data
'test',
'path',
'duration',
'environment',
'require',
'relevancy',
'environment',
'duration',
'enabled',
'result',

# Filtering attributes
'tag',
'tier',
'result',
'enabled',
'relevancy',
]

def __init__(self, data, name=None):
Expand Down
13 changes: 9 additions & 4 deletions tmt/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,18 @@ def read_nitrate(beaker_task, common_data, disabled):

def write(path, data):
""" Write gathered metadata in the fmf format """
# Put keys into a reasonable order
extra_keys = ['extra-summary', 'extra-task', 'extra-nitrate']
sorted_data = dict()
for key in tmt.base.Test._keys + extra_keys:
try:
sorted_data[key] = data[key]
except KeyError:
pass
# Store metadata into a fmf file
try:
with open(path, 'w', encoding='utf-8') as fmf_file:
yaml.safe_dump(
data, fmf_file,
encoding='utf-8', allow_unicode=True,
indent=4, default_flow_style=False)
fmf_file.write(tmt.utils.dict_to_yaml(sorted_data))
except IOError:
raise ConvertError("Unable to write '{0}'".format(path))
echo(style(
Expand Down
4 changes: 2 additions & 2 deletions tmt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,11 @@ def variables_to_dictionary(variables):
return result


def dict_to_yaml(data, width=None):
def dict_to_yaml(data, width=None, sort=False):
""" Convert dictionary into yaml """
output = io.StringIO()
yaml.safe_dump(
data, output,
data, output, sort_keys=sort,
encoding='utf-8', allow_unicode=True,
width=width, indent=4, default_flow_style=False)
return output.getvalue()
Expand Down

0 comments on commit 33caee2

Please sign in to comment.