Issue 2168: Mac OS X fixes for SWIG tests. (Greg Noel)
[scons.git] / test / Mkdir.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 the Mkdir() Action works.
29 """
30
31 import os.path
32
33 import TestSCons
34
35 test = TestSCons.TestSCons()
36
37 test.subdir('work1', 'work2')
38
39 test.write(['work1', 'SConstruct'], """
40 Execute(Mkdir('d1'))
41 Execute(Mkdir(Dir('#d1-Dir')))
42 def cat(env, source, target):
43     target = str(target[0])
44     source = map(str, source)
45     f = open(target, "wb")
46     for src in source:
47         f.write(open(src, "rb").read())
48     f.close()
49 Cat = Action(cat)
50 env = Environment()
51 env.Command('f2.out', 'f2.in', [Cat, Mkdir("d3")])
52 env = Environment(DIR = 'd4')
53 env.Command('f5.out', 'f5.in', [Mkdir("$DIR"), Cat])
54 env.Command('f6.out', 'f6.in', [Cat,
55                                 Mkdir("Mkdir-$SOURCE"),
56                                 Mkdir("$TARGET-Mkdir")])
57 # Make sure that a user-defined Mkdir builder on a directory
58 # doesn't get executed twice if it has to get called to create
59 # directory for another target.
60 env.Command(Dir('hello'), None, [Mkdir('$TARGET')])
61 env.Command('hello/world', None, [Touch('$TARGET')])
62
63 # Make sure Mkdir works with a list of arguments
64 Execute(Mkdir(['d7', Dir('d8')]))
65 """)
66
67 test.write(['work1', 'f2.in'], "f2.in\n")
68 test.write(['work1', 'f5.in'], "f5.in\n")
69 test.write(['work1', 'f6.in'], "f6.in\n")
70
71 expect = test.wrap_stdout(read_str = """\
72 Mkdir("d1")
73 Mkdir("d1-Dir")
74 Mkdir(["d7", "d8"])
75 """,
76                           build_str = """\
77 cat(["f2.out"], ["f2.in"])
78 Mkdir("d3")
79 Mkdir("d4")
80 cat(["f5.out"], ["f5.in"])
81 cat(["f6.out"], ["f6.in"])
82 Mkdir("Mkdir-f6.in")
83 Mkdir("f6.out-Mkdir")
84 Mkdir("hello")
85 Touch("%s")
86 """ % (os.path.join('hello', 'world')))
87 test.run(chdir = 'work1', options = '-n', arguments = '.', stdout = expect)
88
89 test.must_not_exist(['work1', 'd1'])
90 test.must_not_exist(['work1', 'd1-Dir'])
91 test.must_not_exist(['work1', 'f2.out'])
92 test.must_not_exist(['work1', 'd3'])
93 test.must_not_exist(['work1', 'd4'])
94 test.must_not_exist(['work1', 'f5.out'])
95 test.must_not_exist(['work1', 'f6.out'])
96 test.must_not_exist(['work1', 'Mkdir-f6.in'])
97 test.must_not_exist(['work1', 'f6.out-Mkdir'])
98 test.must_not_exist(['work1', 'd7'])
99 test.must_not_exist(['work1', 'd8'])
100
101 test.run(chdir = 'work1')
102
103 test.must_exist(['work1', 'd1'])
104 test.must_exist(['work1', 'd1-Dir'])
105 test.must_match(['work1', 'f2.out'], "f2.in\n")
106 test.must_exist(['work1', 'd3'])
107 test.must_exist(['work1', 'd4'])
108 test.must_match(['work1', 'f5.out'], "f5.in\n")
109 test.must_exist(['work1', 'f6.out'])
110 test.must_exist(['work1', 'Mkdir-f6.in'])
111 test.must_exist(['work1', 'f6.out-Mkdir'])
112 test.must_exist(['work1', 'hello'])
113 test.must_exist(['work1', 'hello/world'])
114 test.must_exist(['work1', 'd7'])
115 test.must_exist(['work1', 'd8'])
116
117 test.write(['work1', 'd1', 'file'], "d1/file\n")
118 test.write(['work1', 'd3', 'file'], "d3/file\n")
119 test.write(['work1', 'Mkdir-f6.in', 'file'], "Mkdir-f6.in/file\n")
120 test.write(['work1', 'f6.out-Mkdir', 'file'], "f6.out-Mkdir/file\n")
121 test.write(['work1', 'hello', 'file'], "hello/file\n")
122
123
124
125
126 test.write(['work2', 'SConstruct'], """\
127 import os
128 def catdir(env, source, target):
129     target = str(target[0])
130     source = map(str, source)
131     outfp = open(target, "wb")
132     for src in source:
133         l = os.listdir(src)
134         l.sort()
135         for f in l:
136             f = os.path.join(src, f)
137             if os.path.isfile(f):
138                 outfp.write(open(f, "rb").read())
139     outfp.close()
140 CatDir = Builder(action = catdir)
141 env = Environment(BUILDERS = {'CatDir' : CatDir})
142 env.Command(Dir('hello'), None, [Mkdir('$TARGET')])
143 env.Command('hello/file1.out', 'file1.in', [Copy('$TARGET', '$SOURCE')])
144 env.Command('hello/file2.out', 'file2.in', [Copy('$TARGET', '$SOURCE')])
145 env.CatDir('output', Dir('hello'))
146 """)
147
148 test.write(['work2', 'file1.in'], "work2/file1.in\n")
149 test.write(['work2', 'file2.in'], "work2/file2.in\n")
150
151 test.run(chdir = 'work2', arguments = 'hello/file2.out output')
152
153 test.must_match(['work2', 'output'], "work2/file1.in\nwork2/file2.in\n")
154
155 test.pass_test()