http://scons.tigris.org/issues/show_bug.cgi?id=2329
[scons.git] / src / engine / SCons / Scanner / DirTests.py
1 #
2 # __COPYRIGHT__
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
16 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #
23
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
25
26 import os.path
27 import sys
28 import unittest
29
30 import TestCmd
31 import SCons.Node.FS
32 import SCons.Scanner.Dir
33
34 #class DummyNode:
35 #    def __init__(self, name, fs):
36 #        self.name = name
37 #        self.abspath = test.workpath(name)
38 #        self.fs = fs
39 #    def __str__(self):
40 #        return self.name
41 #    def Entry(self, name):
42 #        return self.fs.Entry(name)
43
44 class DummyEnvironment:
45     def __init__(self, root):
46         self.fs = SCons.Node.FS.FS(root)
47     def Dir(self, name):
48         return self.fs.Dir(name)
49     def Entry(self, name):
50         return self.fs.Entry(name)
51     def File(self, name):
52         return self.fs.File(name)
53     def get_factory(self, factory):
54         return factory or self.fs.Entry
55
56 class DirScannerTestBase(unittest.TestCase):
57     def setUp(self):
58         self.test = TestCmd.TestCmd(workdir = '')
59
60         self.test.subdir('dir', ['dir', 'sub'])
61
62         self.test.write(['dir', 'f1'], "dir/f1\n")
63         self.test.write(['dir', 'f2'], "dir/f2\n")
64         self.test.write(['dir', '.sconsign'], "dir/.sconsign\n")
65         self.test.write(['dir', '.sconsign.bak'], "dir/.sconsign.bak\n")
66         self.test.write(['dir', '.sconsign.dat'], "dir/.sconsign.dat\n")
67         self.test.write(['dir', '.sconsign.db'], "dir/.sconsign.db\n")
68         self.test.write(['dir', '.sconsign.dblite'], "dir/.sconsign.dblite\n")
69         self.test.write(['dir', '.sconsign.dir'], "dir/.sconsign.dir\n")
70         self.test.write(['dir', '.sconsign.pag'], "dir/.sconsign.pag\n")
71         self.test.write(['dir', 'sub', 'f3'], "dir/sub/f3\n")
72         self.test.write(['dir', 'sub', 'f4'], "dir/sub/f4\n")
73         self.test.write(['dir', 'sub', '.sconsign'], "dir/.sconsign\n")
74         self.test.write(['dir', 'sub', '.sconsign.bak'], "dir/.sconsign.bak\n")
75         self.test.write(['dir', 'sub', '.sconsign.dat'], "dir/.sconsign.dat\n")
76         self.test.write(['dir', 'sub', '.sconsign.dblite'], "dir/.sconsign.dblite\n")
77         self.test.write(['dir', 'sub', '.sconsign.dir'], "dir/.sconsign.dir\n")
78         self.test.write(['dir', 'sub', '.sconsign.pag'], "dir/.sconsign.pag\n")
79
80 class DirScannerTestCase(DirScannerTestBase):
81     def runTest(self):
82         env = DummyEnvironment(self.test.workpath())
83
84         s = SCons.Scanner.Dir.DirScanner()
85
86         expect = [
87             os.path.join('dir', 'f1'),
88             os.path.join('dir', 'f2'),
89             os.path.join('dir', 'sub'),
90         ]
91         deps = s(env.Dir('dir'), env, ())
92         sss = list(map(str, deps))
93         assert sss == expect, sss
94
95         expect = [
96             os.path.join('dir', 'sub', 'f3'),
97             os.path.join('dir', 'sub', 'f4'),
98         ]
99         deps = s(env.Dir('dir/sub'), env, ())
100         sss = list(map(str, deps))
101         assert sss == expect, sss
102
103 class DirEntryScannerTestCase(DirScannerTestBase):
104     def runTest(self):
105         env = DummyEnvironment(self.test.workpath())
106
107         s = SCons.Scanner.Dir.DirEntryScanner()
108
109         deps = s(env.Dir('dir'), env, ())
110         sss = list(map(str, deps))
111         assert sss == [], sss
112
113         deps = s(env.Dir('dir/sub'), env, ())
114         sss = list(map(str, deps))
115         assert sss == [], sss
116
117         # Make sure we don't blow up if handed a non-Dir node.
118         deps = s(env.File('dir/f1'), env, ())
119         sss = list(map(str, deps))
120         assert sss == [], sss
121
122 def suite():
123     suite = unittest.TestSuite()
124     suite.addTest(DirScannerTestCase())
125     suite.addTest(DirEntryScannerTestCase())
126     return suite
127
128 if __name__ == "__main__":
129     runner = unittest.TextTestRunner()
130     result = runner.run(suite())
131     if not result.wasSuccessful():
132         sys.exit(1)
133
134 # Local Variables:
135 # tab-width:4
136 # indent-tabs-mode:nil
137 # End:
138 # vim: set expandtab tabstop=4 shiftwidth=4: