def test_get_current_branch_root_dir_no_git(self): with patch('txclib.utils.os.getcwd') as cwd_mock, \ patch('txclib.utils.os.path.isdir') as isdir_mock: cwd_mock.return_value = '/usr/local/test_cli' isdir_mock.return_value = False b = utils.get_current_branch('/usr/local/test_cli') self.assertEqual(b, None)
def test_get_current_branch_contains_slash(self): data = "ref: refs/heads/test_branch/abc\n" with patch('txclib.utils.os.getcwd') as cwd_mock, \ patch('txclib.utils.os.path.isdir') as isdir_mock, \ patch("txclib.utils.open", mock_open(read_data=data)): cwd_mock.return_value = '/usr/local/test_cli' isdir_mock.side_effects = [False, True] b = utils.get_current_branch('/usr/local/test_cli') self.assertEqual(b, 'test_branch/abc')
def test_get_current_branch_root_dir_with_git(self): data = "ref: refs/heads/test_branch\n" with patch('txclib.utils.os.getcwd') as cwd_mock, \ patch('txclib.utils.os.path.isdir') as isdir_mock, \ patch("txclib.utils.open", mock_open(read_data=data)): cwd_mock.return_value = '/usr/local/test_cli' isdir_mock.return_value = True b = utils.get_current_branch('/usr/local/test_cli') self.assertEqual(b, 'test_branch')
def get_branch_from_options(options, project_root): """ Returns the branch name that needs to be used in command based on parser options. options: optparse parser options as returned from `parse()` project_root: project root directory """ if not options.branch: return if options.branch != '-1': return options.branch branch = utils.get_current_branch(project_root) if not branch: logger.error("You specified the --branch option but current " "directory does not seem to belong in any git repo.") sys.exit(1) return branch