Skip to content
Closed
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
14 changes: 10 additions & 4 deletions sjsonnet/src-jvm-native/sjsonnet/SjsonnetMainBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,17 @@ object SjsonnetMainBase {

private def isScalar(v: ujson.Value) = !v.isInstanceOf[ujson.Arr] && !v.isInstanceOf[ujson.Obj]

/**
* Parse CLI binding arguments.
* @param wd
* ignored since bindings now use importstr/import; retained for binary compatibility
*/
def parseBindings(
strs: Seq[String],
strFiles: Seq[String],
codes: Seq[String],
codeFiles: Seq[String],
wd: os.Path): Map[String, String] = {
@unused wd: os.Path): Map[String, String] = {

def split(s: String) = {
val idx = s.indexOf('=')
Expand All @@ -364,12 +369,13 @@ object SjsonnetMainBase {

def splitMap(s: Seq[String], f: String => String) =
s.map(split).map { case (x, v) => (x, f(v)) }
def readPath(v: String) =
if (v == "-" || v == "/dev/stdin") io.Source.stdin.mkString else os.read(os.Path(v, wd))
def importStrPath(v: String) =
if (v == "-" || v == "/dev/stdin") ujson.write(io.Source.stdin.mkString)
else s"importstr @'${v.replace("'", "''")}'"

Map() ++
splitMap(strs, v => ujson.write(v)) ++
splitMap(strFiles, v => ujson.write(readPath(v))) ++
splitMap(strFiles, importStrPath) ++
splitMap(codes, identity) ++
splitMap(
codeFiles,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.extVar("contents")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"hello from ext-str-file\n"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello from ext-str-file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.extVar("missing")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sjsonnet.Error: Couldn't import file: "sjsonnet/test/resources/db/cli_missing_ext_str_file_input.txt"
at [std.extVar].(<ext-var missing> offset:0)
at [<root>].(sjsonnet/test/resources/db/cli_missing_ext_str_file.jsonnet:1:11)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
48 changes: 48 additions & 0 deletions sjsonnet/test/src-jvm/sjsonnet/MainTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ object MainTests extends TestSuite {
assert(err1.nonEmpty, err3.nonEmpty)
}

test("missingExtStrFileErrorsAsImport") {
checkCliStderrGolden(
"cli_missing_ext_str_file.jsonnet",
"--ext-str-file",
"missing=sjsonnet/test/resources/db/cli_missing_ext_str_file_input.txt"
)
}

test("unusedMissingExtStrFileIsLazy") {
checkCliStdoutGolden(
"cli_missing_ext_str_file_unused.jsonnet",
"--ext-str-file",
"missing=sjsonnet/test/resources/db/cli_missing_ext_str_file_input.txt"
)
}

test("extStrFileReadsRelativePath") {
checkCliStdoutGolden(
"cli_ext_str_file_relative.jsonnet",
"--ext-str-file",
"contents=sjsonnet/test/resources/db/cli_ext_str_file_relative_input.txt"
)
}

val streamedOut =
"""--- 1
|--- 2
Expand Down Expand Up @@ -488,6 +512,30 @@ object MainTests extends TestSuite {
def runMain(args: os.Shellable*): (Int, String, String) =
runMainWithEnv(jsonnetPathEnv = "", args*)

private def checkCliStderrGolden(fileName: String, args: os.Shellable*): Unit = {
val sourceRel = os.RelPath("sjsonnet") / "test" / "resources" / "db" / fileName
val source = workspaceRoot / sourceRel
val golden = os.read(os.Path(source.toString + ".golden"))
val sourceArg: os.Shellable = sourceRel
val (res, out, err) = runMain((args :+ sourceArg)*)
assert(res == 1)
assert(out.isEmpty)
def normalizeStderr(s: String) =
s.replace("\r\n", "\n").replaceAll("\n+\\z", "")
assert(normalizeStderr(err) == normalizeStderr(golden))
}

private def checkCliStdoutGolden(fileName: String, args: os.Shellable*): Unit = {
val sourceRel = os.RelPath("sjsonnet") / "test" / "resources" / "db" / fileName
val source = workspaceRoot / sourceRel
val golden = os.read(os.Path(source.toString + ".golden"))
val sourceArg: os.Shellable = sourceRel
val (res, out, err) = runMain((args :+ sourceArg)*)
assert(res == 0)
assert(err.isEmpty)
assert(out.replace("\r\n", "\n") == golden.replace("\r\n", "\n"))
}

def runMainWithEnv(jsonnetPathEnv: String, args: os.Shellable*): (Int, String, String) = {
val err = new ByteArrayOutputStream()
val perr = new PrintStream(err, true, "UTF-8")
Expand Down
Loading