Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Modularize todo.py to make it easier to test.

Add tests for the addons and usage decorator.
  • Loading branch information...
commit 6aa4f44e47083cd93a3cdc605c20bf54fc74bb2f 1 parent 56a53c7
@sigmavirus24 authored
Showing with 167 additions and 54 deletions.
  1. +78 −0 tests/test_addons.py
  2. +30 −0 tests/test_usage.py
  3. +59 −54 todo.py
View
78 tests/test_addons.py
@@ -0,0 +1,78 @@
+# TODO.TXT-CLI-python test script
+# Copyright (C) 2011-2012 Sigmavirus24
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# TLDR: This is licensed under the GPLv3. See LICENSE for more details.
+
+import os
+import todo
+import base
+import unittest
+
+class TestAddons(base.BaseTest):
+ def setUp(self):
+ super(TestAddons, self).setUp()
+ todo.CONFIG['TODO_DIR'] = todo._path('./todo/')
+ todo.CONFIG['TODO_ACTIONS_DIR'] = todo._path('./todo/actions')
+ if not os.path.exists(todo.CONFIG['TODO_DIR']):
+ os.mkdir(todo.CONFIG['TODO_DIR'])
+ if not os.path.exists(todo.CONFIG['TODO_ACTIONS_DIR']):
+ os.mkdir(todo.CONFIG['TODO_ACTIONS_DIR'])
+
+ def tearDown(self):
+ super(TestAddons, self).tearDown()
+ if os.path.exists(todo.CONFIG['TODO_ACTIONS_DIR']):
+ os.rmdir(todo.CONFIG['TODO_ACTIONS_DIR'])
+ if os.path.exists(todo.CONFIG['TODO_DIR']):
+ os.rmdir(todo.CONFIG['TODO_DIR'])
+
+ def test_noaddons(self):
+ bad_args = ['addp', 'foo']
+ good_args = ['add', 'foo']
+ self.assertEqual(todo.execute_commands(bad_args), 1)
+ self.assertEqual(todo.execute_commands(good_args), 0)
+
+ def test_py_addons(self):
+ lines = ['def addp():\n', '\tpass', '\n\n',
+ 'commands = {"addp": (False, addp)}\n']
+ filename = todo.CONFIG['TODO_ACTIONS_DIR'] + '/addp.py'
+ if not os.path.exists(filename):
+ with open(filename, 'w+') as fd:
+ fd.writelines(lines)
+
+ todo.CONFIG['ACTIONS'] = 'addp'
+ commands = todo.commands.copy()
+ todo.load_actions()
+
+ args = ['addp', '']
+ self.assertEqual(todo.execute_commands(args), 0)
+ os.unlink(filename)
+ os.unlink(filename + 'c')
+ todo.CONFIG['ACTIONS'] = ''
+ todo.commands = commands.copy()
+ self.assertEqual(todo.execute_commands(args), 1)
+
+ def test_sh_addons(self):
+ lines = ['#!/bin/bash\n\n', 'A=$((1+1))']
+ filename = todo.CONFIG['TODO_ACTIONS_DIR'] + '/addnums'
+ if not os.path.exists(filename):
+ with open(filename, 'w+') as fd:
+ fd.writelines(lines)
+ os.chmod(filename, 0700)
+
+ args = ['addnums']
+ self.assertEqual(todo.execute_commands(args), 0)
+ os.unlink(filename)
+ self.assertEqual(todo.execute_commands(args), 1)
View
30 tests/test_usage.py
@@ -0,0 +1,30 @@
+# TODO.TXT-CLI-python test script
+# Copyright (C) 2011-2012 Sigmavirus24
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# TLDR: This is licensed under the GPLv3. See LICENSE for more details.
+
+import todo
+import base
+import unittest
+
+class TestUsage(base.BaseTest):
+
+ def test_usage(self):
+ @todo.usage('Test usage')
+ def func():
+ pass
+
+ self.assertEqual(func.__usage__, 'Test usage')
View
113 todo.py
@@ -1040,61 +1040,9 @@ def opt_setup():
return opts
-if __name__ == "__main__":
- CONFIG["TODO_PY"] = sys.argv[0]
- opts = opt_setup()
-
- valid, args = opts.parse_args()
-
- get_config(valid.config, valid.todo_dir)
-
- global commands
- commands = {
- # command : ( Args, Function),
- "a" : (True, add_todo),
- "add" : (True, add_todo),
- "addm" : (True, addm_todo),
- "app" : (True, append_todo),
- "append" : (True, append_todo),
- "do" : (True, do_todo),
- "p" : (True, prioritize_todo),
- "pri" : (True, prioritize_todo),
- "pre" : (True, prepend_todo),
- "prepend" : (True, prepend_todo),
- "dp" : (True, de_prioritize_todo),
- "depri" : (True, de_prioritize_todo),
- "del" : (True, delete_todo),
- "rm" : (True, delete_todo),
- "ls" : (True, list_todo),
- "list" : (True, list_todo),
- "listall" : (False, list_all),
- "lsa" : (False, list_all),
- "lsc" : (False, list_context),
- "listcon" : (False, list_context),
- "lsd" : (False, list_date),
- "listdate" : (False, list_date),
- "lsp" : (False, list_project),
- "listproj" : (False, list_project),
- "h" : (False, cmd_help),
- "help" : (False, cmd_help),
- }
-
- if CONFIG["USE_GIT"]:
- commands.update(
- [("push", (False, _git_push)),
- ("pull", (False, _git_pull)),
- ("status", (False, _git_status)),
- ("log", (False, _git_log))]
- )
-
- if CONFIG["ACTIONS"]:
- load_actions()
-
+def execute_commands(args):
commandsl = [intern(key) for key in commands.keys()]
- if not len(args) > 0:
- args.append(CONFIG["TODOTXT_DEFAULT_ACTION"])
-
all_re = re.compile('((app|pre)(?:end)?|p(?:ri)?)')
all_set = set(["ls", "list", "a", "add", "addm"])
actions_dir = CONFIG.get('TODO_ACTIONS_DIR',
@@ -1121,4 +1069,61 @@ def opt_setup():
print("Unable to find command: {0}".format(arg))
print("Valid commands: ")
print(concat(commandsl, "\n"))
- sys.exit(1)
+ return 1
+ return 0
+
+
+commands = {
+ # command : ( Args, Function),
+ "a" : (True, add_todo),
+ "add" : (True, add_todo),
+ "addm" : (True, addm_todo),
+ "app" : (True, append_todo),
+ "append" : (True, append_todo),
+ "do" : (True, do_todo),
+ "p" : (True, prioritize_todo),
+ "pri" : (True, prioritize_todo),
+ "pre" : (True, prepend_todo),
+ "prepend" : (True, prepend_todo),
+ "dp" : (True, de_prioritize_todo),
+ "depri" : (True, de_prioritize_todo),
+ "del" : (True, delete_todo),
+ "rm" : (True, delete_todo),
+ "ls" : (True, list_todo),
+ "list" : (True, list_todo),
+ "listall" : (False, list_all),
+ "lsa" : (False, list_all),
+ "lsc" : (False, list_context),
+ "listcon" : (False, list_context),
+ "lsd" : (False, list_date),
+ "listdate" : (False, list_date),
+ "lsp" : (False, list_project),
+ "listproj" : (False, list_project),
+ "h" : (False, cmd_help),
+ "help" : (False, cmd_help),
+ }
+
+
+if __name__ == "__main__":
+ CONFIG["TODO_PY"] = sys.argv[0]
+ opts = opt_setup()
+
+ valid, args = opts.parse_args()
+
+ get_config(valid.config, valid.todo_dir)
+
+ if CONFIG["USE_GIT"]:
+ commands.update(
+ [("push", (False, _git_push)),
+ ("pull", (False, _git_pull)),
+ ("status", (False, _git_status)),
+ ("log", (False, _git_log))]
+ )
+
+ if CONFIG["ACTIONS"]:
+ load_actions()
+
+ if not len(args) > 0:
+ args.append(CONFIG["TODOTXT_DEFAULT_ACTION"])
+
+ sys.exit(execute_commands(args))
Please sign in to comment.
Something went wrong with that request. Please try again.