Skip to content

Commit

Permalink
Fix test to work on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
taldcroft committed Jul 10, 2023
1 parent 35991b8 commit 21ba07c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ska_helpers/tests/test_git_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest

from ska_helpers import git_helpers, paths
from ska_helpers.utils import get_owner

CHANDRA_MODELS = paths.chandra_models_repo_path()

Expand All @@ -15,7 +16,7 @@ def ska_ownership_ok():
# (not the case with shared directories on a Windows VM on parallels)
# and that the chandra_models dir is owned by the current user
try:
return CHANDRA_MODELS.owner() == getpass.getuser()
return get_owner(CHANDRA_MODELS) == getpass.getuser()
except Exception:
return False

Expand Down
32 changes: 32 additions & 0 deletions ska_helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,38 @@
__all__ = ["LazyDict", "LazyVal"]


def get_owner(path):
"""
Returns the owner of a file or directory.
Parameters:
-----------
path : str or pathlib.Path
The path to the file or directory.
Returns:
--------
str
The name of the owner of the file or directory.
"""

from testr import test_helper
from pathlib import Path

if test_helper.is_windows():
import win32security

# Suggested by copilot chat, seems to
security_descriptor = win32security.GetFileSecurity(
str(path), win32security.OWNER_SECURITY_INFORMATION
)
owner_sid = security_descriptor.GetSecurityDescriptorOwner()
owner_name, _, _ = win32security.LookupAccountSid(None, owner_sid)
else:
owner_name = Path(path).owner()
return owner_name


def _lazy_load_wrap(unbound_method):
@functools.wraps(unbound_method)
def wrapper(self, *args, **kwargs):
Expand Down

0 comments on commit 21ba07c

Please sign in to comment.