def test_help() -> None: output = StringIO() with redirect_stdout(output): try: similar.Run(["--help"]) except SystemExit as ex: assert ex.code == 0 else: pytest.fail("not system exit")
def test_ignore_signatures_empty_functions_pass() -> None: output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run( ["--ignore-signatures", EMPTY_FUNCTION_1, EMPTY_FUNCTION_2]) assert ex.value.code == 0 assert (output.getvalue().strip() == """ TOTAL lines=14 duplicates=0 percent=0.00 """.strip())
def test_no_args(): output = StringIO() with redirect_stdout(output): try: similar.Run([]) except SystemExit as ex: assert ex.code == 1 else: pytest.fail("not system exit")
def test_no_args(): sys.stdout = six.StringIO() try: similar.Run([]) except SystemExit as ex: assert ex.code == 1 else: pytest.fail('not system exit') finally: sys.stdout = sys.__stdout__
def test_ignore_imports(): sys.stdout = six.StringIO() with pytest.raises(SystemExit) as ex: similar.Run(['--ignore-imports', SIMILAR1, SIMILAR2]) assert ex.value.code == 0 output = sys.stdout.getvalue() sys.stdout = sys.__stdout__ assert output.strip() == """ TOTAL lines=44 duplicates=0 percent=0.00 """.strip()
def test_no_args(self): sys.stdout = six.StringIO() try: similar.Run([]) except SystemExit as ex: self.assertEqual(ex.code, 1) else: self.fail('not system exit') finally: sys.stdout = sys.__stdout__
def test_help(): sys.stdout = StringIO() try: similar.Run(['--help']) except SystemExit as ex: assert ex.code == 0 else: pytest.fail('not system exit') finally: sys.stdout = sys.__stdout__
def test_ignore_signatures_class_methods_pass() -> None: output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run(["--ignore-signatures", SIMILAR_CLS_B, SIMILAR_CLS_A]) assert ex.value.code == 0 assert ( output.getvalue().strip() == """ TOTAL lines=54 duplicates=0 percent=0.00 """.strip() )
def test_ignore_imports() -> None: output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run(["--ignore-imports", SIMILAR1, SIMILAR2]) assert ex.value.code == 0 assert ( output.getvalue().strip() == """ TOTAL lines=62 duplicates=0 percent=0.00 """.strip() )
def test_ignore_multiline_imports(): output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run(["--ignore-imports", MULTILINE, MULTILINE]) assert ex.value.code == 0 assert ( output.getvalue().strip() == """ TOTAL lines=16 duplicates=0 percent=0.00 """.strip() )
def test_ignore_imports(self): sys.stdout = six.StringIO() try: similar.Run(['--ignore-imports', SIMILAR1, SIMILAR2]) except SystemExit as ex: self.assertEqual(ex.code, 0) output = sys.stdout.getvalue() else: self.fail('not system exit') finally: sys.stdout = sys.__stdout__ self.assertMultiLineEqual( output.strip(), """ TOTAL lines=44 duplicates=0 percent=0.00 """.strip())
def test_ignore_nothing() -> None: output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run([SIMILAR1, SIMILAR2]) assert ex.value.code == 0 assert (output.getvalue().strip() == (f""" 5 similar lines in 2 files =={SIMILAR1}:[0:5] =={SIMILAR2}:[0:5] import one from two import two three four five TOTAL lines=62 duplicates=5 percent=8.06 """).strip())
def test_ignore_nothing(): output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run([SIMILAR1, SIMILAR2]) assert ex.value.code == 0 assert (output.getvalue().strip() == (""" 5 similar lines in 2 files ==%s:0 ==%s:0 import one from two import two three four five TOTAL lines=60 duplicates=5 percent=8.33 """ % (SIMILAR1, SIMILAR2)).strip())
def test_ignore_signatures_empty_functions_fail() -> None: output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run([EMPTY_FUNCTION_1, EMPTY_FUNCTION_2]) assert ex.value.code == 0 assert (output.getvalue().strip() == (f''' 6 similar lines in 2 files =={EMPTY_FUNCTION_1}:[1:7] =={EMPTY_FUNCTION_2}:[1:7] arg1: int = 1, arg2: str = "2", arg3: int = 3, arg4: bool = True, ) -> None: """Valid function definition with docstring only.""" TOTAL lines=14 duplicates=6 percent=42.86 ''').strip())
def test_ignore_nothing(): sys.stdout = six.StringIO() with pytest.raises(SystemExit) as ex: similar.Run([SIMILAR1, SIMILAR2]) assert ex.value.code == 0 output = sys.stdout.getvalue() sys.stdout = sys.__stdout__ assert output.strip() == (""" 5 similar lines in 2 files ==%s:0 ==%s:0 import one from two import two three four five TOTAL lines=44 duplicates=5 percent=11.36 """ % (SIMILAR1, SIMILAR2)).strip()
def test_ignore_signatures_fail(): output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run([SIMILAR5, SIMILAR6]) assert ex.value.code == 0 assert (output.getvalue().strip() == (""" 7 similar lines in 2 files ==%s:1 ==%s:8 arg1: int = 3, arg2: Class1 = val1, arg3: Class2 = func3(val2), arg4: int = 4, arg5: int = 5 ) -> Ret1: pass TOTAL lines=23 duplicates=7 percent=30.43 """ % (SIMILAR5, SIMILAR6)).strip())
def test_ignore_signatures_class_methods_fail() -> None: output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run([SIMILAR_CLS_B, SIMILAR_CLS_A]) assert ex.value.code == 0 assert ( output.getvalue().strip() == ( f''' 15 similar lines in 2 files =={SIMILAR_CLS_A}:[1:18] =={SIMILAR_CLS_B}:[1:18] def parent_method( self, *, a="", b=None, c=True, ): """Overridden method example.""" def _internal_func( arg1: int = 1, arg2: str = "2", arg3: int = 3, arg4: bool = True, ): pass 7 similar lines in 2 files =={SIMILAR_CLS_A}:[20:27] =={SIMILAR_CLS_B}:[20:27] self, *, a=None, b=False, c="", ): pass TOTAL lines=54 duplicates=22 percent=40.74 ''' ).strip() )
def test_multiline_imports(): output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run([MULTILINE, MULTILINE]) assert ex.value.code == 0 assert (output.getvalue().strip() == (""" 8 similar lines in 2 files ==%s:0 ==%s:0 from foo import ( bar, baz, quux, quuux, quuuux, quuuuux, ) TOTAL lines=16 duplicates=8 percent=50.00 """ % (MULTILINE, MULTILINE)).strip())
def test_ignore_comments(): output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run(["--ignore-comments", SIMILAR1, SIMILAR2]) assert ex.value.code == 0 assert (output.getvalue().strip() == (""" 10 similar lines in 2 files ==%s:0 ==%s:0 import one from two import two three four five six seven eight nine ''' ten TOTAL lines=60 duplicates=10 percent=16.67 """ % (SIMILAR1, SIMILAR2)).strip())
def test_ignore_comments() -> None: output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run(["--ignore-comments", SIMILAR1, SIMILAR2]) assert ex.value.code == 0 assert (output.getvalue().strip() == (f""" 10 similar lines in 2 files =={SIMILAR1}:[0:11] =={SIMILAR2}:[0:11] import one from two import two three four five six # A full line comment seven eight nine ''' ten TOTAL lines=62 duplicates=10 percent=16.13 """).strip())
def test_ignore_nothing(self): sys.stdout = six.StringIO() try: similar.Run([SIMILAR1, SIMILAR2]) except SystemExit as ex: self.assertEqual(ex.code, 0) output = sys.stdout.getvalue() else: self.fail('not system exit') finally: sys.stdout = sys.__stdout__ self.assertMultiLineEqual(output.strip(), (""" 5 similar lines in 2 files ==%s:0 ==%s:0 import one from two import two three four five TOTAL lines=44 duplicates=5 percent=11.36 """ % (SIMILAR1, SIMILAR2)).strip())
def test_ignore_comments(): sys.stdout = six.StringIO() with pytest.raises(SystemExit) as ex: similar.Run(['--ignore-comments', SIMILAR1, SIMILAR2]) assert ex.value.code == 0 output = sys.stdout.getvalue() sys.stdout = sys.__stdout__ assert output.strip() == (""" 10 similar lines in 2 files ==%s:0 ==%s:0 import one from two import two three four five six seven eight nine ''' ten TOTAL lines=44 duplicates=10 percent=22.73 """ % (SIMILAR1, SIMILAR2)).strip()
def test_ignore_signatures_fail() -> None: output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run([SIMILAR5, SIMILAR6]) assert ex.value.code == 0 assert ( output.getvalue().strip() == ( f''' 9 similar lines in 2 files =={SIMILAR5}:[7:17] =={SIMILAR6}:[8:18] arg1: int = 3, arg2: Class1 = val1, arg3: Class2 = func3(val2), arg4: int = 4, arg5: int = 5 ) -> Ret1: pass def example(): """Valid function definition with docstring only.""" 6 similar lines in 2 files =={SIMILAR5}:[0:6] =={SIMILAR6}:[1:7] @deco1(dval1) @deco2(dval2) @deco3( dval3, dval4 ) TOTAL lines=35 duplicates=15 percent=42.86 ''' ).strip() )
def test_no_hide_code_with_imports(): output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run(["--ignore-imports"] + 2 * [HIDE_CODE_WITH_IMPORTS]) assert ex.value.code == 0 assert "TOTAL lines=32 duplicates=16 percent=50.00" in output.getvalue()
def test_no_args(self): sys.stdout = StringIO() try: similar.Run([]) except SystemExit, ex: self.assertEqual(ex.code, 1)
def test_set_duplicate_lines_to_zero() -> None: output = StringIO() with redirect_stdout(output), pytest.raises(SystemExit) as ex: similar.Run(["--duplicates=0", SIMILAR1, SIMILAR2]) assert ex.value.code == 0 assert output.getvalue() == ""
def test_help(self): sys.stdout = StringIO() try: similar.Run(['--help']) except SystemExit, ex: self.assertEqual(ex.code, 0)