def test_default_should_use_sysconfig( self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.delattr(sysconfig, "_PIP_USE_SYSCONFIG", raising=False) if sys.version_info[:2] >= (3, 10): assert _should_use_sysconfig() is True else: assert _should_use_sysconfig() is False
def test_get_path_uid_symlink_without_NOFOLLOW( tmpdir: Path, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.delattr("os.O_NOFOLLOW") f = tmpdir / "symlink" / "somefile" f.parent.mkdir() f.write_text("content") fs = f"{f}_link" os.symlink(f, fs) with pytest.raises(OSError): get_path_uid(fs)
async def test_access_token(github_api: GitHubAPI, monkeypatch: MonkeyPatch) -> None: monkeypatch.setattr(apps, "get_installation_access_token", mock_return) monkeypatch.setenv("GITHUB_APP_ID", "") monkeypatch.setenv("GITHUB_PRIVATE_KEY", "") token_cache.clear() # Make sure the token_cache is cleared access_token = await github_api.access_token assert access_token == token # This is to make sure it actually returns the cached token monkeypatch.delattr(apps, "get_installation_access_token") cached_token = await github_api.access_token assert cached_token == token
def test_calculate_benchmark( use_preadv: bool, monkeypatch: pytest.MonkeyPatch, benchmark: pytest_benchmark.fixture.BenchmarkFixture, ) -> None: """Benchmark :func:`checksum.calculate`.""" if not use_preadv and hasattr(os, "preadv"): # pragma: py-lt-37 monkeypatch.delattr(os, "preadv") with gpt.GPTImage(path=conftest.TESTDATA_DISK, open_mode=os.O_RDONLY) as image: benchmark(checksum.calculate, image)
def test_hash_file(use_preadv: bool, monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture) -> None: """Test `checksum.hash_file`, optionally with `os.preadv` support disabled.""" if not use_preadv and hasattr(os, "preadv"): # pragma: py-lt-37 monkeypatch.delattr(os, "preadv") if use_preadv: # pragma: py-lt-37 assert hasattr(os, "preadv") mocked = mocker.patch( "os.preadv", side_effect=getattr(os, "preadv") # noqa: B009 ) else: assert not hasattr(os, "preadv") mocked = mocker.patch("os.pread", side_effect=os.pread) expected_reads = 0 with open(conftest.TESTDATA_DISK, "rb") as fd: # Full read hasher = hashlib.sha1() # noqa: S303 size = os.fstat(fd.fileno()).st_size result = checksum.hash_file(hasher.update, fd.fileno(), size, offset=0) assert result == size # sha1sum tests/testdata/disk assert hasher.hexdigest() == "fdb8443238315360e1c15940e07d5249127fb909" expected_reads += math.floor( (result + checksum._BUFFSIZE - 1) / checksum._BUFFSIZE) # Test 'partial' read: we can read `buffsize` from the file, but are # only processing part of said data. hasher = hashlib.sha1() # noqa: S303 size = 2 * checksum._BUFFSIZE + int(checksum._BUFFSIZE / 2 - 1) assert size % checksum._BUFFSIZE != 0 assert os.fstat(fd.fileno()).st_size > size result = checksum.hash_file(hasher.update, fd.fileno(), size, offset=0) assert result == size # $ dd if=tests/testdata/disk bs=1 \ # count=$(( 2 * 128 * 1024 + 64 * 1024 - 1 )) | sha1sum assert hasher.hexdigest() == "a40ca827c1023a67b5d430b61abd37d05cb681a2" expected_reads += math.floor( (result + checksum._BUFFSIZE - 1) / checksum._BUFFSIZE) mocked.assert_called() assert mocked.call_count == expected_reads
def test_running_under_virtualenv( monkeypatch: pytest.MonkeyPatch, real_prefix: Optional[str], base_prefix: Optional[str], expected: bool, ) -> None: # Use raising=False to prevent AttributeError on missing attribute if real_prefix is None: monkeypatch.delattr(sys, "real_prefix", raising=False) else: monkeypatch.setattr(sys, "real_prefix", real_prefix, raising=False) if base_prefix is None: monkeypatch.delattr(sys, "base_prefix", raising=False) else: monkeypatch.setattr(sys, "base_prefix", base_prefix, raising=False) assert virtualenv.running_under_virtualenv() == expected
def test_auto_detect_cpus(pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch) -> None: import os from xdist.plugin import pytest_cmdline_main as check_options with suppress(ImportError): import psutil monkeypatch.setattr(psutil, "cpu_count", lambda logical=True: None) if hasattr(os, "sched_getaffinity"): monkeypatch.setattr(os, "sched_getaffinity", lambda _pid: set(range(99))) elif hasattr(os, "cpu_count"): monkeypatch.setattr(os, "cpu_count", lambda: 99) else: import multiprocessing monkeypatch.setattr(multiprocessing, "cpu_count", lambda: 99) config = pytester.parseconfigure("-n2") assert config.getoption("numprocesses") == 2 config = pytester.parseconfigure("-nauto") check_options(config) assert config.getoption("numprocesses") == 99 config = pytester.parseconfigure("-nauto", "--pdb") check_options(config) assert config.getoption("usepdb") assert config.getoption("numprocesses") == 0 assert config.getoption("dist") == "no" config = pytester.parseconfigure("-nlogical", "--pdb") check_options(config) assert config.getoption("usepdb") assert config.getoption("numprocesses") == 0 assert config.getoption("dist") == "no" monkeypatch.delattr(os, "sched_getaffinity", raising=False) monkeypatch.setenv("TRAVIS", "true") config = pytester.parseconfigure("-nauto") check_options(config) assert config.getoption("numprocesses") == 2
def no_requests(monkeypatch: MonkeyPatch): """Remove requests.sessions.Session.request for all tests.""" monkeypatch.delattr("requests.sessions.Session.request")
def no_requests(monkeypatch: pytest.MonkeyPatch) -> None: """ Forces all the tests to work offline. """ monkeypatch.delattr("requests.sessions.Session.request")
def test_get_path_uid_without_NOFOLLOW( monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.delattr("os.O_NOFOLLOW") path = os.getcwd() assert get_path_uid(path) == os.stat(path).st_uid
def test_glibc_version_string_confstr_missing( self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.delattr(os, "confstr", raising=False) assert glibc_version_string_confstr() is None