Skip to content

Commit b4b553d

Browse files
committed
More name mangling. Avoids mutant names triggering name-based magic in for example pytest.
1 parent e9012a9 commit b4b553d

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

mutmut/__main__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,8 @@ def mangle_function_name(*, name, class_name):
738738
if class_name:
739739
assert CLASS_NAME_SEPARATOR not in class_name
740740
prefix = f'x{CLASS_NAME_SEPARATOR}{class_name}{CLASS_NAME_SEPARATOR}'
741+
else:
742+
prefix = 'x_'
741743
return f'{prefix}{name}'
742744

743745

@@ -752,6 +754,9 @@ def orig_function_and_class_names_from_key(mutant_name):
752754
if CLASS_NAME_SEPARATOR in r:
753755
class_name = r[r.index(CLASS_NAME_SEPARATOR) + 1: r.rindex(CLASS_NAME_SEPARATOR)]
754756
r = r[r.rindex(CLASS_NAME_SEPARATOR) + 1:]
757+
else:
758+
assert r.startswith('x_')
759+
r = r[2:]
755760
return r, class_name
756761

757762

tests/test_mutation.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ def test_function_with_annotation():
194194
source = "def capitalize(s : str):\n return s[0].upper() + s[1:] if s else s\n".strip()
195195
mutants = mutants_for_source(source)
196196
assert mutants == [
197-
'def capitalize__mutmut_1(s : str):\n return s[1].upper() + s[1:] if s else s',
198-
'def capitalize__mutmut_2(s : str):\n return s[None].upper() + s[1:] if s else s',
199-
'def capitalize__mutmut_3(s : str):\n return s[0].upper() - s[1:] if s else s',
200-
'def capitalize__mutmut_4(s : str):\n return s[0].upper() + s[2:] if s else s',
201-
'def capitalize__mutmut_5(s : str):\n return s[0].upper() + s[None] if s else s'
197+
'def x_capitalize__mutmut_1(s : str):\n return s[1].upper() + s[1:] if s else s',
198+
'def x_capitalize__mutmut_2(s : str):\n return s[None].upper() + s[1:] if s else s',
199+
'def x_capitalize__mutmut_3(s : str):\n return s[0].upper() - s[1:] if s else s',
200+
'def x_capitalize__mutmut_4(s : str):\n return s[0].upper() + s[2:] if s else s',
201+
'def x_capitalize__mutmut_5(s : str):\n return s[0].upper() + s[None] if s else s'
202202
]
203203

204204

@@ -221,12 +221,12 @@ def foo():
221221
'''
222222
mutants = mutants_for_source(source)
223223
assert mutants == [
224-
'\ndef foo__mutmut_1(): \n dict(a=None, c=d)\n',
225-
'\ndef foo__mutmut_2(): \n dict(aXX=b, c=d)\n',
226-
'\ndef foo__mutmut_3(): \n dict(a=b, c=None)\n',
227-
'\ndef foo__mutmut_4(): \n dict(a=b, cXX=d)\n',
228-
'\ndef foo__mutmut_5(): \n dict( c=d)\n',
229-
'\ndef foo__mutmut_6(): \n dict(a=b,)\n',
224+
'\ndef x_foo__mutmut_1(): \n dict(a=None, c=d)\n',
225+
'\ndef x_foo__mutmut_2(): \n dict(aXX=b, c=d)\n',
226+
'\ndef x_foo__mutmut_3(): \n dict(a=b, c=None)\n',
227+
'\ndef x_foo__mutmut_4(): \n dict(a=b, cXX=d)\n',
228+
'\ndef x_foo__mutmut_5(): \n dict( c=d)\n',
229+
'\ndef x_foo__mutmut_6(): \n dict(a=b,)\n',
230230
]
231231

232232

@@ -300,11 +300,11 @@ def foo():
300300

301301
def test_orig_function_name_from_key():
302302
assert orig_function_and_class_names_from_key(f'_{CLASS_NAME_SEPARATOR}Foo{CLASS_NAME_SEPARATOR}bar__mutmut_1') == ('bar', 'Foo')
303-
assert orig_function_and_class_names_from_key('bar__mutmut_1') == ('bar', None)
303+
assert orig_function_and_class_names_from_key('x_bar__mutmut_1') == ('bar', None)
304304

305305

306306
def test_mangle_function_name():
307-
assert mangle_function_name(name='bar', class_name=None) == 'bar'
307+
assert mangle_function_name(name='bar', class_name=None) == 'x_bar'
308308
assert mangle_function_name(name='bar', class_name='Foo') == f'x{CLASS_NAME_SEPARATOR}Foo{CLASS_NAME_SEPARATOR}bar'
309309

310310

tests/test_mutmut3.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@ def foo(a, b, c):
1818
1919
a + 1
2020
21-
def foo__mutmut_orig(a, b, c):
21+
def x_foo__mutmut_orig(a, b, c):
2222
return a + b * c
2323
24-
def foo__mutmut_1(a, b, c):
24+
def x_foo__mutmut_1(a, b, c):
2525
return a - b * c
2626
27-
def foo__mutmut_2(a, b, c):
27+
def x_foo__mutmut_2(a, b, c):
2828
return a + b / c
2929
30-
foo__mutmut_mutants = {
31-
'foo__mutmut_1': foo__mutmut_1,
32-
'foo__mutmut_2': foo__mutmut_2
30+
x_foo__mutmut_mutants = {
31+
'x_foo__mutmut_1': x_foo__mutmut_1,
32+
'x_foo__mutmut_2': x_foo__mutmut_2
3333
}
3434
3535
def foo(*args, **kwargs):
36-
return _mutmut_trampoline(foo__mutmut_orig, foo__mutmut_mutants, *args, **kwargs)
36+
return _mutmut_trampoline(x_foo__mutmut_orig, x_foo__mutmut_mutants, *args, **kwargs)
3737
38-
foo.__signature__ = _mutmut_signature(foo__mutmut_orig)
39-
foo__mutmut_orig.__name__ = 'foo'
38+
foo.__signature__ = _mutmut_signature(x_foo__mutmut_orig)
39+
x_foo__mutmut_orig.__name__ = 'x_foo'
4040
4141
4242
"""
@@ -55,21 +55,21 @@ def foo(a: List[int]) -> int:
5555

5656
expected = trampoline_impl + """
5757
58-
def foo__mutmut_orig(a: List[int]) -> int:
58+
def x_foo__mutmut_orig(a: List[int]) -> int:
5959
return 1
6060
61-
def foo__mutmut_1(a: List[int]) -> int:
61+
def x_foo__mutmut_1(a: List[int]) -> int:
6262
return 2
6363
64-
foo__mutmut_mutants = {
65-
'foo__mutmut_1': foo__mutmut_1
64+
x_foo__mutmut_mutants = {
65+
'x_foo__mutmut_1': x_foo__mutmut_1
6666
}
6767
6868
def foo(*args, **kwargs):
69-
return _mutmut_trampoline(foo__mutmut_orig, foo__mutmut_mutants, *args, **kwargs)
69+
return _mutmut_trampoline(x_foo__mutmut_orig, x_foo__mutmut_mutants, *args, **kwargs)
7070
71-
foo.__signature__ = _mutmut_signature(foo__mutmut_orig)
72-
foo__mutmut_orig.__name__ = 'foo'
71+
foo.__signature__ = _mutmut_signature(x_foo__mutmut_orig)
72+
x_foo__mutmut_orig.__name__ = 'x_foo'
7373
7474
7575
"""

0 commit comments

Comments
 (0)