|
| 1 | +import os |
| 2 | +import platform |
| 3 | +import stat |
| 4 | +import pytest |
| 5 | + |
| 6 | +from conan.errors import ConanException |
| 7 | +from conan.tools.files import chmod |
| 8 | +from conan.test.utils.mocks import ConanFileMock |
| 9 | +from conan.test.utils.tools import temp_folder, save_files |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.skipif(platform.system() == "Windows", reason="validate full permissions only in Unix") |
| 13 | +@pytest.mark.parametrize("read,write,execute,expected", [ |
| 14 | + (True, True, True, 0o700), |
| 15 | + (False, True, False, 0o200), |
| 16 | + (False, False, True, 0o100), |
| 17 | + (True, False, True, 0o500), |
| 18 | + (True, True, False, 0o600), |
| 19 | + (True, False, False, 0o400), |
| 20 | + (False, False, False, 0o000),]) |
| 21 | +def test_chmod_single_file(read, write, execute, expected): |
| 22 | + """ |
| 23 | + The chmod should be able to change the permissions of a single file. |
| 24 | + """ |
| 25 | + tmp = temp_folder() |
| 26 | + save_files(tmp, {"file.txt": "foobar"}) |
| 27 | + file_path = os.path.join(tmp, "file.txt") |
| 28 | + os.chmod(file_path, 0o000) |
| 29 | + conanfile = ConanFileMock() |
| 30 | + chmod(conanfile, file_path, read=read, write=write, execute=execute, recursive=False) |
| 31 | + file_mode = os.stat(file_path).st_mode |
| 32 | + assert stat.S_IMODE(file_mode) == expected |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.skipif(platform.system() == "Windows", reason="validate full permissions only in Unix") |
| 36 | +@pytest.mark.parametrize("read,write,execute,expected", [ |
| 37 | + (True, True, True, 0o700), |
| 38 | + (False, True, False, 0o200), |
| 39 | + (False, False, True, 0o100), |
| 40 | + (True, False, True, 0o500), |
| 41 | + (True, True, False, 0o600), |
| 42 | + (True, False, False, 0o400), |
| 43 | + (False, False, False, 0o000),]) |
| 44 | +def test_chmod_recursive(read, write, execute, expected): |
| 45 | + """ |
| 46 | + The chmod should be able to change the permissions of all files in a folder when recursive is set to True. |
| 47 | + """ |
| 48 | + tmp = temp_folder() |
| 49 | + files = {"foobar/qux/file.txt": "foobar", |
| 50 | + "foobar/file.txt": "qux", |
| 51 | + "foobar/foo/file.txt": "foobar"} |
| 52 | + save_files(tmp, files) |
| 53 | + folder_path = os.path.join(tmp, "foobar") |
| 54 | + for file in files.keys(): |
| 55 | + file_path = os.path.join(tmp, file) |
| 56 | + os.chmod(file_path, 0o000) |
| 57 | + conanfile = ConanFileMock() |
| 58 | + chmod(conanfile, folder_path, read=read, write=write, execute=execute, recursive=True) |
| 59 | + for file in files.keys(): |
| 60 | + file_mode = os.stat(os.path.join(tmp, file)).st_mode |
| 61 | + assert stat.S_IMODE(file_mode) == expected |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.skipif(platform.system() == "Windows", reason="Validate default permissions only in Unix") |
| 65 | +def test_chmod_default_values(): |
| 66 | + """ |
| 67 | + When not passing a permission parameter, chmod should not change the specific permission. |
| 68 | + """ |
| 69 | + tmp = temp_folder() |
| 70 | + save_files(tmp, {"file.txt": "foobar"}) |
| 71 | + file_path = os.path.join(tmp, "file.txt") |
| 72 | + os.chmod(file_path, 0o111) |
| 73 | + conanfile = ConanFileMock() |
| 74 | + chmod(conanfile, file_path, read=True) |
| 75 | + file_mode = os.stat(file_path).st_mode |
| 76 | + assert stat.S_IMODE(file_mode) == 0o511 |
| 77 | + |
| 78 | + |
| 79 | +def test_missing_permission_arguments(): |
| 80 | + """ |
| 81 | + The chmod should raise an exception if no new permission is provided. |
| 82 | + """ |
| 83 | + conanfile = ConanFileMock() |
| 84 | + with pytest.raises(ConanException) as error: |
| 85 | + chmod(conanfile, "invalid_path") |
| 86 | + assert 'Could not change permission: At least one of the permissions should be set.' in str(error.value) |
| 87 | + |
| 88 | + |
| 89 | +def test_invalid_path(): |
| 90 | + """ |
| 91 | + The chmod should raise an exception if the path does not exist. |
| 92 | + """ |
| 93 | + conanfile = ConanFileMock() |
| 94 | + with pytest.raises(ConanException) as error: |
| 95 | + chmod(conanfile, "invalid_path", read=True, write=True, execute=True, recursive=False) |
| 96 | + assert 'Could not change permission: Path "invalid_path" does not exist.' in str(error.value) |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.skipif(platform.system() != "Windows", reason="Validate read-only permissions only in Windows") |
| 100 | +def test_chmod_windows(): |
| 101 | + """ |
| 102 | + The chmod should be able to change read-only state in Windows. |
| 103 | + """ |
| 104 | + tmp = temp_folder() |
| 105 | + save_files(tmp, {"file.txt": "foobar"}) |
| 106 | + file_path = os.path.join(tmp, "file.txt") |
| 107 | + os.chmod(file_path, 0o000) |
| 108 | + conanfile = ConanFileMock() |
| 109 | + chmod(conanfile, file_path, read=True, write=True, execute=True, recursive=False) |
| 110 | + assert os.access(file_path, os.W_OK) |
0 commit comments