Skip to content

Commit 2b15eda

Browse files
RealOrkoRealOrko
authored andcommitted
Fixing code to run on windows
1 parent 3d99ad9 commit 2b15eda

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

tests/sdk/io/test_path.sn

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,33 @@
22
# test_path.sn - Tests for SDK Path Type
33
# ==============================================================================
44
# Tests the SnPath struct and its methods.
5+
# Cross-platform: works on Windows, Linux, and macOS
56
# ==============================================================================
67

78
import "../../../sdk/io/path"
89

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+
932
fn test_directory(): void =>
1033
print("test_directory: ")
1134

@@ -58,49 +81,54 @@ fn test_join(): void =>
5881
fn test_absolute(): void =>
5982
print("test_absolute: ")
6083

84+
# Use cross-platform temp directory
85+
var tempDir: str = getTempDir()
86+
6187
# 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")
6490

6591
# Relative paths should become absolute
6692
var rel: str = SnPath.absolute(".")
67-
assert(rel.startsWith("/"), "Relative should become absolute")
93+
assert(isAbsolutePath(rel), "Relative should become absolute")
6894

6995
print("PASS\n")
7096

7197
fn test_exists(): void =>
7298
print("test_exists: ")
7399

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")
76103

77104
# 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")
79106

80107
print("PASS\n")
81108

82109
fn test_is_file(): void =>
83110
print("test_is_file: ")
84111

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")
89117

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")
93120

94121
print("PASS\n")
95122

96123
fn test_is_directory(): void =>
97124
print("test_is_directory: ")
98125

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")
101129

102130
# 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")
104132

105133
print("PASS\n")
106134

0 commit comments

Comments
 (0)