Skip to content

Commit

Permalink
Merge pull request #105 from MichaelAquilina/fix_macosx
Browse files Browse the repository at this point in the history
Dont crash on OSX because INotify is unsupported
  • Loading branch information
MichaelAquilina committed Feb 24, 2018
2 parents d177f3f + c1f417b commit cc45272
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ If you wish to synchronise your targets continiously, use the ``daemon`` command

$ s4 daemon myfolder1

NOTE: This command is only supported on machines that can run INotify. This typically means
Linux based operating systems.


Handling Conflicts
------------------
Expand Down
14 changes: 12 additions & 2 deletions s4/commands/daemon_command.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
#! -*- encoding: utf-8 -*-
from collections import defaultdict

from inotify_simple import flags
# Dont crash on import if the underlying operating system does not support INotify
try:
from inotify_simple import flags
from s4.inotify_recursive import INotifyRecursive
supported = True
except OSError:
supported = False

from s4.commands import Command
from s4.inotify_recursive import INotifyRecursive


class DaemonCommand(Command):
def run(self, terminator=lambda x: False):
if not supported:
self.logger.info('Cannot run INotify on your operating system')
self.logger.info('Only Linux machines are officially supported for this command')
return

all_targets = list(self.config['targets'].keys())
if not self.args.targets:
targets = all_targets
Expand Down
16 changes: 16 additions & 0 deletions tests/commands/test_daemon_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ def single_term(self, index):
"""Simple terminator for the daemon command"""
return index >= 1

@mock.patch('s4.commands.daemon_command.supported', False)
def test_os_not_supported(self, INotifyRecursive, SyncWorker, capsys):
args = argparse.Namespace(targets=None, conflicts='ignore', read_delay=0)

command = DaemonCommand(args, {}, create_logger())
command.run(terminator=self.single_term)

out, err = capsys.readouterr()
assert out == ''
assert err == (
'Cannot run INotify on your operating system\n'
'Only Linux machines are officially supported for this command\n'
)
assert SyncWorker.call_count == 0
assert INotifyRecursive.call_count == 0

@pytest.mark.timeout(5)
def test_no_targets(self, INotifyRecursive, SyncWorker, capsys):
args = argparse.Namespace(targets=None, conflicts='ignore', read_delay=0)
Expand Down

0 comments on commit cc45272

Please sign in to comment.