|
14 | 14 | from .core import DeferrableTestCase |
15 | 15 | from .core import DeferrableTestLoader |
16 | 16 | from .core import DeferringTextTestRunner |
17 | | -from .reloader import reload_package |
| 17 | +from .reloader import async_reload_package, reload_package |
18 | 18 |
|
19 | 19 | try: |
20 | 20 | import coverage |
@@ -97,91 +97,98 @@ def run(self, package=None, **kwargs): |
97 | 97 |
|
98 | 98 | stream = self.load_stream(package, settings) |
99 | 99 |
|
100 | | - def run_tests(): |
101 | | - if settings["async"]: |
102 | | - threading.Thread( |
103 | | - target=self.run_coverage, args=(package, stream, settings) |
104 | | - ).start() |
105 | | - else: |
106 | | - self.run_coverage(package, stream, settings) |
107 | | - |
108 | | - if settings["reload_package_on_testing"]: |
109 | | - reload_package(package, on_done=run_tests) |
| 100 | + if settings["async"]: |
| 101 | + threading.Thread( |
| 102 | + target=self.run_coverage, args=(package, stream, settings) |
| 103 | + ).start() |
110 | 104 | else: |
111 | | - run_tests() |
| 105 | + self.run_coverage(package, stream, settings) |
112 | 106 |
|
113 | 107 | def run_coverage(self, package, stream, settings): |
114 | | - if not coverage or not settings["coverage"]: |
115 | | - if settings["coverage"]: |
116 | | - stream.write("Warning: coverage cannot be loaded.\n\n") |
| 108 | + # prepare coverage |
| 109 | + cleanup_hooks = [] |
| 110 | + if settings["coverage"] and not coverage: |
| 111 | + stream.write("Warning: coverage cannot be loaded.\n\n") |
| 112 | + |
| 113 | + elif settings["coverage"]: |
| 114 | + packages_path = sublime.packages_path() |
| 115 | + package_path = os.path.join(packages_path, package) |
| 116 | + data_file_dir = os.path.join(packages_path, "User", "UnitTesting", package) |
| 117 | + os.makedirs(data_file_dir, exist_ok=True) |
| 118 | + data_file = os.path.join(data_file_dir, "coverage") |
| 119 | + if os.path.exists(data_file): |
| 120 | + os.unlink(data_file) |
| 121 | + config_file = os.path.join(package_path, ".coveragerc") |
| 122 | + include = "{}/*".format(package_path) |
| 123 | + omit = "{}/{}/*".format(package_path, settings["tests_dir"]) |
| 124 | + if os.path.exists(config_file): |
| 125 | + with open(config_file, "r") as f: |
| 126 | + txt = f.read() |
| 127 | + if re.search("^include", txt, re.M): |
| 128 | + include = None |
| 129 | + if re.search("^omit", txt, re.M): |
| 130 | + omit = None |
| 131 | + else: |
| 132 | + config_file = False |
117 | 133 |
|
118 | | - self.run_tests(stream, package, settings, []) |
119 | | - return |
| 134 | + cov = coverage.Coverage( |
| 135 | + data_file=data_file, config_file=config_file, include=include, omit=omit |
| 136 | + ) |
120 | 137 |
|
121 | | - packages_path = sublime.packages_path() |
122 | | - package_path = os.path.join(packages_path, package) |
123 | | - data_file_dir = os.path.join(packages_path, "User", "UnitTesting", package) |
124 | | - os.makedirs(data_file_dir, exist_ok=True) |
125 | | - data_file = os.path.join(data_file_dir, "coverage") |
126 | | - if os.path.exists(data_file): |
127 | | - os.unlink(data_file) |
128 | | - config_file = os.path.join(package_path, ".coveragerc") |
129 | | - include = "{}/*".format(package_path) |
130 | | - omit = "{}/{}/*".format(package_path, settings["tests_dir"]) |
131 | | - if os.path.exists(config_file): |
132 | | - with open(config_file, "r") as f: |
133 | | - txt = f.read() |
134 | | - if re.search("^include", txt, re.M): |
135 | | - include = None |
136 | | - if re.search("^omit", txt, re.M): |
137 | | - omit = None |
138 | | - else: |
139 | | - config_file = False |
| 138 | + cov.start() |
140 | 139 |
|
141 | | - cov = coverage.Coverage( |
142 | | - data_file=data_file, config_file=config_file, include=include, omit=omit |
143 | | - ) |
| 140 | + if settings["coverage_on_worker_thread"]: |
| 141 | + original_set_timeout_async = sublime.set_timeout_async |
144 | 142 |
|
145 | | - cov.start() |
| 143 | + def set_timeout_async(callback, *args, **kwargs): |
| 144 | + def _(): |
| 145 | + sys.settrace(threading._trace_hook) |
| 146 | + callback() |
146 | 147 |
|
147 | | - if settings["coverage_on_worker_thread"]: |
148 | | - original_set_timeout_async = sublime.set_timeout_async |
| 148 | + original_set_timeout_async(_, *args, **kwargs) |
149 | 149 |
|
150 | | - def set_timeout_async(callback, *args, **kwargs): |
151 | | - def _(): |
152 | | - sys.settrace(threading._trace_hook) |
153 | | - callback() |
| 150 | + sublime.set_timeout_async = set_timeout_async |
154 | 151 |
|
155 | | - original_set_timeout_async(_, *args, **kwargs) |
| 152 | + def cleanup(): |
| 153 | + if settings["coverage_on_worker_thread"]: |
| 154 | + sublime.set_timeout_async = original_set_timeout_async |
| 155 | + |
| 156 | + stream.write("\n") |
| 157 | + cov.stop() |
| 158 | + coverage.files.RELATIVE_DIR = os.path.normcase(package_path + os.sep) |
| 159 | + ignore_errors = cov.get_option("report:ignore_errors") |
| 160 | + show_missing = cov.get_option("report:show_missing") |
| 161 | + cov.report( |
| 162 | + file=stream, ignore_errors=ignore_errors, show_missing=show_missing |
| 163 | + ) |
| 164 | + |
| 165 | + if settings["generate_xml_report"]: |
| 166 | + xml_report_file = os.path.join(package_path, "coverage.xml") |
| 167 | + cov.xml_report(outfile=xml_report_file, ignore_errors=ignore_errors) |
| 168 | + |
| 169 | + if settings["generate_html_report"]: |
| 170 | + html_output_dir = os.path.join(package_path, "htmlcov") |
| 171 | + cov.html_report( |
| 172 | + directory=html_output_dir, ignore_errors=ignore_errors |
| 173 | + ) |
156 | 174 |
|
157 | | - sublime.set_timeout_async = set_timeout_async |
| 175 | + cov.save() |
158 | 176 |
|
159 | | - def cleanup(): |
160 | | - if settings["coverage_on_worker_thread"]: |
161 | | - sublime.set_timeout_async = original_set_timeout_async |
162 | | - |
163 | | - stream.write("\n") |
164 | | - cov.stop() |
165 | | - coverage.files.RELATIVE_DIR = os.path.normcase(package_path + os.sep) |
166 | | - ignore_errors = cov.get_option("report:ignore_errors") |
167 | | - show_missing = cov.get_option("report:show_missing") |
168 | | - cov.report( |
169 | | - file=stream, ignore_errors=ignore_errors, show_missing=show_missing |
170 | | - ) |
| 177 | + cleanup_hooks = [cleanup] |
171 | 178 |
|
172 | | - if settings["generate_xml_report"]: |
173 | | - xml_report_file = os.path.join(package_path, "coverage.xml") |
174 | | - cov.xml_report(outfile=xml_report_file, ignore_errors=ignore_errors) |
| 179 | + def run_tests(): |
| 180 | + self.run_tests(stream, package, settings, cleanup_hooks) |
175 | 181 |
|
176 | | - if settings["generate_html_report"]: |
177 | | - html_output_dir = os.path.join(package_path, "htmlcov") |
178 | | - cov.html_report(directory=html_output_dir, ignore_errors=ignore_errors) |
| 182 | + if settings["reload_package_on_testing"]: |
| 183 | + if not settings["async"]: |
| 184 | + reload_package(package, on_done=run_tests) |
| 185 | + return |
179 | 186 |
|
180 | | - cov.save() |
| 187 | + async_reload_package(package) |
181 | 188 |
|
182 | | - self.run_tests(stream, package, settings, [cleanup]) |
| 189 | + run_tests() |
183 | 190 |
|
184 | | - def run_tests(self, stream, package, settings, cleanup_hooks=[]): |
| 191 | + def run_tests(self, stream, package, settings, cleanup_hooks): |
185 | 192 | if settings["capture_console"]: |
186 | 193 | stdout = sys.stdout |
187 | 194 | stderr = sys.stderr |
|
0 commit comments