Merge lp:~mwhudson/lava-test/help-command-subcommand into lp:lava-test/0.0

Proposed by Michael Hudson-Doyle
Status: Merged
Merged at revision: 46
Proposed branch: lp:~mwhudson/lava-test/help-command-subcommand
Merge into: lp:lava-test/0.0
Diff against target: 887 lines (+333/-249)
10 files modified
abrek/builtins.py (+17/-9)
abrek/command.py (+55/-22)
abrek/dashboard.py (+82/-84)
abrek/main.py (+1/-8)
abrek/results.py (+81/-84)
tests/test_abrekcmd.py (+45/-20)
tests/test_builtins.py (+28/-0)
tests/test_dashboard.py (+8/-8)
tests/test_main.py (+4/-2)
tests/test_results.py (+12/-12)
To merge this branch: bzr merge lp:~mwhudson/lava-test/help-command-subcommand
Reviewer Review Type Date Requested Status
Paul Larson (community) Approve
Review via email: mp+38176@code.launchpad.net

Description of the change

Hi Paul,

I wanted to fix the bug I reported about 'abrek help dashboard put' not being useful and ended up rewriting how subcommands work to a moderate extent :-) Hope that's OK. The changes are I hope reasonably clear and I think I added tests for everything.

Cheers,
mwh

To post a comment you must log in.
Revision history for this message
Paul Larson (pwlars) wrote :

