From: Brian Warner Date: Wed, 25 Feb 2009 05:21:36 +0000 (-0700) Subject: scripts/common: fix alias handling on windows again, emit slightly nicer error messag... X-Git-Url: https://git.rkrishnan.org/maximilian?a=commitdiff_plain;h=7f8708cb7c10e104b490a3d068bcb35ee54c5a50;p=tahoe-lafs%2Ftahoe-lafs.git scripts/common: fix alias handling on windows again, emit slightly nicer error message in response to an unknown alias --- diff --git a/src/allmydata/scripts/common.py b/src/allmydata/scripts/common.py index c06f7a10..de30bc60 100644 --- a/src/allmydata/scripts/common.py +++ b/src/allmydata/scripts/common.py @@ -124,6 +124,9 @@ def platform_uses_lettercolon_drivename(): return True return False +class UnknownAliasError(Exception): + pass + def get_alias(aliases, path, default): # transform "work:path/filename" into (aliases["work"], "path/filename"). # If default=None, then an empty alias is indicated by returning @@ -143,7 +146,7 @@ def get_alias(aliases, path, default): if default == None: return DefaultAliasMarker, path return aliases[default], path - if colon == 1 and platform_uses_lettercolon_drivename(): + if colon == 1 and default == None and platform_uses_lettercolon_drivename(): # treat C:\why\must\windows\be\so\weird as a local path, not a tahoe # file in the "C:" alias return DefaultAliasMarker, path @@ -154,6 +157,8 @@ def get_alias(aliases, path, default): if default == None: return DefaultAliasMarker, path return aliases[default], path + if alias not in aliases: + raise UnknownAliasError("Unknown alias '%s', please create it with 'tahoe add-alias' or 'tahoe create-alias'." % alias) return aliases[alias], path[colon+1:] def escape_path(path): diff --git a/src/allmydata/test/test_cli.py b/src/allmydata/test/test_cli.py index 7d14505f..5330a7b7 100644 --- a/src/allmydata/test/test_cli.py +++ b/src/allmydata/test/test_cli.py @@ -281,6 +281,7 @@ class CLI(unittest.TestCase): "c": "CA"} def ga1(path): return get_alias(aliases, path, "tahoe") + uses_lettercolon = common.platform_uses_lettercolon_drivename() self.failUnlessEqual(ga1("bare"), ("TA", "bare")) self.failUnlessEqual(ga1("baredir/file"), ("TA", "baredir/file")) self.failUnlessEqual(ga1("baredir/file:7"), ("TA", "baredir/file:7")) @@ -290,6 +291,8 @@ class CLI(unittest.TestCase): self.failUnlessEqual(ga1("work:"), ("WA", "")) self.failUnlessEqual(ga1("work:file"), ("WA", "file")) self.failUnlessEqual(ga1("work:dir/file"), ("WA", "dir/file")) + # default != None means we really expect a tahoe path, regardless of + # whether we're on windows or not. This is what 'tahoe get' uses. self.failUnlessEqual(ga1("c:"), ("CA", "")) self.failUnlessEqual(ga1("c:file"), ("CA", "file")) self.failUnlessEqual(ga1("c:dir/file"), ("CA", "dir/file")) @@ -297,6 +300,10 @@ class CLI(unittest.TestCase): self.failUnlessEqual(ga1("URI:stuff:./file"), ("URI:stuff", "file")) self.failUnlessEqual(ga1("URI:stuff:./dir/file"), ("URI:stuff", "dir/file")) + self.failUnlessRaises(common.UnknownAliasError, ga1, "missing:") + self.failUnlessRaises(common.UnknownAliasError, ga1, "missing:dir") + self.failUnlessRaises(common.UnknownAliasError, ga1, "missing:dir/file") + def ga2(path): return get_alias(aliases, path, None) self.failUnlessEqual(ga2("bare"), (DefaultAliasMarker, "bare")) @@ -309,15 +316,26 @@ class CLI(unittest.TestCase): self.failUnlessEqual(ga2("tahoe:"), ("TA", "")) self.failUnlessEqual(ga2("tahoe:file"), ("TA", "file")) self.failUnlessEqual(ga2("tahoe:dir/file"), ("TA", "dir/file")) - self.failUnlessEqual(ga1("c:"), ("CA", "")) - self.failUnlessEqual(ga1("c:file"), ("CA", "file")) - self.failUnlessEqual(ga1("c:dir/file"), ("CA", "dir/file")) + # on windows, we really want c:foo to indicate a local file. + # default==None is what 'tahoe cp' uses. + if uses_lettercolon: + self.failUnlessEqual(ga2("c:"), (DefaultAliasMarker, "c:")) + self.failUnlessEqual(ga2("c:file"), (DefaultAliasMarker, "c:file")) + self.failUnlessEqual(ga2("c:dir/file"), + (DefaultAliasMarker, "c:dir/file")) + else: + self.failUnlessEqual(ga2("c:"), ("CA", "")) + self.failUnlessEqual(ga2("c:file"), ("CA", "file")) + self.failUnlessEqual(ga2("c:dir/file"), ("CA", "dir/file")) self.failUnlessEqual(ga2("work:"), ("WA", "")) self.failUnlessEqual(ga2("work:file"), ("WA", "file")) self.failUnlessEqual(ga2("work:dir/file"), ("WA", "dir/file")) self.failUnlessEqual(ga2("URI:stuff"), ("URI:stuff", "")) self.failUnlessEqual(ga2("URI:stuff:./file"), ("URI:stuff", "file")) self.failUnlessEqual(ga2("URI:stuff:./dir/file"), ("URI:stuff", "dir/file")) + self.failUnlessRaises(common.UnknownAliasError, ga2, "missing:") + self.failUnlessRaises(common.UnknownAliasError, ga2, "missing:dir") + self.failUnlessRaises(common.UnknownAliasError, ga2, "missing:dir/file") def ga3(path): old = common.pretend_platform_uses_lettercolon @@ -347,6 +365,9 @@ class CLI(unittest.TestCase): self.failUnlessEqual(ga3("URI:stuff"), ("URI:stuff", "")) self.failUnlessEqual(ga3("URI:stuff:./file"), ("URI:stuff", "file")) self.failUnlessEqual(ga3("URI:stuff:./dir/file"), ("URI:stuff", "dir/file")) + self.failUnlessRaises(common.UnknownAliasError, ga3, "missing:") + self.failUnlessRaises(common.UnknownAliasError, ga3, "missing:dir") + self.failUnlessRaises(common.UnknownAliasError, ga3, "missing:dir/file") class Help(unittest.TestCase):