Skip to content

Commit

Permalink
test_keep_old_work: Update flaky Windows permissions test (#5087)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodegard authored Nov 29, 2023
1 parent 6db1d26 commit 6b1cab9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
9 changes: 6 additions & 3 deletions conda_build/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""
Module to store conda build settings.
"""
from __future__ import annotations

import copy
import math
import os
Expand All @@ -12,6 +14,7 @@
import time
from collections import namedtuple
from os.path import abspath, expanduser, expandvars, join
from pathlib import Path

from .conda_interface import (
binstar_upload,
Expand Down Expand Up @@ -458,7 +461,7 @@ def src_cache_root(self, value):
self._src_cache_root = value

@property
def croot(self):
def croot(self) -> str:
"""This is where source caches and work folders live"""
if not self._croot:
_bld_root_env = os.getenv("CONDA_BLD_PATH")
Expand All @@ -474,9 +477,9 @@ def croot(self):
return self._croot

@croot.setter
def croot(self, croot):
def croot(self, croot: str | os.PathLike | Path) -> None:
"""Set croot - if None is passed, then the default value will be used"""
self._croot = croot
self._croot = str(croot) if croot else None

@property
def output_folder(self):
Expand Down
9 changes: 1 addition & 8 deletions conda_build/inspect_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
)

from .deprecations import deprecated
from .utils import on_mac, on_win
from .utils import on_mac, on_win, samefile


@deprecated("3.28.0", "4.0.0")
Expand Down Expand Up @@ -72,13 +72,6 @@ def which_package(
# (pathlib correctly handles this even if path is absolute)
path = prefix / path

def samefile(path1: Path, path2: Path) -> bool:
try:
return path1.samefile(path2)
except FileNotFoundError:
# FileNotFoundError: path doesn't exist
return path1 == path2

for prec in PrefixData(str(prefix)).iter_records():
for file in prec["files"]:
if samefile(prefix / file, path):
Expand Down
9 changes: 9 additions & 0 deletions conda_build/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2220,3 +2220,12 @@ def is_conda_pkg(pkg_path: str) -> bool:
return path.is_file() and (
any(path.name.endswith(ext) for ext in CONDA_PACKAGE_EXTENSIONS)
)


def samefile(path1: Path, path2: Path) -> bool:
try:
return path1.samefile(path2)
except (FileNotFoundError, PermissionError):
# FileNotFoundError: path doesn't exist
# PermissionError: don't have permissions to read path
return path1 == path2
42 changes: 22 additions & 20 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
# SPDX-License-Identifier: BSD-3-Clause
import os
import sys
from pathlib import Path

import pytest

from conda_build.conda_interface import TemporaryDirectory
from conda_build.config import Config, get_or_merge_config
from conda_build.utils import on_win
from conda_build.utils import on_win, samefile


@pytest.fixture
def config():
def config() -> Config:
"""a tiny bit of a fixture to save us from manually creating a new Config each test"""
return Config()


@pytest.fixture
def build_id():
def build_id() -> str:
"""Small support fixture for setting build id's in multiple builds which may need them"""
return "test123"

Expand All @@ -34,23 +34,25 @@ def test_set_build_id(config, build_id):
assert config.host_prefix == long_prefix


def test_keep_old_work(config, build_id):
def test_keep_old_work(config: Config, build_id: str, tmp_path: Path):
config.keep_old_work = True
with TemporaryDirectory() as temp_dir:
config.croot = temp_dir
config.build_id = build_id
work_path = os.path.join(temp_dir, build_id, "work")
os.makedirs(work_path)
# assert False
assert len(os.listdir(config.work_dir)) == 0
with open(os.path.join(work_path, "a_touched_file.magic"), "w") as _:
# Touch a random file so the "work_dir" is not empty
pass
assert len(os.listdir(config.work_dir)) > 0
config.compute_build_id("a_new_name", reset=True)
assert config.work_dir != work_path
assert not os.path.exists(work_path)
assert len(os.listdir(config.work_dir)) > 0
config.croot = tmp_path
config.build_id = build_id

# empty working directory
orig_dir = Path(config.work_dir)
assert not len(os.listdir(config.work_dir))

# touch a file so working directory is not empty
(orig_dir / "a_touched_file.magic").touch()
assert len(os.listdir(config.work_dir))

config.compute_build_id("a_new_name", reset=True)

# working directory should still exist and have the touched file
assert not samefile(orig_dir, config.work_dir)
assert not orig_dir.exists()
assert len(os.listdir(config.work_dir))


@pytest.mark.skipif(on_win, reason="Windows uses only the short prefix")
Expand Down

0 comments on commit 6b1cab9

Please sign in to comment.