Skip to content
Open
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
9 changes: 9 additions & 0 deletions bin/apport-cli
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ class CLIDialog:
sys.stdout.flush()

file = sys.stdin.fileno()

if not os.isatty(file):
if multi_char:
response = str(sys.stdin.readline()).strip()
else:
response = str(sys.stdin.read(1))
sys.stdout.write("\n")
return response

saved_attributes = termios.tcgetattr(file)
attributes = termios.tcgetattr(file)
attributes[3] = attributes[3] & ~(termios.ICANON)
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/test_ui_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,29 @@ def test_save_report_in_temp_directory(self, stdout_mock: io.StringIO) -> None:
run_mock.return_value = 4
self.app.ui_present_report_details()
self.assertIn(_("Problem report file:"), stdout_mock.getvalue())

@unittest.mock.patch("os.isatty")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_raw_input_char_non_tty(self, mock_stdout, mock_isatty):
mock_isatty.return_value = False
mock_stdin = unittest.mock.MagicMock()
mock_stdin.fileno.return_value = 0
mock_stdin.read.return_value = "y"
with unittest.mock.patch("sys.stdin", mock_stdin):
response = apport_cli.CLIDialog.raw_input_char("Prompt?")
self.assertEqual(response, "y")
self.assertEqual(mock_stdout.getvalue(), "Prompt? \n")
mock_stdin.read.assert_called_with(1)

@unittest.mock.patch("os.isatty")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_raw_input_char_non_tty_multi(self, mock_stdout, mock_isatty):
mock_isatty.return_value = False
mock_stdin = unittest.mock.MagicMock()
mock_stdin.fileno.return_value = 0
mock_stdin.readline.return_value = "yes\n"
with unittest.mock.patch("sys.stdin", mock_stdin):
response = apport_cli.CLIDialog.raw_input_char("Prompt?", multi_char=True)
self.assertEqual(response, "yes")
self.assertEqual(mock_stdout.getvalue(), "Prompt? \n")
mock_stdin.readline.assert_called_with()
Loading