From 02491601cd995224b18e3aa5d2f6248353e6568b Mon Sep 17 00:00:00 2001 From: Chris Ge Date: Fri, 16 Aug 2024 15:38:30 -0400 Subject: [PATCH 1/7] fix: ast code now doesn't use endline_no to support older python --- CHANGELOG.md | 5 +++++ cls/TestCoverage/Utils.cls | 23 ++++++++++++++--------- module.xml | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5fe327..683e70d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [4.0.3] - 2024-08-16 + +### Changed +- #53: Method mapping code now doesn't use AST's endline_no property to support older python versions + ## [4.0.2] - 2024-08-16 ### Fixed diff --git a/cls/TestCoverage/Utils.cls b/cls/TestCoverage/Utils.cls index ee5256b..2806eb6 100644 --- a/cls/TestCoverage/Utils.cls +++ b/cls/TestCoverage/Utils.cls @@ -451,9 +451,8 @@ ClassMethod GetPythonMethodMapping(pDocumentText) [ Language = python ] import iris import ast source_lines = iris.cls('%SYS.Python').ToList(pDocumentText) - source_lines = [line + "\n" for line in source_lines] # contains a list of each line of the source code - source = ''.join(source_lines) + source = '\n'.join(source_lines) tree = ast.parse(source) line_function_map = [None] * (len(source_lines)+2) method_map = {} # dictionary from the method name to its start and ending line number @@ -473,7 +472,9 @@ ClassMethod GetPythonMethodMapping(pDocumentText) [ Language = python ] def visit_FunctionDef(self, node): if self.outermost_function is None: self.outermost_function = node.name - method_map[node.name] = (node.lineno-1, node.end_lineno-1) + start_line = node.lineno + end_line = self.get_end_line(node) + method_map[node.name] = (start_line, end_line) self.current_function = node.name for lineno in range(node.lineno, node.end_lineno + 1): @@ -483,14 +484,18 @@ ClassMethod GetPythonMethodMapping(pDocumentText) [ Language = python ] self.current_function = None if self.outermost_function == node.name: self.outermost_function = None - - # preprocessing the ending line number for each function - tree_with_line_numbers = ast.increment_lineno(tree, n=1) - for node in ast.walk(tree_with_line_numbers): - if isinstance(node, ast.FunctionDef): - node.end_lineno = node.body[-1].end_lineno + @staticmethod + def get_end_line(node): + return max(child.lineno for child in ast.walk(node) if hasattr(child, 'lineno')) + #; # preprocessing the ending line number for each function + #; tree_with_line_numbers = ast.increment_lineno(tree, n=1) + #; for node in ast.walk(tree_with_line_numbers): + #; if isinstance(node, ast.FunctionDef): + #; node.end_lineno = node.body[-1].end_lineno FunctionMapper().visit(tree) + print(source_lines) + print(method_map) return (line_function_map, method_map) } diff --git a/module.xml b/module.xml index 3b9f9ec..4cf105b 100644 --- a/module.xml +++ b/module.xml @@ -2,7 +2,7 @@ TestCoverage - 4.0.2 + 4.0.3 Run your typical ObjectScript %UnitTest tests and see which lines of your code are executed. Includes Cobertura-style reporting for use in continuous integration tools. module From e1dfcf492c3a426fa439aa79a4d04748b7c747be Mon Sep 17 00:00:00 2001 From: Chris Ge Date: Fri, 16 Aug 2024 15:41:11 -0400 Subject: [PATCH 2/7] docs: changelog number --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 683e70d..aad61d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [4.0.3] - 2024-08-16 +## [4.0.3] - Unreleased ### Changed -- #53: Method mapping code now doesn't use AST's endline_no property to support older python versions +- #52: Method mapping code now doesn't use AST's endline_no property to support older python versions ## [4.0.2] - 2024-08-16 From 799f3dedd2935109756a53cdd05b781cc8072195 Mon Sep 17 00:00:00 2001 From: Chris Ge Date: Fri, 16 Aug 2024 15:42:18 -0400 Subject: [PATCH 3/7] style: killed dead code --- cls/TestCoverage/Utils.cls | 5 ----- 1 file changed, 5 deletions(-) diff --git a/cls/TestCoverage/Utils.cls b/cls/TestCoverage/Utils.cls index 2806eb6..53bb305 100644 --- a/cls/TestCoverage/Utils.cls +++ b/cls/TestCoverage/Utils.cls @@ -487,11 +487,6 @@ ClassMethod GetPythonMethodMapping(pDocumentText) [ Language = python ] @staticmethod def get_end_line(node): return max(child.lineno for child in ast.walk(node) if hasattr(child, 'lineno')) - #; # preprocessing the ending line number for each function - #; tree_with_line_numbers = ast.increment_lineno(tree, n=1) - #; for node in ast.walk(tree_with_line_numbers): - #; if isinstance(node, ast.FunctionDef): - #; node.end_lineno = node.body[-1].end_lineno FunctionMapper().visit(tree) print(source_lines) From fa465153112fb518b2661db026377e6bda2e0723 Mon Sep 17 00:00:00 2001 From: Chris Ge Date: Fri, 16 Aug 2024 15:43:34 -0400 Subject: [PATCH 4/7] style: removed debugging code --- cls/TestCoverage/Utils.cls | 2 -- 1 file changed, 2 deletions(-) diff --git a/cls/TestCoverage/Utils.cls b/cls/TestCoverage/Utils.cls index 53bb305..aab3335 100644 --- a/cls/TestCoverage/Utils.cls +++ b/cls/TestCoverage/Utils.cls @@ -489,8 +489,6 @@ ClassMethod GetPythonMethodMapping(pDocumentText) [ Language = python ] return max(child.lineno for child in ast.walk(node) if hasattr(child, 'lineno')) FunctionMapper().visit(tree) - print(source_lines) - print(method_map) return (line_function_map, method_map) } From c95eafde1040670e376df93ccdef308767ff50e8 Mon Sep 17 00:00:00 2001 From: Chris Ge Date: Fri, 16 Aug 2024 15:59:16 -0400 Subject: [PATCH 5/7] fix: got rid of one more endline_no --- cls/TestCoverage/Utils.cls | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cls/TestCoverage/Utils.cls b/cls/TestCoverage/Utils.cls index aab3335..9780288 100644 --- a/cls/TestCoverage/Utils.cls +++ b/cls/TestCoverage/Utils.cls @@ -477,7 +477,7 @@ ClassMethod GetPythonMethodMapping(pDocumentText) [ Language = python ] method_map[node.name] = (start_line, end_line) self.current_function = node.name - for lineno in range(node.lineno, node.end_lineno + 1): + for lineno in range(node.lineno, self.get_end_line(node) + 1): line_function_map[lineno-1] = self.outermost_function self.generic_visit(node) From a5389ebb83656e1b522fee67b3a3457fd54f919f Mon Sep 17 00:00:00 2001 From: Chris Ge Date: Fri, 16 Aug 2024 16:11:44 -0400 Subject: [PATCH 6/7] fix: some code is executed from apparently unnamed classes --- cls/TestCoverage/Utils/LineByLineMonitor.cls | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cls/TestCoverage/Utils/LineByLineMonitor.cls b/cls/TestCoverage/Utils/LineByLineMonitor.cls index 1e335d5..5b5b69f 100644 --- a/cls/TestCoverage/Utils/LineByLineMonitor.cls +++ b/cls/TestCoverage/Utils/LineByLineMonitor.cls @@ -76,10 +76,10 @@ ClassMethod PyStartWithScope(pCoverageClasses As %List) [ Language = python ] # extracts frame code code = frame.f_code # extracts calling function name and the class that the function is in - class_name = frame.f_globals['__name__'] + class_name = frame.f_globals.get('__name__', None) # Use get to avoid KeyError # extracts the line number line_no = frame.f_lineno - if class_name in tCoverageClasses and line_no > 1: # if this is in a covered class + if class_name and class_name in tCoverageClasses and line_no > 1: # if this is in a covered class tGlob = iris.gref('^IRIS.Temp.TestCoveragePY') # python doesn't have macros -- this is $$$PyMonitorResults # $$$PyMonitorResults(classname, linenumber) = the number of times that linenumber in that class was covered From 3e95e0a1ea304d1e3101f4567fcd0ddc91e5f8e7 Mon Sep 17 00:00:00 2001 From: Chris Ge Date: Fri, 16 Aug 2024 16:15:05 -0400 Subject: [PATCH 7/7] docs: changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aad61d2..905747c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - #52: Method mapping code now doesn't use AST's endline_no property to support older python versions +- #53: Ignore traced commands from code without a class name ## [4.0.2] - 2024-08-16