|
2 | 2 | # test_path.sn - Tests for SDK Path Type |
3 | 3 | # ============================================================================== |
4 | 4 | # Tests the SnPath struct and its methods. |
| 5 | +# Cross-platform: works on Windows, Linux, and macOS |
5 | 6 | # ============================================================================== |
6 | 7 |
|
7 | 8 | import "../../../sdk/io/path" |
8 | 9 |
|
| 10 | +# Cross-platform temp directory helper |
| 11 | +fn getTempDir(): str => |
| 12 | + # Windows uses TEMP, Unix uses TMPDIR or /tmp |
| 13 | + return Environment.get("TEMP", Environment.get("TMPDIR", "/tmp")).replace("\\", "/") |
| 14 | + |
| 15 | +# Check if a path is absolute (cross-platform) |
| 16 | +fn isAbsolutePath(path: str): bool => |
| 17 | + if path.length < 1 => |
| 18 | + return false |
| 19 | + # Unix: starts with / |
| 20 | + if path.startsWith("/") => |
| 21 | + return true |
| 22 | + # Windows: starts with drive letter like C:\ or C:/ |
| 23 | + if path.length >= 3 => |
| 24 | + var firstChar: char = path.charAt(0) |
| 25 | + var secondChar: char = path.charAt(1) |
| 26 | + var thirdChar: char = path.charAt(2) |
| 27 | + if (firstChar >= 'A' && firstChar <= 'Z') || (firstChar >= 'a' && firstChar <= 'z') => |
| 28 | + if secondChar == ':' && (thirdChar == '/' || thirdChar == '\\') => |
| 29 | + return true |
| 30 | + return false |
| 31 | + |
9 | 32 | fn test_directory(): void => |
10 | 33 | print("test_directory: ") |
11 | 34 |
|
@@ -58,49 +81,54 @@ fn test_join(): void => |
58 | 81 | fn test_absolute(): void => |
59 | 82 | print("test_absolute: ") |
60 | 83 |
|
| 84 | + # Use cross-platform temp directory |
| 85 | + var tempDir: str = getTempDir() |
| 86 | + |
61 | 87 | # Absolute paths should return themselves (mostly) |
62 | | - var abs: str = SnPath.absolute("/tmp") |
63 | | - assert(abs.startsWith("/"), "Absolute should start with /") |
| 88 | + var abs: str = SnPath.absolute(tempDir) |
| 89 | + assert(isAbsolutePath(abs), "Absolute should be absolute path") |
64 | 90 |
|
65 | 91 | # Relative paths should become absolute |
66 | 92 | var rel: str = SnPath.absolute(".") |
67 | | - assert(rel.startsWith("/"), "Relative should become absolute") |
| 93 | + assert(isAbsolutePath(rel), "Relative should become absolute") |
68 | 94 |
|
69 | 95 | print("PASS\n") |
70 | 96 |
|
71 | 97 | fn test_exists(): void => |
72 | 98 | print("test_exists: ") |
73 | 99 |
|
74 | | - # /tmp should exist |
75 | | - assert(SnPath.exists("/tmp"), "/tmp should exist") |
| 100 | + # Temp directory should exist |
| 101 | + var tempDir: str = getTempDir() |
| 102 | + assert(SnPath.exists(tempDir), "Temp directory should exist") |
76 | 103 |
|
77 | 104 | # Random path should not exist |
78 | | - assert(!SnPath.exists("/nonexistent_path_12345"), "Nonexistent should not exist") |
| 105 | + assert(!SnPath.exists("/nonexistent_path_12345_xyzabc"), "Nonexistent should not exist") |
79 | 106 |
|
80 | 107 | print("PASS\n") |
81 | 108 |
|
82 | 109 | fn test_is_file(): void => |
83 | 110 | print("test_is_file: ") |
84 | 111 |
|
85 | | - # Create a test file |
86 | | - var testPath: str = "/tmp/sdk_path_test_file.txt" |
87 | | - # We can't use SnTextFile here because we're testing Path independently |
88 | | - # So we'll use a known file that should exist on Linux |
| 112 | + # Get cross-platform temp directory |
| 113 | + var tempDir: str = getTempDir() |
| 114 | + |
| 115 | + # Temp directory should not be a file |
| 116 | + assert(!SnPath.isFile(tempDir), "Temp directory should not be a file") |
89 | 117 |
|
90 | | - # /etc/passwd should be a file on most Linux systems |
91 | | - # But let's just test that directories aren't files |
92 | | - assert(!SnPath.isFile("/tmp"), "/tmp should not be a file") |
| 118 | + # Nonexistent path should not be a file |
| 119 | + assert(!SnPath.isFile("/nonexistent_path_12345_xyzabc"), "Nonexistent should not be a file") |
93 | 120 |
|
94 | 121 | print("PASS\n") |
95 | 122 |
|
96 | 123 | fn test_is_directory(): void => |
97 | 124 | print("test_is_directory: ") |
98 | 125 |
|
99 | | - # /tmp should be a directory |
100 | | - assert(SnPath.isDirectory("/tmp"), "/tmp should be a directory") |
| 126 | + # Temp directory should be a directory |
| 127 | + var tempDir: str = getTempDir() |
| 128 | + assert(SnPath.isDirectory(tempDir), "Temp directory should be a directory") |
101 | 129 |
|
102 | 130 | # A nonexistent path should not be a directory |
103 | | - assert(!SnPath.isDirectory("/nonexistent_path_12345"), "Nonexistent should not be directory") |
| 131 | + assert(!SnPath.isDirectory("/nonexistent_path_12345_xyzabc"), "Nonexistent should not be directory") |
104 | 132 |
|
105 | 133 | print("PASS\n") |
106 | 134 |
|
|
0 commit comments