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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
- """
- This unittest is NOT added to the unit_tests folder because it is NOT testing SG code.
- The breaking change tests are only meant to exist in the CI, and therefore it was not included to the SG package.
- """
- import unittest
- from .code_parser import FunctionParameter, FunctionParameters, FunctionSignature, parse_imports, parse_functions_signatures
- from .breaking_changes_detection import extract_code_breaking_changes
- class TestBreakingChangeDetection(unittest.TestCase):
- def test_module_removed(self):
- old_code = "import super_gradients.missing_module"
- new_code = ""
- self.assertEqual(parse_imports(old_code), {"super_gradients.missing_module": "super_gradients.missing_module"})
- self.assertEqual(parse_imports(new_code), {})
- # Imports not checked in regular modules
- breaking_changes = extract_code_breaking_changes("module.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.imports_removed), 0)
- # Imports checked in __init__.py
- breaking_changes = extract_code_breaking_changes("__init__.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.imports_removed), 1)
- # Attributes of the breaking change
- breaking_change = breaking_changes.imports_removed[0]
- self.assertEqual(breaking_change.import_name, "super_gradients.missing_module")
- def test_module_renamed(self):
- old_code = "import super_gradients"
- new_code = "import new_module"
- self.assertNotEqual(parse_imports(old_code), parse_imports(new_code))
- # Imports not checked in regular modules
- breaking_changes = extract_code_breaking_changes("module.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.imports_removed), 0)
- # Imports checked in __init__.py
- breaking_changes = extract_code_breaking_changes("__init__.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.imports_removed), 1)
- # Check the attributes of the breaking change
- breaking_change = breaking_changes.imports_removed[0]
- self.assertEqual(breaking_change.import_name, "super_gradients")
- def test_module_location_changed(self):
- old_code = "from super_gradients import my_module"
- new_code = "from super_gradients.subpackage import my_module"
- self.assertNotEqual(parse_imports(old_code), parse_imports(new_code))
- # Imports not checked in regular modules
- breaking_changes = extract_code_breaking_changes("module.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.imports_removed), 0)
- # Imports checked in __init__.py
- breaking_changes = extract_code_breaking_changes("__init__.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.imports_removed), 1)
- # Check the attributes of the breaking change
- breaking_change = breaking_changes.imports_removed[0]
- self.assertEqual(breaking_change.import_name, "super_gradients.my_module")
- def test_dependency_version_changed(self):
- """We want to be sensitive to source, not alias! (i.e. we want to distinguish between v1 and v2)"""
- old_code = "import super_gradients.library_v1 as library"
- new_code = "import super_gradients.library_v2 as library"
- self.assertNotEqual(parse_imports(old_code), parse_imports(new_code))
- # Imports not checked in regular modules
- breaking_changes = extract_code_breaking_changes("module.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.imports_removed), 0)
- # Imports checked in __init__.py
- breaking_changes = extract_code_breaking_changes("__init__.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.imports_removed), 1)
- # Check the attributes of the breaking change
- breaking_change = breaking_changes.imports_removed[0]
- self.assertEqual(breaking_change.import_name, "super_gradients.library_v1")
- def test_function_removed(self):
- old_code = "def old_function(): pass"
- new_code = ""
- breaking_changes = extract_code_breaking_changes("module.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.functions_removed), 1)
- # Check the attributes of the breaking change
- breaking_change = breaking_changes.functions_removed[0]
- self.assertEqual(breaking_change.function_name, "old_function")
- def test_param_removed(self):
- old_code = "def my_function(param1, param2): pass"
- new_code = "def my_function(param1): pass"
- breaking_changes = extract_code_breaking_changes("module.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.params_removed), 1)
- # Check the attributes of the breaking change
- breaking_change = breaking_changes.params_removed[0]
- self.assertEqual(breaking_change.function_name, "my_function")
- def test_required_params_added(self):
- old_code = "def my_function(param1): pass"
- new_code = "def my_function(param1, param2): pass"
- breaking_changes = extract_code_breaking_changes("module.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.required_params_added), 1)
- # Check the attributes of the breaking change
- breaking_change = breaking_changes.required_params_added[0]
- self.assertEqual(breaking_change.function_name, "my_function")
- self.assertEqual(breaking_change.parameter_name, "param2")
- def test_default_removed(self):
- old_code = "def my_function(param1=None): pass"
- new_code = "def my_function(param1): pass"
- breaking_changes = extract_code_breaking_changes("module.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.required_params_added), 1)
- # Check the attributes of the breaking change
- breaking_change = breaking_changes.required_params_added[0]
- self.assertEqual(breaking_change.function_name, "my_function")
- self.assertEqual(breaking_change.parameter_name, "param1")
- def test_optional_param_added(self):
- old_code = "def my_function(param1): pass"
- new_code = "def my_function(param1, param2=None): pass"
- breaking_changes = extract_code_breaking_changes("module.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.required_params_added), 0)
- def test_optional_param_added2(self):
- old_code = "def my_function(param=None): pass"
- new_code = "def my_function(): pass"
- breaking_changes = extract_code_breaking_changes("module.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.params_removed), 1)
- # Check the attributes of the breaking change
- breaking_change = breaking_changes.params_removed[0]
- self.assertEqual(breaking_change.function_name, "my_function")
- self.assertEqual(breaking_change.parameter_name, "param")
- def test_no_changes(self):
- old_code = "def my_function(param1): pass"
- new_code = "def my_function(param1): pass"
- breaking_changes = extract_code_breaking_changes("module.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.functions_removed), 0)
- self.assertEqual(len(breaking_changes.params_removed), 0)
- self.assertEqual(len(breaking_changes.required_params_added), 0)
- self.assertEqual(len(breaking_changes.imports_removed), 0)
- def test_multiple_changes(self):
- old_code = "import super_gradients.module1\nfrom super_gradients.module2 import function1\ndef my_function(param1, param2): pass"
- new_code = "import super_gradients.module3\ndef my_function(param1, param2, param3): pass"
- # Imports not checked in regular modules
- breaking_changes = extract_code_breaking_changes("module.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.imports_removed), 0)
- self.assertEqual(len(breaking_changes.required_params_added), 1)
- # Imports checked in __init__.py
- breaking_changes = extract_code_breaking_changes("__init__.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.imports_removed), 2)
- self.assertEqual(len(breaking_changes.required_params_added), 1)
- def test_single_function(self):
- code = "def add(a, b=5):\n return a + b"
- functions_signatures = parse_functions_signatures(code)
- self.assertIn("add", functions_signatures)
- function_signature = functions_signatures["add"]
- self.assertIsInstance(function_signature, FunctionSignature)
- self.assertEqual(function_signature.name, "add")
- self.assertEqual(function_signature.line_num, 1)
- self.assertIsInstance(function_signature.params, FunctionParameters)
- self.assertEqual(len(function_signature.params.all), 2)
- self.assertEqual(len(function_signature.params.required), 1)
- self.assertEqual(len(function_signature.params.optional), 1)
- def test_multiple_functions(self):
- code = "def add(a, b): pass\ndef subtract(a, b): pass"
- expected = {
- "add": FunctionSignature(
- name="add",
- line_num=1,
- params=FunctionParameters([FunctionParameter(name="a", has_default=False), FunctionParameter(name="b", has_default=False)]),
- ),
- "subtract": FunctionSignature(
- name="subtract",
- line_num=2,
- params=FunctionParameters([FunctionParameter(name="a", has_default=False), FunctionParameter(name="b", has_default=False)]),
- ),
- }
- self.assertEqual(parse_functions_signatures(code), expected)
- def test_parse_nested_functions(self):
- """Make sure that we DON'T detect change in nested functions (this is internal implementation, not API change)."""
- code = "def outer():\n def inner(a): pass"
- expected = {
- "outer": FunctionSignature(name="outer", line_num=1, params=FunctionParameters([])),
- }
- self.assertEqual(parse_functions_signatures(code), expected)
- def test_no_functions(self):
- code = "a = 5"
- expected = {}
- self.assertEqual(parse_functions_signatures(code), expected)
- def test_class_removed(self):
- old_code = "class MyClass:\n def method(self): pass"
- new_code = ""
- breaking_changes = extract_code_breaking_changes("module.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.classes_removed), 1)
- self.assertEqual(breaking_changes.classes_removed[0].class_name, "MyClass")
- self.assertEqual(len(breaking_changes.functions_removed), 1)
- self.assertEqual(breaking_changes.functions_removed[0].function_name, "MyClass.method")
- def test_class_methods(self):
- old_code = "class MyClass:\n def method(self): pass"
- new_code = "class MyClass:\n def new_method(self): pass"
- breaking_changes = extract_code_breaking_changes("module.py", old_code, new_code)
- self.assertEqual(len(breaking_changes.functions_removed), 1)
- self.assertEqual(breaking_changes.functions_removed[0].function_name, "MyClass.method")
- if __name__ == "__main__":
- unittest.main()
|