From 8d8da1fecfe8af978d710830573dcbec95349802 Mon Sep 17 00:00:00 2001 From: Bastian Kleineidam Date: Fri, 11 May 2012 20:16:24 +0200 Subject: [PATCH] Add tests for smae file check. --- patoolib/util.py | 10 ++++++---- tests/test_util.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 tests/test_util.py diff --git a/patoolib/util.py b/patoolib/util.py index 6776747..292e8e6 100644 --- a/patoolib/util.py +++ b/patoolib/util.py @@ -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) diff --git a/tests/test_util.py b/tests/test_util.py new file mode 100644 index 0000000..94c4a3b --- /dev/null +++ b/tests/test_util.py @@ -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 . +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))