Skip to content

Commit

Permalink
Support multiple files from the CLI command
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
schneems committed Nov 9, 2021
1 parent e8eb54c commit 959d319
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
41 changes: 22 additions & 19 deletions lib/dead_end/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,37 @@ 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

@io.puts "Record dir: #{options[:record_dir]}" if options[:record_dir]
file = Pathname(file_name)
if !file.exist?
@io.puts "file not found: #{file.expand_path} "
@exit_obj.exit(1)
return
end

display = DeadEnd.call(
io: @io,
source: file.read,
filename: file.expand_path,
terminal: options.fetch(:terminal, DeadEnd::DEFAULT_VALUE),
record_dir: options[:record_dir]
)
@io.puts "Record dir: #{options[:record_dir]}" if options[:record_dir]

if display.document_ok?
@exit_obj.exit(0)
else
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_array.empty? || display_array.detect {|d| !d.document_ok? }
@exit_obj.exit(1)
else
@exit_obj.exit(0)
end
end

Expand Down
24 changes: 23 additions & 1 deletion spec/unit/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -52,7 +75,6 @@ def called?
argv: [file.to_s],
exit_obj: exit_obj
).call

out = io.string
debug_display(out)

Expand Down

0 comments on commit 959d319

Please sign in to comment.