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
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
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')
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'
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)
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')
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')
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
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
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
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
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
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)
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)
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'
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'