<commit_before>import errno
import os

import pytest

from fridge.memoryfs import MemoryFile, MemoryFS


class TestMemoryFile(object):
    def test_can_be_written(self):
        f = MemoryFile()
        f.write('test')
        f.flush()
        assert f.content == 'test'

    def test_close_flushes_content(self):
        f = MemoryFile()
        f.write('test')
        f.close()
        assert f.content == 'test'

    def test_can_be_reopened_and_read(self):
        f = MemoryFile()
        f.write('test')
        f.close()
        f.open()
        assert f.read() == 'test'

    def test_can_be_used_in_with(self):
        with MemoryFile() as f:
            f.write('test')
        assert f.content == 'test'


class TestMemoryFS(object):
    def test_parent_of_top_node_is_node_itself(self):
        fs = MemoryFS()
        assert fs.parent == fs

    def test_get_node_without_path(self):
        fs = MemoryFS()
        assert fs == fs.get_node([])

    def test_get_subnodes(self):
        fs = MemoryFS()
        fs1 = MemoryFS(fs)
        fs2 = MemoryFS(fs1)
        fs.children['1'] = fs1
        fs1.children['2'] = fs2
        assert fs2 == fs.get_node(['1', os.curdir, os.pardir, '1', '2'])

    def test_get_parent_node_of_parent_is_parent(self):
        # This corresponds to Unix behavior
        fs = MemoryFS()
        assert fs.get_node([os.pardir]) == fs

    def test_get_non_existent_node(self):
        fs = MemoryFS()
        with pytest.raises(KeyError):
            fs.get_node(['nonexistent'])

    def test_mkdir(self):
        fs = MemoryFS()
        fs.mkdir('test')
        fs.mkdir('test/subdir')
        assert 'test' in fs.children
        assert 'subdir' in fs.children['test'].children

    def test_mkdir_raises_exception_if_dir_exists(self):
        fs = MemoryFS()
        fs.mkdir('dir')
        with pytest.raises(OSError) as excinfo:
            fs.mkdir('dir')
        assert excinfo.value.errno == errno.EEXIST
        assert excinfo.value.filename == 'dir'

    @pytest.mark.parametrize('mode', ['w', 'w+', 'a', 'a+'])
    def test_allows_writing_of_files(self, mode):
        fs = MemoryFS()
        with fs.open('filename', mode) as f:
            f.write('dummy content')
        assert fs.children['filename'].content == 'dummy content'

    @pytest.mark.parametrize('mode', ['a', 'a+'])
    def test_allows_appending_to_files(self, mode):
        fs = MemoryFS()
        with fs.open('filename', 'w') as f:
            f.write('dummy ')
        with fs.open('filename', mode) as f:
            f.write('content')
        assert fs.children['filename'].content == 'dummy content'

    @pytest.mark.parametrize('mode', ['w', 'w+'])
    def test_allows_overwriting_of_files(self, mode):
        fs = MemoryFS()
        with fs.open('filename', 'w') as f:
            f.write('dummy ')
        with fs.open('filename', mode) as f:
            f.write('content')
        assert fs.children['filename'].content == 'content'
<commit_msg>MemoryFS: Test reading of files.<commit_after>import errno
import os

import pytest

from fridge.memoryfs import MemoryFile, MemoryFS


class TestMemoryFile(object):
    def test_can_be_written(self):
        f = MemoryFile()
        f.write('test')
        f.flush()
        assert f.content == 'test'

    def test_close_flushes_content(self):
        f = MemoryFile()
        f.write('test')
        f.close()
        assert f.content == 'test'

    def test_can_be_reopened_and_read(self):
        f = MemoryFile()
        f.write('test')
        f.close()
        f.open()
        assert f.read() == 'test'

    def test_can_be_used_in_with(self):
        with MemoryFile() as f:
            f.write('test')
        assert f.content == 'test'


class TestMemoryFS(object):
    def test_parent_of_top_node_is_node_itself(self):
        fs = MemoryFS()
        assert fs.parent == fs

    def test_get_node_without_path(self):
        fs = MemoryFS()
        assert fs == fs.get_node([])

    def test_get_subnodes(self):
        fs = MemoryFS()
        fs1 = MemoryFS(fs)
        fs2 = MemoryFS(fs1)
        fs.children['1'] = fs1
        fs1.children['2'] = fs2
        assert fs2 == fs.get_node(['1', os.curdir, os.pardir, '1', '2'])

    def test_get_parent_node_of_parent_is_parent(self):
        # This corresponds to Unix behavior
        fs = MemoryFS()
        assert fs.get_node([os.pardir]) == fs

    def test_get_non_existent_node(self):
        fs = MemoryFS()
        with pytest.raises(KeyError):
            fs.get_node(['nonexistent'])

    def test_mkdir(self):
        fs = MemoryFS()
        fs.mkdir('test')
        fs.mkdir('test/subdir')
        assert 'test' in fs.children
        assert 'subdir' in fs.children['test'].children

    def test_mkdir_raises_exception_if_dir_exists(self):
        fs = MemoryFS()
        fs.mkdir('dir')
        with pytest.raises(OSError) as excinfo:
            fs.mkdir('dir')
        assert excinfo.value.errno == errno.EEXIST
        assert excinfo.value.filename == 'dir'

    @pytest.mark.parametrize('mode', ['w', 'w+', 'a', 'a+'])
    def test_allows_writing_of_files(self, mode):
        fs = MemoryFS()
        with fs.open('filename', mode) as f:
            f.write('dummy content')
        assert fs.children['filename'].content == 'dummy content'

    @pytest.mark.parametrize('mode', ['a', 'a+'])
    def test_allows_appending_to_files(self, mode):
        fs = MemoryFS()
        with fs.open('filename', 'w') as f:
            f.write('dummy ')
        with fs.open('filename', mode) as f:
            f.write('content')
        assert fs.children['filename'].content == 'dummy content'

    @pytest.mark.parametrize('mode', ['w', 'w+'])
    def test_allows_overwriting_of_files(self, mode):
        fs = MemoryFS()
        with fs.open('filename', 'w') as f:
            f.write('dummy ')
        with fs.open('filename', mode) as f:
            f.write('content')
        assert fs.children['filename'].content == 'content'

    @pytest.mark.parametrize('mode', ['r', 'r+'])
    def test_allows_reading_of_files(self, mode):
        fs = MemoryFS()
        with fs.open('filename', 'w') as f:
            f.write('dummy content')
        with fs.open('filename', mode) as f:
            data = f.read()
        assert data == 'dummy content'

{'fraction_non_alphanumeric': 0.14016172506738545, 'fraction_numerical': 0.003805295703186935, 'mean_word_length': 2.3588924387646433, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6117240', 'n_tokens_mistral': 2081, 'n_tokens_neox': 2024, 'n_words': 436}