def test_fails_on_unknown_format(self): try: command.pygount_command( ['--format', 'no_such_encoding', tempfile.gettempdir()]) self.fail('unknown --format must exit') except SystemExit as system_exit: self.assertEqual(system_exit.code, 2)
def test_can_accept_duplicates(self): source_code = "# Duplicate source\nprint('duplicate code')\n" original_path = os.path.join(self.tests_temp_folder, 'original.py') with open(original_path, 'w') as original_file: original_file.write(source_code) duplicate_path = os.path.join(self.tests_temp_folder, 'duplicate.py') with open(duplicate_path, 'w') as duplicate_file: duplicate_file.write(source_code) cloc_xml_path = os.path.join(self.tests_temp_folder, 'cloc.xml') exit_code = command.pygount_command([ '--duplicates', '--verbose', '--format', 'cloc-xml', '--out', cloc_xml_path, original_path, duplicate_path ]) self.assertEqual(exit_code, 0) self.assertTrue(os.path.exists(cloc_xml_path)) cloc_xml_root = ElementTree.parse(cloc_xml_path) file_elements = cloc_xml_root.findall("files/file[@language='__duplicate__']") self.assertIsNotNone(file_elements) self.assertEqual(len(file_elements), 0)
def test_can_detect_generated_code_with_own_pattern(self): generiert_py_path = os.path.join(self.tests_temp_folder, "generiert.py") with open(generiert_py_path, "w", encoding="utf-8") as generiert_py_file: generiert_py_file.write( "# Generiert mit pygount.test_command.PygountCommandTest." "test_can_detect_generated_code_with_own_pattern()\n") generiert_py_file.write("print('hello World')\n") cloc_xml_path = os.path.join(self.tests_temp_folder, "cloc.xml") exit_code = command.pygount_command([ "--verbose", "--format=cloc-xml", "--generated=[regex](?i).*generiert", "--out", cloc_xml_path, generiert_py_path, ]) assert exit_code == 0 assert os.path.exists(cloc_xml_path) cloc_xml_root = ElementTree.parse(cloc_xml_path) file_elements = cloc_xml_root.findall( "files/file[@language='__generated__']") assert file_elements is not None assert len(file_elements) >= 1
def test_can_analyze_pygount_source_code_as_cloc_xml(self): cloc_xml_path = os.path.join(self.tests_temp_folder, "cloc.xml") exit_code = command.pygount_command([ "--verbose", "--format", "cloc-xml", "--out", cloc_xml_path, _PYGOUNT_SOURCE_FOLDER ]) assert exit_code == 0 assert os.path.exists(cloc_xml_path) cloc_xml_root = ElementTree.parse(cloc_xml_path) file_elements = cloc_xml_root.findall("files/file") assert file_elements is not None assert len(file_elements) >= 1
def test_can_analyze_pygount_source_code_as_cloc_xml(self): pygount_folder = os.path.dirname(os.path.dirname(__file__)) cloc_xml_path = os.path.join(self.tests_temp_folder, 'cloc.xml') exit_code = command.pygount_command([ '--verbose', '--format', 'cloc-xml', '--out', cloc_xml_path, pygount_folder ]) self.assertEqual(exit_code, 0) self.assertTrue(os.path.exists(cloc_xml_path)) cloc_xml_root = ElementTree.parse(cloc_xml_path) file_elements = cloc_xml_root.findall('files/file') self.assertIsNotNone(file_elements) self.assertNotEqual(len(file_elements), 0)
def test_can_analyze_pygount_source_code_as_cloc_xml(self): pygount_folder = os.path.dirname(os.path.dirname(__file__)) cloc_xml_path = os.path.join(self.tests_temp_folder, 'cloc.xml') exit_code = command.pygount_command([ '--verbose', '--format', 'cloc-xml', '--out', cloc_xml_path, pygount_folder]) self.assertEqual(exit_code, 0) self.assertTrue(os.path.exists(cloc_xml_path)) cloc_xml_root = ElementTree.parse(cloc_xml_path) file_elements = cloc_xml_root.findall('files/file') self.assertIsNotNone(file_elements) self.assertNotEqual(len(file_elements), 0)
def test_can_accept_duplicates(self): source_code = "# Duplicate source\nprint('duplicate code')\n" original_path = os.path.join(self.tests_temp_folder, 'original.py') with open(original_path, 'w') as original_file: original_file.write(source_code) duplicate_path = os.path.join(self.tests_temp_folder, 'duplicate.py') with open(duplicate_path, 'w') as duplicate_file: duplicate_file.write(source_code) cloc_xml_path = os.path.join(self.tests_temp_folder, 'cloc.xml') exit_code = command.pygount_command([ '--duplicates', '--verbose', '--format', 'cloc-xml', '--out', cloc_xml_path, original_path, duplicate_path ]) self.assertEqual(exit_code, 0) self.assertTrue(os.path.exists(cloc_xml_path)) cloc_xml_root = ElementTree.parse(cloc_xml_path) file_elements = cloc_xml_root.findall( "files/file[@language='__duplicate__']") self.assertIsNotNone(file_elements) self.assertEqual(len(file_elements), 0)
def test_can_accept_duplicates(self): source_code = "# Duplicate source\nprint('duplicate code')\n" original_path = os.path.join(self.tests_temp_folder, "original.py") with open(original_path, "w") as original_file: original_file.write(source_code) duplicate_path = os.path.join(self.tests_temp_folder, "duplicate.py") with open(duplicate_path, "w") as duplicate_file: duplicate_file.write(source_code) cloc_xml_path = os.path.join(self.tests_temp_folder, "cloc.xml") exit_code = command.pygount_command([ "--duplicates", "--verbose", "--format", "cloc-xml", "--out", cloc_xml_path, original_path, duplicate_path ]) assert exit_code == 0 assert os.path.exists(cloc_xml_path) cloc_xml_root = ElementTree.parse(cloc_xml_path) file_elements = cloc_xml_root.findall( "files/file[@language='__duplicate__']") assert file_elements is not None assert len(file_elements) == 0
def test_can_detect_generated_code_with_own_pattern(self): generiert_py_path = os.path.join(self.tests_temp_folder, 'generiert.py') with open(generiert_py_path, 'w', encoding='utf-8') as generiert_py_file: generiert_py_file.write( '# Generiert mit pygount.test_command.PygountCommandTest.' 'test_can_detect_generated_code_with_own_pattern.\n') generiert_py_file.write("print('hello World'\n") cloc_xml_path = os.path.join(self.tests_temp_folder, 'cloc.xml') exit_code = command.pygount_command([ '--verbose', '--format=cloc-xml', '--generated=[regex](?i).*generiert', '--out', cloc_xml_path, generiert_py_path]) self.assertEqual(exit_code, 0) self.assertTrue(os.path.exists(cloc_xml_path)) cloc_xml_root = ElementTree.parse(cloc_xml_path) file_elements = cloc_xml_root.findall("files/file[@language='__generated__']") self.assertIsNotNone(file_elements) self.assertNotEqual(len(file_elements), 0)
def test_can_detect_generated_code_with_own_pattern(self): generiert_py_path = os.path.join(self.tests_temp_folder, 'generiert.py') with open(generiert_py_path, 'w', encoding='utf-8') as generiert_py_file: generiert_py_file.write( '# Generiert mit pygount.test_command.PygountCommandTest.' 'test_can_detect_generated_code_with_own_pattern.\n') generiert_py_file.write("print('hello World'\n") cloc_xml_path = os.path.join(self.tests_temp_folder, 'cloc.xml') exit_code = command.pygount_command([ '--verbose', '--format=cloc-xml', '--generated=[regex](?i).*generiert', '--out', cloc_xml_path, generiert_py_path ]) self.assertEqual(exit_code, 0) self.assertTrue(os.path.exists(cloc_xml_path)) cloc_xml_root = ElementTree.parse(cloc_xml_path) file_elements = cloc_xml_root.findall( "files/file[@language='__generated__']") self.assertIsNotNone(file_elements) self.assertNotEqual(len(file_elements), 0)
def test_can_analyze_pygount_setup_py(self): pygount_setup_py_path = os.path.join(_PYGOUNT_PROJECT_FOLDER, "setup.py") exit_code = command.pygount_command( ["--verbose", pygount_setup_py_path]) assert exit_code == 0
def test_can_analyze_pygount_source_code(self): pygount_folder = os.path.dirname(os.path.dirname(__file__)) exit_code = command.pygount_command(['--verbose', pygount_folder]) self.assertEqual(exit_code, 0)
def test_can_analyze_pygount_setup_py(self): pygount_setup_py_path = os.path.join( os.path.dirname(os.path.dirname(__file__)), 'setup.py') exit_code = command.pygount_command( ['--verbose', pygount_setup_py_path]) self.assertEqual(exit_code, 0)
def test_fails_on_broken_regex_pattern(self): exit_code = command.pygount_command( ['--generated', '[regex](', tempfile.gettempdir()]) self.assertEqual(exit_code, 1)
def test_fails_on_broken_regex_pattern(self): exit_code = command.pygount_command(['--generated', '[regex](', tempfile.gettempdir()]) self.assertEqual(exit_code, 1)
def test_fails_on_unknown_format(self): try: command.pygount_command(['--format', 'no_such_encoding', tempfile.gettempdir()]) self.fail('unknown --format must exit') except SystemExit as system_exit: self.assertEqual(system_exit.code, 2)
def test_can_show_version(self): try: command.pygount_command(['--version']) self.fail('--version must exit') except SystemExit as system_exit: self.assertEqual(system_exit.code, 0)
def test_can_analyze_pygount_setup_py(self): pygount_setup_py_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'setup.py') exit_code = command.pygount_command(['--verbose', pygount_setup_py_path]) self.assertEqual(exit_code, 0)
def test_fails_on_broken_regex_pattern(self): exit_code = command.pygount_command( ["--generated", "[regex](", tempfile.gettempdir()]) assert exit_code == 1
def test_fails_on_unknown_format(self): with pytest.raises(SystemExit) as error_info: command.pygount_command( ["--format", "no_such_encoding", tempfile.gettempdir()]) assert error_info.value.code == 2
def test_can_show_version(self): with pytest.raises(SystemExit) as error_info: command.pygount_command(["--version"]) assert error_info.value.code == 0
def test_can_show_summary(self): pygount_folder = os.path.dirname(os.path.dirname(__file__)) exit_code = command.pygount_command(['--summary', pygount_folder]) self.assertEqual(exit_code, 0)
def test_can_write_all_output_formats(self): for output_format in VALID_OUTPUT_FORMATS: exit_code = command.pygount_command( ["--format", output_format, PYGOUNT_SOURCE_FOLDER]) self.assertEqual(exit_code, 0)
def test_can_analyze_pygount_source_code(self): exit_code = command.pygount_command( ["--verbose", _PYGOUNT_SOURCE_FOLDER]) assert exit_code == 0