http://scons.tigris.org/issues/show_bug.cgi?id=2329
[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     fp = open(str(target[0]), 'wb')
85     for c in sorted(source[0].children(), key=lambda t: t.name):
86         fp.write('%s\\n' % c)
87     fp.close()
88 Command('list.out', 'subdir', foo, source_scanner = DirScanner)
89 SConscript('subdir/SConscript')
90 """)
91
92 test.write(['subdir', 'SConscript'], """\
93 import SCons.Scanner
94 kscan = SCons.Scanner.Classic(name = 'kfile',
95                               suffixes = ['.k'],
96                               path_variable = 'KPATH',
97                               regex = r'^include\s+(\S+)$')
98
99 env = Environment(KPATH=['.', '..'])
100 env.Append(SCANNERS = kscan)
101
102 env.Command('foo', 'foo.k', r'%(_python_)s build.py "$KPATH" $SOURCES $TARGET')
103 """ % locals())
104
105 test.write(['subdir', 'foo.k'], """\
106 subdir/foo.k
107 include inc1/include.h
108 include %(inc2_include_h)s
109 """ % locals())
110
111 test.write(['inc1', 'include.h'], """\
112 inc1/include.h
113 """)
114
115 test.write(['inc2', 'include.h'], """\
116 inc2/include.h
117 """)
118
119 test.run(arguments = '.')
120
121 test.must_match('subdir/foo', """\
122 subdir/foo.k
123 inc1/include.h
124 inc2/include.h
125 """)
126
127 test.must_match('list.out', """\
128 %(subdir_SConscript)s
129 %(subdir_foo)s
130 %(subdir_foo_k)s
131 """ % locals())
132
133 test.pass_test()
134
135 # Local Variables:
136 # tab-width:4
137 # indent-tabs-mode:nil
138 # End:
139 # vim: set expandtab tabstop=4 shiftwidth=4: