Skip to content

Commit 454406c

Browse files
skarpovdevdevcrocod
authored andcommitted
Fix tests on Windows
1 parent 39f2c2a commit 454406c

File tree

1 file changed

+54
-11
lines changed
  • kotlin-sdk-test/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/integration/typescript

1 file changed

+54
-11
lines changed

kotlin-sdk-test/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/integration/typescript/TypeScriptTestBase.kt

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ abstract class TypeScriptTestBase {
3131
@BeforeAll
3232
fun setupTypeScriptSdk() {
3333
println("Cloning TypeScript SDK repository")
34+
3435
if (!sdkDir.exists()) {
35-
val cloneCommand =
36-
"git clone --depth 1 https://github.com/modelcontextprotocol/typescript-sdk.git ${sdkDir.absolutePath}"
37-
val process = ProcessBuilder()
38-
.command("bash", "-c", cloneCommand)
36+
val process = ProcessBuilder(
37+
"git",
38+
"clone",
39+
"--depth",
40+
"1",
41+
"https://github.com/modelcontextprotocol/typescript-sdk.git",
42+
sdkDir.absolutePath,
43+
)
3944
.redirectErrorStream(true)
4045
.start()
4146
val exitCode = process.waitFor()
@@ -54,7 +59,13 @@ abstract class TypeScriptTestBase {
5459

5560
@JvmStatic
5661
protected fun killProcessOnPort(port: Int) {
57-
executeCommand("lsof -ti:$port | xargs kill -9 2>/dev/null || true", File("."))
62+
val isWindows = System.getProperty("os.name").lowercase().contains("windows")
63+
val killCommand = if (isWindows) {
64+
"netstat -ano | findstr :$port | for /f \"tokens=5\" %a in ('more') do taskkill /F /PID %a 2>nul || echo No process found"
65+
} else {
66+
"lsof -ti:$port | xargs kill -9 2>/dev/null || true"
67+
}
68+
runCommand(killCommand, File("."), allowFailure = true, timeoutSeconds = null)
5869
}
5970

6071
@JvmStatic
@@ -70,8 +81,26 @@ abstract class TypeScriptTestBase {
7081
allowFailure: Boolean,
7182
timeoutSeconds: Long?,
7283
): String {
73-
val process = ProcessBuilder()
74-
.command("bash", "-c", "TYPESCRIPT_SDK_DIR='${sdkDir.absolutePath}' $command")
84+
if (!workingDir.exists()) {
85+
if (!workingDir.mkdirs()) {
86+
throw RuntimeException("Failed to create working directory: ${workingDir.absolutePath}")
87+
}
88+
}
89+
90+
if (!workingDir.isDirectory || !workingDir.canRead()) {
91+
throw RuntimeException("Working directory is not accessible: ${workingDir.absolutePath}")
92+
}
93+
94+
val isWindows = System.getProperty("os.name").lowercase().contains("windows")
95+
val processBuilder = if (isWindows) {
96+
ProcessBuilder()
97+
.command("cmd.exe", "/c", "set TYPESCRIPT_SDK_DIR=${sdkDir.absolutePath} && $command")
98+
} else {
99+
ProcessBuilder()
100+
.command("bash", "-c", "TYPESCRIPT_SDK_DIR='${sdkDir.absolutePath}' $command")
101+
}
102+
103+
val process = processBuilder
75104
.directory(workingDir)
76105
.redirectErrorStream(true)
77106
.start()
@@ -89,7 +118,7 @@ abstract class TypeScriptTestBase {
89118
val exitCode = process.waitFor()
90119
if (!allowFailure && exitCode != 0) {
91120
throw RuntimeException(
92-
"Command execution failed with exit code $exitCode: $command\nOutput:\n$output",
121+
"Command execution failed with exit code $exitCode: $command\nWorking dir: ${workingDir.absolutePath}\nOutput:\n$output",
93122
)
94123
}
95124
} else {
@@ -142,11 +171,25 @@ abstract class TypeScriptTestBase {
142171

143172
protected fun startTypeScriptServer(port: Int): Process {
144173
killProcessOnPort(port)
145-
val processBuilder = ProcessBuilder()
146-
.command("bash", "-c", "MCP_PORT=$port npx tsx src/examples/server/simpleStreamableHttp.ts")
174+
175+
if (!sdkDir.exists() || !sdkDir.isDirectory) {
176+
throw IllegalStateException("TypeScript SDK directory does not exist or is not accessible: ${sdkDir.absolutePath}")
177+
}
178+
179+
val isWindows = System.getProperty("os.name").lowercase().contains("windows")
180+
val processBuilder = if (isWindows) {
181+
ProcessBuilder()
182+
.command("cmd.exe", "/c", "set MCP_PORT=$port && npx tsx src/examples/server/simpleStreamableHttp.ts")
183+
} else {
184+
ProcessBuilder()
185+
.command("bash", "-c", "MCP_PORT=$port npx tsx src/examples/server/simpleStreamableHttp.ts")
186+
}
187+
188+
val process = processBuilder
147189
.directory(sdkDir)
148190
.redirectErrorStream(true)
149-
val process = processBuilder.start()
191+
.start()
192+
150193
if (!waitForPort(port = port)) {
151194
throw IllegalStateException("TypeScript server did not become ready on localhost:$port within timeout")
152195
}

0 commit comments

Comments
 (0)