Skip to content

Commit

Permalink
unittests: Test the main function
Browse files Browse the repository at this point in the history
  • Loading branch information
solarnz committed Jun 8, 2014
1 parent 269508d commit 9ed3381
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
4 changes: 4 additions & 0 deletions nose_watcher/nose_watcher.py
Expand Up @@ -25,6 +25,7 @@ class WatcherPlugin(Plugin):

# Files ending with these suffixes will cause us to run nostests again.
python_files = ('.py', '.pyx')
testing = False

def call(self):
args = self.get_commandline_arguments()
Expand Down Expand Up @@ -76,3 +77,6 @@ def finalize(self, result):
else:
timeout = 1000
poll.unregister(watcher)

if self.testing:
break
82 changes: 81 additions & 1 deletion tests/test_nose_watcher.py
@@ -1,7 +1,9 @@
# -*- coding: utf-8 -*-
from collections import namedtuple
import sys
import unittest

from mock import Mock
from mock import Mock, patch

from nose_watcher.nose_watcher import WatcherPlugin

Expand All @@ -10,6 +12,7 @@ class TestNoseWatcher(unittest.TestCase):

def setUp(self):
self.plugin = WatcherPlugin()
self.plugin.testing = True
self.plugin.call = Mock()


Expand Down Expand Up @@ -39,3 +42,80 @@ def test_arguments(self):
args_out,
['laa', '--with-cover']
)

def test_argument_parsing_from_sys_argv(self):
self.assertEqual(
self.plugin.get_commandline_arguments(),
[a for a in sys.argv if a != '--with-watcher']
)


class TestWatching(TestNoseWatcher):
@patch('inotify.watcher.AutoWatcher')
@patch('inotify.watcher.Threshold')
@patch('select.poll')
def test_watch_no_files_modified(self, poll_mock, threshold_patch,
watcher_mock):
threshold = Mock()
threshold_patch.return_value = threshold
threshold.return_value = True
self.plugin.finalize(None)

self.assertFalse(self.plugin.call.called)

@patch('inotify.watcher.AutoWatcher')
@patch('inotify.watcher.Threshold')
@patch('select.poll')
def test_watch_no_files_watched(self, poll_mock, threshold_patch,
watcher_mock):
threshold = Mock()
threshold_patch.return_value = threshold
threshold.return_value = True

watcher = Mock()
watcher_mock.return_value = watcher

watcher.num_watches.return_value = False
self.plugin.finalize(None)

self.assertFalse(self.plugin.call.called)

@patch('inotify.watcher.AutoWatcher')
@patch('inotify.watcher.Threshold')
@patch('select.poll')
def test_watch_python_files_modified(self, poll_mock, threshold_patch,
watcher_mock):
threshold = Mock()
threshold_patch.return_value = threshold
threshold.return_value = True

watcher = Mock()
watcher_mock.return_value = watcher

Event = namedtuple('Event', ['fullpath'])
watcher.read.return_value = [
Event('aaa/python.py')
]
self.plugin.finalize(None)

self.assertTrue(self.plugin.call.called)

@patch('inotify.watcher.AutoWatcher')
@patch('inotify.watcher.Threshold')
@patch('select.poll')
def test_watch_text_files_modified(self, poll_mock, threshold_patch,
watcher_mock):
threshold = Mock()
threshold_patch.return_value = threshold
threshold.return_value = True

watcher = Mock()
watcher_mock.return_value = watcher

Event = namedtuple('Event', ['fullpath'])
watcher.read.return_value = [
Event('aaa/python.txt')
]
self.plugin.finalize(None)

self.assertFalse(self.plugin.call.called)

0 comments on commit 9ed3381

Please sign in to comment.