Add tests for smae file check.

This commit is contained in:
Bastian Kleineidam 2012-05-11 20:16:24 +02:00
parent 96be0020ea
commit 8d8da1fecf
2 changed files with 43 additions and 4 deletions

View File

@ -260,14 +260,14 @@ def guess_mime_file_text (file_prog, filename):
return None
def check_existing_filename (filename):
def check_existing_filename (filename, onlyfiles=True):
"""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):
raise PatoolError("file `%s' was not found" % filename)
if not os.access(filename, os.R_OK):
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):
@ -281,7 +281,7 @@ def check_archive_filelist (filenames):
if not filenames:
raise PatoolError("cannot create archive with empty filelist")
for filename in filenames:
check_existing_filename(filename)
check_existing_filename(filename, onlyfiles=False)
def set_mode (filename, flags):
@ -405,6 +405,8 @@ def strlist_with_or (alist):
def is_same_file (filename1, filename2):
"""Check if filename1 and filename2 point to the same file object."""
if filename1 == filename2:
return True
if os.name == 'posix':
return os.path.samefile(filename1, filename2)
return os.path.realpath(filename1) == os.path.realpath(filename2)

37
tests/test_util.py Normal file
View File

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