garethr / localbuilder

Python script for monitoring a given directory for changes and running a command when something changes

This URL has Read+Write access

localbuilder / test_localbuilder.py
100644 56 lines (44 sloc) 1.856 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
 
import sys
import unittest
import commands
import StringIO
 
from localbuilder import run_if_changes, get_changed_files
 
class CITests(unittest.TestCase):
 
    def setUp(self):
        self.old_value_of_stdout = sys.stdout
        sys.stdout = StringIO.StringIO()
    def tearDown(self):
        sys.stdout = self.old_value_of_stdout
        
    def assert_equal(self, *args, **kwargs):
        "Assert that two values are equal"
        return self.assertEqual(*args, **kwargs)
 
    def assert_not_equal(self, *args, **kwargs):
        "Assert that two values are not equal"
        return not self.assertEqual(*args, **kwargs)
 
    def test_get_changed_files(self):
        outcome = get_changed_files('.',0)
        self.assert_equal([], outcome)
 
    def test_get_changed_files(self):
        outcome = get_changed_files('.',0)
        self.assert_equal([], outcome)
 
    def test_get_changed_files_gets_correct_output(self):
        commands.getoutput("mkdir testdir1234")
        commands.getoutput("touch testdir1234/testfile1234")
        outcome = get_changed_files('testdir1234', 1)
        self.assert_equal(1, len(outcome))
        self.assert_equal(['testdir1234/testfile1234'], outcome)
        commands.getoutput("rm testdir1234/testfile1234")
        commands.getoutput("rmdir testdir1234")
 
    def test_run_if_changes_does_nothing_with_blank_list(self):
        empty_list = []
        outcome = run_if_changes(empty_list, "ls")
        self.assert_equal(False, outcome)
 
    def test_run_if_changes_runs_command_with_list(self):
        empty_list = [1,]
        command_to_run = "ls"
        expected = commands.getoutput(command_to_run)
        outcome = run_if_changes(empty_list, command_to_run)
        self.assert_equal(expected, outcome)
        
if __name__ == "__main__":
    unittest.main()