diff options
| author | Max Smolens <msmolens@gmail.com> | 2019-08-29 14:56:35 -0400 |
|---|---|---|
| committer | David Lord <davidism@gmail.com> | 2020-02-16 16:52:50 -0800 |
| commit | b8125669e3bbd5601de664cb35acd7df17aed31d (patch) | |
| tree | 8637f8522fdece1f7bc7a10463dd3b8e31bd6868 /tests | |
| parent | fe257c9b4275860db37e12b2638f3e7a31f2efcf (diff) | |
| download | click-b8125669e3bbd5601de664cb35acd7df17aed31d.tar.gz | |
Add tests for atomic open_file permissions
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_utils.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/test_utils.py b/tests/test_utils.py index 2741261..751c6ae 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,4 +1,5 @@ import os +import stat import sys import pytest @@ -298,6 +299,48 @@ def test_open_file(runner): assert result.output == 'foobar\nmeep\n' +@pytest.mark.skipif(WIN, reason='os.chmod() is not fully supported on Windows.') +@pytest.mark.parametrize('permissions', [ + 0o400, + 0o444, + 0o600, + 0o644, + ]) +def test_open_file_atomic_permissions_existing_file(runner, permissions): + with runner.isolated_filesystem(): + with open('existing.txt', 'w') as f: + f.write('content') + os.chmod('existing.txt', permissions) + + @click.command() + @click.argument('filename') + def cli(filename): + click.open_file(filename, 'w', atomic=True).close() + + result = runner.invoke(cli, ['existing.txt']) + assert result.exception is None + assert stat.S_IMODE(os.stat('existing.txt').st_mode) == permissions + + +@pytest.mark.skipif(WIN, reason='os.stat() is not fully supported on Windows.') +def test_open_file_atomic_permissions_new_file(runner): + with runner.isolated_filesystem(): + @click.command() + @click.argument('filename') + def cli(filename): + click.open_file(filename, 'w', atomic=True).close() + + # Create a test file to get the expected permissions for new files + # according to the current umask. + with open('test.txt', 'w'): + pass + permissions = stat.S_IMODE(os.stat('test.txt').st_mode) + + result = runner.invoke(cli, ['new.txt']) + assert result.exception is None + assert stat.S_IMODE(os.stat('new.txt').st_mode) == permissions + + @pytest.mark.xfail(WIN and not PY2, reason='God knows ...') def test_iter_keepopenfile(tmpdir): expected = list(map(str, range(10))) |
