Skip to content

Commit 259fa8e

Browse files
committed
Add tests for valid/invalid file with stdout/err as None
1 parent fd8fc3a commit 259fa8e

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

Lib/test/test_argparse.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,60 @@ def test_skip_invalid_stdout(self):
7979
func()
8080
self.assertRegex(mocked_stderr.getvalue(), r'usage:')
8181

82+
def test_invalid_file_only(self):
83+
parser = argparse.ArgumentParser()
84+
for func in (
85+
parser.print_usage,
86+
parser.print_help,
87+
):
88+
self.assertRaises(ValueError, func, "invalid file")
89+
90+
def test_valid_file_invalid_stdout(self):
91+
parser = argparse.ArgumentParser()
92+
for func in (
93+
parser.print_usage,
94+
parser.print_help,
95+
):
96+
with (
97+
self.subTest(func=func),
98+
contextlib.redirect_stdout(None),
99+
StdIOBuffer() as f,
100+
mock.patch('argparse._sys.exit'),
101+
):
102+
func(file=f)
103+
self.assertRegex(f.getvalue(), r'usage:')
104+
105+
def test_valid_file_invalid_stderr(self):
106+
parser = argparse.ArgumentParser()
107+
for func in (
108+
parser.print_usage,
109+
parser.print_help,
110+
):
111+
with (
112+
self.subTest(func=func),
113+
contextlib.redirect_stderr(None),
114+
StdIOBuffer() as f,
115+
mock.patch('argparse._sys.exit'),
116+
):
117+
func(file=f)
118+
self.assertRegex(f.getvalue(), r'usage:')
119+
120+
def test_valid_file_invalid_stdout_stderr(self):
121+
parser = argparse.ArgumentParser()
122+
for func in (
123+
parser.print_usage,
124+
parser.print_help,
125+
):
126+
with (
127+
self.subTest(func=func),
128+
contextlib.redirect_stdout(None),
129+
contextlib.redirect_stderr(None),
130+
StdIOBuffer() as f,
131+
mock.patch('argparse._sys.exit'),
132+
):
133+
func(file=f)
134+
self.assertRegex(f.getvalue(), r'usage:')
135+
82136

83137
class TestLazyImports(unittest.TestCase):
84138
LAZY_IMPORTS = {

0 commit comments

Comments
 (0)