summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/Scanner/FortranTests.py
blob: d70035c665cc7710b5129ea9b03ca09ad3a42794 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#
# Copyright (c) 2001, 2002, 2003 Steven Knight
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import os
import os.path
import sys
import unittest

import SCons.Scanner.Fortran
import SCons.Node.FS
import SCons.Warnings

import TestCmd

original = os.getcwd()

test = TestCmd.TestCmd(workdir = '')

os.chdir(test.workpath(''))

# create some source files and headers:

test.write('fff1.f',"""
      PROGRAM FOO
      INCLUDE 'f1.f'
      INCLUDE 'f2.f'
      STOP
      END
""")

test.write('fff2.f',"""
      PROGRAM FOO
      INCLUDE 'f2.f'
      INCLUDE 'd1/f2.f'
      INCLUDE 'd2/f2.f'
      STOP
      END
""")

test.write('fff3.f',"""
      PROGRAM FOO
      INCLUDE 'f3.f' ; INCLUDE\t'd1/f3.f'
      STOP
      END
""")


# for Emacs -> "

test.subdir('d1', ['d1', 'd2'])

headers = ['fi.f', 'never.f',
           'd1/f1.f', 'd1/f2.f', 'd1/f3.f', 'd1/fi.f',
           'd1/d2/f1.f', 'd1/d2/f2.f', 'd1/d2/f3.f',
           'd1/d2/f4.f', 'd1/d2/fi.f']

for h in headers:
    test.write(h, "\n")


test.subdir('include', 'subdir', ['subdir', 'include'])

test.write('fff4.f',"""
      PROGRAM FOO
      INCLUDE 'f4.f'
      STOP
      END
""")

test.write('include/f4.f', "\n")
test.write('subdir/include/f4.f', "\n")

test.write('fff5.f',"""
      PROGRAM FOO
      INCLUDE 'f5.f'
      INCLUDE 'not_there.f'
      STOP
      END
""")

test.write('f5.f', "\n")

test.subdir('repository', ['repository', 'include'],
            [ 'repository', 'src' ])
test.subdir('work', ['work', 'src'])

test.write(['repository', 'include', 'iii.f'], "\n")

test.write(['work', 'src', 'fff.f'], """
      PROGRAM FOO
      INCLUDE 'iii.f'
      INCLUDE 'jjj.f'
      STOP
      END
""")

test.write([ 'work', 'src', 'aaa.f'], """
      PROGRAM FOO
      INCLUDE 'bbb.f'
      STOP
      END
""")

test.write([ 'work', 'src', 'bbb.f'], "\n")

test.write([ 'repository', 'src', 'ccc.f'], """
      PROGRAM FOO
      INCLUDE 'ddd.f'
      STOP
      END
""")

test.write([ 'repository', 'src', 'ddd.f'], "\n")

# define some helpers:

class DummyEnvironment:
    def __init__(self, listCppPath):
        self.path = listCppPath
        
    def Dictionary(self, *args):
        if not args:
            return { 'F77PATH': self.path }
        elif len(args) == 1 and args[0] == 'F77PATH':
            return self.path
        else:
            raise KeyError, "Dummy environment only has F77PATH attribute."

    def has_key(self, key):
        return self.Dictionary().has_key(key)

    def __getitem__(self,key):
        return self.Dictionary()[key]

    def __setitem__(self,key,value):
        self.Dictionary()[key] = value

    def __delitem__(self,key):
        del self.Dictionary()[key]

    def subst(self, arg):
        return arg

def deps_match(self, deps, headers):
    scanned = map(os.path.normpath, map(str, deps))
    expect = map(os.path.normpath, headers)
    self.failUnless(scanned == expect, "expect %s != scanned %s" % (expect, scanned))

def make_node(filename, fs=SCons.Node.FS.default_fs):
    return fs.File(test.workpath(filename))

# define some tests:

class FortranScannerTestCase1(unittest.TestCase):
    def runTest(self):
        test.write('f1.f', "\n")
        test.write('f2.f', "      INCLUDE 'fi.f'\n")
        env = DummyEnvironment([])
        s = SCons.Scanner.Fortran.FortranScan()
        path = s.path(env)
        fs = SCons.Node.FS.FS(original)
        deps = s(make_node('fff1.f', fs), env, path)
        headers = ['f1.f', 'f2.f']
        deps_match(self, deps, map(test.workpath, headers))
        test.unlink('f1.f')
        test.unlink('f2.f')

class FortranScannerTestCase2(unittest.TestCase):
    def runTest(self):
        test.write('f1.f', "\n")
        test.write('f2.f', "      INCLUDE 'fi.f'\n")
        env = DummyEnvironment([test.workpath("d1")])
        s = SCons.Scanner.Fortran.FortranScan()
        path = s.path(env)
        fs = SCons.Node.FS.FS(original)
        deps = s(make_node('fff1.f', fs), env, path)
        headers = ['f1.f', 'f2.f']
        deps_match(self, deps, map(test.workpath, headers))
        test.unlink('f1.f')
        test.unlink('f2.f')

class FortranScannerTestCase3(unittest.TestCase):
    def runTest(self):
        env = DummyEnvironment([test.workpath("d1")])
        s = SCons.Scanner.Fortran.FortranScan()
        path = s.path(env)
        fs = SCons.Node.FS.FS(original)
        deps = s(make_node('fff1.f', fs), env, path)
        headers = ['d1/f1.f', 'd1/f2.f']
        deps_match(self, deps, map(test.workpath, headers))

class FortranScannerTestCase4(unittest.TestCase):
    def runTest(self):
        test.write(['d1', 'f2.f'], "      INCLUDE 'fi.f'\n")
        env = DummyEnvironment([test.workpath("d1")])
        s = SCons.Scanner.Fortran.FortranScan()
        path = s.path(env)
        fs = SCons.Node.FS.FS(original)
        deps = s(make_node('fff1.f', fs), env, path)
        headers = ['d1/f1.f', 'd1/f2.f']
        deps_match(self, deps, map(test.workpath, headers))
        test.write(['d1', 'f2.f'], "\n")

class FortranScannerTestCase5(unittest.TestCase):
    def runTest(self):
        env = DummyEnvironment([test.workpath("d1")])
        s = SCons.Scanner.Fortran.FortranScan()
        path = s.path(env)
        fs = SCons.Node.FS.FS(original)
        deps = s(make_node('fff2.f', fs), env, path)
        headers = ['d1/d2/f2.f', 'd1/f2.f', 'd1/f2.f']
        deps_match(self, deps, map(test.workpath, headers))

class FortranScannerTestCase6(unittest.TestCase):
    def runTest(self):
        test.write('f2.f', "\n")
        env = DummyEnvironment([test.workpath("d1")])
        s = SCons.Scanner.Fortran.FortranScan()
        path = s.path(env)
        fs = SCons.Node.FS.FS(original)
        deps = s(make_node('fff2.f', fs), env, path)
        headers =  ['d1/d2/f2.f', 'd1/f2.f', 'f2.f']
        deps_match(self, deps, map(test.workpath, headers))
        test.unlink('f2.f')

class FortranScannerTestCase7(unittest.TestCase):
    def runTest(self):
        env = DummyEnvironment([test.workpath("d1/d2"), test.workpath("d1")])
        s = SCons.Scanner.Fortran.FortranScan()
        path = s.path(env)
        fs = SCons.Node.FS.FS(original)
        deps = s(make_node('fff2.f', fs), env, path)
        headers =  ['d1/d2/f2.f', 'd1/d2/f2.f', 'd1/f2.f']
        deps_match(self, deps, map(test.workpath, headers))

class FortranScannerTestCase8(unittest.TestCase):
    def runTest(self):
        test.write('f2.f', "\n")
        env = DummyEnvironment([test.workpath("d1/d2"), test.workpath("d1")])
        s = SCons.Scanner.Fortran.FortranScan()
        path = s.path(env)
        fs = SCons.Node.FS.FS(original)
        deps = s(make_node('fff2.f', fs), env, path)
        headers =  ['d1/d2/f2.f', 'd1/f2.f', 'f2.f']
        deps_match(self, deps, map(test.workpath, headers))
        test.unlink('f2.f')
        
class FortranScannerTestCase9(unittest.TestCase):
    def runTest(self):
        test.write('f3.f', "\n")
        env = DummyEnvironment([])
        s = SCons.Scanner.Fortran.FortranScan()
        path = s.path(env)

        n = make_node('fff3.f')
        def my_rexists(s=n):
            s.rexists_called = 1
            return s.old_rexists()
        setattr(n, 'old_rexists', n.rexists)
        setattr(n, 'rexists', my_rexists)

        deps = s(n, env, path)
        
        # Make sure rexists() got called on the file node being
        # scanned, essential for cooperation with BuildDir functionality.
        assert n.rexists_called
        
        headers =  ['d1/f3.f', 'f3.f']
        deps_match(self, deps, map(test.workpath, headers))
        test.unlink('f3.f')

class FortranScannerTestCase10(unittest.TestCase):
    def runTest(self):
        fs = SCons.Node.FS.FS(test.workpath(''))
        env = DummyEnvironment(["include"])
        s = SCons.Scanner.Fortran.FortranScan(fs = fs)
        path = s.path(env)
        deps1 = s(fs.File('fff4.f'), env, path)
        fs.chdir(fs.Dir('subdir'))
        dir = fs.getcwd()
        fs.chdir(fs.Dir('..'))
        path = s.path(env, dir)
        deps2 = s(fs.File('#fff4.f'), env, path)
        headers1 =  ['include/f4.f']
        headers2 =  ['subdir/include/f4.f']
        deps_match(self, deps1, headers1)
        deps_match(self, deps2, headers2)

class FortranScannerTestCase11(unittest.TestCase):
    def runTest(self):
        SCons.Warnings.enableWarningClass(SCons.Warnings.DependencyWarning)
        class TestOut:
            def __call__(self, x):
                self.out = x

        to = TestOut()
        to.out = None
        SCons.Warnings._warningOut = to
        fs = SCons.Node.FS.FS(test.workpath(''))
        env = DummyEnvironment([])
        s = SCons.Scanner.Fortran.FortranScan(fs=fs)
        path = s.path(env)
        deps = s(fs.File('fff5.f'), env, path)

        # Did we catch the warning from not finding not_there.f?
        assert to.out
        
        deps_match(self, deps, [ 'f5.f' ])

class FortranScannerTestCase12(unittest.TestCase):
    def runTest(self):
        fs = SCons.Node.FS.FS(test.workpath(''))
        fs.chdir(fs.Dir('include'))
        env = DummyEnvironment([])
        s = SCons.Scanner.Fortran.FortranScan(fs=fs)
        path = s.path(env)
        test.write('include/fff4.f', test.read('fff4.f'))
        deps = s(fs.File('#include/fff4.f'), env, path)
        deps_match(self, deps, ['include/f4.f'])
        test.unlink('include/fff4.f')

class FortranScannerTestCase13(unittest.TestCase):
    def runTest(self):
        os.chdir(test.workpath('work'))
        fs = SCons.Node.FS.FS(test.workpath('work'))
        fs.Repository(test.workpath('repository'))

        # Create a derived file in a directory that does not exist yet.
        # This was a bug at one time.
        f1=fs.File('include2/jjj.f')
        f1.builder=1
        env = DummyEnvironment(['include','include2'])
        s = SCons.Scanner.Fortran.FortranScan(fs=fs)
        path = s.path(env)
        deps = s(fs.File('src/fff.f'), env, path)
        deps_match(self, deps, [test.workpath('repository/include/iii.f'), 'include2/jjj.f'])
        os.chdir(test.workpath(''))

class FortranScannerTestCase14(unittest.TestCase):
    def runTest(self):
        os.chdir(test.workpath('work'))
        fs = SCons.Node.FS.FS(test.workpath('work'))
        fs.BuildDir('build1', 'src', 1)
        fs.BuildDir('build2', 'src', 0)
        fs.Repository(test.workpath('repository'))
        env = DummyEnvironment([])
        s = SCons.Scanner.Fortran.FortranScan(fs = fs)
        path = s.path(env)
        deps1 = s(fs.File('build1/aaa.f'), env, path)
        deps_match(self, deps1, [ 'build1/bbb.f' ])
        deps2 = s(fs.File('build2/aaa.f'), env, path)
        deps_match(self, deps2, [ 'src/bbb.f' ])
        deps3 = s(fs.File('build1/ccc.f'), env, path)
        deps_match(self, deps3, [ 'build1/ddd.f' ])
        deps4 = s(fs.File('build2/ccc.f'), env, path)
        deps_match(self, deps4, [ test.workpath('repository/src/ddd.f') ])
        os.chdir(test.workpath(''))

class FortranScannerTestCase15(unittest.TestCase):
    def runTest(self):
        class SubstEnvironment(DummyEnvironment):
            def subst(self, arg, test=test):
                return test.workpath("d1")
        test.write(['d1', 'f2.f'], "      INCLUDE 'fi.f'\n")
        env = SubstEnvironment(["junk"])
        s = SCons.Scanner.Fortran.FortranScan()
        path = s.path(env)
        fs = SCons.Node.FS.FS(original)
        deps = s(make_node('fff1.f', fs), env, path)
        headers = ['d1/f1.f', 'd1/f2.f']
        deps_match(self, deps, map(test.workpath, headers))
        test.write(['d1', 'f2.f'], "\n")

def suite():
    suite = unittest.TestSuite()
    suite.addTest(FortranScannerTestCase1())
    suite.addTest(FortranScannerTestCase2())
    suite.addTest(FortranScannerTestCase3())
    suite.addTest(FortranScannerTestCase4())
    suite.addTest(FortranScannerTestCase5())
    suite.addTest(FortranScannerTestCase6())
    suite.addTest(FortranScannerTestCase7())
    suite.addTest(FortranScannerTestCase8())
    suite.addTest(FortranScannerTestCase9())
    suite.addTest(FortranScannerTestCase10())
    suite.addTest(FortranScannerTestCase11())
    suite.addTest(FortranScannerTestCase12())
    suite.addTest(FortranScannerTestCase13())
    suite.addTest(FortranScannerTestCase14())
    suite.addTest(FortranScannerTestCase15())
    return suite

if __name__ == "__main__":
    runner = unittest.TextTestRunner()
    result = runner.run(suite())
    if not result.wasSuccessful():
        sys.exit(1)