-import os.path, simplejson, shutil
+import os.path, simplejson
from twisted.trial import unittest
from twisted.python import usage
from twisted.internet import defer
from allmydata.util import fileutil
from allmydata.util.encodingutil import (quote_output, get_io_encoding,
unicode_to_output, to_str)
+from allmydata.util.assertutil import _assert
from .no_network import GridTestMixin
from .test_cli import CLITestMixin
# these test cases come from ticket #2329 comment 40
# trailing slash on target *directory* should not matter, test both
-# trailing slash on files should cause error
+# trailing slash on target files should cause error
+# trailing slash on source directory should not matter, test a few
+# trailing slash on source files should cause error
COPYOUT_TESTCASES = """
cp $FILECAP to/existing-file : to/existing-file
cp -r $FILECAP $DIRCAP to/existing-file/ : E7-BADSLASH
# single source to a (present) target directory
-cp $FILECAP to : E2-DESTNAME
-cp -r $FILECAP to : E2-DESTNAME
-cp $DIRCAP/file to : to/file
-cp -r $DIRCAP/file to : to/file
-cp $PARENTCAP/dir to : E4-NEED-R
-cp -r $PARENTCAP/dir to : to/dir/file
-cp $DIRCAP to : E4-NEED-R
-cp -r $DIRCAP to : to/file
-cp $DIRALIAS to : E4-NEED-R
-cp -r $DIRALIAS to : to/file
+cp $FILECAP to : E2-DESTNAME
+cp -r $FILECAP to : E2-DESTNAME
+cp $DIRCAP/file to : to/file
+cp -r $DIRCAP/file to : to/file
+# these two are errors
+cp $DIRCAP/file/ to : E8-BADSLASH
+cp -r $DIRCAP/file/ to : E8-BADSLASH
+cp $PARENTCAP/dir to : E4-NEED-R
+cp -r $PARENTCAP/dir to : to/dir/file
+# but these two should ignore the trailing source slash
+cp $PARENTCAP/dir/ to : E4-NEED-R
+cp -r $PARENTCAP/dir/ to : to/dir/file
+cp $DIRCAP to : E4-NEED-R
+cp -r $DIRCAP to : to/file
+cp $DIRALIAS to : E4-NEED-R
+cp -r $DIRALIAS to : to/file
cp $FILECAP to/ : E2-DESTNAME
cp -r $FILECAP to/ : E2-DESTNAME
cp $DIRALIAS to/missing/ : E4-NEED-R
cp -r $DIRALIAS to/missing/ : to/missing/file
-# multiple files to a missing target: should mkdir
-cp $DIRCAP/file $PARENTCAP/dir2/file2 to/missing : to/missing/file,to/missing/file2
-cp -r $DIRCAP/file $PARENTCAP/dir2/file2 to/missing : to/missing/file,to/missing/file2
-
-cp $DIRCAP/file $PARENTCAP/dir2/file2 to/missing/ : to/missing/file,to/missing/file2
-cp -r $DIRCAP/file $PARENTCAP/dir2/file2 to/missing/ : to/missing/file,to/missing/file2
-
# multiple things to a missing target: should mkdir
cp $DIRCAP/file $PARENTCAP/dir2/file2 to/missing : to/missing/file,to/missing/file2
+cp -r $DIRCAP/file $PARENTCAP/dir2/file2 to/missing : to/missing/file,to/missing/file2
cp $DIRCAP/file $FILECAP to/missing : E2-DESTNAME
+cp -r $DIRCAP/file $FILECAP to/missing : E2-DESTNAME
cp $DIRCAP $FILECAP to/missing : E4-NEED-R
cp -r $DIRCAP $FILECAP to/missing : E2-DESTNAME
# namedfile, unnameddir, nameddir
cp -r $PARENTCAP/dir3/file3 $DIRCAP $PARENTCAP/dir2 $FILECAP to/missing : E2-DESTNAME
cp $DIRCAP/file $PARENTCAP/dir2/file2 to/missing/ : to/missing/file,to/missing/file2
+cp -r $DIRCAP/file $PARENTCAP/dir2/file2 to/missing/ : to/missing/file,to/missing/file2
cp $DIRCAP/file $FILECAP to/missing/ : E2-DESTNAME
+cp -r $DIRCAP/file $FILECAP to/missing/ : E2-DESTNAME
cp $DIRCAP $FILECAP to/missing/ : E4-NEED-R
cp -r $DIRCAP $FILECAP to/missing/ : E2-DESTNAME
# namedfile, unnameddir, nameddir
cp -r $PARENTCAP/dir4 to : to/dir4/emptydir/
cp -r $PARENTCAP/dir4 to/ : to/dir4/emptydir/
-# name collisions: ensure files are copied in order
-cp -r $PARENTCAP/dir6/dir $PARENTCAP/dir5/dir to : to/dir/collide=5
-cp -r $PARENTCAP/dir5/dir $PARENTCAP/dir6/dir to : to/dir/collide=6
-cp -r $DIRCAP6 $DIRCAP5 to : to/dir/collide=5
-cp -r $DIRCAP5 $DIRCAP6 to : to/dir/collide=6
+# name collisions should cause errors, not overwrites
+cp -r $PARENTCAP/dir6/dir $PARENTCAP/dir5/dir to : E9-COLLIDING-TARGETS
+cp -r $PARENTCAP/dir5/dir $PARENTCAP/dir6/dir to : E9-COLLIDING-TARGETS
+cp -r $DIRCAP6 $DIRCAP5 to : E9-COLLIDING-TARGETS
+cp -r $DIRCAP5 $DIRCAP6 to : E9-COLLIDING-TARGETS
"""
here = "/".join(dirpath.split(os.sep)[len(top.split(os.sep))-1:])
results.add(here+"/")
for fn in filenames:
- f = open(os.path.join(dirpath, fn), "rb")
- contents = f.read()
- f.close()
+ contents = fileutil.read(os.path.join(dirpath, fn))
if contents == self.FILE_CONTENTS:
results.add("%s/%s" % (here, fn))
elif contents == self.FILE_CONTENTS_5:
.replace("$FILECAP", self.FILECAP)
.split())
target = cmd[-1]
+ _assert(target == "to" or target.startswith("to/"), target)
cmd[-1] = os.path.abspath(os.path.join(self.basedir, cmd[-1]))
# reset
targetdir = os.path.abspath(os.path.join(self.basedir, "to"))
- if os.path.exists(targetdir):
- shutil.rmtree(targetdir)
+ fileutil.rm_dir(targetdir)
os.mkdir(targetdir)
if target.rstrip("/") == "to/existing-file":
return set(["E6-MANYONE"])
if err == "target is not a directory, but ends with a slash":
return set(["E7-BADSLASH"])
+ if err == "cannot copy multiple files with the same name into the same target directory":
+ return set(["E9-COLLIDING-TARGETS"])
+ if (err.startswith("source ") and
+ "is not a directory, but ends with a slash" in err):
+ return set(["E8-BADSLASH"])
self.fail("unrecognized error ('%s') %s" % (case, res))
d.addCallback(_check)
return d
- def do_one_test(self, case, expected):
- expected = expected.copy()
+ def do_one_test(self, case, orig_expected):
+ expected = set(orig_expected)
printable_expected = ",".join(sorted(expected))
#print "---", case, ":", printable_expected
- for f in list(expected):
+ for f in orig_expected:
# f is "dir/file" or "dir/sub/file" or "dir/" or "dir/sub/"
# we want all parent directories in the set, with trailing /
pieces = f.rstrip("/").split("/")
continue
case, expected = line.split(":")
case = case.strip()
- expected = set(expected.strip().split(","))
+ expected = frozenset(expected.strip().split(","))
d.addCallback(lambda ign, case=case, expected=expected:
self.do_one_test(case, expected))