def test_no_config(self): current_dir = os.path.abspath(os.path.dirname(__file__)) child_dir = os.path.join(current_dir, 'section_manager_test_files', 'child_dir') with change_directory(child_dir): sections, targets = load_configuration([], self.log_printer) self.assertIn('value', sections['cli']) sections, targets = load_configuration( ['--no-config'], self.log_printer) self.assertNotIn('value', sections['cli']) sections, targets = load_configuration( ['--no-config', '-S', 'use_spaces=True'], self.log_printer) self.assertIn('use_spaces', sections['cli']) self.assertNotIn('values', sections['cli']) with self.assertRaises(SystemExit) as cm: sections, target = load_configuration( ['--no-config', '--save'], self.log_printer) self.assertEqual(cm.exception.code, 2) with self.assertRaises(SystemExit) as cm: sections, target = load_configuration( ['--no-config', '--find-config'], self.log_printer) self.assertEqual(cm.exception.code, 2)
def test_find_user_config(self): current_dir = os.path.abspath(os.path.dirname(__file__)) c_file = os.path.join(current_dir, 'section_manager_test_files', 'project', 'test.c') retval = find_user_config(c_file, 1) self.assertEqual('', retval) retval = find_user_config(c_file, 2) self.assertEqual( os.path.join(current_dir, 'section_manager_test_files', '.coafile'), retval) child_dir = os.path.join(current_dir, 'section_manager_test_files', 'child_dir') retval = find_user_config(child_dir, 2) self.assertEqual( os.path.join(current_dir, 'section_manager_test_files', 'child_dir', '.coafile'), retval) with change_directory(child_dir): sections, _, _, _ = gather_configuration( lambda *args: True, self.log_printer, arg_list=['--find-config']) self.assertEqual(bool(sections['cli']['find_config']), True)
def test_find_user_config(self): current_dir = os.path.abspath(os.path.dirname(__file__)) c_file = os.path.join(current_dir, 'section_manager_test_files', 'project', 'test.c') retval = find_user_config(c_file, 1) self.assertEqual('', retval) retval = find_user_config(c_file, 2) self.assertEqual(os.path.join(current_dir, 'section_manager_test_files', '.coafile'), retval) child_dir = os.path.join(current_dir, 'section_manager_test_files', 'child_dir') retval = find_user_config(child_dir, 2) self.assertEqual(os.path.join(current_dir, 'section_manager_test_files', 'child_dir', '.coafile'), retval) with change_directory(child_dir): sections, _, _, _ = gather_configuration( lambda *args: True, self.log_printer, arg_list=['--find-config']) self.assertEqual(bool(sections['cli']['find_config']), True)
def test_change_directory(self): old_dir = os.getcwd() with TemporaryDirectory("temp") as tempdir: tempdir = os.path.realpath(tempdir) with change_directory(tempdir): self.assertEqual(os.getcwd(), tempdir) self.assertEqual(os.getcwd(), old_dir)
def test_no_config(self): current_dir = os.path.abspath(os.path.dirname(__file__)) child_dir = os.path.join(current_dir, 'section_manager_test_files', 'child_dir') with change_directory(child_dir): sections, targets = load_configuration([], self.log_printer) self.assertIn('value', sections['cli']) sections, targets = load_configuration(['--no-config'], self.log_printer) self.assertNotIn('value', sections['cli']) sections, targets = load_configuration( ['--no-config', '-S', 'use_spaces=True'], self.log_printer) self.assertIn('use_spaces', sections['cli']) self.assertNotIn('values', sections['cli']) with self.assertRaises(SystemExit) as cm: sections, target = load_configuration( ['--no-config', '--save'], self.log_printer) self.assertEqual(cm.exception.code, 2) with self.assertRaises(SystemExit) as cm: sections, target = load_configuration( ['--no-config', '--find-config'], self.log_printer) self.assertEqual(cm.exception.code, 2)
def test_change_directory(self): old_dir = os.getcwd() with TemporaryDirectory('temp') as tempdir: tempdir = os.path.realpath(tempdir) with change_directory(tempdir): self.assertEqual(os.getcwd(), tempdir) self.assertEqual(os.getcwd(), old_dir)
def run(self, allow_empty_commit_message: bool = False, **kwargs): """ Check the current git commit message at HEAD. This bear ensures automatically that the shortlog and body do not exceed a given line-length and that a newline lies between them. :param allow_empty_commit_message: Whether empty commit messages are allowed or not. """ with change_directory(self.get_config_dir() or os.getcwd()): stdout, stderr = run_shell_command('git log -1 --pretty=%B') if stderr: self.err('git:', repr(stderr)) return stdout = stdout.rstrip('\n').splitlines() if len(stdout) == 0: if not allow_empty_commit_message: yield Result(self, 'HEAD commit has no message.') return yield from self.check_shortlog( stdout[0], **self.get_shortlog_checks_metadata().filter_parameters(kwargs)) yield from self.check_body( stdout[1:], **self.get_body_checks_metadata().filter_parameters(kwargs))
def test_section_inheritance(self): current_dir = os.path.abspath(os.path.dirname(__file__)) test_dir = os.path.join(current_dir, 'section_manager_test_files') logger = logging.getLogger() with change_directory(test_dir), \ self.assertLogs(logger, 'WARNING') as cm: sections, _, _, _ = gather_configuration( lambda *args: True, self.log_printer, arg_list=['-c', 'inherit_coafile']) self.assertEqual(sections['all.python'].defaults, sections['all']) self.assertEqual(sections['all.c']['key'], sections['cli']['key']) self.assertEqual(sections['java.test'].defaults, sections['cli']) self.assertEqual(int(sections['all.python']['max_line_length']), 80) self.assertEqual(sections['all.python.codestyle'].defaults, sections['all.python']) self.assertEqual(sections['all.java.codestyle'].defaults, sections['all']) self.assertEqual(str(sections['all']['ignore']), './vendor') sections['cli']['ignore'] = './user' self.assertEqual(str(sections['all']['ignore']), './user, ./vendor') sections['cli']['ignore'] = './client' self.assertEqual(str(sections['all']['ignore']), './client, ./vendor') self.assertRegex(cm.output[0], '\'cli\' is an internally reserved section name.')
def get_head_commit(self): with change_directory(self.get_config_dir() or os.getcwd()): command = self.check_github_pull_request_temporary_merge_commit() if command: return run_shell_command(command) return run_shell_command('git log -1 --pretty=%B')
def get_head_commit_sha(self): with change_directory(self.get_config_dir() or os.getcwd()): (stdout, stderr) = run_shell_command('git rev-parse HEAD') if stderr: vcs_name = list(self.LANGUAGES)[0].lower()+':' self.err(vcs_name, repr(stderr)) raise RuntimeError('The directory is not a git repository.') head_commit_sha = stdout.strip('\n') return head_commit_sha
def test_find_user_config(self): current_dir = os.path.abspath(os.path.dirname(__file__)) c_file = os.path.join(current_dir, "section_manager_test_files", "project", "test.c") retval = find_user_config(c_file, 1) self.assertEqual("", retval) retval = find_user_config(c_file, 2) self.assertEqual(os.path.join(current_dir, "section_manager_test_files", ".coafile"), retval) child_dir = os.path.join(current_dir, "section_manager_test_files", "child_dir") retval = find_user_config(child_dir, 2) self.assertEqual(os.path.join(current_dir, "section_manager_test_files", "child_dir", ".coafile"), retval) with change_directory(child_dir): sections, _, _, _ = gather_configuration(lambda *args: True, self.log_printer, arg_list=["--find-config"]) self.assertEqual(bool(sections["cli"]["find_config"]), True)
def test_no_config(self): current_dir = os.path.abspath(os.path.dirname(__file__)) child_dir = os.path.join(current_dir, "section_manager_test_files", "child_dir") with change_directory(child_dir): sections, targets = load_configuration([], self.log_printer) self.assertIn("value", sections["cli"]) sections, targets = load_configuration(["--no-config"], self.log_printer) self.assertNotIn("value", sections["cli"]) sections, targets = load_configuration(["--no-config", "-S", "use_spaces=True"], self.log_printer) self.assertIn("use_spaces", sections["cli"]) self.assertNotIn("values", sections["cli"]) with self.assertRaises(SystemExit) as cm: sections, target = load_configuration(["--no-config", "--save"], self.log_printer) self.assertEqual(cm.exception.code, 2) with self.assertRaises(SystemExit) as cm: sections, target = load_configuration(["--no-config", "--find-config"], self.log_printer) self.assertEqual(cm.exception.code, 2)
def test_section_inheritance(self): current_dir = os.path.abspath(os.path.dirname(__file__)) test_dir = os.path.join(current_dir, "section_manager_test_files") logger = logging.getLogger() with change_directory(test_dir), self.assertLogs(logger, "WARNING") as cm: sections, _, _, _ = gather_configuration( lambda *args: True, self.log_printer, arg_list=["-c", "inherit_coafile"] ) self.assertEqual(sections["all.python"].defaults, sections["all"]) self.assertEqual(sections["all.c"]["key"], sections["cli"]["key"]) self.assertEqual(sections["java.test"].defaults, sections["cli"]) self.assertEqual(int(sections["all.python"]["max_line_length"]), 80) self.assertEqual(sections["all.python.codestyle"].defaults, sections["all.python"]) self.assertEqual(sections["all.java.codestyle"].defaults, sections["all"]) self.assertEqual(str(sections["all"]["ignore"]), "./vendor") sections["cli"]["ignore"] = "./user" self.assertEqual(str(sections["all"]["ignore"]), "./user, ./vendor") sections["cli"]["ignore"] = "./client" self.assertEqual(str(sections["all"]["ignore"]), "./client, ./vendor") self.assertRegex(cm.output[0], "'cli' is an internally reserved section name.")
def get_head_commit(self): with change_directory(self.get_config_dir() or os.getcwd()): return run_shell_command('hg log -l 1 --template "{desc}"')
def get_head_commit(self): with change_directory(self.get_config_dir() or os.getcwd()): return run_shell_command('git log -1 --pretty=%B')