def test_main_full_usage(self): # Print full usage and exit with code 0. with self.assertRaises(SystemExit) as ex: sys.argv = [sys.argv[0], "--help"] main() sys.argv = [sys.argv[0]] self.assertEqual(ex.exception.code, 0)
def test_main_no_paths(self): # The path doesn't exist and isn't a .py file which means no paths are detected. with self.assertRaises(SystemExit) as ex: sys.argv = [sys.argv[0], "nonexistentfilethatisntpy"] main() sys.argv = [sys.argv[0]] self.assertEqual(ex.exception.code, 1)
def test_main_target_not_met(self): # Ensure exit code 1 when target isn't met. fp = ScopedTemporaryFile() fp.close() with self.assertRaises(SystemExit) as ex: sys.argv = [sys.argv[0], "-t=3.0", fp.path()] main() sys.argv = [sys.argv[0]] self.assertEqual(ex.exception.code, 1)
def test_main_no_rules_hit(self): # Python file that doesn't hit any rules should exit successfully. fp = ScopedTemporaryFile() fp.close() with self.assertRaises(SystemExit) as ex: sys.argv = [sys.argv[0], fp.path()] main() sys.argv = [sys.argv[0]] self.assertEqual(ex.exception.code, 0)
def test_main_target_not_met(self): # Ensure exit code 1 when target isn't met. fp = NamedTemporaryFile(suffix=".py", delete=False) fp.close() with self.assertRaises(SystemExit) as ex: sys.argv = [sys.argv[0], "-t=3.0", fp.name] main() os.remove(fp.name) sys.argv = [sys.argv[0]] self.assertEqual(ex.exception.code, 1)
def test_main_no_rules_hit(self): # Python file that doesn't hit any rules should exit successfully. fp = NamedTemporaryFile(suffix=".py", delete=False) fp.close() with self.assertRaises(SystemExit) as ex: sys.argv = [sys.argv[0], fp.name] main() os.remove(fp.name) sys.argv = [sys.argv[0]] self.assertEqual(ex.exception.code, 0)
def test_main_no_rules_hit_target_not_met_violations_mode(self): # Python file that doesn't hit any rules, even with targets, should exit successfully for # violations mode. fp = ScopedTemporaryFile() fp.close() with self.assertRaises(SystemExit) as ex: sys.argv = [sys.argv[0], "-t=3.0", "--violations", fp.path()] main() sys.argv = [sys.argv[0]] self.assertEqual(ex.exception.code, 0)
def test_main_print_version(self): # Print version and exit with code 0. with self.assertRaises(SystemExit) as ex: sys.argv = [sys.argv[0], "--version"] main() self.assertEqual(ex.exception.code, 0) with self.assertRaises(SystemExit) as ex: sys.argv[1] = "-V" main() self.assertEqual(ex.exception.code, 0) sys.argv = [sys.argv[0]]
def test_main_print_versions_range(self): fp = ScopedTemporaryFile() fp.writeln(b"import weakref") fp.close() # Print versions range and exit with code 0. with self.assertRaises(SystemExit) as ex: sys.argv = [sys.argv[0], "--versions", fp.path()] main() sys.argv = [sys.argv[0]] self.assertEqual(ex.exception.code, 0)
def test_main_parsable_dont_ignore_paths_with_colon_in_drive_part(self): # Tests that detection of paths works with ":" in them (due to parsable format) especially on # Windows where the first part of an absolute path, the drive part, contains a ":". # Detect newly created file path in parent folder even though using parsable format. tmp_fld = mkdtemp() touch(tmp_fld, "test.py") with self.assertRaises(SystemExit) as ex: sys.argv = [sys.argv[0], "--format", "parsable", tmp_fld] main() sys.argv = [sys.argv[0]] rmtree(tmp_fld) # Check for no error. It would incorrectly yield "No files specified to analyze!" before the # fix. self.assertEqual(ex.exception.code, 0)
def test_main_no_args(self): # Print usage and exit with code 1. with self.assertRaises(SystemExit) as ex: main() self.assertEqual(ex.exception.code, 1)