-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsmell_detector.py
More file actions
89 lines (63 loc) · 2.89 KB
/
smell_detector.py
File metadata and controls
89 lines (63 loc) · 2.89 KB
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
import python_parser
from test_smell_rule_runners import project_rule_runner
from test_smell_rule_runners import test_case_rule_runner
from test_smell_rule_runners import test_method_rule_runner
import argparse
import sys
import os
def main():
"""Check given project directory for code smells
Identifies all of the python files in the directory (including those in sub
directories). Submits files through 3 stages of rule checking based on the
scope of the rules (project level, test case level, and test method level).
Results from all tests are aggregated
"""
argument_parser = argparse.ArgumentParser(add_help=True)
argument_parser.add_argument("directory", type=str,
help="Directory to detect test smells.")
args = argument_parser.parse_args()
if len(sys.argv) < 1:
argument_parser.print_help()
else:
if os.path.exists(args.directory) or os.path.isdir(args.directory):
#Stage 1: project level rule checking
files = python_parser.get_python_files(os.path.abspath(args.directory))
results_list = project_rule_runner(files)
#Stage 2: test case level rule checking
#test_case_pairs_list is a list of test cases paired with their file of origin
filtered_files = python_parser.filter_python_files(files)
test_case_pairs_list = python_parser.get_test_case_asts(filtered_files)
for test_case_pair in test_case_pairs_list:
results_list = results_list + test_case_rule_runner(test_case_pair)
#Stage 3: test method level rule checking
test_method_list = list()
for test_case_pair in test_case_pairs_list:
test_method_list = test_method_list + python_parser.get_test_asts(test_case_pair)
for test_method in test_method_list:
results_list = results_list + test_method_rule_runner(test_method)
#Output formatting
format_output(results_list)
else:
print("Invalid path given.")
def format_output(results_list):
reordered_results = list()
for result in results_list:
new_result = None
if len(result) == 2:
new_result = (result[1], result[0])
reordered_results.append(new_result)
elif len(result) == 3:
new_result = (result[2], result[1], result[0])
reordered_results.append(new_result)
output_file = "output.txt"
try:
os.remove(output_file)
except:
pass
writing_to = open(output_file, 'w')
for result in reordered_results:
print(result)
writing_to.write(str(result) + '\n')
if __name__ == '__main__':
main()
pass