Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.ProcessBuilder.Redirect;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -103,6 +104,13 @@ public void start() throws IOException {
.redirectErrorStream(false)
.directory(new File(transportOptionsAdapter.getCwd()));

Map<String, String> env = transportOptionsAdapter.getHandledTransportOptions().getEnv();
if (env != null) {
Map<String, String> processEnv = processBuilder.environment();
processEnv.clear();
processEnv.putAll(env);
}

process = processBuilder.start();
processInput = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
processOutput = new BufferedReader(new InputStreamReader(process.getInputStream()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.alibaba.qwen.code.cli.transport.process;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
Expand All @@ -16,12 +20,20 @@
import com.alibaba.qwen.code.cli.transport.TransportOptions;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;

class ProcessTransportTest {

private static final Logger logger = LoggerFactory.getLogger(ProcessTransportTest.class);
private static final String CUSTOM_ENV_NAME = "QWEN_SDK_TEST_ENV";
private static final String CUSTOM_ENV_VALUE = "from-set-env";

@TempDir
Path tempDir;

@Test
void shouldStartAndCloseSuccessfully() throws IOException {
Expand All @@ -30,6 +42,21 @@ void shouldStartAndCloseSuccessfully() throws IOException {
transport.close();
}

@Test
void shouldPassCustomEnvToProcess() throws IOException {
Path executable = createEnvPrinter();
TransportOptions transportOptions = new TransportOptions()
.setPathToQwenExecutable(executable.toString())
.setEnv(Collections.singletonMap(CUSTOM_ENV_NAME, CUSTOM_ENV_VALUE));

ProcessTransport transport = new ProcessTransport(transportOptions);
try {
assertEquals(CUSTOM_ENV_VALUE, transport.processOutput.readLine());
} finally {
transport.close();
}
}

@Test
void shouldInputWaitForOneLineSuccessfully() throws IOException, ExecutionException, InterruptedException, TimeoutException {
TransportOptions transportOptions = new TransportOptions();
Expand Down Expand Up @@ -84,4 +111,15 @@ void shouldSdkMessageSuccessfully() throws IOException, ExecutionException, Inte
line -> "result".equals(JSON.parseObject(line).getString("type")));
}

private Path createEnvPrinter() throws IOException {
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");
Path executable = tempDir.resolve(isWindows ? "print-env.cmd" : "print-env.sh");
String script = isWindows
? "@echo off\r\necho %" + CUSTOM_ENV_NAME + "%\r\n"
: "#!/bin/sh\nprintf '%s\\n' \"$" + CUSTOM_ENV_NAME + "\"\n";
Files.write(executable, script.getBytes(StandardCharsets.UTF_8));
executable.toFile().setExecutable(true);
return executable;
}

}
Loading