Skip to content

Commit

Permalink
Fix: Backslash-escaped syntax is not for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
eliangcs committed Sep 16, 2016
1 parent b0c448d commit 6db8326
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
14 changes: 11 additions & 3 deletions http_prompt/execution.py
Expand Up @@ -126,6 +126,14 @@ def generate_cmds_with_explanations(summary, cmds):
return text


if sys.platform == 'win32': # nocover
def normalize_filepath(path):
return unquote(path)
else:
def normalize_filepath(path):
return unescape(unquote(path))


class DummyExecutionListener(object):

def context_changed(self, context):
Expand Down Expand Up @@ -217,12 +225,12 @@ def visit_redir_out(self, node, children):
else:
mode = 'wb'

filename = unquote(path)
filename = normalize_filepath(path)
self.output = FileWriter(open(filename, mode))
return node

def visit_exec(self, node, children):
path = unescape(unquote(children[3]))
path = normalize_filepath(children[3])
with io.open(path, encoding='utf-8') as f:
# Wipe out context first
execute('rm *', self.context, self.listener)
Expand All @@ -231,7 +239,7 @@ def visit_exec(self, node, children):
return node

def visit_source(self, node, children):
path = unescape(unquote(children[3]))
path = normalize_filepath(children[3])
with io.open(path, encoding='utf-8') as f:
for line in f:
execute(line, self.context, self.listener)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_execution.py
Expand Up @@ -318,6 +318,8 @@ def test_source_quoted_filename(self):
'--verify': 'no'
})

@pytest.mark.skipif(sys.platform == 'win32',
reason="Windows doesn't use backslashes to escape")
def test_source_escaped_filename(self):
new_filename = self.filename + r' copy'
shutil.copyfile(self.filename, new_filename)
Expand Down Expand Up @@ -412,6 +414,8 @@ def test_exec_quoted_filename(self):
'username': 'jane'
})

@pytest.mark.skipif(sys.platform == 'win32',
reason="Windows doesn't use backslashes to escape")
def test_exec_escaped_filename(self):
new_filename = self.filename + r' copy'
shutil.copyfile(self.filename, new_filename)
Expand Down Expand Up @@ -1116,6 +1120,22 @@ def test_httpie_redirect_write_quoted_filename(self):
content = f.read()
self.assertEqual(content, 'http http://localhost')

@pytest.mark.skipif(sys.platform == 'win32',
reason="Windows doesn't use backslashes to escape")
def test_httpie_redirect_write_escaped_filename(self):
filename = self.make_tempfile()
filename += r' copy'

# Write something first to make sure it's a full overwrite
with open(filename, 'w') as f:
f.write('hello world\n')

execute('httpie > %s' % filename.replace(' ', r'\ '), self.context)

with open(filename) as f:
content = f.read()
self.assertEqual(content, 'http http://localhost')

def test_httpie_redirect_write_with_args(self):
filename = self.make_tempfile()

Expand Down

0 comments on commit 6db8326

Please sign in to comment.