def test_replacements_parsed(self): with self.write_config_file() as config: config.write("replace: {'[xy]': z}") ui._raw_main(['test']) replacements = self.test_cmd.lib.replacements self.assertEqual(replacements, [(re.compile(ur'[xy]'), u'z')])
def test_multiple_replacements_parsed(self): with self.write_config_file() as config: config.write("replace: {'[xy]': z, foo: bar}") ui._raw_main(["test"]) replacements = self.test_cmd.lib.replacements self.assertEqual(replacements, [(re.compile(ur"[xy]"), u"z"), (re.compile(ur"foo"), u"bar")])
def _run_main(self, args, config_yaml, func): self.test_cmd.func = func config_yaml = textwrap.dedent(config_yaml).strip() if config_yaml: config_data = yaml.load(config_yaml, Loader=confit.Loader) config.set(config_data) ui._raw_main(args + ['test'])
def test_completion(self): # Load plugin commands config["pluginpath"] = [os.path.join(_common.RSRC, "beetsplug")] config["plugins"] = ["test"] test_script = os.path.join(os.path.dirname(__file__), "test_completion.sh") bash_completion = os.path.abspath(os.environ.get("BASH_COMPLETION_SCRIPT", "/etc/bash_completion")) # Tests run in bash cmd = os.environ.get("BEETS_TEST_SHELL", "/bin/bash --norc").split() if not has_program(cmd[0]): self.skipTest("bash not available") tester = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) # Load bash_completion try: with open(bash_completion, "r") as bash_completion: tester.stdin.writelines(bash_completion) except IOError: self.skipTest("bash-completion script not found") # Load complection script self.io.install() ui._raw_main(["completion"]) completion_script = self.io.getoutput() self.io.restore() tester.stdin.writelines(completion_script) # Load testsuite with open(test_script, "r") as test_script: tester.stdin.writelines(test_script) (out, err) = tester.communicate() if tester.returncode != 0 or out != "completion tests passed\n": print(out) self.fail("test/test_completion.sh did not execute properly")
def test_replacements_parsed(self): with self.write_config_file() as config: config.write("replace: {'[xy]': z}") ui._raw_main(["test"]) replacements = self.test_cmd.lib.replacements self.assertEqual(replacements, [(re.compile(r"[xy]"), b"z")])
def test_cli_config_option(self): config_path = os.path.join(self.temp_dir, "config.yaml") with open(config_path, "w") as file: file.write("anoption: value") ui._raw_main(["--config", config_path, "test"]) self.assertEqual(config["anoption"].get(), "value")
def test_cli_config_option(self): config_path = os.path.join(self.temp_dir, 'config.yaml') with open(config_path, 'w') as file: file.write('anoption: value') ui._raw_main(['--config', config_path, 'test']) self.assertEqual(config['anoption'].get(), 'value')
def test_paths_section_respected(self): with self.write_config_file() as config: config.write('paths: {x: y}') ui._raw_main(['test']) key, template = self.test_cmd.lib.path_formats[0] self.assertEqual(key, 'x') self.assertEqual(template.original, 'y')
def test_paths_section_respected(self): with self.write_config_file() as config: config.write("paths: {x: y}") ui._raw_main(["test"]) key, template = self.test_cmd.lib.path_formats[0] self.assertEqual(key, "x") self.assertEqual(template.original, "y")
def test_cli_skips_calculated_tracks(self): ui._raw_main(['replaygain']) item = self.lib.items()[0] peak = item.rg_track_peak item.rg_track_gain = 0.0 ui._raw_main(['replaygain']) self.assertEqual(item.rg_track_gain, 0.0) self.assertEqual(item.rg_track_peak, peak)
def test_cli_config_file_loads_plugin_commands(self): cli_config_path = os.path.join(self.temp_dir, 'config.yaml') with open(cli_config_path, 'w') as file: file.write('pluginpath: %s\n' % _common.PLUGINPATH) file.write('plugins: test') ui._raw_main(['--config', cli_config_path, 'plugin']) self.assertTrue(plugins.find_plugins()[0].is_test_plugin)
def test_config_editor_not_found(self): def raise_os_error(*args): raise OSError os.execlp = raise_os_error with self.assertRaises(ui.UserError) as user_error: ui._raw_main(['config', '-e']) self.assertIn('Could not edit configuration', str(user_error.exception.args[0]))
def test_cli_config_paths_resolve_relative_to_user_dir(self): cli_config_path = os.path.join(self.temp_dir, "config.yaml") with open(cli_config_path, "w") as file: file.write("library: beets.db\n") file.write("statefile: state") ui._raw_main(["--config", cli_config_path, "test"]) self.assertEqual(config["library"].as_filename(), os.path.join(self.user_config_dir, "beets.db")) self.assertEqual(config["statefile"].as_filename(), os.path.join(self.user_config_dir, "state"))
def runcli(self, *args): # TODO mock stdin with capture_stdout() as out: try: ui._raw_main(_convert_args(list(args)), self.lib) except ui.UserError as u: # TODO remove this and handle exceptions in tests print(u.args[0]) return out.getvalue()
def test_multiple_replacements_parsed(self): with self.write_config_file() as config: config.write("replace: {'[xy]': z, foo: bar}") ui._raw_main(['test']) replacements = self.test_cmd.lib.replacements self.assertEqual(replacements, [ (re.compile(ur'[xy]'), u'z'), (re.compile(ur'foo'), u'bar'), ])
def test_cli_config_file_loads_plugin_commands(self): plugin_path = os.path.join(_common.RSRC, "beetsplug") cli_config_path = os.path.join(self.temp_dir, "config.yaml") with open(cli_config_path, "w") as file: file.write("pluginpath: %s\n" % plugin_path) file.write("plugins: test") ui._raw_main(["--config", cli_config_path, "plugin"]) self.assertTrue(plugins.find_plugins()[0].is_test_plugin)
def test_cli_config_file_overwrites_user_defaults(self): with open(self.user_config_path, 'w') as file: file.write('anoption: value') cli_config_path = os.path.join(self.temp_dir, 'config.yaml') with open(cli_config_path, 'w') as file: file.write('anoption: cli overwrite') ui._raw_main(['--config', cli_config_path, 'test']) self.assertEqual(config['anoption'].get(), 'cli overwrite')
def test_multiple_replacements_parsed(self): with self.write_config_file() as config: config.write("replace: {'[xy]': z, foo: bar}") ui._raw_main(['test']) replacements = self.test_cmd.lib.replacements self.assertEqual(replacements, [ (re.compile(u'[xy]'), u'z'), (re.compile(u'foo'), u'bar'), ])
def test_cli_config_file_loads_plugin_commands(self): plugin_path = os.path.join(_common.RSRC, 'beetsplug') cli_config_path = os.path.join(self.temp_dir, 'config.yaml') with open(cli_config_path, 'w') as file: file.write('pluginpath: %s\n' % plugin_path) file.write('plugins: test') ui._raw_main(['--config', cli_config_path, 'plugin']) self.assertTrue(plugins.find_plugins()[0].is_test_plugin)
def test_cli_config_file_overwrites_user_defaults(self): with open(self.user_config_path, "w") as file: file.write("anoption: value") cli_config_path = os.path.join(self.temp_dir, "config.yaml") with open(cli_config_path, "w") as file: file.write("anoption: cli overwrite") ui._raw_main(["--config", cli_config_path, "test"]) self.assertEqual(config["anoption"].get(), "cli overwrite")
def test_cli_config_paths_resolve_relative_to_user_dir(self): cli_config_path = os.path.join(self.temp_dir, 'config.yaml') with open(cli_config_path, 'w') as file: file.write('library: beets.db\n') file.write('statefile: state') ui._raw_main(['--config', cli_config_path, 'test']) self.assertEqual(config['library'].as_filename(), os.path.join(self.user_config_dir, 'beets.db')) self.assertEqual(config['statefile'].as_filename(), os.path.join(self.user_config_dir, 'state'))
def test_cli_config_file_overwrites_beetsdir_defaults(self): os.environ['BEETSDIR'] = self.beetsdir env_config_path = os.path.join(self.beetsdir, 'config.yaml') with open(env_config_path, 'w') as file: file.write('anoption: value') cli_config_path = os.path.join(self.temp_dir, 'config.yaml') with open(cli_config_path, 'w') as file: file.write('anoption: cli overwrite') ui._raw_main(['--config', cli_config_path, 'test']) self.assertEqual(config['anoption'].get(), 'cli overwrite')
def test_default_paths_preserved(self): default_formats = ui.get_path_formats() self._reset_config() with self.write_config_file() as config: config.write('paths: {x: y}') ui._raw_main(['test']) key, template = self.test_cmd.lib.path_formats[0] self.assertEqual(key, 'x') self.assertEqual(template.original, 'y') self.assertEqual(self.test_cmd.lib.path_formats[1:], default_formats)
def test_default_paths_preserved(self): default_formats = ui.get_path_formats() self._reset_config() with self.write_config_file() as config: config.write("paths: {x: y}") ui._raw_main(["test"]) key, template = self.test_cmd.lib.path_formats[0] self.assertEqual(key, "x") self.assertEqual(template.original, "y") self.assertEqual(self.test_cmd.lib.path_formats[1:], default_formats)
def test_cli_config_file_overwrites_beetsdir_defaults(self): os.environ["BEETSDIR"] = self.beetsdir env_config_path = os.path.join(self.beetsdir, "config.yaml") with open(env_config_path, "w") as file: file.write("anoption: value") cli_config_path = os.path.join(self.temp_dir, "config.yaml") with open(cli_config_path, "w") as file: file.write("anoption: cli overwrite") ui._raw_main(["--config", cli_config_path, "test"]) self.assertEqual(config["anoption"].get(), "cli overwrite")
def test_cli_config_paths_resolve_relative_to_beetsdir(self): os.environ['BEETSDIR'] = self.beetsdir cli_config_path = os.path.join(self.temp_dir, 'config.yaml') with open(cli_config_path, 'w') as file: file.write('library: beets.db\n') file.write('statefile: state') ui._raw_main(['--config', cli_config_path, 'test']) self.assert_equal_path(config['library'].as_filename(), os.path.join(self.beetsdir, 'beets.db')) self.assert_equal_path(config['statefile'].as_filename(), os.path.join(self.beetsdir, 'state'))
def test_cli_saves_track_gain(self): for item in self.lib.items(): self.assertEqual(item.rg_track_peak, 0.0) self.assertEqual(item.rg_track_gain, 0.0) mediafile = MediaFile(item.path) self.assertEqual(mediafile.rg_track_peak, 0.0) self.assertEqual(mediafile.rg_track_gain, 0.0) ui._raw_main(['replaygain']) for item in self.lib.items(): self.assertNotEqual(item.rg_track_peak, 0.0) self.assertNotEqual(item.rg_track_gain, 0.0) mediafile = MediaFile(item.path) self.assertAlmostEqual( mediafile.rg_track_peak, item.rg_track_peak, places=6) self.assertAlmostEqual( mediafile.rg_track_gain, item.rg_track_gain, places=6)
def test_completion(self): # Load plugin commands config['pluginpath'] = [os.path.join(_common.RSRC, 'beetsplug')] config['plugins'] = ['test'] test_script = os.path.join( os.path.dirname(__file__), 'test_completion.sh' ) bash_completion = os.path.abspath(os.environ.get( 'BASH_COMPLETION_SCRIPT', '/etc/bash_completion')) # Tests run in bash shell = os.environ.get('BEETS_TEST_SHELL', '/bin/bash --norc') try: with open(os.devnull, 'wb') as devnull: subprocess.check_call(shell.split() + ['--version'], stdout=devnull) except OSError: self.skipTest('bash not available') tester = subprocess.Popen(shell.split(' '), stdin=subprocess.PIPE, stdout=subprocess.PIPE) # Load bash_completion try: with open(bash_completion, 'r') as bash_completion: tester.stdin.writelines(bash_completion) except IOError: self.skipTest('bash-completion script not found') # Load complection script self.io.install() ui._raw_main(['completion']) completion_script = self.io.getoutput() self.io.restore() tester.stdin.writelines(completion_script) # from beets import plugins # for cmd in plugins.commands(): # print(cmd.name) # Load testsuite with open(test_script, 'r') as test_script: tester.stdin.writelines(test_script) (out, err) = tester.communicate() if tester.returncode != 0 or out != "completion tests passed\n": print(out) self.fail('test/test_completion.sh did not execute properly')
def test_completion(self): # Load plugin commands config['pluginpath'] = [os.path.join(_common.RSRC, 'beetsplug')] config['plugins'] = ['test'] # Tests run in bash cmd = os.environ.get('BEETS_TEST_SHELL', '/bin/bash --norc').split() if not has_program(cmd[0]): self.skipTest(u'bash not available') tester = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) # Load bash_completion library. for path in commands.BASH_COMPLETION_PATHS: if os.path.exists(util.syspath(path)): bash_completion = path break else: self.skipTest(u'bash-completion script not found') try: with open(util.syspath(bash_completion), 'r') as f: tester.stdin.writelines(f) except IOError: self.skipTest(u'could not read bash-completion script') # Load completion script. self.io.install() ui._raw_main(['completion']) completion_script = self.io.getoutput() self.io.restore() tester.stdin.writelines(completion_script) # Load test suite. test_script = os.path.join(_common.RSRC, 'test_completion.sh') with open(test_script, 'r') as test_script: tester.stdin.writelines(test_script) (out, err) = tester.communicate() if tester.returncode != 0 or out != u"completion tests passed\n": print(out) self.fail(u'test/test_completion.sh did not execute properly')
def test_cli_saves_album_gain_to_file(self): for item in self.lib.items(): mediafile = MediaFile(item.path) self.assertEqual(mediafile.rg_album_peak, 0.0) self.assertEqual(mediafile.rg_album_gain, 0.0) ui._raw_main(['replaygain', '-a']) peaks = [] gains = [] for item in self.lib.items(): mediafile = MediaFile(item.path) peaks.append(mediafile.rg_album_peak) gains.append(mediafile.rg_album_gain) # Make sure they are all the same self.assertEqual(max(peaks), min(peaks)) self.assertEqual(max(gains), min(gains)) self.assertNotEqual(max(gains), 0.0) self.assertNotEqual(max(peaks), 0.0)
def test_completion(self): # Load plugin commands config['pluginpath'] = [_common.PLUGINPATH] config['plugins'] = ['test'] # Tests run in bash cmd = os.environ.get('BEETS_TEST_SHELL', '/bin/bash --norc').split() if not has_program(cmd[0]): self.skipTest(u'bash not available') tester = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) # Load bash_completion library. for path in commands.BASH_COMPLETION_PATHS: if os.path.exists(util.syspath(path)): bash_completion = path break else: self.skipTest(u'bash-completion script not found') try: with open(util.syspath(bash_completion), 'r') as f: tester.stdin.writelines(f) except IOError: self.skipTest(u'could not read bash-completion script') # Load completion script. self.io.install() ui._raw_main(['completion']) completion_script = self.io.getoutput() self.io.restore() tester.stdin.writelines(completion_script) # Load test suite. test_script = os.path.join(_common.RSRC, b'test_completion.sh') with open(test_script, 'r') as test_script: tester.stdin.writelines(test_script) (out, err) = tester.communicate() if tester.returncode != 0 or out != u"completion tests passed\n": print(out) self.fail(u'test/test_completion.sh did not execute properly')
def test_completion(self): # Load plugin commands config['pluginpath'] = [os.path.join(_common.RSRC, 'beetsplug')] config['plugins'] = ['test'] test_script = os.path.join(os.path.dirname(__file__), 'test_completion.sh') bash_completion = os.path.abspath( os.environ.get('BASH_COMPLETION_SCRIPT', '/etc/bash_completion')) # Tests run in bash cmd = os.environ.get('BEETS_TEST_SHELL', '/bin/bash --norc').split() if not has_program(cmd[0]): self.skipTest('bash not available') tester = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) # Load bash_completion try: with open(bash_completion, 'r') as bash_completion: tester.stdin.writelines(bash_completion) except IOError: self.skipTest('bash-completion script not found') # Load complection script self.io.install() ui._raw_main(['completion']) completion_script = self.io.getoutput() self.io.restore() tester.stdin.writelines(completion_script) # Load testsuite with open(test_script, 'r') as test_script: tester.stdin.writelines(test_script) (out, err) = tester.communicate() if tester.returncode != 0 or out != "completion tests passed\n": print(out) self.fail('test/test_completion.sh did not execute properly')
def test_completion(self): # Load plugin commands config["pluginpath"] = [os.path.join(_common.RSRC, "beetsplug")] config["plugins"] = ["test"] # Tests run in bash cmd = os.environ.get("BEETS_TEST_SHELL", "/bin/bash --norc").split() if not has_program(cmd[0]): self.skipTest("bash not available") tester = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) # Load bash_completion library. for path in commands.BASH_COMPLETION_PATHS: if os.path.exists(util.syspath(path)): bash_completion = path break else: self.skipTest("bash-completion script not found") try: with open(util.syspath(bash_completion), "r") as f: tester.stdin.writelines(f) except IOError: self.skipTest("could not read bash-completion script") # Load completion script. self.io.install() ui._raw_main(["completion"]) completion_script = self.io.getoutput() self.io.restore() tester.stdin.writelines(completion_script) # Load test suite. test_script = os.path.join(_common.RSRC, "test_completion.sh") with open(test_script, "r") as test_script: tester.stdin.writelines(test_script) (out, err) = tester.communicate() if tester.returncode != 0 or out != "completion tests passed\n": print(out) self.fail("test/test_completion.sh did not execute properly")
def test_completion(self): # Load plugin commands config['pluginpath'] = [os.path.join(_common.RSRC, 'beetsplug')] config['plugins'] = ['test'] test_script = os.path.join(os.path.dirname(__file__), 'test_completion.sh') bash_completion = os.path.abspath(os.environ.get( 'BASH_COMPLETION_SCRIPT', '/etc/bash_completion')) # Tests run in bash shell = os.environ.get('BEETS_TEST_SHELL', '/bin/bash --norc') tester = subprocess.Popen(shell.split(' '), stdin=subprocess.PIPE, stdout=subprocess.PIPE) # Load bash_completion with open(bash_completion, 'r') as bash_completion: tester.stdin.writelines(bash_completion) # Load complection script self.io.install() ui._raw_main(['completion']) completion_script = self.io.getoutput() self.io.restore() tester.stdin.writelines(completion_script) # from beets import plugins # for cmd in plugins.commands(): # print(cmd.name) # Load testsuite with open(test_script, 'r') as test_script: tester.stdin.writelines(test_script) (out, err) = tester.communicate() if tester.returncode != 0 or out != "completion tests passed\n": print(out) self.fail('test/test_completion.sh did not execute properly')
def test_show_user_config_with_defaults(self): ui._raw_main(['config', '-d']) output = yaml.load(self.io.getoutput()) self.assertEqual(output['option'], 'value') self.assertEqual(output['library'], 'lib') self.assertEqual(output['import']['timid'], False)
def test_show_user_config(self): ui._raw_main(['config']) output = yaml.load(self.io.getoutput()) self.assertEqual(output['option'], 'value')
def test_config_paths(self): ui._raw_main(['config', '-p']) paths = self.io.getoutput().split('\n') self.assertEqual(len(paths), 2) self.assertEqual(paths[0], self.config_path)
def test_show_user_config_with_cli(self): ui._raw_main(['--config', self.cli_config_path, 'config']) output = yaml.load(self.io.getoutput()) self.assertEqual(output['library'], 'lib') self.assertEqual(output['option'], 'cli overwrite')
def test_edit_config_with_editor_env(self): self.execlp_stub() os.environ['EDITOR'] = 'myeditor' ui._raw_main(['config', '-e']) self.assertEqual(self._execlp_call, ['myeditor', self.config_path])
def test_config_paths_with_cli(self): ui._raw_main(['--config', self.cli_config_path, 'config', '-p']) paths = self.io.getoutput().split('\n') self.assertEqual(len(paths), 3) self.assertEqual(paths[0], self.cli_config_path)
def test_edit_config_with_windows_exec(self): self.execlp_stub() with _common.system_mock('Windows'): ui._raw_main(['config', '-e']) self.assertEqual(self._execlp_call, [self.config_path])
def test_edit_config_with_xdg_open(self): self.execlp_stub() with _common.system_mock('Linux'): ui._raw_main(['config', '-e']) self.assertEqual(self._execlp_call, ['xdg-open', self.config_path])
def write_cmd(self, *args): ui._raw_main(['write'] + list(args), self.lib)
def test_nonexistant_db(self): with self.write_config_file() as config: config.write('library: /xxx/yyy/not/a/real/path') with self.assertRaises(ui.UserError): ui._raw_main(['test'])
def _run_main(self, args, config, func): self.test_cmd.func = func ui._raw_main(args + ['test'], StringIO(config))
def test_command_line_option_relative_to_working_dir(self): os.chdir(self.temp_dir) ui._raw_main(['--library', 'foo.db', 'test']) self.assertEqual(config['library'].as_filename(), os.path.join(os.getcwd(), 'foo.db'))
def modify(self, *args): with control_stdin('y'): ui._raw_main(['modify'] + list(args), self.lib)
def test_plugin_command_from_pluginpath(self): config['pluginpath'] = [os.path.join(_common.RSRC, 'beetsplug')] config['plugins'] = ['test'] ui._raw_main(['test'])
def test_plugin_command_from_pluginpath(self): config['pluginpath'] = [_common.PLUGINPATH] config['plugins'] = ['test'] ui._raw_main(['test'])
def test_user_config_file(self): with self.write_config_file() as file: file.write('anoption: value') ui._raw_main(['test']) self.assertEqual(config['anoption'].get(), 'value')
def modify_inp(self, inp, *args): with control_stdin(inp): ui._raw_main(['modify'] + list(args), self.lib)