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

Allow invalid configuration files to be edited #1128

Merged
merged 2 commits into from
Dec 2, 2014
Merged
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
7 changes: 7 additions & 0 deletions beets/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,13 @@ def _raw_main(args, lib=None):
help=optparse.SUPPRESS_HELP)

options, subargs = parser.parse_global_options(args)

# Bypass _setup so that an invalid configuration does not prevent
# the editor from starting.
if subargs[0] == 'config' and ('-e' in subargs or '--edit' in subargs):
from beets.ui.commands import config_edit
return config_edit()

subcommands, plugins, lib = _setup(options, lib)
parser.add_subcommand(*subcommands)

Expand Down
46 changes: 26 additions & 20 deletions beets/ui/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1485,32 +1485,38 @@ def config_func(lib, opts, args):

# Open in editor.
elif opts.edit:
path = config.user_config_path()

if 'EDITOR' in os.environ:
editor = os.environ['EDITOR']
args = [editor, editor, path]
elif platform.system() == 'Darwin':
args = ['open', 'open', '-n', path]
elif platform.system() == 'Windows':
# On windows we can execute arbitrary files. The os will
# take care of starting an appropriate application
args = [path, path]
else:
# Assume Unix
args = ['xdg-open', 'xdg-open', path]

try:
os.execlp(*args)
except OSError:
raise ui.UserError("Could not edit configuration. Please"
"set the EDITOR environment variable.")
config_edit()

# Dump configuration.
else:
print(config.dump(full=opts.defaults))


def config_edit():
"""Open a program to edit the user configuration.
"""
path = config.user_config_path()

if 'EDITOR' in os.environ:
editor = os.environ['EDITOR']
args = [editor, editor, path]
elif platform.system() == 'Darwin':
args = ['open', 'open', '-n', path]
elif platform.system() == 'Windows':
# On windows we can execute arbitrary files. The os will
# take care of starting an appropriate application
args = [path, path]
else:
# Assume Unix
args = ['xdg-open', 'xdg-open', path]

try:
os.execlp(*args)
except OSError:
raise ui.UserError("Could not edit configuration. Please"
"set the EDITOR environment variable.")


config_cmd = ui.Subcommand('config',
help='show or edit the user configuration')
config_cmd.parser.add_option(
Expand Down
14 changes: 14 additions & 0 deletions test/test_config_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import _common
from _common import unittest
from helper import TestHelper, capture_stdout
from beets.library import Library


class ConfigCommandTest(unittest.TestCase, TestHelper):
Expand Down Expand Up @@ -105,6 +106,19 @@ def test_config_editor_not_found(self):
self.assertIn('Could not edit configuration',
str(user_error.exception.args[0]))

def test_edit_invalid_config_file(self):
self.lib = Library(':memory:')
with open(self.config_path, 'w') as file:
file.write('invalid: [')
config.clear()
config._materialized = False

os.environ['EDITOR'] = 'myeditor'
with patch('os.execlp') as execlp:
self.run_command('config', '-e')
execlp.assert_called_once_with(
'myeditor', 'myeditor', self.config_path)


def suite():
return unittest.TestLoader().loadTestsFromName(__name__)
Expand Down