[#149 state:fixed] Fixed crash on result saving.

This commit is contained in:
Virgil Dupras 2011-04-13 16:59:02 +02:00
parent f28ffc680a
commit 54ffcfab79
3 changed files with 32 additions and 2 deletions

View File

@ -8,10 +8,13 @@
import logging
import re
import os
import os.path as op
from xml.etree import ElementTree as ET
from . import engine
from jobprogress.job import nulljob
from hscommon.conflict import get_conflicted_name
from hscommon.markable import Markable
from hscommon.util import flatten, nonone, FileOrPath, format_size
from hscommon.trans import tr
@ -304,8 +307,22 @@ class Results(Markable):
match_elem.set('second', str(dupe2index[match.second]))
match_elem.set('percentage', str(int(match.percentage)))
tree = ET.ElementTree(root)
with FileOrPath(outfile, 'wb') as fp:
tree.write(fp, encoding='utf-8')
def do_write(outfile):
with FileOrPath(outfile, 'wb') as fp:
tree.write(fp, encoding='utf-8')
try:
do_write(outfile)
except IOError as e:
if e.errno == 21: # outfile is a directory
p = str(outfile)
dirname, basename = op.split(p)
otherfiles = os.listdir(dirname)
newname = get_conflicted_name(otherfiles, basename)
do_write(op.join(dirname, newname))
else:
raise
self.is_modified = False
def sort_dupes(self, key, asc=True, delta=False):

View File

@ -137,6 +137,7 @@ class TestCaseDupeGuru:
eq_(len(app.results.groups), 0)
def test_rename_when_nothing_is_selected(self):
# Issue #140
# It's possible that rename operation has its selected row swept off from under it, thus
# making the selected row None. Don't crash when it happens.
dgapp = DupeGuru()

View File

@ -77,6 +77,18 @@ class TestCaseResultsEmpty:
self.results.groups = []
assert not self.results.is_modified
def test_save_to_same_name_as_folder(self, tmpdir):
# Issue #149
# When saving to a filename that already exists, the file is overwritten. However, when
# the name exists but that it's a folder, then there used to be a crash. The proper fix
# would have been some kind of feedback to the user, but the work involved for something
# that simply never happens (I never received a report of this crash, I experienced it
# while fooling around) is too much. Instead, use standard name conflict resolution.
folderpath = tmpdir.join('foo')
folderpath.mkdir()
self.results.save_to_xml(str(folderpath)) # no crash
assert tmpdir.join('[000] foo').check()
class TestCaseResultsWithSomeGroups:
def setup_method(self, method):