[ci skip] Add note that this change may break SetOption() + ninja usage with fix
[scons.git] / test / file-names.py
blobbaafbee814fa246ed22b66915a048de70e6fb3eb
1 #!/usr/bin/env python
3 # __COPYRIGHT__
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:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
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.
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
27 import sys
28 import TestSCons
30 test = TestSCons.TestSCons()
32 # Right now, due to interaction with external quoting conventions,
33 # we do NOT actually support arbitrary characters in file names.
34 # (For example, double-quotes in a file name on POSIX break due
35 # to interaction with the "sh -c" quoting conventions.)
37 # This is a tough nut to crack, though. Right now, we use the
38 # external command interpreters so we don't have to roll our own
39 # parsing and interpretation of redirection and piping. But that
40 # means we have to find ways to work with *all* of their quoting
41 # conventions.
43 def contents(c):
44 return "|" + c + "|\n"
46 invalid_chars = '/\0'
48 if sys.platform == 'win32':
49 invalid_chars = set(invalid_chars)
51 # See the 'naming conventions' section of
52 # https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
53 invalid_chars.update([ chr(c) for c in range(32) ])
54 invalid_chars.update(r'\/:*?"<>|')
55 invalid_chars.update(chr(127))
57 # Win32 filesystems are case insensitive so don't do half the alphabet.
58 import string
59 invalid_chars.update(string.ascii_lowercase)
61 # See the 'naming conventions' section of
62 # https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
63 def bad_char(c):
64 return c in invalid_chars
65 def invalid_leading_char(c):
66 # the hash character as a leading character is interpreted to mean the project root
67 return c in ' #'
68 def invalid_trailing_char(c):
69 return c in ' .'
70 commandString = "copy $SOURCE $TARGET"
71 else:
72 invalid_chars = set(invalid_chars)
73 invalid_chars.add(chr(10))
74 invalid_chars.add(chr(13))
75 invalid_chars.add(chr(92)) # forward slash (dirsep)
76 invalid_chars.add(chr(96)) # backtick
79 def bad_char(c):
80 return c in invalid_chars
81 def invalid_leading_char(c):
82 # the hash character as a leading character is interpreted to mean the project root
83 return c in '#'
84 def invalid_trailing_char(c):
85 return False
86 commandString = "cp $SOURCE $TARGET"
88 goodChars = [ chr(c) for c in range(1, 128) if not bad_char(chr(c)) ]
90 def get_filename(ftype,c):
91 return ftype+"%d"%ord(c[-1])+c+ftype
93 for c in goodChars:
94 test.write(get_filename("in",c), contents(c))
96 def create_command(a, b, c):
97 a = ('', 'out')[a]
98 b = ('', 'out')[b]
99 return 'env.Command("' + a + get_filename('',c) + b + '", "'+get_filename("in",c)+ '","' + commandString + '")'
101 sconstruct = [ 'import sys', 'env = Environment(tools=[])' ]
102 for c in goodChars:
103 if c == '$':
104 c = '$$'
105 if c == '"':
106 c = r'\"'
107 infile = get_filename("in",c)
108 if not invalid_leading_char(c):
109 sconstruct.append(create_command(False, True, c))
110 sconstruct.append(create_command(True, True, c))
111 if not invalid_trailing_char(c):
112 sconstruct.append(create_command(True, False, c))
113 test.write('SConstruct', '\n'.join(sconstruct))
115 test.run(arguments = '.')
117 for c in goodChars:
118 # print "Checking %d "%ord(c)
120 c_str = ("%d"%ord(c[-1]))+c
121 if not invalid_leading_char(c):
122 test.must_match(c_str + "out", contents(c))
123 test.must_match("out" + c_str + "out", contents(c))
124 if not invalid_trailing_char(c):
125 test.must_match("out" + c_str, contents(c))
127 test.pass_test()
129 # Local Variables:
130 # tab-width:4
131 # indent-tabs-mode:nil
132 # End:
133 # vim: set expandtab tabstop=4 shiftwidth=4: