| 1 |
import os |
| 2 |
from pathlib import Path |
| 3 |
from unittest.mock import patch |
| 4 |
|
| 5 |
import pytest |
| 6 |
import yaml |
| 7 |
|
| 8 |
from beets import config |
| 9 |
from beets.exceptions import UserError |
| 10 |
from beets.test.helper import BeetsTestCase, IOMixin |
| 11 |
|
| 12 |
|
| 13 |
class ConfigCommandTest(IOMixin, BeetsTestCase): |
| 14 |
def setUp(self): |
| 15 |
super().setUp() |
| 16 |
for k in ("VISUAL", "EDITOR"): |
| 17 |
if k in os.environ: |
| 18 |
del os.environ[k] |
| 19 |
|
| 20 |
temp_dir = self.temp_path |
| 21 |
|
| 22 |
config_path = temp_dir / "config.yaml" |
| 23 |
lines = ["library: lib", "option: value", "password: password_value"] |
| 24 |
config_path.write_text("\n".join(lines)) |
| 25 |
self.config_path = str(config_path) |
| 26 |
|
| 27 |
cli_config_path = temp_dir / "cli_config.yaml" |
| 28 |
cli_config_path.write_text("option: cli overwrite") |
| 29 |
self.cli_config_path = str(cli_config_path) |
| 30 |
|
| 31 |
config.clear() |
| 32 |
config["password"].redact = True |
| 33 |
config._materialized = False |
| 34 |
|
| 35 |
def _run_with_yaml_output(self, *args): |
| 36 |
output = self.run_with_output(*args) |
| 37 |
return yaml.safe_load(output) |
| 38 |
|
| 39 |
def test_show_user_config(self): |
| 40 |
output = self._run_with_yaml_output("config", "-c") |
| 41 |
|
| 42 |
assert output["option"] == "value" |
| 43 |
assert output["password"] == "password_value" |
| 44 |
|
| 45 |
def test_show_user_config_with_defaults(self): |
| 46 |
output = self._run_with_yaml_output("config", "-dc") |
| 47 |
|
| 48 |
assert output["option"] == "value" |
| 49 |
assert output["password"] == "password_value" |
| 50 |
assert output["library"] == "lib" |
| 51 |
assert not output["import"]["timid"] |
| 52 |
|
| 53 |
def test_show_user_config_with_cli(self): |
| 54 |
output = self._run_with_yaml_output( |
| 55 |
"--config", self.cli_config_path, "config" |
| 56 |
) |
| 57 |
|
| 58 |
assert output["library"] == "lib" |
| 59 |
assert output["option"] == "cli overwrite" |
| 60 |
|
| 61 |
def test_show_redacted_user_config(self): |
| 62 |
output = self._run_with_yaml_output("config") |
| 63 |
|
| 64 |
assert output["option"] == "value" |
| 65 |
assert output["password"] == "REDACTED" |
| 66 |
|
| 67 |
def test_show_redacted_user_config_with_defaults(self): |
| 68 |
output = self._run_with_yaml_output("config", "-d") |
| 69 |
|
| 70 |
assert output["option"] == "value" |
| 71 |
assert output["password"] == "REDACTED" |
| 72 |
assert not output["import"]["timid"] |
| 73 |
|
| 74 |
def test_config_paths(self): |
| 75 |
output = self.run_with_output("config", "-p") |
| 76 |
|
| 77 |
paths = output.split("\n") |
| 78 |
assert len(paths) == 2 |
| 79 |
assert paths[0] == self.config_path |
| 80 |
|
| 81 |
def test_config_paths_with_cli(self): |
| 82 |
output = self.run_with_output( |
| 83 |
"--config", self.cli_config_path, "config", "-p" |
| 84 |
) |
| 85 |
paths = output.split("\n") |
| 86 |
assert len(paths) == 3 |
| 87 |
assert paths[0] == self.cli_config_path |
| 88 |
|
| 89 |
def test_edit_config_with_visual_or_editor_env(self): |
| 90 |
os.environ["EDITOR"] = "myeditor" |
| 91 |
with patch("os.execlp") as execlp: |
| 92 |
self.run_command("config", "-e") |
| 93 |
execlp.assert_called_once_with("myeditor", "myeditor", self.config_path) |
| 94 |
|
| 95 |
os.environ["VISUAL"] = "" # empty environment variables gets ignored |
| 96 |
with patch("os.execlp") as execlp: |
| 97 |
self.run_command("config", "-e") |
| 98 |
execlp.assert_called_once_with("myeditor", "myeditor", self.config_path) |
| 99 |
|
| 100 |
os.environ["VISUAL"] = "myvisual" |
| 101 |
with patch("os.execlp") as execlp: |
| 102 |
self.run_command("config", "-e") |
| 103 |
execlp.assert_called_once_with("myvisual", "myvisual", self.config_path) |
| 104 |
|
| 105 |
def test_edit_config_with_automatic_open(self): |
| 106 |
with patch("beets.util.open_anything") as open_: |
| 107 |
open_.return_value = "please_open" |
| 108 |
with patch("os.execlp") as execlp: |
| 109 |
self.run_command("config", "-e") |
| 110 |
execlp.assert_called_once_with( |
| 111 |
"please_open", "please_open", self.config_path |
| 112 |
) |
| 113 |
|
| 114 |
def test_config_editor_not_found(self): |
| 115 |
msg_match = "Could not edit configuration.*here is problem" |
| 116 |
with ( |
| 117 |
patch("os.execlp", side_effect=OSError("here is problem")), |
| 118 |
pytest.raises(UserError, match=msg_match), |
| 119 |
): |
| 120 |
self.run_command("config", "-e") |
| 121 |
|
| 122 |
def test_edit_invalid_config_file(self): |
| 123 |
Path(self.config_path).write_text("invalid: [") |
| 124 |
config.clear() |
| 125 |
config._materialized = False |
| 126 |
|
| 127 |
os.environ["EDITOR"] = "myeditor" |
| 128 |
with patch("os.execlp") as execlp: |
| 129 |
self.run_command("config", "-e") |
| 130 |
execlp.assert_called_once_with("myeditor", "myeditor", self.config_path) |
| 131 |
|
| 132 |
def test_edit_config_with_custom_config_path(self): |
| 133 |
os.environ["EDITOR"] = "myeditor" |
| 134 |
with patch("os.execlp") as execlp: |
| 135 |
self.run_command("--config", self.cli_config_path, "config", "-e") |
| 136 |
execlp.assert_called_once_with( |
| 137 |
"myeditor", "myeditor", self.cli_config_path |
| 138 |
) |