<s> from os import unlink <EOL> from os . path import join , isfile <EOL> from contextlib import contextmanager <EOL> from functools import partial <EOL> from itertools import chain , combinations <EOL> from git import Repo , Head , Commit <EOL> from mock import patch , MagicMock <EOL> from jig . tests . testcase import JigTestCase <EOL> from jig . exc import ( <EOL> GitRevListMissing , GitRevListFormatError , GitWorkingDirectoryDirty , <EOL> TrackingBranchMissing ) <EOL> from jig . gitutils . branches import ( <EOL> parse_rev_range , prepare_working_directory , <EOL> _prepare_against_staged_index , _prepare_with_rev_range , Tracked ) <EOL> @ contextmanager <EOL> def assert_git_status_unchanged ( repository ) : <EOL> """<STR_LIT>""" <EOL> def long_status ( repository ) : <EOL> output = Repo ( repository ) . git . status ( '<STR_LIT>' ) <EOL> return output . splitlines ( ) [ <NUM_LIT:1> : ] <EOL> before = long_status ( repository ) <EOL> yield <EOL> after = long_status ( repository ) <EOL> assert before == after , "<STR_LIT>" <EOL> class PrepareTestCase ( JigTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( PrepareTestCase , self ) . setUp ( ) <EOL> self . reset_gitrepo ( ) <EOL> def reset_gitrepo ( self ) : <EOL> del self . gitrepodir <EOL> self . commits = [ <EOL> self . commit ( self . gitrepodir , '<STR_LIT>' , '<STR_LIT:a>' ) , <EOL> self . commit ( self . gitrepodir , '<STR_LIT>' , '<STR_LIT:b>' ) , <EOL> self . commit ( self . gitrepodir , '<STR_LIT>' , '<STR_LIT:c>' ) , <EOL> self . commit ( self . gitrepodir , '<STR_LIT>' , '<STR_LIT:d>' ) <EOL> ] <EOL> @ property <EOL> def repo ( self ) : <EOL> return Repo ( self . gitrepodir ) <EOL> def diff_head ( self ) : <EOL> return self . repo . index . diff ( '<STR_LIT>' ) <EOL> @ contextmanager <EOL> def prepare ( self ) : <EOL> with assert_git_status_unchanged ( self . gitrepodir ) : <EOL> with self . prepare_context_manager ( ) as subject : <EOL> yield subject <EOL> class TestParseRevRange ( JigTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestParseRevRange , self ) . setUp ( ) <EOL> self . gitrepo , self . gitrepodir , _ = self . repo_from_fixture ( '<STR_LIT>' ) <EOL> def assertIsRevRange ( self , rev_range ) : <EOL> self . assertIsInstance ( rev_range . a , Commit ) <EOL> self . assertIsInstance ( rev_range . b , Commit ) <EOL> def test_bad_format ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( GitRevListFormatError ) : <EOL> parse_rev_range ( self . gitrepodir , '<STR_LIT>' ) <EOL> def test_bad_format_missing_rev ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( GitRevListFormatError ) : <EOL> parse_rev_range ( self . gitrepodir , '<STR_LIT>' ) <EOL> with self . assertRaises ( GitRevListFormatError ) : <EOL> parse_rev_range ( self . gitrepodir , '<STR_LIT>' ) <EOL> def test_bad_revs ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( GitRevListMissing ) : <EOL> parse_rev_range ( self . gitrepodir , '<STR_LIT>' ) <EOL> def test_good_revs ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertIsRevRange ( parse_rev_range ( self . gitrepodir , '<STR_LIT>' ) ) <EOL> def test_local_branch ( self ) : <EOL> """<STR_LIT>""" <EOL> self . gitrepo . create_head ( '<STR_LIT>' ) <EOL> self . assertIsRevRange ( <EOL> parse_rev_range ( self . gitrepodir , '<STR_LIT>' ) <EOL> ) <EOL> def test_out_of_range ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( GitRevListMissing ) : <EOL> parse_rev_range ( self . gitrepodir , '<STR_LIT>' ) <EOL> class TestPrepareAgainstStagedIndex ( PrepareTestCase ) : <EOL> """<STR_LIT>""" <EOL> def prepare_context_manager ( self ) : <EOL> return _prepare_against_staged_index ( self . repo ) <EOL> def test_working_directory_clean ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . prepare ( ) as stash : <EOL> self . assertIsNone ( stash ) <EOL> def test_untracked ( self ) : <EOL> """<STR_LIT>""" <EOL> self . create_file ( self . gitrepodir , '<STR_LIT>' , '<STR_LIT:e>' ) <EOL> expected_untracked = self . repo . untracked_files <EOL> with self . prepare ( ) as stash : <EOL> self . assertIsNone ( stash ) <EOL> self . assertEqual ( expected_untracked , self . repo . untracked_files ) <EOL> def test_staged ( self ) : <EOL> """<STR_LIT>""" <EOL> self . stage ( self . gitrepodir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> before = self . diff_head ( ) <EOL> with self . prepare ( ) as stash : <EOL> self . assertIsNone ( stash ) <EOL> self . assertEqual ( before , self . diff_head ( ) ) <EOL> def test_modified ( self ) : <EOL> """<STR_LIT>""" <EOL> self . modify_file ( self . gitrepodir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> with self . prepare ( ) as stash : <EOL> self . assertIsNotNone ( stash ) <EOL> self . assertEqual ( [ ] , self . repo . index . diff ( None ) ) <EOL> def test_stageremoved ( self ) : <EOL> """<STR_LIT>""" <EOL> self . stage_remove ( self . gitrepodir , '<STR_LIT>' ) <EOL> with self . prepare ( ) as stash : <EOL> self . assertIsNone ( stash ) <EOL> def test_fsremoved ( self ) : <EOL> """<STR_LIT>""" <EOL> unlink ( join ( self . gitrepodir , '<STR_LIT>' ) ) <EOL> with self . prepare ( ) as stash : <EOL> self . assertIsNotNone ( stash ) <EOL> self . assertTrue ( isfile ( join ( self . gitrepodir , '<STR_LIT>' ) ) ) <EOL> def test_combinations ( self ) : <EOL> """<STR_LIT>""" <EOL> def modified ( ) : <EOL> self . modify_file ( self . gitrepodir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> modified . should_stash = True <EOL> def staged ( ) : <EOL> self . stage ( self . gitrepodir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> staged . should_stash = False <EOL> def indexremoved ( ) : <EOL> self . stage_remove ( self . gitrepodir , '<STR_LIT>' ) <EOL> indexremoved . should_stash = False <EOL> def fsremoved ( ) : <EOL> unlink ( join ( self . gitrepodir , '<STR_LIT>' ) ) <EOL> fsremoved . should_stash = True <EOL> def untracked ( ) : <EOL> self . create_file ( self . gitrepodir , '<STR_LIT>' , '<STR_LIT:e>' ) <EOL> untracked . should_stash = False <EOL> mutation = partial ( <EOL> combinations , <EOL> ( modified , staged , indexremoved , fsremoved , untracked ) <EOL> ) <EOL> options = chain . from_iterable ( map ( mutation , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) ) <EOL> for option in options : <EOL> map ( lambda x : x ( ) , option ) <EOL> with self . prepare ( ) as stash : <EOL> should_stash = any ( map ( lambda x : x . should_stash , option ) ) <EOL> if should_stash : <EOL> self . assertIsNotNone ( stash ) <EOL> else : <EOL> self . assertIsNone ( stash ) <EOL> self . reset_gitrepo ( ) <EOL> class TestPrepareWithRevRange ( PrepareTestCase ) : <EOL> """<STR_LIT>""" <EOL> def prepare_context_manager ( self ) : <EOL> rev_range_parsed = parse_rev_range ( <EOL> self . repo . working_dir , <EOL> self . rev_range <EOL> ) <EOL> return _prepare_with_rev_range ( self . repo , rev_range_parsed ) <EOL> def test_dirty_working_directory ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rev_range = '<STR_LIT>' <EOL> self . modify_file ( self . gitrepodir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> with self . assertRaises ( GitWorkingDirectoryDirty ) : <EOL> self . prepare ( ) . __enter__ ( ) <EOL> def test_yields_git_named_head ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rev_range = '<STR_LIT>' <EOL> with self . prepare ( ) as head : <EOL> self . assertIsInstance ( head , Head ) <EOL> def test_yields_git_detached_head ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rev_range = '<STR_LIT>' <EOL> Repo ( self . gitrepodir ) . git . checkout ( self . commits [ - <NUM_LIT:1> ] . hexsha ) <EOL> with self . prepare ( ) as head : <EOL> self . assertIsInstance ( head , Commit ) <EOL> def test_detached_head_right_side_of_rev_range ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rev_range = '<STR_LIT>' <EOL> expected = self . commits [ - <NUM_LIT:2> ] <EOL> with self . prepare ( ) : <EOL> self . assertEqual ( <EOL> Repo ( self . gitrepodir ) . head . commit , <EOL> expected <EOL> ) <EOL> def test_returns_to_master ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rev_range = '<STR_LIT>' <EOL> with self . prepare ( ) : <EOL> pass <EOL> self . assertEqual ( <EOL> Repo ( self . gitrepodir ) . head . reference . path , <EOL> '<STR_LIT>' <EOL> ) <EOL> def test_returns_to_detached_head ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rev_range = '<STR_LIT>' <EOL> Repo ( self . gitrepodir ) . git . checkout ( self . commits [ - <NUM_LIT:2> ] . hexsha ) <EOL> expected = self . commits [ - <NUM_LIT:3> ] <EOL> with self . prepare ( ) : <EOL> self . assertEqual ( <EOL> Repo ( self . gitrepodir ) . head . commit , <EOL> expected <EOL> ) <EOL> self . assertEqual ( <EOL> Repo ( self . gitrepodir ) . head . commit , <EOL> self . commits [ - <NUM_LIT:2> ] <EOL> ) <EOL> class TestPrepareWorkingDirectory ( JigTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestPrepareWorkingDirectory , self ) . setUp ( ) <EOL> self . gitrepo , self . gitrepodir , _ = self . repo_from_fixture ( '<STR_LIT>' ) <EOL> def test_no_rev_range ( self ) : <EOL> """<STR_LIT>""" <EOL> prepare_function = '<STR_LIT>' <EOL> with patch ( prepare_function ) as p : <EOL> p . return_value = MagicMock ( ) <EOL> with prepare_working_directory ( self . gitrepodir ) : <EOL> pass <EOL> self . assertTrue ( p . return_value . __enter__ . called ) <EOL> def test_rev_range ( self ) : <EOL> """<STR_LIT>""" <EOL> prepare_function = '<STR_LIT>' <EOL> with patch ( prepare_function ) as p : <EOL> p . return_value = MagicMock ( ) <EOL> rev_range_parsed = parse_rev_range ( <EOL> self . gitrepodir , '<STR_LIT>' <EOL> ) <EOL> with prepare_working_directory ( self . gitrepodir , rev_range_parsed ) : <EOL> pass <EOL> self . assertTrue ( p . return_value . __enter__ . called ) <EOL> class TestTracked ( JigTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestTracked , self ) . setUp ( ) <EOL> self . commits = [ <EOL> self . commit ( self . gitrepodir , '<STR_LIT>' , '<STR_LIT:a>' ) , <EOL> self . commit ( self . gitrepodir , '<STR_LIT>' , '<STR_LIT:b>' ) , <EOL> self . commit ( self . gitrepodir , '<STR_LIT>' , '<STR_LIT:c>' ) , <EOL> ] <EOL> def test_tracking_branch_does_not_exist ( self ) : <EOL> """<STR_LIT>""" <EOL> tracked = Tracked ( self . gitrepodir ) <EOL> self . assertFalse ( tracked . exists ) <EOL> def test_tracking_branch_exists ( self ) : <EOL> """<STR_LIT>""" <EOL> tracking_branch = Repo ( self . gitrepodir ) . create_head ( '<STR_LIT>' ) <EOL> tracking_branch . commit = '<STR_LIT>' <EOL> tracked = Tracked ( self . gitrepodir ) <EOL> self . assertTrue ( tracked . exists ) <EOL> def test_tracking_branch_by_a_different_name ( self ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> tracking_branch = Repo ( self . gitrepodir ) . create_head ( name ) <EOL> tracking_branch . commit = '<STR_LIT>' <EOL> tracked = Tracked ( self . gitrepodir , name ) <EOL> self . assertTrue ( tracked . exists ) <EOL> def test_update_defaults_to_head ( self ) : <EOL> """<STR_LIT>""" <EOL> tracked = Tracked ( self . gitrepodir ) <EOL> reference = tracked . update ( ) <EOL> self . assertEqual ( <EOL> reference . commit , <EOL> self . commits [ - <NUM_LIT:1> ] <EOL> ) <EOL> def test_non_existent_reference ( self ) : <EOL> """<STR_LIT>""" <EOL> tracked = Tracked ( self . gitrepodir ) <EOL> with self . assertRaises ( TrackingBranchMissing ) : <EOL> tracked . reference <EOL> def test_tracking_branch_reference ( self ) : <EOL> """<STR_LIT>""" <EOL> tracking_branch = Repo ( self . gitrepodir ) . create_head ( '<STR_LIT>' ) <EOL> tracking_branch . commit = '<STR_LIT>' <EOL> tracked = Tracked ( self . gitrepodir ) <EOL> self . assertEqual ( <EOL> tracked . reference . commit , <EOL> self . commits [ <NUM_LIT:0> ] <EOL> ) <EOL> def test_update_takes_commit_hash ( self ) : <EOL> """<STR_LIT>""" <EOL> tracked = Tracked ( self . gitrepodir ) <EOL> tracked . update ( self . commits [ <NUM_LIT:0> ] . hexsha ) <EOL> self . assertEqual ( <EOL> tracked . reference . commit , <EOL> self . commits [ <NUM_LIT:0> ] <EOL> ) <EOL> def test_update_moves_head_forward ( self ) : <EOL> """<STR_LIT>""" <EOL> tracking_branch = Repo ( self . gitrepodir ) . create_head ( '<STR_LIT>' ) <EOL> tracking_branch . commit = '<STR_LIT>' <EOL> tracked = Tracked ( self . gitrepodir ) <EOL> tracked . update ( ) <EOL> self . assertEqual ( <EOL> tracked . reference . commit , <EOL> self . commits [ - <NUM_LIT:1> ] <EOL> ) </s>
