|
6 | 6 | import os |
7 | 7 | import sys |
8 | 8 | import re |
| 9 | +import subprocess |
9 | 10 | import fileinput |
10 | 11 | import collections |
11 | 12 | import builtins |
|
29 | 30 | from test.support.os_helper import unlink as safe_unlink |
30 | 31 | from test.support import os_helper |
31 | 32 | from test import support |
| 33 | +from test.support import script_helper |
32 | 34 | from unittest import mock |
33 | 35 |
|
34 | 36 |
|
@@ -1098,5 +1100,81 @@ def test_all(self): |
1098 | 1100 | support.check__all__(self, fileinput) |
1099 | 1101 |
|
1100 | 1102 |
|
| 1103 | +@support.requires_subprocess() |
| 1104 | +class CommandLineTest(unittest.TestCase): |
| 1105 | + |
| 1106 | + def setUp(self): |
| 1107 | + self.tmpdir = self.enterContext(os_helper.temp_dir()) |
| 1108 | + |
| 1109 | + def write_file(self, name, content): |
| 1110 | + with open(os.path.join(self.tmpdir, name), 'wb') as f: |
| 1111 | + f.write(content) |
| 1112 | + |
| 1113 | + def read_file(self, name): |
| 1114 | + with open(os.path.join(self.tmpdir, name), 'rb') as f: |
| 1115 | + return f.read() |
| 1116 | + |
| 1117 | + def run_cli(self, *args): |
| 1118 | + _, out, err = script_helper.assert_python_ok( |
| 1119 | + '-m', 'fileinput', *args, __cwd=self.tmpdir) |
| 1120 | + self.assertEqual(err, b'') |
| 1121 | + return out.decode().splitlines() |
| 1122 | + |
| 1123 | + def test_files(self): |
| 1124 | + self.write_file('a', b'one\ntwo\n') |
| 1125 | + self.write_file('b', b'three') |
| 1126 | + self.assertEqual(self.run_cli('a', 'b'), [ |
| 1127 | + '1: a[1]* one', |
| 1128 | + '2: a[2] two', |
| 1129 | + '3: b[1]* three', |
| 1130 | + '3: b[1]', |
| 1131 | + ]) |
| 1132 | + |
| 1133 | + def test_strip_line_endings(self): |
| 1134 | + self.write_file('a', b'one\r\ntwo\rthree\n') |
| 1135 | + self.assertEqual(self.run_cli('a'), [ |
| 1136 | + '1: a[1]* one', |
| 1137 | + '2: a[2] two', |
| 1138 | + '3: a[3] three', |
| 1139 | + '3: a[3]', |
| 1140 | + ]) |
| 1141 | + |
| 1142 | + def test_stdin(self): |
| 1143 | + p = script_helper.spawn_python('-m', 'fileinput', |
| 1144 | + stderr=subprocess.PIPE) |
| 1145 | + out, err = p.communicate(b'one\ntwo\n') |
| 1146 | + self.assertEqual(p.returncode, 0) |
| 1147 | + self.assertEqual(err, b'') |
| 1148 | + self.assertEqual(out.decode().splitlines(), [ |
| 1149 | + '1: <stdin>[1]* one', |
| 1150 | + '2: <stdin>[2] two', |
| 1151 | + '2: <stdin>[2]', |
| 1152 | + ]) |
| 1153 | + |
| 1154 | + def test_inplace(self): |
| 1155 | + self.write_file('a', b'one\ntwo\n') |
| 1156 | + self.assertEqual(self.run_cli('-i', 'a'), ['2: a[2]']) |
| 1157 | + self.assertEqual(self.read_file('a').decode().splitlines(), [ |
| 1158 | + '1: a[1]* one', |
| 1159 | + '2: a[2] two', |
| 1160 | + ]) |
| 1161 | + self.assertEqual(os.listdir(self.tmpdir), ['a']) |
| 1162 | + |
| 1163 | + def test_inplace_backup(self): |
| 1164 | + self.write_file('a', b'one\ntwo\n') |
| 1165 | + self.assertEqual(self.run_cli('-i', '-b', '.orig', 'a'), ['2: a[2]']) |
| 1166 | + self.assertEqual(self.read_file('a').decode().splitlines(), [ |
| 1167 | + '1: a[1]* one', |
| 1168 | + '2: a[2] two', |
| 1169 | + ]) |
| 1170 | + self.assertEqual(self.read_file('a.orig'), b'one\ntwo\n') |
| 1171 | + |
| 1172 | + def test_unknown_option(self): |
| 1173 | + _, out, err = script_helper.assert_python_failure( |
| 1174 | + '-m', 'fileinput', '-z', __cwd=self.tmpdir) |
| 1175 | + self.assertEqual(out, b'') |
| 1176 | + self.assertIn(b'option -z not recognized', err) |
| 1177 | + |
| 1178 | + |
1101 | 1179 | if __name__ == "__main__": |
1102 | 1180 | unittest.main() |
0 commit comments