-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed regression that would crash trash-restore on non parseable tras…
…hinfo, with an error like: "TypeError: not enough arguments for format string" The regression was introduced in commit 006774e (Improved error message when TrashInfo is not parsable in trash-restore Andrea Francia 04/05/23, 18:23)
- Loading branch information
1 parent
6c223a5
commit cc2dd9f
Showing
10 changed files
with
146 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 79 additions & 18 deletions
97
tests/test_restore/components/trashed_files/test_trashed_files.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,95 @@ | ||
import datetime | ||
import unittest | ||
|
||
from mock import Mock | ||
from six import StringIO | ||
|
||
from trashcli.restore.file_system import FakeFileReader | ||
from trashcli.restore.info_dir_searcher import InfoDirSearcher, FileFound | ||
from tests.support.put.fake_fs.fake_fs import FakeFs | ||
from tests.test_restore.support.fake_logger import FakeLogger | ||
from tests.test_restore.support.restore_fake_fs import RestoreFakeFs | ||
from trashcli.restore.info_dir_searcher import InfoDirSearcher | ||
from trashcli.restore.info_files import InfoFiles | ||
from trashcli.restore.trash_directories import TrashDirectories | ||
from trashcli.restore.trashed_file import TrashedFile | ||
from trashcli.restore.trashed_files import TrashedFiles | ||
|
||
|
||
class TestTrashedFiles(unittest.TestCase): | ||
def setUp(self): | ||
self.file_reader = FakeFileReader() | ||
self.logger = Mock(spec=[]) | ||
self.searcher = Mock(spec=InfoDirSearcher) | ||
self.fs = FakeFs() | ||
self.out = StringIO() | ||
self.logger = FakeLogger(self.out) | ||
|
||
class FakeTrashDirectories(TrashDirectories): | ||
def list_trash_dirs(self, trash_dir_from_cli): | ||
return [('/trash-dir', '/')] | ||
|
||
self.searcher = InfoDirSearcher(FakeTrashDirectories(), | ||
InfoFiles(RestoreFakeFs(self.fs))) | ||
self.trashed_files = TrashedFiles(self.logger, | ||
self.file_reader, | ||
RestoreFakeFs(self.fs), | ||
self.searcher) | ||
self.fs.mkdir_p("/trash-dir/info") | ||
|
||
def test(self): | ||
self.searcher.all_file_in_info_dir.return_value = [ | ||
FileFound('trashinfo', 'info/info_path.trashinfo', '/volume') | ||
] | ||
self.file_reader.set_content( | ||
'Path=name\nDeletionDate=2001-01-01T10:10:10') | ||
self.fs.write_file('/trash-dir/info/info_path.trashinfo', | ||
'Path=name\n' | ||
'DeletionDate=2001-01-01T10:10:10') | ||
|
||
trashed_files = list(self.trashed_files.all_trashed_files(None)) | ||
|
||
assert { | ||
'trashed_files': trashed_files, | ||
'out': self.out.getvalue()} == { | ||
'trashed_files': [ | ||
TrashedFile('/name', | ||
datetime.datetime(2001, 1, 1, | ||
10, 10, | ||
10), | ||
'/trash-dir/info/info_path.trashinfo', | ||
'/trash-dir/files/info_path'), | ||
], | ||
'out': '' | ||
} | ||
|
||
def test_on_non_trashinfo(self): | ||
self.fs.touch('/trash-dir/info/info_path.non-trashinfo') | ||
|
||
trashed_files = list(self.trashed_files.all_trashed_files(None)) | ||
|
||
assert { | ||
'trashed_files': trashed_files, | ||
'out': self.out.getvalue()} == { | ||
'trashed_files': [], | ||
'out': 'WARN: Non .trashinfo file in info dir\n' | ||
} | ||
|
||
def test_on_non_parsable_trashinfo(self): | ||
self.fs.write_file('/trash-dir/info/info_path.trashinfo', | ||
'') | ||
|
||
trashed_files = list(self.trashed_files.all_trashed_files(None)) | ||
|
||
assert { | ||
'trashed_files': trashed_files, | ||
'out': self.out.getvalue()} == { | ||
'trashed_files': [], | ||
'out': 'WARN: Non parsable trashinfo file: ' | ||
'/trash-dir/info/info_path.trashinfo, because ' | ||
'Unable to parse Path\n' | ||
} | ||
|
||
def test_on_io_error(self): | ||
self.fs.mkdir_p('/trash-dir/info/info_path.trashinfo') | ||
|
||
trashed_files = list(self.trashed_files.all_trashed_files(None)) | ||
|
||
trashed_file = trashed_files[0] | ||
assert '/volume/name' == trashed_file.original_location | ||
assert (datetime.datetime(2001, 1, 1, 10, 10, 10) == | ||
trashed_file.deletion_date) | ||
assert 'info/info_path.trashinfo' == trashed_file.info_file | ||
assert 'files/info_path' == trashed_file.original_file | ||
assert { | ||
'trashed_files': trashed_files, | ||
'out': self.out.getvalue() | ||
} == { | ||
'trashed_files': [], | ||
'out': "WARN: IOErrorReadingTrashInfo(" | ||
"path='/trash-dir/info/info_path.trashinfo', " | ||
"error='Unable to read: " | ||
"/trash-dir/info/info_path.trashinfo')\n" | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from __future__ import print_function | ||
from trashcli.restore.trashed_files import RestoreLogger | ||
|
||
|
||
class FakeLogger(RestoreLogger): | ||
def __init__(self, out): | ||
self.out = out | ||
|
||
def warning(self, message): | ||
print("WARN: %s" % message, file=self.out) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import os | ||
|
||
from typing import Iterable | ||
|
||
from trashcli.restore.file_system import FileReader | ||
from trashcli.restore.file_system import ListingFileSystem | ||
|
||
|
||
class RestoreFakeFs(FileReader, ListingFileSystem): | ||
def __init__(self, | ||
fs, # type FakeFs | ||
): | ||
self.fs = fs | ||
|
||
def contents_of(self, path): | ||
return self.fs.read(path) | ||
|
||
def list_files_in_dir(self, path): # type: (str) -> Iterable[str] | ||
for entry in self.fs.listdir(path): | ||
result = os.path.join(path, entry) | ||
yield result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from logging import Logger | ||
|
||
from trashcli.restore.restore_logger import RestoreLogger | ||
|
||
|
||
class RealRestoreLogger(RestoreLogger): | ||
def __init__(self, | ||
logger, # type: Logger | ||
): | ||
self._logger = logger | ||
|
||
def warning(self, message): | ||
self._logger.warning(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from trashcli.compat import Protocol | ||
|
||
|
||
class RestoreLogger(Protocol): | ||
def warning(self, message): | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters