"""
Main plugin tests.

.. note:: Please to not run nosetests with this plugin for testing the plugin
   itself. Also, mock.patch is not much help here as it would try to mock the
   module that would be already imported by nose itself if plugin was installed
   globally (it would take module from sys.modules).
"""
import unittest
from mock import Mock
from nosewatch.plugin import WatchPlugin


class TestWatchPlugin(unittest.TestCase):

    def setUp(self):
        self.plugin = WatchPlugin()
        self.plugin.stdout = Mock()
        self.plugin.argv = ['program', 'arg1', '--with-watch', 'arg3', 'arg4']

    def test_finalize(self):
        self.plugin.call = Mock()
        self.plugin.finalize(Mock())

        watchcmd = 'clear && program arg1 arg3 arg4'
        self.plugin.call.assert_called_once_with([
            'watchmedo', 'shell-command', '-c', watchcmd,
            '-R', '-p', '*.py', '-W', '.'])

    def test_finalize_interrupted(self):
        self.plugin.call = Mock(side_effect=KeyboardInterrupt)
        self.plugin.finalize(Mock())
        self.plugin.stdout.write.assert_called_once_with('\nStopped\n')


@@ -347,16 +347,23 @@
 ort Mock
+, patch
 %0Afrom no
@@ -1136,8 +1136,765 @@
 ed%5Cn')%0A%0A
+    def test_works_even_when_tests_mock_sys_argv(self):%0A        with patch('sys.argv') as argv:%0A            argv.return_value = %5B'mocked'%5D%0A            self.plugin.call = Mock()%0A            self.plugin.finalize(Mock())%0A%0A        watchcmd = 'clear && program arg1 arg3 arg4'%0A        self.plugin.call.assert_called_once_with(%5B%0A            'watchmedo', 'shell-command', '-c', watchcmd,%0A            '-R', '-p', '*.py', '-W', '.'%5D)%0A%0A    def test_works_even_when_tests_mock_sys_stdout(self):%0A        with patch('sys.stdout') as stdout:%0A            stdout.return_value = %5B'mocked'%5D%0A            self.plugin.call = Mock(side_effect=KeyboardInterrupt)%0A            self.plugin.finalize(Mock())%0A            self.plugin.stdout.write.assert_called_once_with('%5CnStopped%5Cn')%0A

add tests for #2