Skip to content
Myr

Add tests for atomic open_file permissions

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Max Smolens · committed by David Lord · parent fe257c9 · Browse files · 1 file+43 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+43 added−0 removed

  • tests:+43 added−0 removed

1 file+43 added−0 removed

modified:tests/test_utils.py+43 added
1import os2import stat3import sys45import pytest6
291 unmodified lines
298 assert result.exception is None299 assert result.output == 'foobar\nmeep\n'300301302@pytest.mark.skipif(WIN, reason='os.chmod() is not fully supported on Windows.')303@pytest.mark.parametrize('permissions', [304 0o400,305 0o444,306 0o600,307 0o644,308 ])309def test_open_file_atomic_permissions_existing_file(runner, permissions):310 with runner.isolated_filesystem():311 with open('existing.txt', 'w') as f:312 f.write('content')313 os.chmod('existing.txt', permissions)314315 @click.command()316 @click.argument('filename')317 def cli(filename):318 click.open_file(filename, 'w', atomic=True).close()319320 result = runner.invoke(cli, ['existing.txt'])321 assert result.exception is None322 assert stat.S_IMODE(os.stat('existing.txt').st_mode) == permissions323324325@pytest.mark.skipif(WIN, reason='os.stat() is not fully supported on Windows.')326def test_open_file_atomic_permissions_new_file(runner):327 with runner.isolated_filesystem():328 @click.command()329 @click.argument('filename')330 def cli(filename):331 click.open_file(filename, 'w', atomic=True).close()332333 # Create a test file to get the expected permissions for new files334 # according to the current umask.335 with open('test.txt', 'w'):336 pass337 permissions = stat.S_IMODE(os.stat('test.txt').st_mode)338339 result = runner.invoke(cli, ['new.txt'])340 assert result.exception is None341 assert stat.S_IMODE(os.stat('new.txt').st_mode) == permissions342343344@pytest.mark.xfail(WIN and not PY2, reason='God knows ...')345def test_iter_keepopenfile(tmpdir):346 expected = list(map(str, range(10)))347 p = tmpdir.mkdir('testdir').join('testfile')
15 unmodified lines