Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
eftype test should handle macos
  • Loading branch information
sorcio committed Sep 16, 2023
commit 54f5548b0e7dbefd1d12e43cfdf96e5903099e2a
31 changes: 30 additions & 1 deletion Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3057,6 +3057,32 @@ def test_keyword_only(self, mock_geteuid):
tarfl.extract, filename_1, TEMPDIR, False, True)


_can_chmod_set_sticky = None


def can_chmod_set_sticky():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You should move it to support.os_helper.

I suggest to rename to can_chmod_set_sticky_bit().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

def can_chmod_set_sticky_inner():
if not os_helper.can_chmod():
return False
filename = os_helper.TESTFN + "-chmod-sticky"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

honestly, just use "chmod-sticky", to avoid further troubles with non-ASCII filenames.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Decided not to do it, and instead just use TESTFN like can_chmod() does. The concern is fair but I don't want to touch os_helper more than necessary.

try:
with open(filename, "w") as f:
os.chmod(f.fileno(), 0o666)
try:
os.chmod(f.fileno(), 0o666 | stat.S_ISVTX)
except OSError:
return False
mode = os.stat(f.fileno()).st_mode
return bool(mode & stat.S_ISVTX)
finally:
os.unlink(filename)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's safer to use os_helper.unlink() here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.


global _can_chmod_set_sticky
if _can_chmod_set_sticky is None:
_can_chmod_set_sticky = can_chmod_set_sticky_inner()
return _can_chmod_set_sticky


@os_helper.skip_unless_working_chmod
class FileModesTest(unittest.TestCase):
@unittest.skipUnless(hasattr(errno, "EFTYPE"), "errno.EFTYPE required")
Expand All @@ -3076,7 +3102,10 @@ def test_extract_chmod_eftype(self):
# this should not raise:
tar.extract("sticky1", DIR, filter="fully_trusted")
got_mode = stat.filemode(os.stat(os.path.join(DIR, "sticky1")).st_mode)
expected_mode = "-rwxrwxrwx" if os.geteuid() != 0 else "-rwxrwxrwt"
if can_chmod_set_sticky():
expected_mode = "-rwxrwxrwt"
else:
expected_mode = "-rwxrwxrwx"
self.assertEqual(got_mode, expected_mode)

# but we can create a situation where it does raise:
Expand Down