Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle EOF input from jq #83

Merged
merged 4 commits into from
Aug 1, 2021
Merged
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
8 changes: 8 additions & 0 deletions spec/converters/simple_yaml_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,14 @@ describe OQ::Converters::SimpleYAML do
end
end
end

describe "when the jq filter doesn't return data" do
it "should return an empty string" do
run_binary(%([{"name":"foo"}]), args: ["-i", "simpleyaml", "-o", "simpleyaml", %<.[] | select(.name != "foo")>]) do |output|
output.should be_empty
end
end
end
end

describe Object do
Expand Down
3 changes: 1 addition & 2 deletions spec/converters/xml_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,8 @@ describe OQ::Converters::XML do

describe "should raise if invalid" do
it "should output correctly" do
run_binary(%(<root id="1<child/></root>), args: ["-i", "xml", "-c", "."]) do |_, status, error|
run_binary(%(<root id="1<child/></root>), args: ["-i", "xml", "-c", "."], success: false) do |_, _, error|
error.should eq "oq error: Couldn't find end of Start Tag root\n"
status.exit_code.should eq 1
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/converters/yaml_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,14 @@ describe OQ::Converters::YAML do
end
end
end

describe "when the jq filter doesn't return data" do
it "should return an empty string" do
run_binary(%([{"name":"foo"}]), args: ["-i", "yaml", "-o", "yaml", %<.[] | select(.name != "foo")>]) do |output|
output.should be_empty
end
end
end
end

describe Object do
Expand Down
9 changes: 3 additions & 6 deletions spec/oq_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -260,27 +260,24 @@ describe OQ do

describe "when there is a jq error" do
it "should return the error and correct exit code" do
run_binary(input: ARRAY_JSON_OBJECT, args: [".names | .[] | .name"]) do |_, status, error|
run_binary(input: ARRAY_JSON_OBJECT, args: [".names | .[] | .name"], success: false) do |_, _, error|
error.should eq %(jq: error (at <stdin>:0): Cannot index number with string "name"\n)
status.exit_code.should eq 1
end
end
end

describe "with an invalid input format" do
it "should return the error and correct exit code" do
run_binary(input: SIMPLE_JSON_OBJECT, args: ["-i", "foo"]) do |_, status, error|
run_binary(input: SIMPLE_JSON_OBJECT, args: ["-i", "foo"], success: false) do |_, _, error|
error.should eq %(Invalid input format: 'foo'\n)
status.exit_code.should eq 1
end
end
end

describe "with an invalid output format" do
it "should return the error and correct exit code" do
run_binary(input: SIMPLE_JSON_OBJECT, args: ["-o", "foo"]) do |_, status, error|
run_binary(input: SIMPLE_JSON_OBJECT, args: ["-o", "foo"], success: false) do |_, _, error|
error.should eq %(Invalid output format: 'foo'\n)
status.exit_code.should eq 1
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ require "spec"
require "../src/oq"

# Runs the the binary with the given *name* and *args*.
def run_binary(input : String? = nil, name : String = "bin/oq", args : Array(String) = [] of String, &block : String, Process::Status, String -> Nil)
def run_binary(input : String? = nil, name : String = "bin/oq", args : Array(String) = [] of String, *, success : Bool = true, file = __FILE__, line = __LINE__, & : String, Process::Status, String -> Nil)
buffer_io = IO::Memory.new
error_io = IO::Memory.new
input_io = IO::Memory.new
input_io << input if input
status = Process.run(name, args, output: buffer_io, input: input_io.rewind, error: error_io)

if success
status.success?.should be_true, file: file, line: line
else
status.success?.should_not be_true, file: file, line: line
end

yield buffer_io.to_s, status, error_io.to_s
end
16 changes: 15 additions & 1 deletion src/converters/yaml.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@ module OQ::Converters::YAML

# ameba:disable Metrics/CyclomaticComplexity
def serialize(input : IO, output : IO, **args) : Nil
json = ::JSON::PullParser.new input
# TODO: Remove this in favor of the .eof? check after
# https://github.com/crystal-lang/crystal/pull/10864 is released.
begin
json = ::JSON::PullParser.new input
rescue ex : ::JSON::ParseException
if (message = ex.message) && (message.includes? "Unexpected token: <EOF>")
return
end

raise ex
end

yaml = ::YAML::Builder.new output

# Return early is there is no JSON to be read.
return if json.kind.eof?

yaml.stream do
yaml.document do
loop do
Expand Down