From 76961d2c7cfb0ac15bbad2de9004ebaf2521ed11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20Mari=C3=A9?= Date: Sun, 1 Sep 2024 12:32:37 +0200 Subject: [PATCH] add new file and update gemspec, lockfile, bin, and spec files --- :w | 22 ++++++++++++++++++++++ Gemfile.lock | 3 ++- dorian.gemspec | 1 + lib/dorian/bin.rb | 13 ++++++------- spec/dorian_spec.rb | 34 ++++++++++++++++++++++++++++++---- 5 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 :w diff --git a/:w b/:w new file mode 100644 index 0000000..cd950a7 --- /dev/null +++ b/:w @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require "spec_helper" +require "json" + +RSpec.describe "Dorian" do + it "works" do + version = File.read("VERSION") + expect(`bin/dorian -v 2>&1`).to eq(version) + expect(`bin/dorian --version 2>&1`).to eq(version) + expect(`bin/dorian -h 2>&1`).to include("USAGE") + expect(`bin/dorian --help 2>&1`).to include("USAGE") + books_pretty = + JSON.pretty_generate(JSON.parse(File.read("samples/books.json"))) + + "\n" + expect(`bin/dorian samples/books.json`).to eq(books_pretty) + expect(`bin/dorian --pretty true samples/books.json`).to eq(books_pretty) + expect(`bin/dorian --pretty samples/books.json`).to eq(books_pretty) + books_ugly = JSON.parse(File.read("samples/books.json")).to_json + "\n" + expect(`bin/dorian --pretty false samples/books.json`).to eq(books_ugly) + end +end diff --git a/Gemfile.lock b/Gemfile.lock index a262990..d49b1c7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,7 @@ PATH remote: . specs: dorian (2.0.2) + activesupport csv dorian-arguments dorian-eval @@ -41,7 +42,7 @@ GEM yaml dorian-progress (1.1.0) ruby-progressbar - dorian-to_struct (1.2.0) + dorian-to_struct (2.0.0) drb (2.2.1) i18n (1.14.5) concurrent-ruby (~> 1.0) diff --git a/dorian.gemspec b/dorian.gemspec index dfdfbbe..f8090b4 100644 --- a/dorian.gemspec +++ b/dorian.gemspec @@ -12,6 +12,7 @@ Gem::Specification.new do |s| s.files = %w[VERSION bin/dorian lib/dorian/bin.rb] s.executables << "dorian" + s.add_dependency "activesupport" s.add_dependency "csv" s.add_dependency "dorian-arguments" s.add_dependency "dorian-eval" diff --git a/lib/dorian/bin.rb b/lib/dorian/bin.rb index 8dc453e..64bf79e 100644 --- a/lib/dorian/bin.rb +++ b/lib/dorian/bin.rb @@ -1,13 +1,16 @@ # frozen_string_literal: true +require "active_support" +require "active_support/core_ext/hash" +require "active_support/core_ext/array" require "csv" require "dorian/arguments" require "dorian/eval" require "dorian/progress" require "dorian/to_struct" require "json" -require "yaml" require "parallel" +require "yaml" class Dorian class Bin @@ -152,12 +155,8 @@ def to_output(content) map(content, &:to_json).join("\n") when :raw content - when :ruby - content.inspect - when :rubyl - map(content, &:inspect).join("\n") when :yaml - content.to_yaml + content.deep_stringify_keys.to_yaml else abort "#{output} not supported" end @@ -178,7 +177,7 @@ def reads(content) when :raw content when :yaml - YAML.safe_load(content) + YAML.safe_load(content).to_deep_struct else abort "#{input} not supported" end diff --git a/spec/dorian_spec.rb b/spec/dorian_spec.rb index d9f6218..a3c794a 100644 --- a/spec/dorian_spec.rb +++ b/spec/dorian_spec.rb @@ -1,13 +1,39 @@ # frozen_string_literal: true require "spec_helper" +require "json" +require "yaml" RSpec.describe "Dorian" do - it "has the dorian-times gem" do - expect(`bin/times 3`).to eq("1\n2\n3\n") + describe "--version" do + it "works" do + version = File.read("VERSION") + expect(`bin/dorian -v 2>&1`).to eq(version) + expect(`bin/dorian --version 2>&1`).to eq(version) + end end - it "has the dorian-each gem" do - expect(`bin/times 2 | each "puts it.to_i * 2"`).to eq("2\n4\n") + describe "--help" do + it "works" do + expect(`bin/dorian -h 2>&1`).to include("USAGE") + expect(`bin/dorian --help 2>&1`).to include("USAGE") + end + end + + describe "read" do + it "works" do + books = JSON.parse(File.read("samples/books.json")) + books_pretty = "#{JSON.pretty_generate(books)}\n" + books_ugly = "#{books.to_json}\n" + books_yaml = "#{books.to_yaml}\n" + + expect(`bin/dorian samples/books.json`).to eq(books_pretty) + expect(`bin/dorian --pretty samples/books.json`).to eq(books_pretty) + expect(`bin/dorian --pretty true samples/books.json`).to eq(books_pretty) + expect(`bin/dorian --pretty false samples/books.json`).to eq(books_ugly) + expect(`bin/dorian --input json samples/books.json`).to eq(books_pretty) + expect(`bin/dorian --io json samples/books.json`).to eq(books_pretty) + expect(`bin/dorian --output yaml samples/books.json`).to eq(books_yaml) + end end end