]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/test/test_cli.py
Remove -u shortcut for 'tahoe ls --uri' which clashes with --node-url. fixes ticket...
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / test / test_cli.py
index 3caf7687ff9fb33a20a3fa1267e0d5568ed92be6..789e177d15c0689453ec9b9354db2f826a86fd2d 100644 (file)
@@ -1,13 +1,14 @@
 
 import os.path
-from twisted.trial import unittest
 from cStringIO import StringIO
 import urllib, sys
 
-from mock import Mock, call
+from twisted.trial import unittest
+from twisted.python.monkey import MonkeyPatcher
 
 import allmydata
 from allmydata.util import fileutil, hashutil, base32, keyutil
+from allmydata.util.namespace import Namespace
 from allmydata import uri
 from allmydata.immutable import upload
 from allmydata.dirnode import normalize
@@ -35,7 +36,7 @@ from twisted.python import usage
 
 from allmydata.util.assertutil import precondition
 from allmydata.util.encodingutil import listdir_unicode, unicode_platform, \
-    get_io_encoding, get_filesystem_encoding
+    get_io_encoding, get_filesystem_encoding, unicode_to_argv
 
 timeout = 480 # deep_check takes 360s on Zandr's linksys box, others take > 240s
 
@@ -48,8 +49,14 @@ def parse_options(basedir, command, args):
 
 class CLITestMixin(ReallyEqualMixin):
     def do_cli(self, verb, *args, **kwargs):
+        precondition(not [True for arg in args if not isinstance(arg, str)],
+                     "arguments to do_cli must be strs -- convert using unicode_to_argv", args=args)
+
+        # client_num is used to execute client CLI commands on a specific client.
+        client_num = kwargs.get("client_num", 0)
+
         nodeargs = [
-            "--node-directory", self.get_clientdir(),
+            "--node-directory", unicode_to_argv(self.get_clientdir(i=client_num)),
             ]
         argv = nodeargs + [verb] + list(args)
         stdin = kwargs.get("stdin", "")
@@ -541,26 +548,38 @@ class CLI(CLITestMixin, unittest.TestCase):
     def test_exception_catcher(self):
         self.basedir = "cli/exception_catcher"
 
-        runner_mock = Mock()
-        sys_exit_mock = Mock()
         stderr = StringIO()
-        self.patch(sys, "argv", ["tahoe"])
-        self.patch(runner, "runner", runner_mock)
-        self.patch(sys, "exit", sys_exit_mock)
-        self.patch(sys, "stderr", stderr)
         exc = Exception("canary")
+        ns = Namespace()
 
+        ns.runner_called = False
         def call_runner(args, install_node_control=True):
+            ns.runner_called = True
+            self.failUnlessEqual(install_node_control, True)
             raise exc
-        runner_mock.side_effect = call_runner
 
-        runner.run()
-        self.failUnlessEqual(runner_mock.call_args_list, [call([], install_node_control=True)])
-        self.failUnlessEqual(sys_exit_mock.call_args_list, [call(1)])
+        ns.sys_exit_called = False
+        def call_sys_exit(exitcode):
+            ns.sys_exit_called = True
+            self.failUnlessEqual(exitcode, 1)
+
+        patcher = MonkeyPatcher((runner, 'runner', call_runner),
+                                (sys, 'argv', ["tahoe"]),
+                                (sys, 'exit', call_sys_exit),
+                                (sys, 'stderr', stderr))
+        patcher.runWithPatches(runner.run)
+
+        self.failUnless(ns.runner_called)
+        self.failUnless(ns.sys_exit_called)
         self.failUnlessIn(str(exc), stderr.getvalue())
 
 
 class Help(unittest.TestCase):
+    def failUnlessInNormalized(self, x, y):
+        # helper function to deal with the --help output being wrapped to
+        # various widths, depending on the $COLUMNS environment variable
+        self.failUnlessIn(x.replace("\n", " "), y.replace("\n", " "))
+
     def test_get(self):
         help = str(cli.GetOptions())
         self.failUnlessIn("[options] REMOTE_FILE LOCAL_FILE", help)
@@ -586,22 +605,22 @@ class Help(unittest.TestCase):
     def test_mv(self):
         help = str(cli.MvOptions())
         self.failUnlessIn("[options] FROM TO", help)
-        self.failUnlessIn("Use 'tahoe mv' to move files", help)
+        self.failUnlessInNormalized("Use 'tahoe mv' to move files", help)
 
     def test_cp(self):
         help = str(cli.CpOptions())
         self.failUnlessIn("[options] FROM.. TO", help)
-        self.failUnlessIn("Use 'tahoe cp' to copy files", help)
+        self.failUnlessInNormalized("Use 'tahoe cp' to copy files", help)
 
     def test_ln(self):
         help = str(cli.LnOptions())
         self.failUnlessIn("[options] FROM_LINK TO_LINK", help)
-        self.failUnlessIn("Use 'tahoe ln' to duplicate a link", help)
+        self.failUnlessInNormalized("Use 'tahoe ln' to duplicate a link", help)
 
     def test_mkdir(self):
         help = str(cli.MakeDirectoryOptions())
         self.failUnlessIn("[options] [REMOTE_DIR]", help)
-        self.failUnlessIn("Create a new directory", help)
+        self.failUnlessInNormalized("Create a new directory", help)
 
     def test_backup(self):
         help = str(cli.BackupOptions())
@@ -670,13 +689,13 @@ class Help(unittest.TestCase):
     def test_debug_trial(self):
         help = str(debug.TrialOptions())
         self.failUnlessIn(" [global-options] debug trial [options] [[file|package|module|TestCase|testmethod]...]", help)
-        self.failUnlessIn("The 'tahoe debug trial' command uses the correct imports", help)
+        self.failUnlessInNormalized("The 'tahoe debug trial' command uses the correct imports", help)
 
     def test_debug_flogtool(self):
         options = debug.FlogtoolOptions()
         help = str(options)
         self.failUnlessIn(" [global-options] debug flogtool ", help)
-        self.failUnlessIn("The 'tahoe debug flogtool' command uses the correct imports", help)
+        self.failUnlessInNormalized("The 'tahoe debug flogtool' command uses the correct imports", help)
 
         for (option, shortcut, oClass, desc) in options.subCommands:
             subhelp = str(oClass())
@@ -1223,6 +1242,19 @@ class Options(ReallyEqualMixin, unittest.TestCase):
         self.failUnlessEqual(o.aliases[DEFAULT_ALIAS], private_uri)
         self.failUnlessEqual(o.where, u"")
 
+        # -u for --node-url used to clash with -u for --uri (tickets #1949 and #2137).
+        o = parse2(["-u", "http://example.org:8111/"])
+        self.failUnlessEqual(o['node-url'], "http://example.org:8111/")
+        self.failUnlessEqual(o.aliases[DEFAULT_ALIAS], private_uri)
+        self.failUnlessEqual(o.where, u"")
+        self.failIf(o["uri"])
+
+        o = parse2(["-u", "http://example.org:8111/", "--uri"])
+        self.failUnlessEqual(o['node-url'], "http://example.org:8111/")
+        self.failUnlessEqual(o.aliases[DEFAULT_ALIAS], private_uri)
+        self.failUnlessEqual(o.where, u"")
+        self.failUnless(o["uri"])
+
         o = parse2(["--dir-cap", "root"])
         self.failUnlessEqual(o['node-url'], "http://localhost:8080/")
         self.failUnlessEqual(o.aliases[DEFAULT_ALIAS], "root")