Add tests for smae file check.
This commit is contained in:
parent
96be0020ea
commit
8d8da1fecf
|
@ -260,14 +260,14 @@ def guess_mime_file_text (file_prog, filename):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def check_existing_filename (filename):
|
def check_existing_filename (filename, onlyfiles=True):
|
||||||
"""Ensure that given filename is a valid, existing file."""
|
"""Ensure that given filename is a valid, existing file."""
|
||||||
if not os.path.isfile(filename):
|
|
||||||
raise PatoolError("`%s' is not a file" % filename)
|
|
||||||
if not os.path.exists(filename):
|
if not os.path.exists(filename):
|
||||||
raise PatoolError("file `%s' was not found" % filename)
|
raise PatoolError("file `%s' was not found" % filename)
|
||||||
if not os.access(filename, os.R_OK):
|
if not os.access(filename, os.R_OK):
|
||||||
raise PatoolError("file `%s' is not readable" % filename)
|
raise PatoolError("file `%s' is not readable" % filename)
|
||||||
|
if onlyfiles and not os.path.isfile(filename):
|
||||||
|
raise PatoolError("`%s' is not a file" % filename)
|
||||||
|
|
||||||
|
|
||||||
def check_new_filename (filename):
|
def check_new_filename (filename):
|
||||||
|
@ -281,7 +281,7 @@ def check_archive_filelist (filenames):
|
||||||
if not filenames:
|
if not filenames:
|
||||||
raise PatoolError("cannot create archive with empty filelist")
|
raise PatoolError("cannot create archive with empty filelist")
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
check_existing_filename(filename)
|
check_existing_filename(filename, onlyfiles=False)
|
||||||
|
|
||||||
|
|
||||||
def set_mode (filename, flags):
|
def set_mode (filename, flags):
|
||||||
|
@ -405,6 +405,8 @@ def strlist_with_or (alist):
|
||||||
|
|
||||||
def is_same_file (filename1, filename2):
|
def is_same_file (filename1, filename2):
|
||||||
"""Check if filename1 and filename2 point to the same file object."""
|
"""Check if filename1 and filename2 point to the same file object."""
|
||||||
|
if filename1 == filename2:
|
||||||
|
return True
|
||||||
if os.name == 'posix':
|
if os.name == 'posix':
|
||||||
return os.path.samefile(filename1, filename2)
|
return os.path.samefile(filename1, filename2)
|
||||||
return os.path.realpath(filename1) == os.path.realpath(filename2)
|
return os.path.realpath(filename1) == os.path.realpath(filename2)
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Copyright (C) 2010-2011 Bastian Kleineidam
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
import unittest
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
from patoolib import util
|
||||||
|
|
||||||
|
class UtilTest (unittest.TestCase):
|
||||||
|
|
||||||
|
def test_samefile1 (self):
|
||||||
|
filename1 = filename2 = __file__
|
||||||
|
self.assertTrue(util.is_same_file(filename1, filename2))
|
||||||
|
|
||||||
|
def test_samefile2 (self):
|
||||||
|
parentdir = os.path.dirname(__file__)
|
||||||
|
filename1 = os.path.dirname(parentdir)
|
||||||
|
filename2 = os.path.join(parentdir, '..')
|
||||||
|
self.assertTrue(util.is_same_file(filename1, filename2))
|
||||||
|
|
||||||
|
def test_samefile3 (self):
|
||||||
|
parentdir = os.path.dirname(__file__)
|
||||||
|
filename1 = os.path.dirname(parentdir)
|
||||||
|
filename2 = os.path.join(parentdir, '.')
|
||||||
|
self.assertFalse(util.is_same_file(filename1, filename2))
|
Loading…
Reference in New Issue