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