I like it, much nicer. Thanks a lot Michael!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'abrek/builtins.py'
--- abrek/builtins.py 2010-10-01 14:45:24 +0000
+++ abrek/builtins.py 2010-10-12 02:45:55 +0000
@@ -13,7 +13,6 @@
13# You should have received a copy of the GNU General Public License13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.14# along with this program. If not, see <http://www.gnu.org/licenses/>.
1515
16import json
17import os16import os
18import sys17import sys
19from optparse import make_option18from optparse import make_option
@@ -38,21 +37,30 @@
38 If the command name is ommited, calling the help command will return a37 If the command name is ommited, calling the help command will return a
39 list of valid commands.38 list of valid commands.
40 """39 """
41 arglist = ['command']40 arglist = ['command', 'subcommand']
42 def run(self):41 def run(self):
43 if len(self.args) != 1:42 if len(self.args) < 1:
44 print "Available commands:"43 print "Available commands:"
45 for cmd in abrek.command.get_all_cmds():44 for cmd in abrek.command.get_all_cmds():
46 print " %s" % cmd45 print " %s" % cmd
47 print46 print
48 print "To access extended help on a command use 'abrek help " \47 print "To access extended help on a command use 'abrek help " \
49 "[command]'"48 "[command]'"
50 else:49 return
51 cmd = abrek.command.get_command(self.args[0])50 command_name = self.args.pop(0)
52 if cmd:51 cmd = abrek.command.get_command(command_name)
53 print cmd.help()52 if not cmd:
54 else:53 print "No command found for '%s'" % command_name
55 print "No command found for '%s'" % self.args[0]54 return
55 while self.args:
56 subcommand_name = self.args.pop(0)
57 cmd = cmd.get_subcommand(subcommand_name)
58 if not cmd:
59 print "No sub-command of '%s' found for '%s'" % (
60 command_name, subcommand_name)
61 return
62 command_name += ' ' + subcommand_name
63 print cmd.help()
5664
5765
58class cmd_install(abrek.command.AbrekCmd):66class cmd_install(abrek.command.AbrekCmd):
5967
=== modified file 'abrek/command.py'
--- abrek/command.py 2010-09-03 14:49:59 +0000
+++ abrek/command.py 2010-10-12 02:45:55 +0000
@@ -53,27 +53,25 @@
53 """53 """
54 options = []54 options = []
55 arglist = []55 arglist = []
56 subcmds = {}
5756
58 def __init__(self):57 def __init__(self, name_prefix=''):
58 self._name_prefix = name_prefix
59 self.parser = _AbrekOptionParser(usage=self._usage(),59 self.parser = _AbrekOptionParser(usage=self._usage(),
60 epilog=self._desc())60 epilog=self._desc())
61 for opt in self.options:61 for opt in self.options:
62 self.parser.add_option(opt)62 self.parser.add_option(opt)
6363
64 def main(self, argv):64 def main(self, argv):
65 if len(argv) and argv[0] in self.subcmds.keys():65 (self.opts, self.args) = self.parser.parse_args(argv)
66 return self.subcmds[argv[0]].main(argv[1:])66 return self.run()
67 else:
68 (self.opts, self.args) = self.parser.parse_args(argv)
69 return self.run()
7067
71 def name(self):68 def name(self):
72 return _convert_command_name(self.__class__.__name__)69 return self._name_prefix + _convert_command_name(self.__class__.__name__)
7370
74 def run(self):71 def run(self):
75 raise NotImplementedError("%s: command defined but not implemented!" %72 raise NotImplementedError("%s: command defined but not implemented!" %
76 self.name())73 self.name())
74
77 def _usage(self):75 def _usage(self):
78 usagestr = "Usage: abrek %s" % self.name()76 usagestr = "Usage: abrek %s" % self.name()
79 for arg in self.arglist:77 for arg in self.arglist:
@@ -81,7 +79,6 @@
81 usagestr += " %s" % arg[1:].upper()79 usagestr += " %s" % arg[1:].upper()
82 else:80 else:
83 usagestr += " [%s]" % arg.upper()81 usagestr += " [%s]" % arg.upper()
84 usagestr += self._list_subcmds()
85 return usagestr82 return usagestr
8683
87 def _desc(self):84 def _desc(self):
@@ -93,29 +90,67 @@
93 description += docstr + "\n"90 description += docstr + "\n"
94 return description91 return description
9592
96 def _list_subcmds(self):
97 str = ""
98 if self.subcmds:
99 str = "\n\nSub-Commands:"
100 for cmd in self.subcmds.keys():
101 str += "\n " + cmd
102 return str
103
104 def help(self):93 def help(self):
105 #For some reason, format_help includes an extra \n94 #For some reason, format_help includes an extra \n
106 return self.parser.format_help()[:-1]95 return self.parser.format_help()[:-1]
10796
97 def get_subcommand(self, name):
98 return None
99
100
101class AbrekCmdWithSubcommands(AbrekCmd):
102
103 arglist = ['subcommand']
104
105 def main(self, argv):
106 if not argv:
107 print "Missing sub-command." + self._list_subcmds()
108 else:
109 subcmd = self.get_subcommand(argv[0])
110 if subcmd is None:
111 # This line might print the help and raise SystemExit if
112 # --help is passed or if an invalid option was passed.
113 opts, args = self.parser.parse_args(argv)
114 # If it didn't, complain.
115 print "'%s' not found as a sub-command of '%s'" % (
116 args[0], self.name()) + self._list_subcmds()
117 else:
118 return subcmd.main(argv[1:])
119
120 def get_subcommand(self, name):
121 subcmd_cls = getattr(self, 'cmd_' + name.replace('_', '-'), None)
122 if subcmd_cls is None:
123 return None
124 return subcmd_cls(self.name() + ' ')
125
126 def _usage(self):
127 usagestr = AbrekCmd._usage(self)
128 usagestr += self._list_subcmds()
129 return usagestr
130
131 def _list_subcmds(self):
132 subcmds = []
133 for attrname in self.__class__.__dict__.keys():
134 if attrname.startswith('cmd_'):
135 subcmds.append(_convert_command_name(attrname))
136 if not subcmds:
137 return ''
138 return "\n\nAvailable sub-commands:\n " + "\n ".join(subcmds)
139
140
108def _convert_command_name(cmd):141def _convert_command_name(cmd):
109 return cmd[4:].replace('_','-')142 return cmd[4:].replace('_','-')
110143
144
111def _find_commands(module):145def _find_commands(module):
112 cmds = {}146 cmds = {}
113 for name, func in module.__dict__.iteritems():147 for name, func in module.__dict__.iteritems():
114 if name.startswith("cmd_"):148 if name.startswith("cmd_"):
115 real_name = _convert_command_name(name)149 real_name = _convert_command_name(name)
116 cmds[real_name] = func150 cmds[real_name] = func()
117 return cmds151 return cmds
118152
153
119def get_all_cmds():154def get_all_cmds():
120 from abrek import builtins, dashboard, results155 from abrek import builtins, dashboard, results
121 cmds = _find_commands(builtins)156 cmds = _find_commands(builtins)
@@ -123,9 +158,7 @@
123 cmds.update(_find_commands(results))158 cmds.update(_find_commands(results))
124 return cmds159 return cmds
125160
161
126def get_command(cmd_name):162def get_command(cmd_name):
127 cmds = get_all_cmds()163 cmds = get_all_cmds()
128 try:164 return cmds.get(cmd_name)
129 return cmds[cmd_name]()
130 except KeyError:
131 return None
132165
=== modified file 'abrek/dashboard.py'
--- abrek/dashboard.py 2010-10-07 19:34:16 +0000
+++ abrek/dashboard.py 2010-10-12 02:45:55 +0000
@@ -24,7 +24,7 @@
24from getpass import getpass24from getpass import getpass
25from optparse import make_option25from optparse import make_option
2626
27from abrek.command import AbrekCmd27from abrek.command import AbrekCmd, AbrekCmdWithSubcommands
28from abrek.config import get_config28from abrek.config import get_config
29from abrek.testdef import testloader29from abrek.testdef import testloader
3030
@@ -92,82 +92,87 @@
92 self.dashboardconf.write(fd)92 self.dashboardconf.write(fd)
9393
9494
95class subcmd_dashboard_setup(AbrekCmd):95class cmd_dashboard(AbrekCmdWithSubcommands):
96 """96 """
97 Configure information needed to push results to the dashboard97 Connect to the Launch-control dashboard
98 """98 """
99 options = [make_option("-u", "--user", dest="user"),99
100 make_option("-p", "--password", dest="password")]100 class cmd_setup(AbrekCmd):
101 arglist = ["*server"]101 """
102102 Configure information needed to push results to the dashboard
103 def run(self):103 """
104 if len(self.args) != 1:104 options = [make_option("-u", "--user", dest="user"),
105 print "You must specify a server"105 make_option("-p", "--password", dest="password")]
106 sys.exit(1)106 arglist = ["*server"]
107 config = DashboardConfig()107
108 if self.opts.user:108 def run(self):
109 user = self.opts.user109 if len(self.args) != 1:
110 else:110 print "You must specify a server"
111 user = raw_input("Username: ")111 sys.exit(1)
112 if self.opts.password:112 config = DashboardConfig()
113 password = self.opts.password113 if self.opts.user:
114 else:114 user = self.opts.user
115 password = getpass()115 else:
116 config.host = self.args[0]116 user = raw_input("Username: ")
117 config.user = user117 if self.opts.password:
118 config.password = password118 password = self.opts.password
119 config.write()119 else:
120120 password = getpass()
121121 config.host = self.args[0]
122class subcmd_dashboard_put(AbrekCmd):122 config.user = user
123 """123 config.password = password
124 Push the results from a test to the server124 config.write()
125 The stream name must include slashes (e.g. /anonymous/foo/)125
126 """126
127 arglist = ["*stream", "*result"]127 class cmd_put(AbrekCmd):
128128 """
129 def run(self):129 Push the results from a test to the server
130 if len(self.args) != 2:130 The stream name must include slashes (e.g. /anonymous/foo/)
131 print "You must specify a stream and a result"131 """
132 sys.exit(1)132 arglist = ["*stream", "*result"]
133 stream_name = self.args[0]133
134 result_name = self.args[1]134 def run(self):
135 bundle = generate_bundle(result_name)135 if len(self.args) != 2:
136 db_config = DashboardConfig()136 print "You must specify a stream and a result"
137 hosturl = urllib.basejoin(db_config.host, "xml-rpc/")137 sys.exit(1)
138 try:138 stream_name = self.args[0]
139 server = xmlrpclib.Server(hosturl)139 result_name = self.args[1]
140 except IOError:140 bundle = generate_bundle(result_name)
141 print "Error connecting to server, please run 'abrek " \141 db_config = DashboardConfig()
142 "dashboard setup [host]'"142 hosturl = urllib.basejoin(db_config.host, "xml-rpc/")
143 sys.exit(1)143 try:
144 try:144 server = xmlrpclib.Server(hosturl)
145 result = server.put(json.dumps(bundle, indent=2), result_name,145 except IOError:
146 stream_name)146 print "Error connecting to server, please run 'abrek " \
147 print "Bundle successfully uploaded to id: %s" % result147 "dashboard setup [host]'"
148 except xmlrpclib.Fault as strerror:148 sys.exit(1)
149 print "Error uploading bundle: %s" % strerror.faultString149 try:
150 sys.exit(1)150 result = server.put(json.dumps(bundle, indent=2), result_name,
151 except socket.error as strerror:151 stream_name)
152 print "Unable to connect to host: %s" % strerror152 print "Bundle successfully uploaded to id: %s" % result
153 sys.exit(1)153 except xmlrpclib.Fault as strerror:
154 except xmlrpclib.ProtocolError as strerror:154 print "Error uploading bundle: %s" % strerror.faultString
155 print "Connection error: %s" % strerror155 sys.exit(1)
156 sys.exit(1)156 except socket.error as strerror:
157157 print "Unable to connect to host: %s" % strerror
158158 sys.exit(1)
159class subcmd_dashboard_bundle(AbrekCmd):159 except xmlrpclib.ProtocolError as strerror:
160 """160 print "Connection error: %s" % strerror
161 Print JSON output that can be imported into the dashboard161 sys.exit(1)
162 """162
163 arglist = ["*result"]163
164164 class cmd_bundle(AbrekCmd):
165 def run(self):165 """
166 if len(self.args) != 1:166 Print JSON output that can be imported into the dashboard
167 print "You must specify a result"167 """
168 sys.exit(1)168 arglist = ["*result"]
169 bundle = generate_bundle(self.args[0])169
170 print json.dumps(bundle, indent=2)170 def run(self):
171 if len(self.args) != 1:
172 print "You must specify a result"
173 sys.exit(1)
174 bundle = generate_bundle(self.args[0])
175 print json.dumps(bundle, indent=2)
171176
172177
173def generate_bundle(result):178def generate_bundle(result):
@@ -188,10 +193,3 @@
188 return testdata193 return testdata
189194
190195
191class cmd_dashboard(AbrekCmd):
192 """
193 Connect to the Launch-control dashboard
194 """
195 subcmds = {'bundle':subcmd_dashboard_bundle(),
196 'put':subcmd_dashboard_put(),
197 'setup':subcmd_dashboard_setup()}
198196
=== modified file 'abrek/main.py'
--- abrek/main.py 2010-09-15 20:32:29 +0000
+++ abrek/main.py 2010-10-12 02:45:55 +0000
@@ -13,12 +13,10 @@
13# You should have received a copy of the GNU General Public License13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.14# along with this program. If not, see <http://www.gnu.org/licenses/>.
1515
16import sys
17import abrek.command16import abrek.command
1817
1918
20def main(argv):19def main(argv):
21 origargv = argv
22 argv = argv[1:]20 argv = argv[1:]
23 if not argv:21 if not argv:
24 argv = ['help']22 argv = ['help']
@@ -27,9 +25,4 @@
27 if not cmd_func:25 if not cmd_func:
28 print "command '%s' not found" % cmd26 print "command '%s' not found" % cmd
29 return 127 return 1
30 main = cmd_func.main28 return cmd_func.main(argv)
31 try:
32 main(argv)
33 except NotImplementedError:
34 print >>sys.stderr, "Unknown usage '%s'" % " ".join(origargv)
35 print >>sys.stderr, "Use 'abrek help [cmd]' for help"
3629
=== modified file 'abrek/results.py'
--- abrek/results.py 2010-09-28 18:53:14 +0000
+++ abrek/results.py 2010-10-12 02:45:55 +0000
@@ -18,94 +18,91 @@
18import sys18import sys
19from optparse import make_option19from optparse import make_option
2020
21from abrek.command import AbrekCmd21from abrek.command import AbrekCmd, AbrekCmdWithSubcommands
22from abrek.config import get_config22from abrek.config import get_config
23from abrek.utils import read_file23from abrek.utils import read_file
2424
2525
26class subcmd_results_list(AbrekCmd):26class cmd_results(AbrekCmdWithSubcommands):
27 """
28 List results of previous runs
29 """
30 def run(self):
31 config = get_config()
32 print "Saved results:"
33 try:
34 for dir in os.listdir(config.resultsdir):
35 print dir
36 except OSError:
37 print "No results found"
38
39
40class subcmd_results_show(AbrekCmd):
41 """
42 Display the output from a previous test run
43 """
44 arglist = ['*result']
45 def run(self):
46 if len(self.args) != 1:
47 print "please specify the name of the result dir"
48 sys.exit(1)
49 config = get_config()
50 resultsdir = os.path.join(config.resultsdir, self.args[0])
51 testoutput = os.path.join(resultsdir, "testoutput.log")
52 if not os.path.exists(testoutput):
53 print "No result found for '%s'" % self.args[0]
54 sys.exit(1)
55 print(read_file(testoutput))
56
57
58class subcmd_results_remove(AbrekCmd):
59 """
60 Remove the results of a previous test run
61 """
62 arglist = ['*result']
63 options = [make_option('-f', '--force', action='store_true',
64 dest='force')]
65 def run(self):
66 if len(self.args) != 1:
67 print "please specify the name of the result dir"
68 sys.exit(1)
69 config = get_config()
70 resultsdir = os.path.join(config.resultsdir, self.args[0])
71 if not os.path.exists(resultsdir):
72 print "No result found for '%s'" % self.args[0]
73 sys.exit(1)
74 if not self.opts.force:
75 print "Delete result '%s' for good? [Y/N]" % self.args[0],
76 response = raw_input()
77 if response[0].upper() != 'Y':
78 sys.exit(0)
79 shutil.rmtree(resultsdir)
80
81
82class subcmd_results_rename(AbrekCmd):
83 """
84 Rename the results from a previous test run
85 """
86 arglist = ['*source', '*destination']
87
88 def run(self):
89 if len(self.args) != 2:
90 print "please specify the name of the result, and the new name"
91 sys.exit(1)
92 config = get_config()
93 srcdir = os.path.join(config.resultsdir, self.args[0])
94 destdir = os.path.join(config.resultsdir, self.args[1])
95 if not os.path.exists(srcdir):
96 print "Result directory not found"
97 sys.exit(1)
98 if os.path.exists(destdir):
99 print "Destination result name already exists"
100 sys.exit(1)
101 shutil.move(srcdir, destdir)
102
103
104class cmd_results(AbrekCmd):
105 """27 """
106 Operate on results of previous test runs stored locally28 Operate on results of previous test runs stored locally
107 """29 """
108 subcmds = {'list':subcmd_results_list(),30
109 'rm':subcmd_results_remove(),31 class cmd_list(AbrekCmd):
110 'rename':subcmd_results_rename(),32 """
111 'show':subcmd_results_show()}33 List results of previous runs
34 """
35 def run(self):
36 config = get_config()
37 print "Saved results:"
38 try:
39 for dir in os.listdir(config.resultsdir):
40 print dir
41 except OSError:
42 print "No results found"
43
44
45 class cmd_show(AbrekCmd):
46 """
47 Display the output from a previous test run
48 """
49 arglist = ['*result']
50 def run(self):
51 if len(self.args) != 1:
52 print "please specify the name of the result dir"
53 sys.exit(1)
54 config = get_config()
55 resultsdir = os.path.join(config.resultsdir, self.args[0])
56 testoutput = os.path.join(resultsdir, "testoutput.log")
57 if not os.path.exists(testoutput):
58 print "No result found for '%s'" % self.args[0]
59 sys.exit(1)
60 print(read_file(testoutput))
61
62
63 class cmd_remove(AbrekCmd):
64 """
65 Remove the results of a previous test run
66 """
67 arglist = ['*result']
68 options = [make_option('-f', '--force', action='store_true',
69 dest='force')]
70 def run(self):
71 if len(self.args) != 1:
72 print "please specify the name of the result dir"
73 sys.exit(1)
74 config = get_config()
75 resultsdir = os.path.join(config.resultsdir, self.args[0])
76 if not os.path.exists(resultsdir):
77 print "No result found for '%s'" % self.args[0]
78 sys.exit(1)
79 if not self.opts.force:
80 print "Delete result '%s' for good? [Y/N]" % self.args[0],
81 response = raw_input()
82 if response[0].upper() != 'Y':
83 sys.exit(0)
84 shutil.rmtree(resultsdir)
85
86
87 class cmd_rename(AbrekCmd):
88 """
89 Rename the results from a previous test run
90 """
91 arglist = ['*source', '*destination']
92
93 def run(self):
94 if len(self.args) != 2:
95 print "please specify the name of the result, and the new name"
96 sys.exit(1)
97 config = get_config()
98 srcdir = os.path.join(config.resultsdir, self.args[0])
99 destdir = os.path.join(config.resultsdir, self.args[1])
100 if not os.path.exists(srcdir):
101 print "Result directory not found"
102 sys.exit(1)
103 if os.path.exists(destdir):
104 print "Destination result name already exists"
105 sys.exit(1)
106 shutil.move(srcdir, destdir)
107
108
112109
=== modified file 'tests/test_abrekcmd.py'
--- tests/test_abrekcmd.py 2010-09-10 17:09:14 +0000
+++ tests/test_abrekcmd.py 2010-10-12 02:45:55 +0000
@@ -15,7 +15,12 @@
1515
16import unittest16import unittest
17from optparse import make_option17from optparse import make_option
18from abrek.command import AbrekCmd, get_command, get_all_cmds18from abrek.command import (
19 AbrekCmd,
20 AbrekCmdWithSubcommands,
21 get_command,
22 get_all_cmds,
23 )
1924
2025
21class testAbrekCmd(unittest.TestCase):26class testAbrekCmd(unittest.TestCase):
@@ -72,40 +77,60 @@
72 self.assertTrue(expected_str in cmd.help())77 self.assertTrue(expected_str in cmd.help())
7378
74 def test_subcmds(self):79 def test_subcmds(self):
75 expected_str = 'Sub-Commands:\n foo'80 expected_str = 'Available sub-commands:\n foo'
76 class subcmd_test(AbrekCmd):
77 pass
7881
79 class cmd_test_subcmds(AbrekCmd):82 class cmd_test_subcmds(AbrekCmdWithSubcommands):
80 subcmds = {'foo':subcmd_test()}83 """Help for test-subcmds."""
81 pass84 class cmd_foo(AbrekCmd):
85 pass
82 cmd = cmd_test_subcmds()86 cmd = cmd_test_subcmds()
83 self.assertTrue(expected_str in cmd.help())87 self.assertTrue(
88 expected_str in cmd.help()
89 and 'Help for test-subcmds.' in cmd.help())
8490
85 def test_subcmds_run(self):91 def test_subcmds_run(self):
86 expected_str = "subcmd test str"92 expected_str = "subcmd test str"
87 class subcmd_test(AbrekCmd):
88 def run(self):
89 return expected_str
9093
91 class cmd_test_subcmds(AbrekCmd):94 class cmd_test_subcmds(AbrekCmdWithSubcommands):
92 subcmds = {'foo':subcmd_test()}95 class cmd_foo(AbrekCmd):
93 pass96 def run(self):
97 return expected_str
94 cmd = cmd_test_subcmds()98 cmd = cmd_test_subcmds()
95 argv = ['foo']99 argv = ['foo']
96 self.assertEqual(expected_str, cmd.main(argv))100 self.assertEqual(expected_str, cmd.main(argv))
97101
102 def test_subcmds_name(self):
103 expected_str = "subcmd test str"
104
105 class cmd_test_subcmds(AbrekCmdWithSubcommands):
106 class cmd_foo(AbrekCmd):
107 def run(self):
108 return expected_str
109 cmd = cmd_test_subcmds().get_subcommand('foo')
110 self.assertEqual('test-subcmds foo', cmd.name())
111
112 def test_subcmds_help(self):
113 expected_str = "subcmd test str"
114
115 class cmd_test_subcmds(AbrekCmdWithSubcommands):
116 class cmd_foo(AbrekCmd):
117 """Help for foo."""
118 def run(self):
119 return expected_str
120 cmd = cmd_test_subcmds().get_subcommand('foo')
121 self.assertTrue(
122 'test-subcmds foo' in cmd.help()
123 and 'Help for foo.' in cmd.help())
124
98 def test_subcmd_strip_argv(self):125 def test_subcmd_strip_argv(self):
99 """126 """
100 Make sure that the argv list is stripped after calling the subcmd127 Make sure that the argv list is stripped after calling the subcmd
101 """128 """
102 class subcmd_test(AbrekCmd):
103 def main(self, argv):
104 return len(argv)
105129
106 class cmd_test_subcmds(AbrekCmd):130 class cmd_test_subcmds(AbrekCmdWithSubcommands):
107 subcmds = {'foo':subcmd_test()}131 class cmd_foo(AbrekCmd):
108 pass132 def main(self, argv):
133 return len(argv)
109 cmd = cmd_test_subcmds()134 cmd = cmd_test_subcmds()
110 argv = ['foo']135 argv = ['foo']
111 self.assertEqual(0, cmd.main(argv))136 self.assertEqual(0, cmd.main(argv))
112137
=== modified file 'tests/test_builtins.py'
--- tests/test_builtins.py 2010-09-10 17:09:14 +0000
+++ tests/test_builtins.py 2010-10-12 02:45:55 +0000
@@ -36,3 +36,31 @@
36 cmd = abrek.builtins.cmd_list_installed()36 cmd = abrek.builtins.cmd_list_installed()
37 cmd.run()37 cmd.run()
38 self.assertTrue(test_name in out.getvalue())38 self.assertTrue(test_name in out.getvalue())
39
40class TestHelp(TestCaseWithFixtures):
41
42 def test_command_help(self):
43 out = self.add_fixture(OutputImposter())
44 abrek.builtins.cmd_help().main(['results'])
45 self.assertEqual(
46 abrek.results.cmd_results().help() + '\n', out.getvalue())
47
48 def test_subcommand_help(self):
49 out = self.add_fixture(OutputImposter())
50 abrek.builtins.cmd_help().main(['results', 'list'])
51 self.assertEqual(
52 abrek.results.cmd_results().get_subcommand('list').help() + '\n',
53 out.getvalue())
54
55 def test_bad_command(self):
56 out = self.add_fixture(OutputImposter())
57 abrek.builtins.cmd_help().main(['foo'])
58 self.assertEqual(
59 "No command found for 'foo'\n", out.getvalue())
60
61 def test_bad_subcommand(self):
62 out = self.add_fixture(OutputImposter())
63 abrek.builtins.cmd_help().main(['results', 'foo'])
64 self.assertEqual(
65 "No sub-command of 'results' found for 'foo'\n",
66 out.getvalue())
3967
=== modified file 'tests/test_dashboard.py'
--- tests/test_dashboard.py 2010-10-07 17:11:55 +0000
+++ tests/test_dashboard.py 2010-10-12 02:45:55 +0000
@@ -16,10 +16,10 @@
16import json16import json
17import os17import os
18from uuid import uuid118from uuid import uuid1
19from abrek.dashboard import (DashboardConfig,19from abrek.dashboard import (
20 cmd_dashboard,20 DashboardConfig,
21 subcmd_dashboard_bundle,21 cmd_dashboard,
22 subcmd_dashboard_setup)22 )
23from imposters import ConfigImposter, OutputImposter23from imposters import ConfigImposter, OutputImposter
24from fixtures import TestCaseWithFixtures24from fixtures import TestCaseWithFixtures
2525
@@ -44,7 +44,7 @@
4444
45 def test_dashboard_setup_noserver(self):45 def test_dashboard_setup_noserver(self):
46 errmsg = "You must specify a server"46 errmsg = "You must specify a server"
47 cmd = subcmd_dashboard_setup()47 cmd = cmd_dashboard.cmd_setup()
48 self.assertRaises(SystemExit, cmd.main, argv=[])48 self.assertRaises(SystemExit, cmd.main, argv=[])
49 self.assertEqual(errmsg, self.out.getvalue().strip())49 self.assertEqual(errmsg, self.out.getvalue().strip())
5050
@@ -56,13 +56,13 @@
5656
57 def test_dashboard_bundle_badresult(self):57 def test_dashboard_bundle_badresult(self):
58 errmsg = "Result directory not found"58 errmsg = "Result directory not found"
59 cmd = subcmd_dashboard_bundle()59 cmd = cmd_dashboard.cmd_bundle()
60 self.assertRaises(SystemExit, cmd.main, argv=['badresult'])60 self.assertRaises(SystemExit, cmd.main, argv=['badresult'])
61 self.assertEqual(errmsg, self.out.getvalue().strip())61 self.assertEqual(errmsg, self.out.getvalue().strip())
6262
63 def test_dashboard_bundle_noresult(self):63 def test_dashboard_bundle_noresult(self):
64 errmsg = "You must specify a result"64 errmsg = "You must specify a result"
65 cmd = subcmd_dashboard_bundle()65 cmd = cmd_dashboard.cmd_bundle()
66 self.assertRaises(SystemExit, cmd.main, argv=[])66 self.assertRaises(SystemExit, cmd.main, argv=[])
67 self.assertEqual(errmsg, self.out.getvalue().strip())67 self.assertEqual(errmsg, self.out.getvalue().strip())
6868
@@ -73,7 +73,7 @@
73 self.out = self.add_fixture(OutputImposter())73 self.out = self.add_fixture(OutputImposter())
7474
75 def test_dashboard_bundle_good(self):75 def test_dashboard_bundle_good(self):
76 cmd = subcmd_dashboard_bundle()76 cmd = cmd_dashboard.cmd_bundle()
77 (testname, testuuid) = make_stream_result(self.config)77 (testname, testuuid) = make_stream_result(self.config)
78 expected_dict = {78 expected_dict = {
79 "test_runs": [{79 "test_runs": [{
8080
=== modified file 'tests/test_main.py'
--- tests/test_main.py 2010-09-18 04:07:37 +0000
+++ tests/test_main.py 2010-10-12 02:45:55 +0000
@@ -24,7 +24,9 @@
24 self.out = self.add_fixture(OutputImposter())24 self.out = self.add_fixture(OutputImposter())
2525
26 def test_bad_subcmd(self):26 def test_bad_subcmd(self):
27 errmsg = "Unknown usage './abrek results foo'\nUse 'abrek help [cmd]' for help\n"27 # Running a subcommand that does not exist of a command that does
28 # gives a nice error message.
29 errmsg = "'foo' not found as a sub-command of 'results'"
28 main(['./abrek', 'results', 'foo'])30 main(['./abrek', 'results', 'foo'])
29 self.assertEqual(errmsg, self.out.getvalue())31 self.assertEqual(errmsg, self.out.getvalue().splitlines()[0])
3032
3133
=== modified file 'tests/test_results.py'
--- tests/test_results.py 2010-09-10 17:09:14 +0000
+++ tests/test_results.py 2010-10-12 02:45:55 +0000
@@ -15,7 +15,7 @@
1515
16import os16import os
1717
18import abrek.results18from abrek.results import cmd_results
19from abrek.utils import write_file19from abrek.utils import write_file
20from imposters import ConfigImposter, OutputImposter20from imposters import ConfigImposter, OutputImposter
21from fixtures import TestCaseWithFixtures21from fixtures import TestCaseWithFixtures
@@ -30,13 +30,13 @@
30 def test_results_list(self):30 def test_results_list(self):
31 result_name = "test_results_list000"31 result_name = "test_results_list000"
32 os.makedirs(os.path.join(self.config.resultsdir, result_name))32 os.makedirs(os.path.join(self.config.resultsdir, result_name))
33 cmd = abrek.results.subcmd_results_list()33 cmd = cmd_results.cmd_list()
34 cmd.run()34 cmd.run()
35 self.assertTrue(result_name in self.out.getvalue())35 self.assertTrue(result_name in self.out.getvalue())
3636
37 def test_results_list_nodir(self):37 def test_results_list_nodir(self):
38 errmsg = "No results found"38 errmsg = "No results found"
39 cmd = abrek.results.subcmd_results_list()39 cmd = cmd_results.cmd_list()
40 cmd.run()40 cmd.run()
41 self.assertTrue(errmsg in self.out.getvalue())41 self.assertTrue(errmsg in self.out.getvalue())
4242
@@ -47,20 +47,20 @@
47 os.makedirs(result_dir)47 os.makedirs(result_dir)
48 outputfile = os.path.join(result_dir, 'testoutput.log')48 outputfile = os.path.join(result_dir, 'testoutput.log')
49 write_file(result_output, outputfile)49 write_file(result_output, outputfile)
50 cmd = abrek.results.subcmd_results_show()50 cmd = cmd_results.cmd_show()
51 cmd.main(argv=[result_name])51 cmd.main(argv=[result_name])
52 self.assertEqual(result_output, self.out.getvalue().strip())52 self.assertEqual(result_output, self.out.getvalue().strip())
5353
54 def test_results_show_noarg(self):54 def test_results_show_noarg(self):
55 errmsg = "please specify the name of the result dir"55 errmsg = "please specify the name of the result dir"
56 cmd = abrek.results.subcmd_results_show()56 cmd = cmd_results.cmd_show()
57 self.assertRaises(SystemExit, cmd.main, argv=[])57 self.assertRaises(SystemExit, cmd.main, argv=[])
58 self.assertEqual(errmsg, self.out.getvalue().strip())58 self.assertEqual(errmsg, self.out.getvalue().strip())
5959
60 def test_results_show_nodir(self):60 def test_results_show_nodir(self):
61 testname = "foo"61 testname = "foo"
62 errmsg = "No result found for '%s'" % testname62 errmsg = "No result found for '%s'" % testname
63 cmd = abrek.results.subcmd_results_show()63 cmd = cmd_results.cmd_show()
64 self.assertRaises(SystemExit, cmd.main, argv=[testname])64 self.assertRaises(SystemExit, cmd.main, argv=[testname])
65 self.assertEqual(errmsg, self.out.getvalue().strip())65 self.assertEqual(errmsg, self.out.getvalue().strip())
6666
@@ -68,20 +68,20 @@
68 result_name = "test_results_remove000"68 result_name = "test_results_remove000"
69 result_dir = os.path.join(self.config.resultsdir, result_name)69 result_dir = os.path.join(self.config.resultsdir, result_name)
70 os.makedirs(result_dir)70 os.makedirs(result_dir)
71 cmd = abrek.results.subcmd_results_remove()71 cmd = cmd_results.cmd_remove()
72 cmd.main(argv=[result_name, '-f'])72 cmd.main(argv=[result_name, '-f'])
73 self.assertFalse(os.path.exists(result_dir))73 self.assertFalse(os.path.exists(result_dir))
7474
75 def test_results_remove_noarg(self):75 def test_results_remove_noarg(self):
76 errmsg = "please specify the name of the result dir"76 errmsg = "please specify the name of the result dir"
77 cmd = abrek.results.subcmd_results_remove()77 cmd = cmd_results.cmd_remove()
78 self.assertRaises(SystemExit, cmd.main, argv=[])78 self.assertRaises(SystemExit, cmd.main, argv=[])
79 self.assertEqual(errmsg, self.out.getvalue().strip())79 self.assertEqual(errmsg, self.out.getvalue().strip())
8080
81 def test_results_remove_nodir(self):81 def test_results_remove_nodir(self):
82 testname = "foo"82 testname = "foo"
83 errmsg = "No result found for '%s'" % testname83 errmsg = "No result found for '%s'" % testname
84 cmd = abrek.results.subcmd_results_remove()84 cmd = cmd_results.cmd_remove()
85 self.assertRaises(SystemExit, cmd.main, argv=[testname])85 self.assertRaises(SystemExit, cmd.main, argv=[testname])
86 self.assertEqual(errmsg, self.out.getvalue().strip())86 self.assertEqual(errmsg, self.out.getvalue().strip())
8787
@@ -91,7 +91,7 @@
91 result_srcdir = os.path.join(self.config.resultsdir, result_src)91 result_srcdir = os.path.join(self.config.resultsdir, result_src)
92 result_destdir = os.path.join(self.config.resultsdir, result_dest)92 result_destdir = os.path.join(self.config.resultsdir, result_dest)
93 os.makedirs(result_srcdir)93 os.makedirs(result_srcdir)
94 cmd = abrek.results.subcmd_results_rename()94 cmd = cmd_results.cmd_rename()
95 cmd.main(argv=[result_src, result_dest])95 cmd.main(argv=[result_src, result_dest])
96 self.assertFalse(os.path.exists(result_srcdir))96 self.assertFalse(os.path.exists(result_srcdir))
97 self.assertTrue(os.path.exists(result_destdir))97 self.assertTrue(os.path.exists(result_destdir))
@@ -100,7 +100,7 @@
100 errmsg = "Result directory not found"100 errmsg = "Result directory not found"
101 result_src = "test_results_old"101 result_src = "test_results_old"
102 result_dest = "test_results_new"102 result_dest = "test_results_new"
103 cmd = abrek.results.subcmd_results_rename()103 cmd = cmd_results.cmd_rename()
104 self.assertRaises(SystemExit, cmd.main, argv=[result_src, result_dest])104 self.assertRaises(SystemExit, cmd.main, argv=[result_src, result_dest])
105 self.assertEqual(errmsg, self.out.getvalue().strip())105 self.assertEqual(errmsg, self.out.getvalue().strip())
106106
@@ -112,6 +112,6 @@
112 result_destdir = os.path.join(self.config.resultsdir, result_dest)112 result_destdir = os.path.join(self.config.resultsdir, result_dest)
113 os.makedirs(result_srcdir)113 os.makedirs(result_srcdir)
114 os.makedirs(result_destdir)114 os.makedirs(result_destdir)
115 cmd = abrek.results.subcmd_results_rename()115 cmd = cmd_results.cmd_rename()
116 self.assertRaises(SystemExit, cmd.main, argv=[result_src, result_dest])116 self.assertRaises(SystemExit, cmd.main, argv=[result_src, result_dest])
117 self.assertEqual(errmsg, self.out.getvalue().strip())117 self.assertEqual(errmsg, self.out.getvalue().strip())

Subscribers

People subscribed via source and target branches