mirror of
https://github.com/QwenLM/qwen-code.git
synced 2026-04-28 11:41:04 +00:00
fix(sdk-java): pass custom env to CLI process (#3543)
Co-authored-by: lawrence3699 <lawrence3699@users.noreply.github.com>
This commit is contained in:
parent
3182500835
commit
3e74a33460
2 changed files with 46 additions and 0 deletions
|
|
@ -16,6 +16,7 @@ import java.io.IOException;
|
|||
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;
|
||||
|
|
@ -103,6 +104,13 @@ public class ProcessTransport implements Transport {
|
|||
.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()));
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -16,12 +20,20 @@ import com.alibaba.qwen.code.cli.transport.Transport;
|
|||
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 {
|
||||
|
|
@ -30,6 +42,21 @@ class ProcessTransportTest {
|
|||
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();
|
||||
|
|
@ -84,4 +111,15 @@ class ProcessTransportTest {
|
|||
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…
Add table
Add a link
Reference in a new issue