Merge pull request #91 from pubs/fix/88
[Fix #88] Adds proper escaping for arguments in alias plugin.
This commit is contained in:
commit
2cab1666e8
@ -1,5 +1,6 @@
|
||||
import subprocess
|
||||
import shlex
|
||||
import subprocess
|
||||
from pipes import quote as shell_quote
|
||||
|
||||
from ...plugins import PapersPlugin
|
||||
from ...pubs_cmd import execute
|
||||
@ -50,9 +51,10 @@ class ShellAlias(Alias):
|
||||
as shell arguments.
|
||||
"""
|
||||
subprocess.call(
|
||||
'papers_alias_fun () {{\n{}\n}}\npapers_alias_fun {}'.format(
|
||||
self.definition, ' '.join(args.arguments)),
|
||||
shell=True)
|
||||
'pubs_alias_fun () {{\n{}\n}}\npubs_alias_fun {}'.format(
|
||||
self.definition,
|
||||
' '.join([shell_quote(a) for a in args.arguments])
|
||||
), shell=True)
|
||||
|
||||
|
||||
class AliasPlugin(PapersPlugin):
|
||||
|
@ -1,3 +1,4 @@
|
||||
import shlex
|
||||
import unittest
|
||||
|
||||
import dotdot
|
||||
@ -8,10 +9,10 @@ from pubs.plugs.alias.alias import (Alias, AliasPlugin, CommandAlias,
|
||||
ShellAlias)
|
||||
|
||||
|
||||
def to_args(arg_str):
|
||||
def to_args(arg_strings):
|
||||
o = lambda: None # Dirty hack
|
||||
o.prog = 'pubs'
|
||||
o.arguments = arg_str.split(' ')
|
||||
o.arguments = arg_strings
|
||||
return o
|
||||
|
||||
|
||||
@ -37,7 +38,7 @@ class AliasTestCase(unittest.TestCase):
|
||||
|
||||
def testAlias(self):
|
||||
alias = Alias.create_alias('print', 'open -w lpppp')
|
||||
alias.command(None, to_args('CiteKey'))
|
||||
alias.command(None, to_args(['CiteKey']))
|
||||
self.assertIsNone(self.subprocess.called)
|
||||
self.assertEqual(self.cmd_execute.executed,
|
||||
['pubs', 'open', '-w', 'lpppp', 'CiteKey'])
|
||||
@ -46,10 +47,20 @@ class AliasTestCase(unittest.TestCase):
|
||||
"""This actually just test that subprocess.call is called.
|
||||
"""
|
||||
alias = Alias.create_alias('count', '!pubs list -k | wc -l')
|
||||
alias.command(None, to_args(''))
|
||||
alias.command(None, to_args([]))
|
||||
self.assertIsNone(self.cmd_execute.executed)
|
||||
self.assertIsNotNone(self.subprocess.called)
|
||||
|
||||
def testShellAliasEscapes(self):
|
||||
alias = Alias.create_alias('count', '!echo "$@"')
|
||||
args = ['a b c', "d,e f\""]
|
||||
alias.command(None, to_args(args))
|
||||
self.assertIsNone(self.cmd_execute.executed)
|
||||
self.assertIsNotNone(self.subprocess.called)
|
||||
self.assertEqual(
|
||||
shlex.split(self.subprocess.called.splitlines()[-1])[1:],
|
||||
args)
|
||||
|
||||
|
||||
class AliasPluginTestCase(unittest.TestCase):
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user