summaryrefslogtreecommitdiff
path: root/tests/scripts/smarty_parser.py
blob: 40f2a500d45f665ec0be2ccdffb489786a917b1e (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python
#
## Licensed to the .NET Foundation under one or more agreements.
## The .NET Foundation licenses this file to you under the MIT license.
## See the LICENSE file in the project root for more information.
#
##
# Title               :smarty_parser.py
#
# Notes:
#
# Simple class to parse through the smarty .smrt files and individual output
# files.
#
# Expects:
#
# Smarty results directory:
#
#   --Smarty.run.xx
#     |
#     |---
#        |
#        |--- Smarty.run.xx.fail.smrt (optional, if there are failures)
#        |--- Smarty.run.xx.pass.smrt (optional, if there are passes)
#        |--- Smarty.rpt.xx.html
#        |--- Smarty.rpt.xx.passed.html
#        |--- Smarty.xml
#        |--- Smrt00000xx
#             |
#             |---
#                |
#                |---Tests.lst_<test_name>.cmd_xx.x.y.html
#                |---Tests.lst_<test_name>.cmd_xx.x.y.txt
#    
################################################################################

from collections import defaultdict
import os
import re
import unittest
import sys

################################################################################

class SmartyParser:

   def __init__(self, path):
      if not os.path.isdir(path):
         raise Exception("Expected a valid path to parse through")

      self.m_path = path
      self.m_missing = []
      self.m_tests = []
      self.m_passed = []
      self.m_failed = []
 
   def parse(self):
      files = os.listdir(self.m_path)
      
      failed_smrt_files = []
      passed_smrt_files = []
      
      for file in files:
         if "fail.smrt" in file:
            failed_smrt_files.append(file)
         elif "pass.smrt" in file:
            passed_smrt_files.append(file)
            
      def parse_smrt_files(smrt_list):
         test_list = []
      
         for smrt_file in smrt_list:
            lines = None
            with open(os.path.join(self.m_path, smrt_file)) as file_handle:
               lines = file_handle.readlines()
            
            lines = "\n".join(lines)
            lines = lines.split("[TESTS]")

            tests = lines[1].split("Tests.lst=")[1:]
            
            test_names = []
            for test in tests:
               split = test.split(",")
               
               dir = split[3].strip()
               test_name = split[0].strip()
               test_name = "cmd_".join(test_name.split("cmd"))
            
               test_names.append((dir, test_name))
            
            for test in test_names:
               test_list.append(test)
               
         return test_list
      
      failed_tests = parse_smrt_files(failed_smrt_files)
      passed_tests = parse_smrt_files(passed_smrt_files)
      
      cached_ls = defaultdict(lambda: None)
 
      def iterate_tests(test_list):
         local_tests = []
 
         for test in test_list:
            smrt_dir = test[0]
            
            if cached_ls[smrt_dir] is None:
               cached_ls[smrt_dir] = os.listdir(os.path.join(self.m_path, smrt_dir))
               ds = defaultdict(lambda: [])
               file_names = cached_ls[smrt_dir]
               
               for file_name in file_names:
                  split = file_name.split(".result")
                  ds[split[0]].append(".result".join(split))
                  
               cached_ls[smrt_dir] = ds
            
            result_files = cached_ls[test[0]]["Tests.lst_" + test[1]]
            
            if len(result_files) == 0:
               self.m_missing.append(test[0])

            else:
               for file in result_files:  
                  if os.path.splitext(file)[1] == ".html":
                     result = self.parse_smarty_file(os.path.join(self.m_path, test[0], file))
                     
                     local_tests.append(result)
                     self.m_tests.append(result)
                     
      self.m_failed = iterate_tests(failed_tests)
      self.m_passed = iterate_tests(passed_tests)

   def parse_smarty_file(self, path):
      lines = self.remove_tags(path)

      tags = defaultdict(lambda: False)

      tags["TEST_IDENTIFIER"] = True
      tags["CATEGORIES"] = True
      tags["RELATIVEPATH"] = True
      tags["WORKINGDIR"] = True
      tags["TEST_CMD_LINE"] = True
      tags["TEST_EXPECTED_RETURN_CODE"] = True
      tags["TEST_ACTUAL_RETURN_CODE"] = True
      tags["TEST_START_TIME"] = True
      tags["TEST_END_TIME"] = True
      tags["TEST_RESULT"] = True
      tags["TEST_OUTPUT"] = True

      capturing_output = False

      for line in lines:
         if "TEST OUTPUT" in line:
            capturing_output = True
            tags["TEST_OUTPUT"] = []

         elif capturing_output is True:
            if "TEXT_EXPECTED_RETURN_CODE" in line:
               capturing_output = False
               tags["TEST_OUTPUT"] = "\n".join(tags["TEST_OUTPUT"])

            else:
               tags["TEST_OUTPUT"].append(line)

         elif "=" in line:
            split = line.split(" = ")

            if tags[split[0]] is True:
               value = "=".join(split[1:])

               tags[split[0]] = value.strip()

         elif ":" in line:
            # TEST_CMD_LINE does not use =
            split = line.split(": ")

            if tags[split[0]] is True:
               tags[split[0]] = ":".join(split[1:])

      return tags        

   def remove_tags(self, path):
      tag_re = re.compile(r'<[^>]+>')
      lines = []

      with open(path) as file_handle:
         for line in file_handle:
            line = tag_re.sub('', line)
            
            # Smarty has a bug such that </BODY will
            # possible be missing the ending >
            # Check for this and remove the tag if found.

            if "</BODY" in line and "</BODY>" not in line:
               line = line.replace("</BODY", "")

            line = line.replace("\r\n", "")

            if len(line) != 0:
               lines.append(line)
      
      return lines