<s> from contextlib import contextmanager , nested <EOL> import copy <EOL> from mock import patch <EOL> import pytest <EOL> from gonzo . test_utils import assert_called_once_with , assert_has_calls <EOL> from gonzo . tasks . release import ( <EOL> append_to_history , get_previous_release , rollback , activate , <EOL> purge_release , prune ) <EOL> @ contextmanager <EOL> def disable_external_commands ( ) : <EOL> with nested ( <EOL> patch ( '<STR_LIT>' ) , <EOL> patch ( '<STR_LIT>' ) , <EOL> patch ( '<STR_LIT>' ) , <EOL> ) as ( local , run , sudo ) : <EOL> local . side_effect = RuntimeError ( "<STR_LIT>" ) <EOL> run . side_effect = RuntimeError ( "<STR_LIT>" ) <EOL> sudo . side_effect = RuntimeError ( "<STR_LIT>" ) <EOL> yield <EOL> @ contextmanager <EOL> def mock_history ( initial ) : <EOL> """<STR_LIT>""" <EOL> releases = copy . copy ( initial ) <EOL> project = '<STR_LIT>' <EOL> def list_releases ( ) : <EOL> return releases <EOL> def _append_to_history ( release ) : <EOL> releases . append ( release ) <EOL> def _replace_history ( release_list ) : <EOL> releases [ : ] = release_list <EOL> with nested ( <EOL> patch ( '<STR_LIT>' , list_releases ) , <EOL> patch ( '<STR_LIT>' , _append_to_history ) , <EOL> patch ( '<STR_LIT>' , _replace_history ) , <EOL> patch ( '<STR_LIT>' ) , <EOL> ) as ( _ , _ , _ , get_project ) : <EOL> get_project . return_value = project <EOL> with disable_external_commands ( ) : <EOL> yield releases <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_get_previous_release ( list_releases ) : <EOL> releases = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> list_releases . return_value = releases <EOL> assert get_previous_release ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert get_previous_release ( None ) is None <EOL> assert get_previous_release ( '<STR_LIT>' ) is None <EOL> assert get_previous_release ( '<STR_LIT>' ) is None <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_get_previous_release_ho_history ( list_releases ) : <EOL> releases = [ ] <EOL> list_releases . return_value = releases <EOL> assert get_previous_release ( None ) is None <EOL> assert get_previous_release ( '<STR_LIT>' ) is None <EOL> def test_append_to_history ( ) : <EOL> with mock_history ( initial = [ '<STR_LIT>' ] ) as releases : <EOL> append_to_history ( '<STR_LIT>' ) <EOL> assert releases == [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def test_append_to_history_empty ( ) : <EOL> with mock_history ( initial = [ ] ) as releases : <EOL> append_to_history ( '<STR_LIT>' ) <EOL> assert releases == [ '<STR_LIT>' ] <EOL> def test_append_to_history_noop ( ) : <EOL> with mock_history ( initial = [ '<STR_LIT>' ] ) as releases : <EOL> append_to_history ( '<STR_LIT>' ) <EOL> assert releases == [ '<STR_LIT>' ] <EOL> def test_append_to_history_repeat ( ) : <EOL> with mock_history ( initial = [ '<STR_LIT>' , '<STR_LIT>' ] ) as releases : <EOL> append_to_history ( '<STR_LIT>' ) <EOL> assert releases == [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_rollback ( get_current , set_current ) : <EOL> get_current . return_value = '<STR_LIT>' <EOL> with mock_history ( initial = [ '<STR_LIT>' , '<STR_LIT>' ] ) as releases : <EOL> rollback ( ) <EOL> assert releases == [ '<STR_LIT>' ] <EOL> assert_called_once_with ( set_current , '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_rollback_nowhere_to_go ( get_current , set_current ) : <EOL> get_current . return_value = '<STR_LIT>' <EOL> with mock_history ( initial = [ '<STR_LIT>' ] ) as releases : <EOL> with pytest . raises ( RuntimeError ) : <EOL> rollback ( ) <EOL> assert releases == [ '<STR_LIT>' ] <EOL> assert_has_calls ( set_current , [ ] ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_activate ( get_current , set_current , get_commit ) : <EOL> get_current . return_value = '<STR_LIT>' <EOL> get_commit . return_value = '<STR_LIT>' <EOL> with mock_history ( initial = [ '<STR_LIT>' , '<STR_LIT>' ] ) as releases : <EOL> activate ( ) <EOL> assert releases == [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert_called_once_with ( set_current , '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_activate_existing ( get_current , set_current , get_commit ) : <EOL> get_current . return_value = '<STR_LIT>' <EOL> get_commit . return_value = '<STR_LIT>' <EOL> with mock_history ( initial = [ '<STR_LIT>' , '<STR_LIT>' ] ) as releases : <EOL> activate ( ) <EOL> assert releases == [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert_called_once_with ( set_current , '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_purge_release ( get_current , exists ) : <EOL> get_current . return_value = '<STR_LIT>' <EOL> exists . return_value = True <EOL> with mock_history ( initial = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) as releases : <EOL> with patch ( '<STR_LIT>' ) : <EOL> purge_release ( '<STR_LIT>' ) <EOL> assert releases == [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_purge_release_current ( get_current , exists ) : <EOL> get_current . return_value = '<STR_LIT>' <EOL> exists . return_value = True <EOL> with mock_history ( initial = [ '<STR_LIT>' , '<STR_LIT>' ] ) as releases : <EOL> with patch ( '<STR_LIT>' ) : <EOL> with pytest . raises ( RuntimeError ) : <EOL> purge_release ( '<STR_LIT>' ) <EOL> assert releases == [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_prune ( get_current , exists ) : <EOL> get_current . return_value = '<STR_LIT>' <EOL> exists . return_value = True <EOL> with mock_history ( initial = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) as releases : <EOL> with patch ( '<STR_LIT>' ) : <EOL> prune ( <NUM_LIT:2> ) <EOL> assert releases == [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_prune_not_enough_left ( get_current , exists ) : <EOL> get_current . return_value = '<STR_LIT>' <EOL> exists . return_value = True <EOL> with mock_history ( initial = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) as releases : <EOL> with patch ( '<STR_LIT>' ) : <EOL> prune ( <NUM_LIT:5> ) <EOL> assert releases == [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] </s>
