Repository: pimutils/khal
Pull request #723 (2017-10-07 22:28:49)

Fix/683


Patch:
diff --git a/khal/cli.py b/khal/cli.py
index f53a6d9..94a272c 100644
--- a/khal/cli.py
+++ b/khal/cli.py
@@ -21,6 +21,7 @@
 #
 import datetime as dt
 import logging
+import os
 import sys
 import textwrap
 from shutil import get_terminal_size
@@ -435,7 +436,11 @@ def _get_cli():
             # Default to stdin:
             if not ics:
                 ics_strs = (sys.stdin.read(),)
-                sys.stdin = open('/dev/tty', 'r')
+                if not batch:
+                    if os.path.isfile('/dev/tty'):
+                        sys.stdin = open('/dev/tty', 'r')
+                    else:
+                        logger.warning('/dev/tty does not exist, importing might not work')
             else:
                 ics_strs = (ics_file.read() for ics_file in ics)
 
@@ -529,7 +534,6 @@ def _get_cli():
             else:
                 ics_str = sys.stdin.read()
                 name = 'stdin input'
-                sys.stdin = open('/dev/tty', 'r')
             controllers.print_ics(ctx.obj['conf'], name, ics_str, format)
         except FatalError as error:
             logger.debug(error, exc_info=True)


Test patch:
diff --git a/tests/cli_test.py b/tests/cli_test.py
index 7f97707..90d4558 100644
--- a/tests/cli_test.py
+++ b/tests/cli_test.py
@@ -1,7 +1,6 @@
 import datetime as dt
 import os
 import sys
-from unittest import mock
 
 import pytest
 from click.testing import CliRunner
@@ -493,16 +492,31 @@ def test_import_invalid_choice_and_prefix(runner):
     assert result.output == '09.04.-09.04. An Event\n'
 
 
-def test_import_from_stdin(runner):
+def test_import_from_stdin(runner, monkeypatch):
     ics_data = 'This is some really fake icalendar data'
 
-    with mock.patch('khal.controllers.import_ics') as mocked_import:
-        runner = runner()
-        result = runner.invoke(main_khal, ['import'], input=ics_data)
+    class FakeImport():
+        args, kwargs = None, None
+        call_count = 0
+
+        def clean(self):
+            self.args, self.kwargs = None, None
+
+        def import_ics(self, *args, **kwargs):
+            print('saving args')
+            print(args)
+            self.call_count += 1
+            self.args = args
+            self.kwargs = kwargs
+
+    importer = FakeImport()
+    monkeypatch.setattr('khal.controllers.import_ics', importer.import_ics)
+    runner = runner()
+    result = runner.invoke(main_khal, ['import'], input=ics_data)
 
     assert not result.exception
-    assert mocked_import.call_count == 1
-    assert mocked_import.call_args[1]['ics'] == ics_data
+    assert importer.call_count == 1
+    assert importer.kwargs['ics'] == ics_data
 
 
 def test_interactive_command(runner, monkeypatch):
@@ -679,7 +693,7 @@ def test_printics_read_from_stdin(runner):
     runner = runner(command='printics')
     result = runner.invoke(main_khal, ['printics'], input=_get_text('cal_d'))
     assert not result.exception
-    assert result.output == '1 events found in stdin input\n An Event\n'
+    assert '1 events found in stdin input\n An Event\n' in result.output
 
 
 def test_configure_command_config_exists(runner):