github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/repository/trusty/haproxy/hooks/tests/utils_for_tests.py (about)

     1  from contextlib import contextmanager
     2  
     3  from mock import patch, MagicMock
     4  
     5  
     6  @contextmanager
     7  def patch_open():
     8      '''Patch open() to allow mocking both open() itself and the file that is
     9      yielded.
    10  
    11      Yields the mock for "open" and "file", respectively.'''
    12      mock_open = MagicMock(spec=open)
    13      mock_file = MagicMock(spec=file)
    14  
    15      @contextmanager
    16      def stub_open(*args, **kwargs):
    17          mock_open(*args, **kwargs)
    18          yield mock_file
    19  
    20      with patch('__builtin__.open', stub_open):
    21          yield mock_open, mock_file