Skip to content

Commit 079cf47

Browse files
committed
add tests
1 parent ee74f3a commit 079cf47

File tree

7 files changed

+136
-5
lines changed

7 files changed

+136
-5
lines changed

.github/workflows/build.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: build
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
- name: Set up JDK
16+
uses: actions/setup-java@v3
17+
with:
18+
java-version: "21"
19+
distribution: "temurin"
20+
cache: maven
21+
- name: Build with Maven
22+
env:
23+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
24+
run: mvn --batch-mode -ntp package

core/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@
4141
</execution>
4242
</executions>
4343
</plugin>
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-jar-plugin</artifactId>
47+
<version>3.0.2</version>
48+
<executions>
49+
<execution>
50+
<goals>
51+
<goal>test-jar</goal>
52+
</goals>
53+
</execution>
54+
</executions>
55+
</plugin>
4456
<plugin>
4557
<artifactId>maven-surefire-plugin</artifactId>
4658
</plugin>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.javaaidev.llmcodeexecutor.core;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
import org.junit.jupiter.api.Tag;
8+
9+
@Target({ElementType.TYPE, ElementType.METHOD})
10+
@Retention(RetentionPolicy.RUNTIME)
11+
@Tag("manual")
12+
public @interface Manual {
13+
14+
}

executors/python/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@
7979
<artifactId>kotlin-stdlib</artifactId>
8080
<version>2.1.10</version>
8181
</dependency>
82+
<dependency>
83+
<groupId>com.javaaidev.llmcodeexecutor</groupId>
84+
<artifactId>core</artifactId>
85+
<version>${project.version}</version>
86+
<classifier>tests</classifier>
87+
<type>test-jar</type>
88+
<scope>test</scope>
89+
</dependency>
8290
</dependencies>
8391

8492
</project>

executors/python/src/main/kotlin/com/javaaidev/llmcodeexecutor/executor/python/PythonCodeExecutor.kt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@ package com.javaaidev.llmcodeexecutor.executor.python
22

33
import com.javaaidev.llmcodeexecutor.core.*
44
import java.nio.file.Files
5+
import java.time.Duration
6+
7+
data class PythonCodeExecutorConfig(
8+
val containerImage: String? = null,
9+
)
510

611
object PythonCodeExecutor {
712

8-
fun execute(request: CodeExecutionRequest): CodeExecutionResponse {
13+
fun execute(
14+
request: CodeExecutionRequest,
15+
config: PythonCodeExecutorConfig? = null
16+
): CodeExecutionResponse {
917
val codeDir = Files.createTempDirectory("code_executor")
1018
val codeFile = "app.py"
1119
Files.writeString(codeDir.resolve(codeFile), request.code)
1220
val codeExecutor = LLMCodeExecutor(
1321
CodeExecutorConfig(
14-
"python-executor",
22+
config?.containerImage ?: "ghcr.io/javaaidev/llm-code-executor-python:base-3.12",
1523
listOf("uv", "run", codeFile),
1624
listOf("/app/.venv"),
1725
listOf(
@@ -21,10 +29,18 @@ object PythonCodeExecutor {
2129
)
2230
),
2331
"/app",
24-
containerOutputDirectory = "/app",
32+
Duration.ofMinutes(3),
33+
"/app",
34+
)
35+
)
36+
return codeExecutor.execute(
37+
request.copy(
38+
outputFileCollectionConfig = request.outputFileCollectionConfig?.copy(
39+
includedFilePattern = request.outputFileCollectionConfig?.includedFilePattern
40+
?: "!${codeFile}"
41+
)
2542
)
2643
)
27-
return codeExecutor.execute(request)
2844
}
2945
}
3046

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.javaaidev.llmcodeexecutor.executor.python
2+
3+
import com.javaaidev.llmcodeexecutor.core.CodeExecutionRequest
4+
import com.javaaidev.llmcodeexecutor.core.Manual
5+
import com.javaaidev.llmcodeexecutor.core.OutputFileCollectionConfig
6+
import org.junit.jupiter.api.Test
7+
import kotlin.test.assertEquals
8+
9+
@Manual
10+
class PythonCodeExecutorTest {
11+
@Test
12+
fun basic() {
13+
val result = PythonCodeExecutor.execute(
14+
CodeExecutionRequest(
15+
"""
16+
print("Hello")
17+
""".trimIndent()
18+
)
19+
)
20+
assertEquals("Hello", result.output.trim())
21+
}
22+
23+
@Test
24+
fun collectFiles() {
25+
val result = PythonCodeExecutor.execute(
26+
CodeExecutionRequest(
27+
"""
28+
import numpy as np
29+
import matplotlib.pyplot as plt
30+
x = np.random.randint(0, 100, 100)
31+
y = np.random.randint(0, 100, 100)
32+
plt.scatter(x, y)
33+
plt.savefig('scatter.png')
34+
print('Scatter plot saved to scatter.png')
35+
36+
""".trimIndent(),
37+
OutputFileCollectionConfig(
38+
copyFiles = true,
39+
copiedFilesPath = "./target",
40+
includedFilePattern = "*.png"
41+
)
42+
)
43+
)
44+
assertEquals(1, result.copiedFiles?.size)
45+
}
46+
}

pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@
2626
<build>
2727
<sourceDirectory>src/main/kotlin</sourceDirectory>
2828
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
29+
<pluginManagement>
30+
<plugins>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-surefire-plugin</artifactId>
34+
<version>3.2.5</version>
35+
<configuration>
36+
<excludedGroups>manual</excludedGroups>
37+
</configuration>
38+
</plugin>
39+
</plugins>
40+
</pluginManagement>
2941
<plugins>
3042
<plugin>
3143
<groupId>org.jetbrains.kotlin</groupId>
@@ -50,7 +62,6 @@
5062
</plugin>
5163
<plugin>
5264
<artifactId>maven-surefire-plugin</artifactId>
53-
<version>2.22.2</version>
5465
</plugin>
5566
<plugin>
5667
<artifactId>maven-failsafe-plugin</artifactId>

0 commit comments

Comments
 (0)