Debian Codegraph @

test/ui/commands/test_completion.py

raw
1 import os
2 import subprocess
3 import sys
4 from pathlib import Path
5
6 import pytest
7
8 from beets.test import _common
9 from beets.test.helper import RUNNING_IN_CI, IOMixin, has_program
10 from beets.ui.commands.completion import BASH_COMPLETION_PATHS
11
12 from ..test_ui import TestPluginTestCase
13
14
15 @pytest.mark.xfail(
16 RUNNING_IN_CI and sys.platform == "linux",
17 reason="Completion is for some reason unhappy on Ubuntu 24.04 in CI",
18 )
19 class CompletionTest(IOMixin, TestPluginTestCase):
20 def test_completion(self):
21 # Do not load any other bash completion scripts on the system.
22 env = dict(os.environ)
23 env["BASH_COMPLETION_DIR"] = os.devnull
24 env["BASH_COMPLETION_COMPAT_DIR"] = os.devnull
25
26 # Open a `bash` process to run the tests in. We'll pipe in bash
27 # commands via stdin.
28 cmd = os.environ.get("BEETS_TEST_SHELL", "/bin/bash --norc").split()
29 if not has_program(cmd[0]):
30 self.skipTest("bash not available")
31 tester = subprocess.Popen(
32 cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, env=env
33 )
34
35 # Load bash_completion library.
36 completion_paths = map(Path, map(os.fsdecode, BASH_COMPLETION_PATHS))
37 for path in completion_paths:
38 if path.exists():
39 bash_completion = path
40 break
41 else:
42 self.skipTest("bash-completion script not found")
43 try:
44 with bash_completion.open("rb") as f:
45 tester.stdin.writelines(f)
46 except OSError:
47 self.skipTest("could not read bash-completion script")
48
49 # Load completion script.
50 self.run_command("completion")
51 completion_script = self.io.getoutput().encode("utf-8")
52 tester.stdin.writelines(completion_script.splitlines(True))
53
54 # Load test suite.
55 test_script_name = _common.RSRC / "test_completion.sh"
56 with test_script_name.open("rb") as test_script_file:
57 tester.stdin.writelines(test_script_file)
58 out, _ = tester.communicate()
59 assert tester.returncode == 0
60 assert out == b"completion tests passed\n", (
61 "test/test_completion.sh did not execute properly. "
62 f"Output:{out.decode('utf-8')}"
63 )