Ejemplo n.º 1
0
def test_remove_removes_file(fsx_fake, monkeypatch):
    file_path = '/tmp/file'
    fsx_fake.add_file(file_path)

    assert fsx.os.path.exists(file_path) is True
    fsx.os.remove(file_path)
    assert fsx.os.path.exists(file_path) is False
    assert fsx.os.path.isfile(file_path) is False
Ejemplo n.º 2
0
    def test_ZipFile_read_works_on_fake_files_received_an_empty_archive_as_content(
            self, fsx_fake):
        io_obj = io.BytesIO()
        zipfile.ZipFile(io_obj, 'w').close()
        zipcontent = io_obj.getvalue()

        fsx_fake.add_file('arch.zip', zipcontent)
        res = fsx.open('arch.zip', 'rb').read()
        assert res == zipcontent
Ejemplo n.º 3
0
    def test_ZipFile_read_works_on_fake_files_which_received_content(
            self, fsx_fake):
        io_obj = io.BytesIO()
        with zipfile.ZipFile(io_obj, 'w') as file_:
            file_.writestr('f.txt', 'foo')
        zipcontent = io_obj.getvalue()

        fsx_fake.add_file('arch.zip', content=zipcontent)
        res = fsx.zipfile.ZipFile('arch.zip', 'r').read('f.txt')
        assert res == six.b('foo')
Ejemplo n.º 4
0
def test_open_append_as_ctxmgr_works(fsx_fake):
    fsx_fake.add_file('f', '1')

    with fsx.open('f', 'a') as file_:
        file_.write('23')

    with fsx.open('f', 'r') as file_:
        res = file_.read()

    assert res == '123'
Ejemplo n.º 5
0
def test_stat_times_are_larger_than_beginning_of_today(fsx_fake):
    date_now = datetime.now()
    begin_of_today = datetime(date_now.year, date_now.month, date_now.day)

    file_path = '/tmp/file'
    fsx_fake.add_file(file_path)

    file_stat = fsx.os.stat(file_path)
    assert file_stat.st_ctime >= _get_timestamp(begin_of_today)
    assert file_stat.st_atime >= _get_timestamp(begin_of_today)
    assert file_stat.st_mtime >= _get_timestamp(begin_of_today)
Ejemplo n.º 6
0
    def test_ZipFile_append_as_ctxmgr_works(self, fsx_fake):
        io_obj = io.BytesIO()
        zipfile.ZipFile(io_obj, 'w').writestr('f1', 'foo')
        zipcontent = io_obj.getvalue()

        fsx_fake.add_file('arch.zip', zipcontent)

        with fsx.zipfile.ZipFile('arch.zip', 'a') as file_:
            file_.writestr('f2', 'bar')

        with fsx.zipfile.ZipFile('arch.zip', 'r') as file_:
            assert file_.read('f1') == six.b('foo')
            assert file_.read('f2') == six.b('bar')
Ejemplo n.º 7
0
    def test_ZipFile_append_adds_new_files_inside_archive(self, fsx_fake):
        io_obj = io.BytesIO()
        zipfile.ZipFile(io_obj, 'w').writestr('f1', 'foo')
        zipcontent = io_obj.getvalue()

        fsx_fake.add_file('arch.zip', zipcontent)

        with fsx.zipfile.ZipFile('arch.zip', 'a') as file_:
            file_.writestr('f2', 'bar')
            file_.writestr('f3', 'spam')

        assert fsx.zipfile.ZipFile('arch.zip', 'r').read('f1') == six.b('foo')
        assert fsx.zipfile.ZipFile('arch.zip', 'r').read('f2') == six.b('bar')
        assert fsx.zipfile.ZipFile('arch.zip', 'r').read('f3') == six.b('spam')
Ejemplo n.º 8
0
    def test_ZipFile_write_creates_file_which_can_be_read(
            self, curdir, zipArchivePath, memberFilePath, expArchivePaths,
            expZipMemberName, fsx_fake):
        fsx_fake.add_dir(curdir)
        fsx_fake.add_file(memberFilePath, 'file-content')

        fsx.os.chdir(curdir)
        with fsx.zipfile.ZipFile(zipArchivePath, 'w') as file_:
            file_.write(memberFilePath)

        with fsx.zipfile.ZipFile(zipArchivePath, 'r') as file_:
            assert file_.namelist() == [expZipMemberName]
            assert file_.read(expZipMemberName) == six.b('file-content')

        for expArchivePath in expArchivePaths:
            assert fsx.os.path.exists(expArchivePath) == True
Ejemplo n.º 9
0
def test_add_file_flips_fwdslashes_on_win(filepath, exp, monkeypatch,
                                          fsx_fake):
    monkeypatch.setattr(sys, 'platform', 'win32')

    fsx_fake.add_file(filepath)
    assert fsx_fake.as_dict() == exp
Ejemplo n.º 10
0
def test_glob(pat, exist_files, exp, fsx_fake):
    [fsx_fake.add_file(item) for item in exist_files]
    res = fsx.glob.glob(pat)
    assert exp == res
Ejemplo n.º 11
0
 def test_returns_0_for_files_without_content(self, fsx_fake):
     fsx_fake.add_file('X:/a/file.txt')
     assert fsx.os.path.getsize('X:/a/file.txt') == 0
Ejemplo n.º 12
0
def test_listdir_works(files, path, exp, fsx_fake):
    [fsx_fake.add_file(item) for item in files]
    res = fsx.os.listdir(path)
    assert res == exp
Ejemplo n.º 13
0
def test_isdir(path, files, dirs, type_, fsx_fake):
    [fsx_fake.add_file(item) for item in files]
    [fsx_fake.add_dir(item) for item in dirs]
    exp = type_ == 'd'
    assert exp == fsx.os.path.isdir(path)
Ejemplo n.º 14
0
def test_exists(path, files, dirs, type_, fsx_fake):
    [fsx_fake.add_file(item) for item in files]
    [fsx_fake.add_dir(item) for item in dirs]
    exp = type_ in ['f', 'd']
    assert exp == fsx.os.path.exists(path)
Ejemplo n.º 15
0
def test_open_read_works_on_fake_files_which_received_content(fsx_fake):
    fsx_fake.add_file('f', content='test')
    res = fsx.open('f', 'r').read()
    assert res == 'test'
Ejemplo n.º 16
0
def test_open_append_works(fsx_fake):
    fsx_fake.add_file('f', '1')
    fsx.open('f', 'a').write('23')
    res = fsx.open('f', 'r').read()
    assert res == '123'