Skip to content

Commit

Permalink
Test + refactor to the escapearg method to use regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
mondaini committed Aug 1, 2011
1 parent eb3ecac commit ad5784a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.pyc
7 changes: 3 additions & 4 deletions tdaemon.py
Expand Up @@ -18,7 +18,9 @@
import hashlib
import commands
import datetime
import re

SPECIAL_CHARS_REGEX_PATTERN = r'[#&;`|*?~<>^()\[\]{}$\\]+'
IGNORE_EXTENSIONS = ('pyc', 'pyo')
IGNORE_DIRS = ('.bzr', '.git', '.hg', '.darcs', '.svn')
IMPLEMENTED_TEST_PROGRAMS = ('nose', 'nosetests', 'django', 'py', 'symfony',
Expand Down Expand Up @@ -50,10 +52,7 @@ def ask(message='Are you sure? [y/N]'):
def escapearg(args):
"""Escapes characters you don't want in arguments (preventing shell
injection)"""
special_chars = '#&;`|*?~<>^()[]{}$\\'
for char in special_chars:
args = args.replace(char, '')
return args
return re.sub(SPECIAL_CHARS_REGEX_PATTERN, '', args)

class Watcher(object):
"""
Expand Down
11 changes: 11 additions & 0 deletions test.py
@@ -0,0 +1,11 @@
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import tdaemon
import unittest

class Test(unittest.TestCase):

def test_remove_special_chars(self):
arguments = r'This is an arguments list + special chars #&;`|*?~<>^()[]{}$\\'
expected = 'This is an arguments list + special chars '
self.assertEqual(tdaemon.escapearg(arguments), expected)

0 comments on commit ad5784a

Please sign in to comment.