def test_create_broken(self, tmpdir): with pytest.raises(TypeError): file_no_args = FileBase() with pytest.raises(FileNotFoundError): file_empty_args = FileBase('', '') with pytest.raises(IsADirectoryError): file_directory = FileBase(tmpdir.strpath, tmpdir.strpath)
def test_has_symlink(self, tmpdir): file_path = tmpdir.join('test.txt') file_path.write('testing') file_path = file_path.strpath symlink_path = tmpdir.join('symlinked.txt') symlink_path = symlink_path.strpath file_symlink = os.symlink(file_path, symlink_path) file = FileBase(file_path, file_path) symlink = FileBase(symlink_path, symlink_path) assert file.is_symlink() is False assert symlink.is_symlink() is True
def test_init_mimetype_attribute_assigned_correctly(self): """When libmagic returns a given mimetype, the mimetype should be assigned properly.""" with mock.patch('kittengroomer.helpers.magic.from_file', return_value='text/plain'): file = FileBase('', '') assert file.mimetype == 'text/plain'
def text_file(self): with mock.patch('kittengroomer.helpers.magic.from_file', return_value='text/plain'): src_path = 'src/test.txt' dst_path = 'dst/test.txt' file = FileBase(src_path, dst_path) return file
def test_maintype_and_subtype_attributes(self): """If a file has a full mimetype, maintype and subtype should == the appropriate values.""" with mock.patch('kittengroomer.helpers.magic.from_file', return_value='text/plain'): file = FileBase('', '') assert file.maintype == 'text' assert file.subtype == 'plain'
def symlink(self, tmpdir): file_path = tmpdir.join('test.txt') file_path.write('testing') file_path = file_path.strpath symlink_path = tmpdir.join('symlinked.txt') symlink_path = symlink_path.strpath os.symlink(file_path, symlink_path) return FileBase(symlink_path, symlink_path)
def test_safe_copy(self, tmpdir): file = tmpdir.join('test.txt') file.write('testing') testdir = tmpdir.join('testdir') os.mkdir(testdir.strpath) filedest = testdir.join('test.txt') simple_groomer = KittenGroomerBase(tmpdir.strpath, testdir.strpath) simple_groomer.cur_file = FileBase(file.strpath, filedest.strpath) assert simple_groomer._safe_copy() is True
def test_safe_metadata_split(self, tmpdir): file = tmpdir.join('test.txt') file.write('testing') simple_groomer = KittenGroomerBase(tmpdir.strpath, tmpdir.strpath) simple_groomer.cur_file = FileBase(file.strpath, file.strpath) metadata_file = simple_groomer._safe_metadata_split('metadata.log') metadata_file.write('Have some metadata!') metadata_file.close() assert simple_groomer._safe_metadata_split('') is False
def test_safe_copy_calls_copy(self, src_dir_path, dest_dir_path): """Calling safe_copy should copy the file from the correct path to the correct destination path.""" file_path = os.path.join(src_dir_path, 'test.txt') with open(file_path, 'w+') as file: file.write('') dst_path = os.path.join(dest_dir_path, 'test.txt') with mock.patch('kittengroomer.helpers.magic.from_file', return_value='text/plain'): file = FileBase(file_path, dst_path) with mock.patch('kittengroomer.helpers.shutil.copy') as mock_copy: file.safe_copy() mock_copy.assert_called_once_with(file_path, dst_path)
def test_init_identify_filename(self, mock_libmagic): """Init should identify the filename correctly for src_path.""" src_path = 'src/test.txt' dst_path = 'dst/test.txt' file = FileBase(src_path, dst_path) assert file.filename == 'test.txt'
def test_init_symlink(self, mock_libmagic, symlink_file_path): """Init should properly identify symlinks.""" file = FileBase(symlink_file_path, '') assert file.mimetype == 'inode/symlink'
def test_extension_uppercase(self, tmpdir): file_path = tmpdir.join('TEST.TXT') file_path.write('testing') file_path = file_path.strpath file = FileBase(file_path, file_path) assert file.extension == '.txt'
def test_init_srcpath_is_directory(self, tmpdir): """Init should raise an exception if given a path to a directory.""" with pytest.raises(IsADirectoryError): FileBase(tmpdir.strpath, tmpdir.strpath)
def test_init_file_doesnt_exist(self): """Init should raise an exception if the file doesn't exist.""" with pytest.raises(FileNotFoundError): FileBase('', '')
def test_has_extension_false(self, mock_libmagic): """If the file has no extension, has_extensions should == False.""" src_path = 'src/test' dst_path = 'dst/test' file = FileBase(src_path, dst_path) assert file.has_extension is False
def test_init_uppercase_extension(self, mock_libmagic): """Init should coerce uppercase extension to lowercase""" src_path = Path('src/test.txt') dst_path = Path('dst/test.txt') file = FileBase(src_path, dst_path) assert file.extension == '.txt'
def test_create(self): file = FileBase('tests/src_valid/blah.conf', '/tests/dst/blah.conf')
def test_is_symlink_attribute(self, mock_libmagic, symlink_file_path): """If a file is a symlink, is_symlink should return True.""" file = FileBase(symlink_file_path, '') assert file.is_symlink is True
def test_has_mimetype_no_full_type(self): """If a file doesn't have a full mimetype has_mimetype should == False.""" with mock.patch('kittengroomer.helpers.magic.from_file', return_value='data'): file = FileBase('', '') assert file.has_mimetype is False
def test_has_mimetype_mimetype_is_none(self): """If a file doesn't have a full mimetype has_mimetype should == False.""" with mock.patch('kittengroomer.helpers.FileBase._determine_mimetype', return_value=None): file = FileBase('', '') assert file.has_mimetype is False
def generic_conf_file(self, source_file, dest_file): return FileBase(source_file, dest_file)
def test_init_identify_extension(self, mock_libmagic): """Init should identify the extension for src_path.""" src_path = 'src/test.txt' dst_path = 'dst/test.txt' file = FileBase(src_path, dst_path) assert file.extension == '.txt'
def temp_file_no_ext(self, tmpdir): file_path = tmpdir.join('test') file_path.write('testing') file_path = file_path.strpath return FileBase(file_path, file_path)
def test_init_uppercase_extension(self, mock_libmagic): """Init should coerce uppercase extension to lowercase""" src_path = 'src/TEST.TXT' dst_path = 'dst/TEST.TXT' file = FileBase(src_path, dst_path) assert file.extension == '.txt'
def test_has_extension_true(self, mock_libmagic): """If the file has an extension, has_extension should == True.""" src_path = 'src/test.txt' dst_path = 'dst/test.txt' file = FileBase(src_path, dst_path) assert file.has_extension is True
def text_file(self, tmpfile_path, dest_dir_path): with mock.patch('kittengroomer.helpers.magic.from_file', return_value='text/plain'): dst_path = dest_dir_path / 'test.txt' file = FileBase(tmpfile_path, dst_path) return file