Skip to content

gh-103186: assert in tests that UnsafeMailcapInput warnings are provided #103217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 6, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions Lib/test/test_mailcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,18 @@ def test_subst(self):
(["", "audio/*", "foo.txt"], ""),
(["echo foo", "audio/*", "foo.txt"], "echo foo"),
(["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),
(["echo %t", "audio/*", "foo.txt"], None),
(["echo %t", "audio/*", "foo.txt"], None, {"warn_type": mailcap.UnsafeMailcapInput}),
(["echo %t", "audio/wav", "foo.txt"], "echo audio/wav"),
(["echo \\%t", "audio/*", "foo.txt"], "echo %t"),
(["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
(["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")
]
for tc in test_cases:
self.assertEqual(mailcap.subst(*tc[0]), tc[1])
if len(tc) == 3:
with warnings_helper.check_warnings(('', tc[2]["warn_type"]), quiet=True):
self.assertEqual(mailcap.subst(*tc[0]), tc[1])
else:
self.assertEqual(mailcap.subst(*tc[0]), tc[1])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit over-engineered. I'd split this into a separate test function, which tests that the warning is issued in cases that deserve the warning. This exception is raised from 3 places in the Lib/mailcap.py, so shouldn't we have at least 3 test cases for it?

CC @encukou .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Irit,

Thank you, I'll rework the affected cases into a separate test function and update the patch.
Will add 2x new test cases for unsafe filename and parameters, only the MIME type warnings are raised at present



class GetcapsTest(unittest.TestCase):
Expand Down Expand Up @@ -212,7 +216,8 @@ def test_findmatch(self):
('"An audio fragment"', audio_basic_entry)),
([c, "audio/*"],
{"filename": fname},
(None, None)),
(None, None),
{"warn_type": mailcap.UnsafeMailcapInput}),
([c, "audio/wav"],
{"filename": fname},
("/usr/local/bin/showaudio audio/wav", audio_entry)),
Expand Down Expand Up @@ -247,7 +252,11 @@ def test_test(self):

def _run_cases(self, cases):
for c in cases:
self.assertEqual(mailcap.findmatch(*c[0], **c[1]), c[2])
if len(c) == 4:
with warnings_helper.check_warnings(('', c[3]["warn_type"]), quiet=True):
self.assertEqual(mailcap.findmatch(*c[0], **c[1]), c[2])
else:
self.assertEqual(mailcap.findmatch(*c[0], **c[1]), c[2])


if __name__ == '__main__':
Expand Down