|
12 | 12 | # language governing permissions and limitations under the License.
|
13 | 13 | # language governing permissions and limitations under the License.
|
14 | 14 | from __future__ import absolute_import
|
15 |
| - |
| 15 | +import os |
| 16 | +from pathlib import Path |
16 | 17 | from sagemaker._studio import (
|
17 | 18 | _append_project_tags,
|
18 | 19 | _find_config,
|
19 | 20 | _load_config,
|
20 | 21 | _parse_tags,
|
21 | 22 | )
|
22 | 23 |
|
| 24 | +def test_find_config_cross_platform(tmpdir): |
| 25 | + """Test _find_config works correctly across different platforms.""" |
| 26 | + # Create a completely separate directory for isolated tests |
| 27 | + import tempfile |
| 28 | + with tempfile.TemporaryDirectory() as isolated_root: |
| 29 | + # Setup test directory structure for positive tests |
| 30 | + config = tmpdir.join(".sagemaker-code-config") |
| 31 | + config.write('{"sagemakerProjectId": "proj-1234"}') |
| 32 | + |
| 33 | + # Test 1: Direct parent directory |
| 34 | + working_dir = tmpdir.mkdir("sub") |
| 35 | + found_path = _find_config(working_dir) |
| 36 | + assert found_path == config |
| 37 | + |
| 38 | + # Test 2: Deeply nested directories |
| 39 | + nested_dir = tmpdir.mkdir("deep").mkdir("nested").mkdir("path") |
| 40 | + found_path = _find_config(nested_dir) |
| 41 | + assert found_path == config |
| 42 | + |
| 43 | + # Test 3: Start from root directory |
| 44 | + import os |
| 45 | + root_dir = os.path.abspath(os.sep) |
| 46 | + found_path = _find_config(root_dir) |
| 47 | + assert found_path is None |
| 48 | + |
| 49 | + # Test 4: No config file in path - using truly isolated directory |
| 50 | + isolated_path = Path(isolated_root) / "nested" / "path" |
| 51 | + isolated_path.mkdir(parents=True) |
| 52 | + found_path = _find_config(isolated_path) |
| 53 | + assert found_path is None |
| 54 | + |
| 55 | +def test_find_config_path_separators(tmpdir): |
| 56 | + """Test _find_config handles different path separator styles. |
| 57 | + |
| 58 | + Tests: |
| 59 | + 1. Forward slashes |
| 60 | + 2. Backslashes |
| 61 | + 3. Mixed separators |
| 62 | + """ |
| 63 | + # Setup |
| 64 | + config = tmpdir.join(".sagemaker-code-config") |
| 65 | + config.write('{"sagemakerProjectId": "proj-1234"}') |
| 66 | + base_path = str(tmpdir) |
| 67 | + |
| 68 | + # Test different path separator styles |
| 69 | + paths = [ |
| 70 | + os.path.join(base_path, "dir1", "dir2"), # OS native |
| 71 | + "/".join([base_path, "dir1", "dir2"]), # Forward slashes |
| 72 | + "\\".join([base_path, "dir1", "dir2"]), # Backslashes |
| 73 | + base_path + "/dir1\\dir2" # Mixed |
| 74 | + ] |
| 75 | + |
| 76 | + for path in paths: |
| 77 | + os.makedirs(path, exist_ok=True) |
| 78 | + found_path = _find_config(path) |
| 79 | + assert found_path == config |
23 | 80 |
|
24 | 81 | def test_find_config(tmpdir):
|
25 | 82 | path = tmpdir.join(".sagemaker-code-config")
|
|
0 commit comments