예제 #1
0
    def test_should_match_regex_normalized_filepath(self):
        fakeio_session = fakeio.FakeIOSession()

        fakeio_session.intercept_regex(re.compile("^/memfile/"))
        with fakeio_session:
            # Assert not raise IOError
            open("/memfile\\something.txt", 'w')
예제 #2
0
    def test_should_intercept_regex(self):
        fakeio_session = fakeio.FakeIOSession()

        fakeio_session.intercept_regex(re.compile("^/memfile/"))
        with fakeio_session:
            fileobj = open("/memfile/something.txt", 'w')
            fileobj.write('something')
예제 #3
0
    def test_should_mapped_string_has_name(self):
        fakeio_session = fakeio.FakeIOSession()
        filepath = "/memfile/something.txt"

        fakeio_session.create_file(filepath, 'r', 'content')
        with fakeio_session:
            fileobj = open(filepath, 'r')
            self.assertEqual(fileobj.name, filepath)
예제 #4
0
    def test_should_normalize_filepath_when_open(self):
        fakeio_session = fakeio.FakeIOSession()
        content = "something"

        fakeio_session.create_file("/memfile/something.txt", 'r', content)
        with fakeio_session:
            # Assert not raise IOError
            open("/memfile\\something.txt", 'r')
예제 #5
0
    def test_should_unmap_when_exit_with_statement(self):
        fakeio_session = fakeio.FakeIOSession()
        filepath = "/memfile/something.txt"
        content = "something"

        fakeio_session.create_file(filepath, 'r', content)
        with fakeio_session:
            pass
        with self.assertRaises(IOError):
            open(filepath, 'r')
예제 #6
0
    def test_should_mapping_have_precedence_over_regex(self):
        fakeio_session = fakeio.FakeIOSession()
        filepath = "/memfile/something.txt"
        regex = "^/memfile/"
        content = "something"

        fakeio_session.create_file(filepath, 'r', content)
        with fakeio_session:
            fileobj = open(filepath, 'r')
            self.assertEqual(fileobj.read(), content)
예제 #7
0
    def test_maps_io_open_when_entering_with_statement(self):
        fakeio_session = fakeio.FakeIOSession()
        filepath = "/memfile/something.txt"
        content = "something"

        fakeio_session.create_file(filepath, 'r', content)
        with self.assertRaises(IOError):
            io.open(filepath, 'r')
        with fakeio_session:
            self.assertEqual(io.open(filepath, 'r').read(), content)
예제 #8
0
    def test_should_create_writable_file(self):
        fakeio_session = fakeio.FakeIOSession()
        filepath = "/memfile/writable.txt"
        content = 'something'

        writable_fileobj = fakeio_session.create_file(filepath, 'rw')
        with fakeio_session:
            fileobj = open(filepath, 'w')
            fileobj.write(content)
            fileobj.close()
        self.assertEqual(writable_fileobj.getvalue(), content)
예제 #9
0
    def test_should_be_able_to_open_again_after_closed(self):
        fakeio_session = fakeio.FakeIOSession()
        filepath = "/memfile/something.txt"

        fakeio_session.create_file(filepath, 'r', 'content')
        with fakeio_session:
            fileobj = open(filepath, 'r')
            fileobj.read()
            fileobj.close()
            fileobj = open(filepath, 'r')
            fileobj.read()
            fileobj.close()
예제 #10
0
    def test_should_be_able_to_open_again_after_closed_wriable(self):
        fakeio_session = fakeio.FakeIOSession()
        filepath = "/memfile/writable.txt"
        content = 'something'

        writable_fileobj = fakeio_session.create_file(filepath, 'rw')
        with fakeio_session:
            fileobj = open(filepath, 'w')
            fileobj.write(content)
            fileobj.close()

            fileobj = open(filepath, 'r')
            self.assertEqual(fileobj.read(), content)
예제 #11
0
    def test_should_return_faked_file_mappings(self):
        fakeio_session = fakeio.FakeIOSession()
        filepath_regex = "/memfile/something_regex.txt"
        filepath_mapping = "/memfile/something_mapping.txt"

        readable_fileobj = fakeio_session.create_file(filepath_mapping, 'r')
        fakeio_session.intercept_regex(re.compile("^/memfile/"))
        with fakeio_session:
            open(filepath_regex, 'w')

        mappings = fakeio_session.mappings
        self.assertIn(filepath_regex, mappings)
        self.assertIn(filepath_mapping, mappings)
        self.assertIs(mappings[filepath_mapping], readable_fileobj)
예제 #12
0
    def test_should_not_normalized_not_matched_path(self):
        filepath = "/memfile\\something.txt"

        class OpenMock(object):
            def __init__(self):
                self.called = None

            def __call__(self, f, m, b):
                self.called = f

        open_mock = OpenMock()
        saved_open = __builtin__.open
        try:
            __builtin__.open = open_mock

            filepath = "/memfile\\something.txt"
            fakeio_session = fakeio.FakeIOSession()
            with fakeio_session:
                open(filepath)
            self.assertEqual(filepath, open_mock.called)
        finally:
            __builtin__.open = saved_open
예제 #13
0
    def test_should_pass_through_normal_file(self):
        fakeio_session = fakeio.FakeIOSession()
        content = open(__file__, 'r').read()

        with fakeio_session:
            self.assertEqual(open(__file__, 'r').read(), content)
예제 #14
0
    def test_should_raise_IOError_when_no_such_file_exists(self):
        fakeio_session = fakeio.FakeIOSession()

        with self.assertRaises(IOError), fakeio_session:
            open("/memfile/notfound.txt", 'r')