Skip to content

Commit

Permalink
Fix upstream breaking with empty repositories
Browse files Browse the repository at this point in the history
Fixes #72
  • Loading branch information
Brickster committed Aug 22, 2016
1 parent 21fcf4f commit 318f612
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. This projec
- `git-tuck` not preserving index changes [#62][]
- `git-tuck` attempting to run in a non-git repository [#66][]
- `git-snapshot` not preserving restoring index changes [#64][]
- `git-upstream` not working for empty/new repositories [#72][]
- Documentation issues [#59][]

[#58]: https://github.com/Brickstertwo/git-commands/issues/58
Expand All @@ -24,6 +25,7 @@ All notable changes to this project will be documented in this file. This projec
[#64]: https://github.com/Brickstertwo/git-commands/issues/64
[#66]: https://github.com/Brickstertwo/git-commands/issues/66
[#67]: https://github.com/Brickstertwo/git-commands/issues/67
[#72]: https://github.com/Brickstertwo/git-commands/issues/72

## [v0.5.0][] - 2016-04-14
### Added
Expand Down
2 changes: 2 additions & 0 deletions bin/commands/upstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def upstream(branch=None, include_remote=False):

if not directories.is_git_repository():
messages.error('{0!r} not a git repository'.format(os.getcwd()))
elif git.is_empty_repository():
return None

if not branch:
branch = git.current_branch()
Expand Down
16 changes: 16 additions & 0 deletions tests/functional/test_upstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,22 @@ def test_upstream_nonGitRepository(self):
self.assertEqual(expected, stderr.strip())
self.assertFalse(stdout)

def test_upstream_emptyRepository(self):

# setup
# create a new repo in a sub-directory (lazy)
os.mkdir(self.dirpath + '/dir')
os.chdir(self.dirpath + '/dir')
subprocess.check_output('git init'.split())

# when
p = subprocess.Popen('git upstream'.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()

# then
self.assertFalse(stdout)
self.assertFalse(stderr)

def test_upstream_includeRemoteAndNoIncludeRemote(self):

expected = """usage: git upstream [-h] [-v] [-r | -R] [-b BRANCH]
Expand Down
37 changes: 25 additions & 12 deletions tests/unit/test_upstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
from bin.commands import upstream


@mock.patch('bin.commands.utils.directories.is_git_repository', return_value=True)
@mock.patch('bin.commands.utils.git.is_empty_repository', return_value=False)
class TestUpstream(unittest.TestCase):

@mock.patch('bin.commands.utils.directories.is_git_repository', return_value=True)
@mock.patch('bin.commands.utils.git.current_branch', return_value='the-branch')
@mock.patch('subprocess.Popen')
def test_upstream(self, mock_popen, mock_currentbranch, mock_isgitrepository):
def test_upstream(self, mock_popen, mock_currentbranch, mock_isemptyrepository, mock_isgitrepository):

# setup
expected_upstream = "the-upstream"
Expand All @@ -28,14 +29,14 @@ def test_upstream(self, mock_popen, mock_currentbranch, mock_isgitrepository):
self.assertEqual(actual_upstream, expected_upstream)

mock_isgitrepository.assert_called_once_with()
mock_isemptyrepository.assert_called_once_with()
mock_currentbranch.assert_called_once_with()
mock_process.communicate.assert_called_once_with()
mock_popen.assert_called_once_with('git config --local branch.the-branch.merge'.split(), stdout=PIPE)

@mock.patch('bin.commands.utils.directories.is_git_repository', return_value=True)
@mock.patch('bin.commands.utils.git.current_branch', return_value='the-branch')
@mock.patch('subprocess.Popen')
def test_upstream_includeRemote_noUpstream(self, mock_popen, mock_currentbranch, mock_isgitrepository):
def test_upstream_includeRemote_noUpstream(self, mock_popen, mock_currentbranch, mock_isemptyrepository, mock_isgitrepository):

# setup
mock_process = mock.Mock()
Expand All @@ -53,10 +54,12 @@ def test_upstream_includeRemote_noUpstream(self, mock_popen, mock_currentbranch,
mock_process.communicate.assert_called_once_with()
mock_popen.assert_called_once_with('git config --local branch.the-branch.merge'.split(), stdout=PIPE)

@mock.patch('bin.commands.utils.directories.is_git_repository', return_value=False)
@mock.patch('bin.commands.utils.messages.error', side_effect=utils.and_exit)
@mock.patch('os.getcwd', return_value='working_dir')
def test_upstream_notAGitRepository(self, mock_getcwd, mock_error, mock_isgitrepository):
def test_upstream_notAGitRepository(self, mock_getcwd, mock_error, mock_isemptyrepository, mock_isgitrepository):

# setup
mock_isgitrepository.return_value = False

# when
try:
Expand All @@ -70,11 +73,23 @@ def test_upstream_notAGitRepository(self, mock_getcwd, mock_error, mock_isgitrep
mock_error.assert_called_once_with("'working_dir' not a git repository")
mock_getcwd.assert_called_once_with()

@mock.patch('bin.commands.utils.directories.is_git_repository', return_value=True)
def test_upstream_repositoryIsEmpty(self, mock_isemptyrepository, mock_isgitrepository):

# setup
mock_isemptyrepository.return_value = True

# when
upstream_result = upstream.upstream()

# then
self.assertEqual(upstream_result, None)
mock_isemptyrepository.assert_called_once_with()


@mock.patch('bin.commands.utils.git.current_branch', return_value='the-branch')
@mock.patch('bin.commands.utils.git.is_valid_reference', return_value=True)
@mock.patch('subprocess.Popen')
def test_upstream_branchIncluded(self, mock_popen, mock_isvalidreference, mock_currentbranch, mock_isgitrepository):
def test_upstream_branchIncluded(self, mock_popen, mock_isvalidreference, mock_currentbranch, mock_isemptyrepository, mock_isgitrepository):

# setup
branch_name = 'the-branch'
Expand All @@ -97,10 +112,9 @@ def test_upstream_branchIncluded(self, mock_popen, mock_isvalidreference, mock_c
mock_process.communicate.assert_called_once_with()
mock_popen.assert_called_once_with('git config --local branch.the-branch.merge'.split(), stdout=PIPE)

@mock.patch('bin.commands.utils.directories.is_git_repository', return_value=True)
@mock.patch('bin.commands.utils.git.is_valid_reference', return_value=False)
@mock.patch('bin.commands.utils.messages.error', side_effect=utils.and_exit)
def test_upstream_notAValidReference(self, mock_error, mock_isvalidreference, mock_isgitrepository):
def test_upstream_notAValidReference(self, mock_error, mock_isvalidreference, mock_isemptyrepository, mock_isgitrepository):

# when
try:
Expand All @@ -113,11 +127,10 @@ def test_upstream_notAValidReference(self, mock_error, mock_isvalidreference, mo
mock_isvalidreference.assert_called_once_with('bad-branch')
mock_error.assert_called_once_with("'bad-branch' is not a valid branch")

@mock.patch('bin.commands.utils.directories.is_git_repository', return_value=True)
@mock.patch('bin.commands.utils.git.current_branch', return_value='the-branch')
@mock.patch('subprocess.Popen')
@mock.patch('subprocess.check_output', return_value='the-remote')
def test_upstream_includeRemote(self, mock_checkoutput, mock_popen, mock_currentbranch, mock_isgitrepository):
def test_upstream_includeRemote(self, mock_checkoutput, mock_popen, mock_currentbranch, mock_isemptyrepository, mock_isgitrepository):

# setup
expected_upstream = "the-upstream"
Expand Down

0 comments on commit 318f612

Please sign in to comment.