Skip to content

Commit 0e8761b

Browse files
committed
gh-131178: Add tests for fileinput command-line interface
1 parent 9fe52e9 commit 0e8761b

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

‎Lib/test/test_fileinput.py‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import sys
88
import re
9+
import subprocess
910
import fileinput
1011
import collections
1112
import builtins
@@ -29,6 +30,7 @@
2930
from test.support.os_helper import unlink as safe_unlink
3031
from test.support import os_helper
3132
from test import support
33+
from test.support import script_helper
3234
from unittest import mock
3335

3436

@@ -1098,5 +1100,81 @@ def test_all(self):
10981100
support.check__all__(self, fileinput)
10991101

11001102

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+
11011179
if __name__ == "__main__":
11021180
unittest.main()

0 commit comments

Comments
 (0)