5f180bcb6b27d9ac3953354bd45179d98f583cf1
[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 string
28 import sys
29 import types
30 import unittest
31
32 import TestCmd
33 import SCons.Node.FS
34 import SCons.Scanner.Dir
35
36 #class DummyNode:
37 #    def __init__(self, name, fs):
38 #        self.name = name
39 #        self.abspath = test.workpath(name)
40 #        self.fs = fs
41 #    def __str__(self):
42 #        return self.name
43 #    def Entry(self, name):
44 #        return self.fs.Entry(name)
45
46 class DummyEnvironment:
47     def __init__(self, root):
48         self.fs = SCons.Node.FS.FS(root)
49     def Dir(self, name):
50         return self.fs.Dir(name)
51     def Entry(self, name):
52         return self.fs.Entry(name)
53     def File(self, name):
54         return self.fs.File(name)
55     def get_factory(self, factory):
56         return factory or self.fs.Entry
57
58 class DirScannerTestBase(unittest.TestCase):
59     def setUp(self):
60         self.test = TestCmd.TestCmd(workdir = '')
61
62         self.test.subdir('dir', ['dir', 'sub'])
63
64         self.test.write(['dir', 'f1'], "dir/f1\n")
65         self.test.write(['dir', 'f2'], "dir/f2\n")
66         self.test.write(['dir', '.sconsign'], "dir/.sconsign\n")
67         self.test.write(['dir', '.sconsign.bak'], "dir/.sconsign.bak\n")
68         self.test.write(['dir', '.sconsign.dat'], "dir/.sconsign.dat\n")
69         self.test.write(['dir', '.sconsign.db'], "dir/.sconsign.db\n")
70         self.test.write(['dir', '.sconsign.dblite'], "dir/.sconsign.dblite\n")
71         self.test.write(['dir', '.sconsign.dir'], "dir/.sconsign.dir\n")
72         self.test.write(['dir', '.sconsign.pag'], "dir/.sconsign.pag\n")
73         self.test.write(['dir', 'sub', 'f3'], "dir/sub/f3\n")
74         self.test.write(['dir', 'sub', 'f4'], "dir/sub/f4\n")
75         self.test.write(['dir', 'sub', '.sconsign'], "dir/.sconsign\n")
76         self.test.write(['dir', 'sub', '.sconsign.bak'], "dir/.sconsign.bak\n")
77         self.test.write(['dir', 'sub', '.sconsign.dat'], "dir/.sconsign.dat\n")
78         self.test.write(['dir', 'sub', '.sconsign.dblite'], "dir/.sconsign.dblite\n")
79         self.test.write(['dir', 'sub', '.sconsign.dir'], "dir/.sconsign.dir\n")
80         self.test.write(['dir', 'sub', '.sconsign.pag'], "dir/.sconsign.pag\n")
81
82 class DirScannerTestCase(DirScannerTestBase):
83     def runTest(self):
84         env = DummyEnvironment(self.test.workpath())
85
86         s = SCons.Scanner.Dir.DirScanner()
87
88         expect = [
89             os.path.join('dir', 'f1'),
90             os.path.join('dir', 'f2'),
91             os.path.join('dir', 'sub'),
92         ]
93         deps = s(env.Dir('dir'), env, ())
94         sss = map(str, deps)
95         assert sss == expect, sss
96
97         expect = [
98             os.path.join('dir', 'sub', 'f3'),
99             os.path.join('dir', 'sub', 'f4'),
100         ]
101         deps = s(env.Dir('dir/sub'), env, ())
102         sss = map(str, deps)
103         assert sss == expect, sss
104
105 class DirEntryScannerTestCase(DirScannerTestBase):
106     def runTest(self):
107         env = DummyEnvironment(self.test.workpath())
108
109         s = SCons.Scanner.Dir.DirEntryScanner()
110
111         deps = s(env.Dir('dir'), env, ())
112         sss = map(str, deps)
113         assert sss == [], sss
114
115         deps = s(env.Dir('dir/sub'), env, ())
116         sss = map(str, deps)
117         assert sss == [], sss
118
119         # Make sure we don't blow up if handed a non-Dir node.
120         deps = s(env.File('dir/f1'), env, ())
121         sss = map(str, deps)
122         assert sss == [], sss
123
124 def suite():
125     suite = unittest.TestSuite()
126     suite.addTest(DirScannerTestCase())
127     suite.addTest(DirEntryScannerTestCase())
128     return suite
129
130 if __name__ == "__main__":
131     runner = unittest.TextTestRunner()
132     result = runner.run(suite())
133     if not result.wasSuccessful():
134         sys.exit(1)