]> git.lizzy.rs Git - rust.git/blob - src/etc/generate-keyword-tests.py
Auto merge of #101947 - aliemjay:astconv-normalize, r=lcnr
[rust.git] / src / etc / generate-keyword-tests.py
1 #!/usr/bin/env python
2
3 """
4 This script takes a list of keywords and generates a testcase, that checks
5 if using the keyword as identifier fails, for every keyword. The generate
6 test files are set read-only.
7 Test for https://github.com/rust-lang/rust/issues/2275
8
9 sample usage: src/etc/generate-keyword-tests.py as break
10 """
11
12 import sys
13 import os
14 import stat
15
16
17 template = """\
18 // This file was auto-generated using 'src/etc/generate-keyword-tests.py %s'
19
20 fn main() {
21     let %s = "foo"; //~ error: expected pattern, found keyword `%s`
22 }
23 """
24
25 test_dir = os.path.abspath(
26     os.path.join(os.path.dirname(__file__), '../test/ui/parser')
27 )
28
29 for kw in sys.argv[1:]:
30     test_file = os.path.join(test_dir, 'keyword-%s-as-identifier.rs' % kw)
31
32     # set write permission if file exists, so it can be changed
33     if os.path.exists(test_file):
34         os.chmod(test_file, stat.S_IWUSR)
35
36     with open(test_file, 'wt') as f:
37         f.write(template % (kw, kw, kw))
38
39     # mark file read-only
40     os.chmod(test_file, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)