Clean up and simplify logic. TODO: scons -c isn't removing the .di files. Mainly...
[scons.git] / test / YACC / BISONFLAGS.py
blobcae673ded2335efa3110a75897d82d000b3d088d
1 #!/usr/bin/env python
3 # MIT License
5 # Copyright The SCons Foundation
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice shall be included
16 # in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 """
27 Test that detection of file-writing options in YACCFLAGS works.
28 Also test that the construction vars for the same purpose work.
29 """
31 from pathlib import Path
33 import TestSCons
34 from TestCmd import IS_WINDOWS
36 _python_ = TestSCons._python_
37 _exe = TestSCons._exe
39 if IS_WINDOWS:
40 compiler = 'msvc'
41 linker = 'mslink'
42 else:
43 compiler = 'gcc'
44 linker = 'gnulink'
46 test = TestSCons.TestSCons()
48 test.subdir('sub1')
49 test.subdir('sub2')
51 test.dir_fixture('YACCFLAGS-fixture')
53 test.write('SConstruct', """\
54 DefaultEnvironment(tools=[])
55 SConscript(dirs=['sub1', 'sub2'])
56 """)
58 # this SConscript is for the options-in-flags version
59 test.write(['sub1', 'SConscript'], """\
60 import sys
62 env = Environment(
63 YACC=r'%(_python_)s myyacc.py',
64 YACCFLAGS='-x --header=header.h --graph=graph.g',
65 tools=['yacc', '%(linker)s', '%(compiler)s'],
67 targs = env.CFile(target='aaa', source='aaa.y')
68 t = [str(target) for target in targs]
69 # fail ourselves if the two extra files were not detected
70 if not all((len(t) == 3, "header.h" in t, "graph.g" in t)):
71 sys.exit(1)
72 """ % locals())
73 test.write(['sub1', 'aaa.y'], "aaa.y\nYACCFLAGS\n")
75 # this SConscript is for the construction var version
76 test.write(['sub2', 'SConscript'], """\
77 import sys
79 env = Environment(
80 YACC=r'%(_python_)s myyacc.py',
81 YACCFLAGS='-x',
82 tools=['yacc', '%(linker)s', '%(compiler)s'],
84 env.CFile(
85 target='aaa',
86 source='aaa.y',
87 YACC_HEADER_FILE='header.h',
88 YACC_GRAPH_FILE='graph.g',
90 """ % locals())
91 test.write(['sub2', 'aaa.y'], "aaa.y\nYACCFLAGS\n")
93 test.run('.', stderr=None)
94 test.must_match(['sub1', 'aaa.c'], "aaa.y\n -x --header=header.h --graph=graph.g\n")
96 # NOTE: this behavior is "wrong" but we're keeping it for compat:
97 # the generated files should go into 'sub1', not the topdir.
98 test.must_match(['header.h'], 'yacc header\n')
99 test.must_match(['graph.g'], 'yacc graph\n')
101 # To confirm the files from the file-output options were tracked,
102 # we should do a clean and make sure they got removed.
103 # As noted, they currently don't go into the tracked location,
104 # so using the check in the SConscript instead.
105 #test.run(arguments='-c .')
106 #test.must_not_exist(test.workpath(['sub1', 'header.h']))
107 #test.must_not_exist(test.workpath(['sub1', 'graph.g']))
109 sub2 = Path('sub2')
110 headerfile = sub2 / 'header.h'
111 graphfile = sub2 / 'graph.g'
112 yaccflags = f"aaa.y\n -x --header={headerfile} --graph={graphfile}\n"
113 test.must_match(['sub2', 'aaa.c'], yaccflags)
114 test.must_match(['sub2', 'header.h'], 'yacc header\n')
115 test.must_match(['sub2', 'graph.g'], 'yacc graph\n')
117 # To confirm the files from the file-output options were tracked,
118 # do a clean and make sure they got removed. As noted, they currently
119 # don't go into the tracked location, so using the the SConscript check instead.
120 test.run(arguments='-c .')
121 test.must_not_exist(test.workpath('sub2', 'header.h'))
122 test.must_not_exist(test.workpath('sub2', 'graph.g'))
124 test.pass_test()
126 # Local Variables:
127 # tab-width:4
128 # indent-tabs-mode:nil
129 # End:
130 # vim: set expandtab tabstop=4 shiftwidth=4: