summaryrefslogtreecommitdiff
path: root/test/textfile/textfile.py
blob: f614dfc9456785a4325bfc4936b46c3cdc99aa1d (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
#!/usr/bin/env python
#
# MIT License
#
# Copyright The SCons Foundation
#
# 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.

import TestSCons

import os

test = TestSCons.TestSCons()

# foo1 = test.workpath('foo1.txt')
# foo2  = test.workpath('foo2.txt')
# foo1a = test.workpath('foo1a.txt')
# foo2a = test.workpath('foo2a.txt')

match_mode = 'r'

test.file_fixture('fixture/SConstruct', 'SConstruct')

test.run(arguments='.')

linesep = '\n'

textparts = ['lalala', '42',
             'Goethe', 'Schiller',
             'tanteratei',
             '×']  # <-- this is unicode /xd7 symbol   
foo1Text = linesep.join(textparts)
foo2Text = '|*'.join(textparts)
foo1aText = foo1Text + linesep
foo2aText = foo2Text + '|*'

issue_4021_text = r'<HintPath>..\..\..\NETCore\bin\$(Platform)\$(Configuration)\Datalogics.PDFL.dll</HintPath>'

test.up_to_date(arguments='.')

files = list(map(test.workpath, (
    'foo1.txt', 'foo2.txt', 'foo1a.txt', 'foo2a.txt',
    'bar1', 'bar2', 'bar1a.txt', 'bar2a.txt',
)))


def check_times():
    """
    make sure the files didn't get rewritten, because nothing changed:
    """
    before = list(map(os.path.getmtime, files))
    # introduce a small delay, to make the test valid
    test.sleep()
    # should still be up-to-date
    test.up_to_date(arguments='.')
    after = list(map(os.path.getmtime, files))
    test.fail_test(before != after)


# make sure that the file content is as expected
test.must_match('foo1.txt', foo1Text, mode=match_mode)
test.must_match('bar1', foo1Text, mode=match_mode)
test.must_match('foo2.txt', foo2Text, mode=match_mode)
test.must_match('bar2', foo2Text, mode=match_mode)
test.must_match('foo1a.txt', foo1aText, mode=match_mode)
test.must_match('bar1a.txt', foo1aText, mode=match_mode)
test.must_match('foo2a.txt', foo2aText, mode=match_mode)
test.must_match('bar2a.txt', foo2aText, mode=match_mode)
test.must_match('issue-4021.txt', issue_4021_text, mode=match_mode)
check_times()

# write the contents and make sure the files
# didn't get rewritten, because nothing changed:
test.write('foo1.txt', foo1Text)
test.write('bar1', foo1Text)
test.write('foo2.txt', foo2Text)
test.write('bar2', foo2Text)
test.write('foo1a.txt', foo1aText)
test.write('bar1a.txt', foo1aText)
test.write('foo2a.txt', foo2aText)
test.write('bar2a.txt', foo2aText)
check_times()

# now that textfile is part of default tool list, run one testcase
# without adding it explicitly as a tool to make sure.
test.file_fixture('fixture/SConstruct.2', 'SConstruct.2')

test.run(options='-f SConstruct.2', arguments='.')

line1 = 'This line has no substitutions'
line2a = 'This line has @subst@ substitutions'
line2b = 'This line has most substitutions'
line3a = 'This line has %subst% substitutions'
line3b = 'This line has many substitutions'


def matchem(match_file, lines):
    """
    Join all the lines with correct line separator,
    then compare
    """
    lines = linesep.join(lines)
    test.must_match(match_file, lines, mode=match_mode, message="Expected:\n%s\n" % lines)


matchem('text.txt', [line1, line2a, line3a])
matchem('sub1', [line1, line2a, line3a])
matchem('sub2', [line1, line2b, line3a])
matchem('sub3', [line1, line2b, line3b])
matchem('sub4', [line1, line2a, line3b])
matchem('sub5', [line1, line2b, line3b])
matchem('sub6', [line1, line2b, line3b])

test.up_to_date(options='-f SConstruct.2', arguments='.')

test.pass_test()