Skip to content

Commit a39a129

Browse files
elihartholgerbrandl
authored andcommitted
Improved and documented basic testing support (#247)
1 parent 120ff45 commit a39a129

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

docs/user_guide.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Feel welcome to submit a [ticket](https://github.com/holgerbrandl/kscript/issues
1717
- [Create interpreters for custom DSLs](#create-interpreters-for-custom-dsls)
1818
- [Tips and tricks](#tips-and-tricks)
1919
- [Display images inline and open other files](#display-images-inline-and-open-other-files)
20+
- [Testing](#testing)
2021
- [Text Processing](#text-processing)
2122
- [Examples](#examples)
2223
- [Bioinformatics](#bioinformatics)
@@ -114,7 +115,51 @@ When using iterm2 it is possible to print inlined image output directly into the
114115

115116
Suggested by [@yschimke](https://github.com/yschimke) in [#51](https://github.com/holgerbrandl/kscript/issues/51)
116117

118+
## Testing
119+
When an IDEA project is generated with the `--idea` command it is configured to consider all project files as test sources.
117120

121+
This means that you can include unit test code, such as with JUnit, in your script files and run the tests with Gradle.
122+
123+
For example, to run JUnit tests you would include the junit dependency at the top of your Kotlin file:
124+
```kotlin
125+
@file:DependsOn("junit:junit:4.12")
126+
```
127+
128+
and then include a class with your tests somewhere in that file:
129+
```kotlin
130+
class MyTests {
131+
@Test
132+
fun myTest() {
133+
check(1 + 1 == 2)
134+
}
135+
}
136+
```
137+
138+
You can run this test like usual, either from the IDE (by right clicking on the function or class and selecting "run") or from the command line (with `./gradlew test`).
139+
140+
### Caveats
141+
- These tests can go in either .kts files or the .kt files that your script depend on via `@file:Include`. However, tests in a `.kts` file can only be run from the command line
142+
- If you encounter any Gradle build errors such as "Duplicate JVM class name" then try cleaning the project first - `./gradlew clean test`
143+
- Kscript does not differentiate test dependencies, so any test code or test imports in your script will be included when creating a packaged binary with `--package`
144+
145+
### Testing on CI
146+
If you have script tests that you would like to run in automated environments, like CI, then you likely don't want to open up an Intellij IDEA instance to do so.
147+
148+
However, to setup the gradle project to run the tests we still need to use the `--idea` command. To prevent this command from failing, you can create a mock `idea`:
149+
```
150+
touch idea && chmod +x idea
151+
```
152+
153+
The `--idea` command outputs the directory that the project was created in, which you can use to determine where to run your tests.
154+
155+
For example, you could do something like this to generate the project and test it in one command.
156+
```bash
157+
project_path=$(kscript --idea YourScript.kts 2>&1 | grep --line-buffered "Project set up at" | cut -d'/' -f2-)
158+
cd "/$project_path"
159+
gradle test
160+
```
161+
162+
Note that the Gradlew wrapper is not automatically created by `kscript --idea` so `./gradlew` will not be available. This uses a gradle instance from the PATH instead.
118163

119164
## Text Processing
120165

src/main/kotlin/kscript/app/AppHelpers.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ $stringifiedDeps
261261
}
262262
263263
sourceSets.main.java.srcDirs 'src'
264+
sourceSets.test.java.srcDirs 'src'
264265
""".trimIndent()
265266

266267
File(tmpProjectDir, "build.gradle").writeText(gradleScript)
@@ -285,7 +286,10 @@ sourceSets.main.java.srcDirs 'src'
285286
}
286287
}
287288

288-
return "idea \"${tmpProjectDir.absolutePath}\""
289+
val projectPath = tmpProjectDir.absolutePath
290+
infoMsg("Project set up at $projectPath")
291+
292+
return "idea \"$projectPath\""
289293
}
290294

291295
private fun URL.fileName() = this.toURI().path.split("/").last()

0 commit comments

Comments
 (0)