1 | n/a | '''Test Tools/scripts/fixcid.py.''' |
---|
2 | n/a | |
---|
3 | n/a | from io import StringIO |
---|
4 | n/a | import os, os.path |
---|
5 | n/a | import runpy |
---|
6 | n/a | import sys |
---|
7 | n/a | from test import support |
---|
8 | n/a | from test.test_tools import skip_if_missing, scriptsdir |
---|
9 | n/a | import unittest |
---|
10 | n/a | |
---|
11 | n/a | skip_if_missing() |
---|
12 | n/a | |
---|
13 | n/a | class Test(unittest.TestCase): |
---|
14 | n/a | def test_parse_strings(self): |
---|
15 | n/a | old1 = 'int xx = "xx\\"xx"[xx];\n' |
---|
16 | n/a | old2 = "int xx = 'x\\'xx' + xx;\n" |
---|
17 | n/a | output = self.run_script(old1 + old2) |
---|
18 | n/a | new1 = 'int yy = "xx\\"xx"[yy];\n' |
---|
19 | n/a | new2 = "int yy = 'x\\'xx' + yy;\n" |
---|
20 | n/a | self.assertMultiLineEqual(output, |
---|
21 | n/a | "1\n" |
---|
22 | n/a | "< {old1}" |
---|
23 | n/a | "> {new1}" |
---|
24 | n/a | "{new1}" |
---|
25 | n/a | "2\n" |
---|
26 | n/a | "< {old2}" |
---|
27 | n/a | "> {new2}" |
---|
28 | n/a | "{new2}".format(old1=old1, old2=old2, new1=new1, new2=new2) |
---|
29 | n/a | ) |
---|
30 | n/a | |
---|
31 | n/a | def test_alter_comments(self): |
---|
32 | n/a | output = self.run_script( |
---|
33 | n/a | substfile= |
---|
34 | n/a | "xx yy\n" |
---|
35 | n/a | "*aa bb\n", |
---|
36 | n/a | args=("-c", "-",), |
---|
37 | n/a | input= |
---|
38 | n/a | "/* xx altered */\n" |
---|
39 | n/a | "int xx;\n" |
---|
40 | n/a | "/* aa unaltered */\n" |
---|
41 | n/a | "int aa;\n", |
---|
42 | n/a | ) |
---|
43 | n/a | self.assertMultiLineEqual(output, |
---|
44 | n/a | "1\n" |
---|
45 | n/a | "< /* xx altered */\n" |
---|
46 | n/a | "> /* yy altered */\n" |
---|
47 | n/a | "/* yy altered */\n" |
---|
48 | n/a | "2\n" |
---|
49 | n/a | "< int xx;\n" |
---|
50 | n/a | "> int yy;\n" |
---|
51 | n/a | "int yy;\n" |
---|
52 | n/a | "/* aa unaltered */\n" |
---|
53 | n/a | "4\n" |
---|
54 | n/a | "< int aa;\n" |
---|
55 | n/a | "> int bb;\n" |
---|
56 | n/a | "int bb;\n" |
---|
57 | n/a | ) |
---|
58 | n/a | |
---|
59 | n/a | def test_directory(self): |
---|
60 | n/a | os.mkdir(support.TESTFN) |
---|
61 | n/a | self.addCleanup(support.rmtree, support.TESTFN) |
---|
62 | n/a | c_filename = os.path.join(support.TESTFN, "file.c") |
---|
63 | n/a | with open(c_filename, "w") as file: |
---|
64 | n/a | file.write("int xx;\n") |
---|
65 | n/a | with open(os.path.join(support.TESTFN, "file.py"), "w") as file: |
---|
66 | n/a | file.write("xx = 'unaltered'\n") |
---|
67 | n/a | script = os.path.join(scriptsdir, "fixcid.py") |
---|
68 | n/a | output = self.run_script(args=(support.TESTFN,)) |
---|
69 | n/a | self.assertMultiLineEqual(output, |
---|
70 | n/a | "{}:\n" |
---|
71 | n/a | "1\n" |
---|
72 | n/a | '< int xx;\n' |
---|
73 | n/a | '> int yy;\n'.format(c_filename) |
---|
74 | n/a | ) |
---|
75 | n/a | |
---|
76 | n/a | def run_script(self, input="", *, args=("-",), substfile="xx yy\n"): |
---|
77 | n/a | substfilename = support.TESTFN + ".subst" |
---|
78 | n/a | with open(substfilename, "w") as file: |
---|
79 | n/a | file.write(substfile) |
---|
80 | n/a | self.addCleanup(support.unlink, substfilename) |
---|
81 | n/a | |
---|
82 | n/a | argv = ["fixcid.py", "-s", substfilename] + list(args) |
---|
83 | n/a | script = os.path.join(scriptsdir, "fixcid.py") |
---|
84 | n/a | with support.swap_attr(sys, "argv", argv), \ |
---|
85 | n/a | support.swap_attr(sys, "stdin", StringIO(input)), \ |
---|
86 | n/a | support.captured_stdout() as output, \ |
---|
87 | n/a | support.captured_stderr(): |
---|
88 | n/a | try: |
---|
89 | n/a | runpy.run_path(script, run_name="__main__") |
---|
90 | n/a | except SystemExit as exit: |
---|
91 | n/a | self.assertEqual(exit.code, 0) |
---|
92 | n/a | return output.getvalue() |
---|