9a47c018915dab117c7998cc3179560256bb42cd
[scons.git] / test / Scanner / no-Dir-node.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 """
28 Verify that use of a Scanner that searches a *PATH list doesn't create
29 nodes for directories that don't exist, so they don't get picked up
30 by DirScanner.
31
32 Under the covers, this tests the behavior of the SCons.Node.FS.find_file()
33 utility function that is used by the Scanner.Classic class to search
34 directories in variables such as $CPPPATH.
35 """
36
37 import os.path
38
39 import TestSCons
40
41 _python_ = TestSCons._python_
42
43 test = TestSCons.TestSCons()
44
45 subdir_SConscript = os.path.join('subdir', 'SConscript')
46 subdir_foo        = os.path.join('subdir', 'foo')
47 subdir_foo_k      = os.path.join('subdir', 'foo.k')
48
49 test.subdir('subdir', 'inc1', 'inc2')
50
51 inc2_include_h = test.workpath('inc2', 'include.h')
52
53 test.write('build.py', r"""
54 import os.path
55 import sys
56 path = sys.argv[1].split()
57 input = open(sys.argv[2], 'rb')
58 output = open(sys.argv[3], 'wb')
59
60 def find_file(f):
61     if os.path.isabs(f):
62         return open(f, 'rb')
63     for dir in path:
64         p = dir + os.sep + f
65         if os.path.exists(p):
66             return open(p, 'rb')
67     return None
68
69 def process(infp, outfp):
70     for line in infp.readlines():
71         if line[:8] == 'include ':
72             file = line[8:-1]
73             process(find_file(file), outfp)
74         else:
75             outfp.write(line)
76
77 process(input, output)
78
79 sys.exit(0)
80 """)
81
82 test.write('SConstruct', """\
83 def foo(target, source, env):
84     children = source[0].children()
85     children.sort(lambda a,b: cmp(a.name, b.name))
86     fp = open(str(target[0]), 'wb')
87     for c in children:
88         fp.write('%s\\n' % c)
89     fp.close()
90 Command('list.out', 'subdir', foo, source_scanner = DirScanner)
91 SConscript('subdir/SConscript')
92 """)
93
94 test.write(['subdir', 'SConscript'], """\
95 import SCons.Scanner
96 kscan = SCons.Scanner.Classic(name = 'kfile',
97                               suffixes = ['.k'],
98                               path_variable = 'KPATH',
99                               regex = r'^include\s+(\S+)$')
100
101 env = Environment(KPATH=['.', '..'])
102 env.Append(SCANNERS = kscan)
103
104 env.Command('foo', 'foo.k', r'%(_python_)s build.py "$KPATH" $SOURCES $TARGET')
105 """ % locals())
106
107 test.write(['subdir', 'foo.k'], """\
108 subdir/foo.k
109 include inc1/include.h
110 include %(inc2_include_h)s
111 """ % locals())
112
113 test.write(['inc1', 'include.h'], """\
114 inc1/include.h
115 """)
116
117 test.write(['inc2', 'include.h'], """\
118 inc2/include.h
119 """)
120
121 test.run(arguments = '.')
122
123 test.must_match('subdir/foo', """\
124 subdir/foo.k
125 inc1/include.h
126 inc2/include.h
127 """)
128
129 test.must_match('list.out', """\
130 %(subdir_SConscript)s
131 %(subdir_foo)s
132 %(subdir_foo_k)s
133 """ % locals())
134
135 test.pass_test()
136
137 # Local Variables:
138 # tab-width:4
139 # indent-tabs-mode:nil
140 # End:
141 # vim: set expandtab tabstop=4 shiftwidth=4: