Skip to content

Commit 3e93688

Browse files
committed
adjusted tests and dependency resolution to new execution model
1 parent 45d6e3b commit 3e93688

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package kscript.app
33
import java.io.BufferedReader
44
import java.io.File
55
import java.io.InputStreamReader
6-
import kotlin.system.exitProcess
76

87
/**
98
* @author Holger Brandl
@@ -42,9 +41,8 @@ fun resolveDependencies(depIds: List<String>): String? {
4241
val splitDepId = it.split(":")
4342

4443
if (!listOf(3, 4).contains(splitDepId.size)) {
45-
System.err.println("invalid dependency locator: ${it}")
46-
System.err.println("Expected format is groupId:artifactId:version[:classifier]")
47-
exitProcess(1)
44+
System.err.println("[ERROR] Invalid dependency locator: '${it}'. Expected format is groupId:artifactId:version[:classifier]")
45+
quit(1)
4846
}
4947

5048
"""
@@ -107,7 +105,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
107105
// val failedDep = matchResult !!.groupValues[1]
108106
// System.err.println("Failed to resolve: ${failedDep}")
109107

110-
exitProcess(1)
108+
quit(1)
111109
}
112110

113111

@@ -119,7 +117,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
119117

120118
if (classPath == null) {
121119
System.err.println("Failed to lookup dependencies. Check dependency locators or file a bug on https://github.com/holgerbrandl/kscript")
122-
exitProcess(1)
120+
quit(1)
123121
}
124122

125123

@@ -132,5 +130,5 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
132130

133131

134132
fun main(args: Array<String>) {
135-
println(resolveDependencies(args.toList()))
133+
System.err.println(resolveDependencies(args.toList()))
136134
}

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import kscript.app.ShellUtils.requireInPath
44
import org.docopt.DocOptWrapper
55
import java.io.BufferedReader
66
import java.io.File
7+
import java.io.FileInputStream
78
import java.io.InputStreamReader
89
import java.net.URL
910
import java.nio.file.Files
@@ -246,17 +247,26 @@ private fun guessKotlinHome(): String? {
246247
fun prepareScript(scriptResource: String): File {
247248
var scriptFile: File? = null
248249

249-
// if script input was file just use it as it is
250-
if (File(scriptResource).run { isFile() && canRead() }) {
251-
scriptFile = File(scriptResource)
250+
// map script argument to script file
251+
scriptFile = with(File(scriptResource)) {
252+
if (!canRead()) {
253+
// not a file so let's keep the script-file undefined here
254+
null
255+
} else if (listOf("kts", "kt").contains(extension)) {
256+
// script input is a regular script or clas file
257+
this
258+
} else {
259+
// if we can "just" read from script resource create tmp file
260+
// i.e. script input is process substitution file handle
261+
// not FileInputStream(this).bufferedReader().use{ readText()} does not work nor does this.readText
262+
createTmpScript(FileInputStream(this).bufferedReader().readText())
263+
}
252264
}
253265

254266
// support stdin
255267
if (scriptResource == "-" || scriptResource == "/dev/stdin") {
256268
val scriptText = generateSequence() { readLine() }.joinToString("\n").trim()
257-
258-
scriptFile = File(createTempDir(), "scriptlet.${md5(scriptText)}.kts")
259-
scriptFile.writeText(scriptText)
269+
scriptFile = createTmpScript(scriptText)
260270
}
261271

262272

@@ -290,9 +300,7 @@ fun prepareScript(scriptResource: String): File {
290300
script.trim()
291301
}
292302

293-
294-
scriptFile = File(createTempDir(), "scriptlet.${md5(scriptText)}.kts")
295-
scriptFile.writeText(scriptText)
303+
scriptFile = createTmpScript(scriptText)
296304
}
297305

298306

@@ -304,6 +312,12 @@ fun prepareScript(scriptResource: String): File {
304312
return scriptFile!!
305313
}
306314

315+
private fun createTmpScript(scriptText: String): File {
316+
return File(createTempDir(), "scriptlet.${md5(scriptText)}.kts").apply {
317+
writeText(scriptText)
318+
}
319+
}
320+
307321
fun fetchFromURL(scriptURL: String): File? {
308322
val urlHash = md5(scriptURL)
309323
val urlCache = File(KSCRIPT_CACHE_DIR, "/url_cache_${urlHash}.kts")

test/test_suite.sh

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ assert_statement(){
1515
#assert_statment "echo foo; echo bar >&2; exit 1" "foo" "bar" 1
1616

1717

18+
assert_stderr(){
19+
assert "( $1 ) 2>&1 >/dev/null" "$2"
20+
}
21+
#assert_stderr "echo foo" "bar"
22+
1823
#http://stackoverflow.com/questions/3005963/how-can-i-have-a-newline-in-a-string-in-sh
1924
#http://stackoverflow.com/questions/3005963/how-can-i-have-a-newline-in-a-string-in-sh
2025
export NL=$'\n'
@@ -99,24 +104,25 @@ assert_end environment_tests
99104
## dependency_lookup
100105

101106
# export KSCRIPT_HOME="/Users/brandl/projects/kotlin/kscript"; export PATH=${KSCRIPT_HOME}:${PATH}
102-
alias resdeps.kts='kotlin -classpath kscript.jar kscript.app.DepedencyUtilKt'
107+
resolve_deps() { kotlin -classpath kscript.jar kscript.app.DepedencyUtilKt "$@";}
108+
export -f resolve_deps
109+
103110

104-
assert "resdeps.kts log4j:log4j:1.2.14" "${HOME}/.m2/repository/log4j/log4j/1.2.14/log4j-1.2.14.jar"
111+
assert_stderr "resolve_deps log4j:log4j:1.2.14" "${HOME}/.m2/repository/log4j/log4j/1.2.14/log4j-1.2.14.jar"
105112

106113
## impossible version
107-
assert_raises "resdeps.kts log4j:log4j:9.8.76" 1
114+
assert "resolve_deps log4j:log4j:9.8.76" "false"
108115

109116
## wrong format should exit with 1
110-
assert_raises "resdeps.kts log4j:1.0" 1
117+
assert "resolve_deps log4j:1.0" "false"
111118

112-
## wrong format should give meaningful error message
113-
assert "resdeps.kts log4j:1.0 2>&1" "invalid dependency locator: log4j:1.0\nExpected format is groupId:artifactId:version[:classifier]"
119+
assert_stderr "resolve_deps log4j:1.0" "[ERROR] Invalid dependency locator: 'log4j:1.0'. Expected format is groupId:artifactId:version[:classifier]"
114120

115121
## other version of wrong format should die with useful error.
116-
assert "resdeps.kts log4j:::1.0 2>&1" "Failed to lookup dependencies. Check dependency locators or file a bug on https://github.com/holgerbrandl/kscript"
122+
assert_stderr "resolve_deps log4j:::1.0" "Failed to lookup dependencies. Check dependency locators or file a bug on https://github.com/holgerbrandl/kscript"
117123

118124
## one good dependency, one wrong
119-
assert_raises "resdeps.kts org.org.docopt:org.docopt:0.9.0-SNAPSHOT log4j:log4j:1.2.14" 1
125+
assert_raises "resolve_deps org.org.docopt:org.docopt:0.9.0-SNAPSHOT log4j:log4j:1.2.14" 1
120126

121127
assert_end dependency_lookup
122128

0 commit comments

Comments
 (0)