Skip to content

Commit 31124fe

Browse files
committed
feat: add system! method and spec
Fix the forgot input nil check
1 parent 24f2629 commit 31124fe

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

.rubocop.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ inherit_gem:
22
rubocop-config-crystal: .rubocop.yml
33

44
Metrics/BlockLength:
5-
Enabled: false
5+
Enabled: false
6+
7+
Style/NonNilCheck:
8+
Enabled: false

lib/cr/exec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def output(*args, chomp: true, **options)
2929
end
3030

3131
def each_line(*args, chomp: false, **options, &block)
32-
if block
32+
if block_given?
3333
IO.popen(*args, **options) do |pipe|
3434
pipe.each_line(chomp: chomp, &block)
3535
end
@@ -40,9 +40,9 @@ def each_line(*args, chomp: false, **options, &block)
4040

4141
def answer(*args, input: nil, **options)
4242
IO.popen(*args, **options) do |pipe|
43-
if input.is_a? Array
43+
if input.is_a?(Array)
4444
input.each { |cmd| pipe.puts(cmd) }
45-
else
45+
elsif input != nil
4646
pipe.puts(input)
4747
end
4848
pipe.close_write
@@ -53,5 +53,9 @@ def answer(*args, input: nil, **options)
5353
def system?(...)
5454
system(...) ? true : false
5555
end
56+
57+
def system!(*args, **options)
58+
system(*args, exception: true, **options)
59+
end
5660
end
5761
end

spec/cr/exec_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,9 @@
8383
p system("unamea")
8484
expect(Cr::Exec.system?("unamea")).to eq false
8585
end
86+
87+
it "system! raise" do
88+
system("unamea")
89+
expect { Cr::Exec.system!("unamea") }.to raise_error(Errno::ENOENT)
90+
end
8691
end

0 commit comments

Comments
 (0)