Skip to content

Commit 58a8d74

Browse files
authored
Merge pull request #102 from zombocom/schneems/fix-cli
Fix CLI edge case around position of arguments
2 parents 050a6be + dab1cd6 commit 58a8d74

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## HEAD (unreleased)
22

3+
- Fix CLI parsing when flags come before filename (https://github.com/zombocom/dead_end/pull/102)
4+
35
## 3.0.0
46

57
- [Breaking] Remove previously deprecated `require "dead_end/fyi"` interface (https://github.com/zombocom/dead_end/pull/94)

lib/dead_end/cli.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module DeadEnd
1212
# Cli.new(argv: ["<path/to/file>.rb", "--terminal"]).call
1313
#
1414
class Cli
15-
attr_accessor :options, :file_name
15+
attr_accessor :options
1616

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

2727
@io = io
2828
@argv = argv
29-
@file_name = argv[0]
3029
@exit_obj = exit_obj
3130
end
3231

3332
def call
34-
if file_name.nil? || file_name.empty?
33+
if @argv.empty?
3534
# Display help if raw command
3635
parser.parse! %w[--help]
36+
return
3737
else
38+
# Mutates @argv
3839
parse
40+
return if options[:exit]
3941
end
4042

41-
# Needed for testing since we fake exit
42-
return if options[:exit]
43+
file_name = @argv.first
44+
if file_name.nil?
45+
@io.puts "No file given"
46+
@exit_obj.exit(1)
47+
return
48+
end
4349

4450
file = Pathname(file_name)
51+
if !file.exist?
52+
@io.puts "file not found: #{file.expand_path} "
53+
@exit_obj.exit(1)
54+
return
55+
end
4556

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

spec/unit/cli_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,58 @@ def called?
6161
expect(out.strip).to include("❯ 36 def filename")
6262
end
6363

64+
it "parses valid code with flags" do
65+
Dir.mktmpdir do |dir|
66+
dir = Pathname(dir)
67+
file = dir.join("script.rb")
68+
file.write("puts 'lol'")
69+
70+
io = StringIO.new
71+
exit_obj = FakeExit.new
72+
cli = Cli.new(
73+
io: io,
74+
argv: ["--terminal", file.to_s],
75+
exit_obj: exit_obj
76+
)
77+
cli.call
78+
79+
expect(exit_obj.called?).to be_truthy
80+
expect(exit_obj.value).to eq(0)
81+
expect(cli.options[:terminal]).to be_truthy
82+
expect(io.string.strip).to eq("Syntax OK")
83+
end
84+
end
85+
86+
it "errors when no file given" do
87+
io = StringIO.new
88+
exit_obj = FakeExit.new
89+
cli = Cli.new(
90+
io: io,
91+
argv: ["--terminal"],
92+
exit_obj: exit_obj
93+
)
94+
cli.call
95+
96+
expect(exit_obj.called?).to be_truthy
97+
expect(exit_obj.value).to eq(1)
98+
expect(io.string.strip).to eq("No file given")
99+
end
100+
101+
it "errors when file does not exist" do
102+
io = StringIO.new
103+
exit_obj = FakeExit.new
104+
cli = Cli.new(
105+
io: io,
106+
argv: ["lol-i-d-o-not-ex-ist-yololo.txtblerglol"],
107+
exit_obj: exit_obj
108+
)
109+
cli.call
110+
111+
expect(exit_obj.called?).to be_truthy
112+
expect(exit_obj.value).to eq(1)
113+
expect(io.string.strip).to include("file not found:")
114+
end
115+
64116
# We cannot execute the parser here
65117
# because it calls `exit` and it will exit
66118
# our tests, however we can assert that the

0 commit comments

Comments
 (0)