def get_root() -> str: try: root = cmd_output('git', 'rev-parse', '--show-toplevel')[1].strip() except CalledProcessError: raise FatalError( 'git failed. Is it installed, and are you in a Git repository ' 'directory?', ) else: if root == '': # pragma: no cover (old git) raise FatalError( 'git toplevel unexpectedly empty! make sure you are not ' 'inside the `.git` directory of your repository.', ) else: return root
def get_root(): try: return cmd_output('git', 'rev-parse', '--show-toplevel')[1].strip() except CalledProcessError: raise FatalError( 'git failed. Is it installed, and are you in a Git repository ' 'directory?')
def get_root() -> str: # Git 2.25 introduced a change to "rev-parse --show-toplevel" that exposed # underlying volumes for Windows drives mapped with SUBST. We use # "rev-parse --show-cdup" to get the appropriate path, but must perform # an extra check to see if we are in the .git directory. try: root = os.path.realpath( cmd_output('git', 'rev-parse', '--show-cdup')[1].strip(), ) git_dir = os.path.realpath(get_git_dir()) except CalledProcessError: raise FatalError( 'git failed. Is it installed, and are you in a Git repository ' 'directory?', ) if os.path.commonpath((root, git_dir)) == git_dir: raise FatalError( 'git toplevel unexpectedly empty! make sure you are not ' 'inside the `.git` directory of your repository.', ) return root
def get_root(): path = os.getcwd() while path != os.path.normpath(os.path.join(path, '../')): if os.path.exists(os.path.join(path, '.git')): return path else: path = os.path.normpath(os.path.join(path, '../')) raise FatalError('Called from outside of the gits. ' 'Please cd to a git repository.')
def test_log_and_exit(mock_out_store_directory): mocked_print = mock.Mock() with pytest.raises(error_handler.PreCommitSystemExit): error_handler._log_and_exit( 'msg', FatalError('hai'), "I'm a stacktrace", print_fn=mocked_print, ) printed = '\n'.join(call[0][0] for call in mocked_print.call_args_list) assert printed == ('msg: FatalError: hai\n' 'Check the log at ~/.pre-commit/pre-commit.log') log_file = os.path.join(mock_out_store_directory, 'pre-commit.log') assert os.path.exists(log_file) contents = io.open(log_file).read() assert contents == ('msg: FatalError: hai\n' "I'm a stacktrace\n")
def test_log_and_exit(cap_out, mock_store_dir): tb = ( 'Traceback (most recent call last):\n' ' File "<stdin>", line 2, in <module>\n' 'pre_commit.errors.FatalError: hai\n' ) with pytest.raises(SystemExit) as excinfo: error_handler._log_and_exit('msg', 1, FatalError('hai'), tb) assert excinfo.value.code == 1 printed = cap_out.get() log_file = os.path.join(mock_store_dir, 'pre-commit.log') assert printed == f'msg: FatalError: hai\nCheck the log at {log_file}\n' assert os.path.exists(log_file) with open(log_file) as f: logged = f.read() pattern = re_assert.Matches( r'^### version information\n' r'\n' r'```\n' r'pre-commit version: \d+\.\d+\.\d+\n' r'git --version: git version .+\n' r'sys.version:\n' r'( .*\n)*' r'sys.executable: .*\n' r'os.name: .*\n' r'sys.platform: .*\n' r'```\n' r'\n' r'### error information\n' r'\n' r'```\n' r'msg: FatalError: hai\n' r'```\n' r'\n' r'```\n' r'Traceback \(most recent call last\):\n' r' File "<stdin>", line 2, in <module>\n' r'pre_commit\.errors\.FatalError: hai\n' r'```\n', ) pattern.assert_matches(logged)
def test_log_and_exit(cap_out, mock_out_store_directory): with pytest.raises(error_handler.PreCommitSystemExit): error_handler._log_and_exit( 'msg', FatalError('hai'), "I'm a stacktrace", ) printed = cap_out.get() assert printed == ( 'msg: FatalError: hai\n' 'Check the log at ~/.pre-commit/pre-commit.log\n' ) log_file = os.path.join(mock_out_store_directory, 'pre-commit.log') assert os.path.exists(log_file) contents = io.open(log_file).read() assert contents == ( 'msg: FatalError: hai\n' "I'm a stacktrace\n" )
def test_error_handler_fatal_error(mocked_log_and_exit): exc = FatalError('just a test') with error_handler.error_handler(): raise exc mocked_log_and_exit.assert_called_once_with( 'An error has occurred', exc, # Tested below mock.ANY, ) pattern = re_assert.Matches( r'Traceback \(most recent call last\):\n' r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n' r' yield\n' r' File ".+tests.error_handler_test.py", line \d+, ' r'in test_error_handler_fatal_error\n' r' raise exc\n' r'(pre_commit\.errors\.)?FatalError: just a test\n', ) pattern.assert_matches(mocked_log_and_exit.call_args[0][2])
def test_error_handler_fatal_error(mocked_log_and_exit): exc = FatalError('just a test') with error_handler.error_handler(): raise exc mocked_log_and_exit.assert_called_once_with( 'An error has occurred', exc, # Tested below mock.ANY, ) assert re.match( 'Traceback \(most recent call last\):\n' ' File ".+/pre_commit/error_handler.py", line \d+, in error_handler\n' ' yield\n' ' File ".+/tests/error_handler_test.py", line \d+, ' 'in test_error_handler_fatal_error\n' ' raise exc\n' '(pre_commit\.errors\.)?FatalError: just a test\n', mocked_log_and_exit.call_args[0][2], )
def get_root(): try: return cmd_output('git', 'rev-parse', '--show-toplevel')[1].strip() except CalledProcessError: raise FatalError( 'Called from outside of the gits. Please cd to a git repository.')