[Fix #88] Adds proper escaping for arguments in alias plugin.
This commit is contained in:
parent
be3c124079
commit
5c74f942c4
@ -28,6 +28,8 @@ if sys.version_info[0] == 2:
|
|||||||
ustdio.seek(0)
|
ustdio.seek(0)
|
||||||
return ustdio.read()
|
return ustdio.read()
|
||||||
|
|
||||||
|
from pipes import quote as shell_quote
|
||||||
|
|
||||||
else:
|
else:
|
||||||
ustr = str
|
ustr = str
|
||||||
uchr = chr
|
uchr = chr
|
||||||
@ -52,6 +54,7 @@ else:
|
|||||||
return stdio.read()
|
return stdio.read()
|
||||||
|
|
||||||
import pickle
|
import pickle
|
||||||
|
from pipes import quote as shell_quote
|
||||||
|
|
||||||
input = input
|
input = input
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
import shlex
|
import shlex
|
||||||
|
|
||||||
|
from ...p3 import shell_quote
|
||||||
from ...plugins import PapersPlugin
|
from ...plugins import PapersPlugin
|
||||||
from ...pubs_cmd import execute
|
from ...pubs_cmd import execute
|
||||||
|
|
||||||
@ -50,9 +51,10 @@ class ShellAlias(Alias):
|
|||||||
as shell arguments.
|
as shell arguments.
|
||||||
"""
|
"""
|
||||||
subprocess.call(
|
subprocess.call(
|
||||||
'papers_alias_fun () {{\n{}\n}}\npapers_alias_fun {}'.format(
|
'pubs_alias_fun () {{\n{}\n}}\npubs_alias_fun {}'.format(
|
||||||
self.definition, ' '.join(args.arguments)),
|
self.definition,
|
||||||
shell=True)
|
' '.join([shell_quote(a) for a in args.arguments])
|
||||||
|
), shell=True)
|
||||||
|
|
||||||
|
|
||||||
class AliasPlugin(PapersPlugin):
|
class AliasPlugin(PapersPlugin):
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import shlex
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import dotdot
|
import dotdot
|
||||||
@ -8,10 +9,10 @@ from pubs.plugs.alias.alias import (Alias, AliasPlugin, CommandAlias,
|
|||||||
ShellAlias)
|
ShellAlias)
|
||||||
|
|
||||||
|
|
||||||
def to_args(arg_str):
|
def to_args(arg_strings):
|
||||||
o = lambda: None # Dirty hack
|
o = lambda: None # Dirty hack
|
||||||
o.prog = 'pubs'
|
o.prog = 'pubs'
|
||||||
o.arguments = arg_str.split(' ')
|
o.arguments = arg_strings
|
||||||
return o
|
return o
|
||||||
|
|
||||||
|
|
||||||
@ -37,7 +38,7 @@ class AliasTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
def testAlias(self):
|
def testAlias(self):
|
||||||
alias = Alias.create_alias('print', 'open -w lpppp')
|
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.assertIsNone(self.subprocess.called)
|
||||||
self.assertEqual(self.cmd_execute.executed,
|
self.assertEqual(self.cmd_execute.executed,
|
||||||
['pubs', 'open', '-w', 'lpppp', 'CiteKey'])
|
['pubs', 'open', '-w', 'lpppp', 'CiteKey'])
|
||||||
@ -46,10 +47,20 @@ class AliasTestCase(unittest.TestCase):
|
|||||||
"""This actually just test that subprocess.call is called.
|
"""This actually just test that subprocess.call is called.
|
||||||
"""
|
"""
|
||||||
alias = Alias.create_alias('count', '!pubs list -k | wc -l')
|
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.assertIsNone(self.cmd_execute.executed)
|
||||||
self.assertIsNotNone(self.subprocess.called)
|
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):
|
class AliasPluginTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user