Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## HEAD (unreleased)

- Fix CLI parsing when flags come before filename (https://github.com/zombocom/dead_end/pull/102)

## 3.0.0

- [Breaking] Remove previously deprecated `require "dead_end/fyi"` interface (https://github.com/zombocom/dead_end/pull/94)
Expand Down
21 changes: 16 additions & 5 deletions lib/dead_end/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module DeadEnd
# Cli.new(argv: ["<path/to/file>.rb", "--terminal"]).call
#
class Cli
attr_accessor :options, :file_name
attr_accessor :options

# ARGV is Everything passed to the executable, does not include executable name
#
Expand All @@ -26,22 +26,33 @@ def initialize(argv:, exit_obj: Kernel, io: $stdout, env: ENV)

@io = io
@argv = argv
@file_name = argv[0]
@exit_obj = exit_obj
end

def call
if file_name.nil? || file_name.empty?
if @argv.empty?
# Display help if raw command
parser.parse! %w[--help]
return
else
# Mutates @argv
parse
return if options[:exit]
end

# Needed for testing since we fake exit
return if options[:exit]
file_name = @argv.first
if file_name.nil?
@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

@io.puts "Record dir: #{options[:record_dir]}" if options[:record_dir]

Expand Down
52 changes: 52 additions & 0 deletions spec/unit/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,58 @@ def called?
expect(out.strip).to include("❯ 36 def filename")
end

it "parses valid code with flags" do
Dir.mktmpdir do |dir|
dir = Pathname(dir)
file = dir.join("script.rb")
file.write("puts 'lol'")

io = StringIO.new
exit_obj = FakeExit.new
cli = Cli.new(
io: io,
argv: ["--terminal", file.to_s],
exit_obj: exit_obj
)
cli.call

expect(exit_obj.called?).to be_truthy
expect(exit_obj.value).to eq(0)
expect(cli.options[:terminal]).to be_truthy
expect(io.string.strip).to eq("Syntax OK")
end
end

it "errors when no file given" do
io = StringIO.new
exit_obj = FakeExit.new
cli = Cli.new(
io: io,
argv: ["--terminal"],
exit_obj: exit_obj
)
cli.call

expect(exit_obj.called?).to be_truthy
expect(exit_obj.value).to eq(1)
expect(io.string.strip).to eq("No file given")
end

it "errors when file does not exist" do
io = StringIO.new
exit_obj = FakeExit.new
cli = Cli.new(
io: io,
argv: ["lol-i-d-o-not-ex-ist-yololo.txtblerglol"],
exit_obj: exit_obj
)
cli.call

expect(exit_obj.called?).to be_truthy
expect(exit_obj.value).to eq(1)
expect(io.string.strip).to include("file not found:")
end

# We cannot execute the parser here
# because it calls `exit` and it will exit
# our tests, however we can assert that the
Expand Down