|
1 | 1 | import io |
2 | 2 | import logging |
3 | 3 | import os |
| 4 | +import stat |
4 | 5 | import sys |
5 | 6 | import textwrap |
6 | 7 | from unittest import mock |
7 | 8 |
|
8 | 9 | import pytest |
9 | | -import sh |
10 | 10 |
|
11 | 11 | import dotenv |
12 | 12 |
|
| 13 | +if not sys.platform.startswith("win"): |
| 14 | + import sh |
| 15 | + |
13 | 16 |
|
14 | 17 | def test_set_key_no_file(tmp_path): |
15 | 18 | nx_path = tmp_path / "nx" |
@@ -62,15 +65,25 @@ def test_set_key_encoding(dotenv_path): |
62 | 65 |
|
63 | 66 |
|
64 | 67 | @pytest.mark.skipif( |
65 | | - os.geteuid() == 0, reason="Root user can access files even with 000 permissions." |
| 68 | + not sys.platform.startswith("win") and os.geteuid() == 0, |
| 69 | + reason="Root user can access files even with 000 permissions.", |
66 | 70 | ) |
67 | 71 | def test_set_key_permission_error(dotenv_path): |
68 | | - dotenv_path.chmod(0o000) |
| 72 | + if sys.platform.startswith("win"): |
| 73 | + # On Windows, make file read-only |
| 74 | + dotenv_path.chmod(stat.S_IREAD) |
| 75 | + else: |
| 76 | + # On Unix, remove all permissions |
| 77 | + dotenv_path.chmod(0o000) |
69 | 78 |
|
70 | 79 | with pytest.raises(PermissionError): |
71 | 80 | dotenv.set_key(dotenv_path, "a", "b") |
72 | 81 |
|
73 | | - dotenv_path.chmod(0o600) |
| 82 | + # Restore permissions |
| 83 | + if sys.platform.startswith("win"): |
| 84 | + dotenv_path.chmod(stat.S_IWRITE | stat.S_IREAD) |
| 85 | + else: |
| 86 | + dotenv_path.chmod(0o600) |
74 | 87 | assert dotenv_path.read_text() == "" |
75 | 88 |
|
76 | 89 |
|
@@ -170,16 +183,6 @@ def test_unset_encoding(dotenv_path): |
170 | 183 | assert dotenv_path.read_text(encoding=encoding) == "" |
171 | 184 |
|
172 | 185 |
|
173 | | -@pytest.mark.skipif( |
174 | | - os.geteuid() == 0, reason="Root user can access files even with 000 permissions." |
175 | | -) |
176 | | -def test_set_key_unauthorized_file(dotenv_path): |
177 | | - dotenv_path.chmod(0o000) |
178 | | - |
179 | | - with pytest.raises(PermissionError): |
180 | | - dotenv.set_key(dotenv_path, "a", "x") |
181 | | - |
182 | | - |
183 | 186 | def test_unset_non_existent_file(tmp_path): |
184 | 187 | nx_path = tmp_path / "nx" |
185 | 188 | logger = logging.getLogger("dotenv.main") |
@@ -456,6 +459,9 @@ def test_load_dotenv_file_stream(dotenv_path): |
456 | 459 | assert os.environ == {"a": "b"} |
457 | 460 |
|
458 | 461 |
|
| 462 | +@pytest.mark.skipif( |
| 463 | + sys.platform.startswith("win"), reason="sh module doesn't support Windows" |
| 464 | +) |
459 | 465 | def test_load_dotenv_in_current_dir(tmp_path): |
460 | 466 | dotenv_path = tmp_path / ".env" |
461 | 467 | dotenv_path.write_bytes(b"a=b") |
@@ -484,6 +490,9 @@ def test_dotenv_values_file(dotenv_path): |
484 | 490 | assert result == {"a": "b"} |
485 | 491 |
|
486 | 492 |
|
| 493 | +@pytest.mark.skipif( |
| 494 | + sys.platform.startswith("win"), reason="This test assumes case-sensitive variables" |
| 495 | +) |
487 | 496 | @pytest.mark.parametrize( |
488 | 497 | "env,string,interpolate,expected", |
489 | 498 | [ |
|
0 commit comments