From 8fd0b949cce2e5f13cc7e9fa29be8a329fe264c7 Mon Sep 17 00:00:00 2001 From: schneems Date: Mon, 8 Nov 2021 19:24:41 -0600 Subject: [PATCH] Support multiple files from the CLI command Close #109 This change allows multiple files to be passed into the CLI with one call: ``` $ ./exe/dead_end **/*.rb Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK Syntax OK ``` --- CHANGELOG.md | 2 ++ lib/dead_end/cli.rb | 40 +++++++++++++++++++++------------------- spec/unit/cli_spec.rb | 24 +++++++++++++++++++++++- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c67e29..ac6f2f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## HEAD (unreleased) +- Support checking multiple files from the CLI at once (https://github.com/zombocom/dead_end/pull/110) + ## 3.0.1 - Fix CLI parsing when flags come before filename (https://github.com/zombocom/dead_end/pull/102) diff --git a/lib/dead_end/cli.rb b/lib/dead_end/cli.rb index 6ea3aba..58c9363 100644 --- a/lib/dead_end/cli.rb +++ b/lib/dead_end/cli.rb @@ -40,34 +40,36 @@ def call return if options[:exit] end - file_name = @argv.first - if file_name.nil? + if @argv.empty? @io.puts "No file given" @exit_obj.exit(1) return end - file = Pathname(file_name) - if !file.exist? - @io.puts "file not found: #{file.expand_path} " - @exit_obj.exit(1) - return - end + display_array = [] + while (file_name = @argv.pop) + file = Pathname(file_name) + if !file.exist? + @io.puts "file not found: #{file.expand_path} " + @exit_obj.exit(1) + return + end - @io.puts "Record dir: #{options[:record_dir]}" if options[:record_dir] + @io.puts "Record dir: #{options[:record_dir]}" if options[:record_dir] - display = DeadEnd.call( - io: @io, - source: file.read, - filename: file.expand_path, - terminal: options.fetch(:terminal, DeadEnd::DEFAULT_VALUE), - record_dir: options[:record_dir] - ) + display_array << DeadEnd.call( + io: @io, + source: file.read, + filename: file.expand_path, + terminal: options.fetch(:terminal, DeadEnd::DEFAULT_VALUE), + record_dir: options[:record_dir] + ) + end - if display.document_ok? - @exit_obj.exit(0) - else + if display_array.empty? || display_array.detect { |d| !d.document_ok? } @exit_obj.exit(1) + else + @exit_obj.exit(0) end end diff --git a/spec/unit/cli_spec.rb b/spec/unit/cli_spec.rb index 8925eb4..a25fa15 100644 --- a/spec/unit/cli_spec.rb +++ b/spec/unit/cli_spec.rb @@ -42,6 +42,29 @@ def called? end end + it "works on multiple files" do + Dir.mktmpdir do |dir| + dir = Pathname(dir) + file_one = dir.join("script_one.rb") + file_one.write("puts 'lol'") + + file_two = dir.join("script_two.rb") + file_two.write("puts 'lol'") + + io = StringIO.new + exit_obj = FakeExit.new + Cli.new( + io: io, + argv: [file_one.to_s, file_two.to_s], + exit_obj: exit_obj + ).call + + expect(exit_obj.called?).to be_truthy + expect(exit_obj.value).to eq(0) + expect(io.string.strip).to eq("Syntax OK\nSyntax OK") + end + end + it "parses invalid code" do file = fixtures_dir.join("this_project_extra_def.rb.txt") @@ -52,7 +75,6 @@ def called? argv: [file.to_s], exit_obj: exit_obj ).call - out = io.string debug_display(out)