Use try/finally to ensure chmod is run, filter warning

This commit is contained in:
Daniel Hahler 2019-11-05 22:28:32 +01:00
parent dc2c51302a
commit 9309ae299a
1 changed files with 24 additions and 16 deletions

View File

@ -48,16 +48,21 @@ class TestNewAPI:
testdir.makeini("[pytest]") testdir.makeini("[pytest]")
mode = os.stat(testdir.tmpdir.ensure_dir(".pytest_cache"))[stat.ST_MODE] mode = os.stat(testdir.tmpdir.ensure_dir(".pytest_cache"))[stat.ST_MODE]
testdir.tmpdir.ensure_dir(".pytest_cache").chmod(0) testdir.tmpdir.ensure_dir(".pytest_cache").chmod(0)
try:
config = testdir.parseconfigure() config = testdir.parseconfigure()
cache = config.cache cache = config.cache
cache.set("test/broken", []) cache.set("test/broken", [])
finally:
testdir.tmpdir.ensure_dir(".pytest_cache").chmod(mode) testdir.tmpdir.ensure_dir(".pytest_cache").chmod(mode)
@pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows") @pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows")
@pytest.mark.filterwarnings("default") @pytest.mark.filterwarnings(
"ignore:could not create cache path:pytest.PytestWarning"
)
def test_cache_failure_warns(self, testdir): def test_cache_failure_warns(self, testdir):
mode = os.stat(testdir.tmpdir.ensure_dir(".pytest_cache"))[stat.ST_MODE] mode = os.stat(testdir.tmpdir.ensure_dir(".pytest_cache"))[stat.ST_MODE]
testdir.tmpdir.ensure_dir(".pytest_cache").chmod(0) testdir.tmpdir.ensure_dir(".pytest_cache").chmod(0)
try:
testdir.makepyfile( testdir.makepyfile(
""" """
def test_error(): def test_error():
@ -66,10 +71,13 @@ class TestNewAPI:
""" """
) )
result = testdir.runpytest("-rw") result = testdir.runpytest("-rw")
testdir.tmpdir.ensure_dir(".pytest_cache").chmod(mode)
assert result.ret == 1 assert result.ret == 1
# warnings from nodeids, lastfailed, and stepwise # warnings from nodeids, lastfailed, and stepwise
result.stdout.fnmatch_lines(["*could not create cache path*", "*3 warnings*"]) result.stdout.fnmatch_lines(
["*could not create cache path*", "*3 warnings*"]
)
finally:
testdir.tmpdir.ensure_dir(".pytest_cache").chmod(mode)
def test_config_cache(self, testdir): def test_config_cache(self, testdir):
testdir.makeconftest( testdir.makeconftest(