-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscenario_parser.py
137 lines (118 loc) · 4.86 KB
/
scenario_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import argparse
import os
import yaml
from behave.model import Scenario
from behave.model import ScenarioOutline
from behave.parser import Parser
CLI = argparse.ArgumentParser()
CLI.add_argument(
'--exclude-tags',
nargs='*',
type=str,
default=['skip', 'need_fix']
)
CLI.add_argument(
'--page-name',
nargs=1,
type=str,
)
CLI.add_argument(
'--repo-name',
nargs=1,
type=str,
)
args = CLI.parse_args()
excluded_tags = args.exclude_tags
print(f"Excluded tags: {excluded_tags}")
def parse_feature_files(feat_files):
feature_scenarios = {}
parser = Parser()
for curr_feature_file in feat_files:
with open(curr_feature_file, 'r') as f:
feature_content = f.read()
feature = parser.parse(feature_content, filename=curr_feature_file)
scenarios = []
for element in feature:
if isinstance(element, (ScenarioOutline, Scenario)):
scenario_name = element.name
scenario_description = element.description
scenario_tags = element.tags
scenarios.append({
'name': scenario_name,
'description': scenario_description,
'tags': scenario_tags + feature.tags
})
if scenarios:
feature_file_name = os.path.splitext(os.path.basename(curr_feature_file))[0]
feature_scenarios[feature_file_name] = {
'feature_name': feature.name,
'scenarios': scenarios,
}
return feature_scenarios
root_directory = 'src/bdd'
feature_files = []
folder_links = []
component_features = {}
for dir in os.listdir(root_directory):
if os.path.isdir(os.path.join(root_directory, dir)) and dir != '__pycache__':
folder_links.append(f"- [{dir}]({os.path.join('components', dir+'.md')})")
component_features[dir] = []
for root, _, files in os.walk(os.path.join(root_directory, dir)):
for file in files:
if file.endswith('.feature'):
feature_files.append(os.path.join(root, file))
component_features[dir].append(os.path.join(root, file))
all_scenarios = []
scenarios_by_feature = parse_feature_files(feature_files)
output_directory = 'docs/components'
os.makedirs(output_directory, exist_ok=True)
index_content = '# Components\n\n'
index_content += '\n'.join(folder_links) + '\n\n'
nav_links = []
for component, files in component_features.items():
component_dir_path = os.path.join(output_directory, component)
os.makedirs(component_dir_path, exist_ok=True)
component_file_path = os.path.join(output_directory, f'{component}.md')
with open(component_file_path, 'w') as component_file:
component_file.write(f'# {component} features:\n\n')
for file in files:
feature_name = os.path.splitext(os.path.basename(file))[0]
feature_data = scenarios_by_feature.get(feature_name, {})
feature_title = feature_data.get('feature_name', feature_name)
feature_file_path = os.path.join(component_dir_path, f'{feature_name}.md')
component_file.write(f'## [{feature_title}]({os.path.join(component, feature_name+".md")})\n\n')
with open(feature_file_path, 'w') as feature_file:
feature_file.write(f'# {feature_title}\n\n')
feature_file.write(f'## Scenarios:\n\n')
for scenario in feature_data.get('scenarios', []):
if not set(excluded_tags).intersection(set(scenario['tags'])):
all_scenarios.append(scenario['name'])
feature_file.write(f'* {scenario["name"]}\n')
component_file.write('<hr>\n')
component_file.write(f'## [Allure Test report](https://pagopa.github.io/{args.repo_name[0]}/{component}-tests)')
nav_links.append({component: f'components/{component}.md'})
index_file_path = os.path.join('docs', 'index.md')
with open(index_file_path, 'w') as index_file:
index_file.write(index_content)
mkdocs_config = {
'site_name': args.page_name[0],
'site_url': f'https://pagopa.github.io/{args.repo_name[0]}',
'repo_name': f'pagopa/{args.repo_name[0]}',
'repo_url': f'https://github.com/pagopa/{args.repo_name[0]}',
'site_author': 'PagoPA',
'use_directory_urls': True,
'nav': [
{'Home': 'index.md'},
{'Components': nav_links},
],
'theme': {
'name': 'material',
'font': {'text': 'Roboto', 'code': 'Roboto Mono'},
},
}
mkdocs_yaml_path = 'mkdocs.yml'
with open(mkdocs_yaml_path, 'w') as mkdocs_yaml:
yaml.dump(mkdocs_config, mkdocs_yaml)
all_scenarios_file = os.path.join('docs', 'all_scenarios.txt')
with open(all_scenarios_file, 'w') as all_scenarios_file:
all_scenarios_file.write('\n'.join(sorted(all_scenarios)))