#!/usr/bin/python

import os
import time
import shutil
from livereload.watcher import Watcher

tmpdir = os.path.join(os.path.dirname(__file__), 'tmp')


class TestWatcher(object):

    def setUp(self):
        if os.path.isdir(tmpdir):
            shutil.rmtree(tmpdir)
        os.mkdir(tmpdir)

    def test_watch_dir(self):
        os.mkdir(os.path.join(tmpdir, '.git'))
        os.mkdir(os.path.join(tmpdir, '.hg'))
        os.mkdir(os.path.join(tmpdir, '.svn'))
        os.mkdir(os.path.join(tmpdir, '.cvs'))

        watcher = Watcher()
        watcher.watch(tmpdir)
        assert watcher.is_changed(tmpdir) is False

        with open(os.path.join(tmpdir, 'foo'), 'w') as f:
            f.write('')

        assert watcher.is_changed(tmpdir)
        assert watcher.is_changed(tmpdir) is False

    def test_watch_file(self):
        watcher = Watcher()
        watcher.count = 0

        filepath = os.path.join(tmpdir, 'foo')
        with open(filepath, 'w') as f:
            f.write('')

        def add_count():
            watcher.count += 1

        watcher.watch(filepath, add_count)
        assert watcher.is_changed(filepath)

        # sleep 1 second so that mtime will be different
        time.sleep(1)

        with open(filepath, 'w') as f:
            f.write('')

        assert watcher.examine() == os.path.abspath(filepath)
        assert watcher.count == 1

    def test_watch_glob(self):
        watcher = Watcher()
        watcher.watch(tmpdir + '/*')
        assert watcher.examine() is None

        with open(os.path.join(tmpdir, 'foo.pyc'), 'w') as f:
            f.write('')

        assert watcher.examine() is None

        filepath = os.path.join(tmpdir, 'foo')

        with open(filepath, 'w') as f:
            f.write('')

        assert watcher.examine() == os.path.abspath(filepath)

@@ -48,16 +48,32 @@
  shutil%0A
+import unittest%0A
 from liv
@@ -184,14 +184,25 @@
 her(
-object
+unittest.TestCase
 ):%0A%0A
@@ -316,16 +316,71 @@
 mpdir)%0A%0A
+    def tearDown(self):%0A        shutil.rmtree(tmpdir)%0A%0A
     def 
@@ -695,24 +695,104 @@
 ) is False%0A%0A
+        # sleep 1 second so that mtime will be different%0A        time.sleep(1)%0A%0A
         with
@@ -1042,16 +1042,96 @@
 nt = 0%0A%0A
+        # sleep 1 second so that mtime will be different%0A        time.sleep(1)%0A%0A
         
@@ -1522,38 +1522,36 @@
 te('')%0A%0A        
-assert
+rv =
  watcher.examine
@@ -1544,32 +1544,53 @@
 atcher.examine()
+%0A        assert rv%5B0%5D
  == os.path.absp
@@ -1759,39 +1759,47 @@
 tcher.examine() 
-is None
+== (None, None)
 %0A%0A        with o
@@ -1908,15 +1908,23 @@
 e() 
-is None
+== (None, None)
 %0A%0A  
@@ -2033,38 +2033,36 @@
 te('')%0A%0A        
-assert
+rv =
  watcher.examine
@@ -2059,24 +2059,45 @@
 er.examine()
+%0A        assert rv%5B0%5D
  == os.path.

Fix test cases