diff --git a/cwltool/pathmapper.py b/cwltool/pathmapper.py index e5f75f1e2..0f9cdcf08 100644 --- a/cwltool/pathmapper.py +++ b/cwltool/pathmapper.py @@ -69,18 +69,25 @@ def addLocation(d): "Anonymous directory object must have 'listing' and 'basename' fields.") d["location"] = "_:" + Text(uuid.uuid4()) if "basename" not in d: - d["basename"] = Text(uuid.uuid4()) + d["basename"] = d["location"][2:] + + parse = urllib.parse.urlparse(d["location"]) + path = parse.path + # strip trailing slash + if path.endswith("/"): + if d["class"] != "Directory": + raise validate.ValidationException( + "location '%s' ends with '/' but is not a Directory" % d["location"]) + path = path.rstrip("/") + d["location"] = urllib.parse.urlunparse((parse.scheme, parse.netloc, path, parse.params, parse.query, parse.fragment)) if "basename" not in d: - parse = urllib.parse.urlparse(d["location"]) - d["basename"] = os.path.basename(urllib.request.url2pathname(parse.path)) + d["basename"] = os.path.basename(urllib.request.url2pathname(path)) adjustFileObjs(job, addLocation) adjustDirObjs(job, addLocation) - - def dedup(listing): # type: (List[Any]) -> List[Any] marksub = set() diff --git a/tests/test_pathmapper.py b/tests/test_pathmapper.py index a5302140a..6ffd1fdac 100644 --- a/tests/test_pathmapper.py +++ b/tests/test_pathmapper.py @@ -1,6 +1,6 @@ import unittest -from cwltool.pathmapper import PathMapper +from cwltool.pathmapper import PathMapper, normalizeFilesDirs class TestPathMapper(unittest.TestCase): @@ -12,3 +12,17 @@ def __init__(self, referenced_files, basedir, stagedir, new): a = SubPathMapper([], '', '', "new") self.assertTrue(a.new, "new") + + def test_strip_trailing(self): + d = { + "class": "Directory", + "location": "/foo/bar/" + } + normalizeFilesDirs(d) + self.assertEqual( + { + "class": "Directory", + "location": "/foo/bar", + "basename": "bar" + }, + d)