Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2847 #2851

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions beets/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,9 @@ def open_anything():
if sys_name == 'Darwin':
base_cmd = 'open'
elif sys_name == 'Windows':
base_cmd = 'start'
# python is not aware of cmd commands
# Start cmd.exe with start as argument
base_cmd = 'cmd /c \"start {}\"'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the last thing we need a way around: using interpolation will cause unpredictable problems here when the filename contains special characters (including spaces). Do you know whether it's possible to get the same effect while using proper argument lists?

For example, what I would want to work would be:

subprocess.call('start', filename)

but, as you realized, that won't work because start is a cmd shell built-in. See this SO answer, for example.

Is there any clever way we can pass the filename argument as a proper shell argument without shell interpolation? As a last resort, we may need to escape the filename ourselves. 😕

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subprocess.call(["cmd", "/c", "start", file])
I got this working

else: # Assume Unix
base_cmd = 'xdg-open'
return base_cmd
Expand Down Expand Up @@ -894,10 +896,11 @@ def shlex_split(s):


def interactive_open(targets, command):
"""Open the files in `targets` by `exec`ing a new `command`, given
as a Unicode string. (The new program takes over, and Python
execution ends: this does not fork a subprocess.)

"""On Unix like OS's open the files in `targets`
by `exec`ing a new `command` given as a Unicode string.
The new program takes over and Python execution ends.
This does not fork a subprocess.
On Windows each file in targets is invoked via `start file`.
Can raise `OSError`.
"""
assert command
Expand All @@ -908,6 +911,11 @@ def interactive_open(targets, command):
except ValueError: # Malformed shell tokens.
args = [command]

if platform.system() == 'Windows':
for f in targets:
subprocess.call(' '.join(args).format(f))
return

args.insert(0, args[0]) # for argv[0]

args += targets
Expand Down
52 changes: 41 additions & 11 deletions test/test_config_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import division, absolute_import, print_function

import os
import platform
import yaml
from mock import patch
from tempfile import mkdtemp
Expand Down Expand Up @@ -97,23 +98,44 @@ def test_config_paths_with_cli(self):

def test_edit_config_with_editor_env(self):
os.environ['EDITOR'] = 'myeditor'
with patch('os.execlp') as execlp:
if platform.system() == 'Windows':
func = 'subprocess.call'
else:
func = 'os.execlp'

with patch(func) as process:
self.run_command('config', '-e')
execlp.assert_called_once_with(
'myeditor', 'myeditor', self.config_path)
if platform.system() == 'Windows':
process.assert_called_once_with('myeditor')
else:
process.assert_called_once_with(
'myeditor', 'myeditor', self.config_path)

def test_edit_config_with_automatic_open(self):
with patch('beets.util.open_anything') as open:
open.return_value = 'please_open'
with patch('os.execlp') as execlp:
if platform.system() == 'Windows':
func = 'subprocess.call'
else:
func = 'os.execlp'

with patch(func) as process:
self.run_command('config', '-e')
execlp.assert_called_once_with(
'please_open', 'please_open', self.config_path)
if platform.system() == 'Windows':
process.assert_called_once_with('please_open')
else:
process.assert_called_once_with(
'please_open', 'please_open', self.config_path)

def test_config_editor_not_found(self):
with self.assertRaises(ui.UserError) as user_error:
with patch('os.execlp') as execlp:
execlp.side_effect = OSError('here is problem')
if platform.system() == 'Windows':
func = 'subprocess.call'
else:
func = 'os.execlp'

with patch(func) as process:
process.side_effect = OSError('here is problem')
self.run_command('config', '-e')
self.assertIn('Could not edit configuration',
six.text_type(user_error.exception))
Expand All @@ -126,10 +148,18 @@ def test_edit_invalid_config_file(self):
config._materialized = False

os.environ['EDITOR'] = 'myeditor'
with patch('os.execlp') as execlp:
if platform.system() == 'Windows':
func = 'subprocess.call'
else:
func = 'os.execlp'

with patch(func) as process:
self.run_command('config', '-e')
execlp.assert_called_once_with(
'myeditor', 'myeditor', self.config_path)
if platform.system() == 'Windows':
process.assert_called_once_with('myeditor')
else:
process.assert_called_once_with(
'myeditor', 'myeditor', self.config_path)


def suite():
Expand Down
10 changes: 8 additions & 2 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import sys
import re
import os
import platform
import subprocess
import unittest

Expand All @@ -32,15 +33,20 @@
class UtilTest(unittest.TestCase):
def test_open_anything(self):
with _common.system_mock('Windows'):
self.assertEqual(util.open_anything(), 'start')
self.assertEqual(util.open_anything(), 'cmd /c \"start {}\"')

with _common.system_mock('Darwin'):
self.assertEqual(util.open_anything(), 'open')

with _common.system_mock('Tagada'):
self.assertEqual(util.open_anything(), 'xdg-open')

@patch('os.execlp')
if platform.system() == 'Windows':
process = 'subprocess.call'
else:
process = 'os.execlp'

@patch(process)
@patch('beets.util.open_anything')
def test_interactive_open(self, mock_open, mock_execlp):
mock_open.return_value = u'tagada'
Expand Down