Skip to content

Commit

Permalink
Add test for bin/dev script
Browse files Browse the repository at this point in the history
  • Loading branch information
ahangarha committed Dec 31, 2022
1 parent 40a052e commit 7e84926
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/generators/react_on_rails/bin/dev
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ rescue Errno::ENOENT
end

def run(process)
argv = ARGV.join(" ")
args = [*ARGV]
args.shift
puts "Using #{process}"
exec "#{process} start -f Procfile.dev", argv
exec "#{process} start -f Procfile.dev", args.join(" ")
rescue Errno::ENOENT
puts <<~MSG
warn <<~MSG
ERROR:
Please ensure `Procfile.dev` exist in your project!
MSG
Expand All @@ -24,7 +25,7 @@ if installed? "overmind"
elsif installed? "foreman"
run "foreman"
else
puts <<~MSG
warn <<~MSG
NOTICE:
For this script to run, you need either 'overmind' or 'foreman' installed on your machine. Please try this script after installing one of them.
MSG
Expand Down
66 changes: 66 additions & 0 deletions spec/react_on_rails/dev_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

# To suppress stdout during tests
RSpec.configure do |config|
original_stderr = $stderr
original_stdout = $stdout
config.before(:all) do
$stderr = File.open(File::NULL, "w")
$stdout = File.open(File::NULL, "w")
end
config.after(:all) do
$stderr = original_stderr
$stdout = original_stdout
end
end

RSpec.describe "bin/dev script" do
let(:script_path) { "lib/generators/react_on_rails/bin/dev" }

it "with Overmind installed, uses Overmind" do
allow(IO).to receive(:popen).with("overmind -v").and_return("Some truthy result")

expect_any_instance_of(Kernel).to receive(:exec).with("overmind start -f Procfile.dev", "")

load script_path
end

it "without Overmind and with Foreman installed, uses Foreman" do
allow(IO).to receive(:popen).with("overmind -v").and_raise(Errno::ENOENT)
allow(IO).to receive(:popen).with("foreman -v").and_return("Some truthy result")

expect_any_instance_of(Kernel).to receive(:exec).with("foreman start -f Procfile.dev", "")

load script_path
end

it "without Overmind and Foreman installed, exits with error message" do
allow(IO).to receive(:popen).with("overmind -v").and_raise(Errno::ENOENT)
allow(IO).to receive(:popen).with("foreman -v").and_raise(Errno::ENOENT)
allow_any_instance_of(Kernel).to receive(:exit!)

expected_message = <<~MSG
NOTICE:
For this script to run, you need either 'overmind' or 'foreman' installed on your machine. Please try this script after installing one of them.
MSG

expect { load script_path }.to output(expected_message).to_stderr_from_any_process
end

it "With Overmind and without Procfile, exits with error message" do
allow(IO).to receive(:popen).with("overmind -v").and_return("Some truthy result")

allow_any_instance_of(Kernel)
.to receive(:exec)
.with("overmind start -f Procfile.dev", "")
.and_raise(Errno::ENOENT)
allow_any_instance_of(Kernel).to receive(:exit!)

expected_message = <<~MSG
ERROR:
Please ensure `Procfile.dev` exist in your project!
MSG

expect { load script_path }.to output(expected_message).to_stderr_from_any_process
end
end

0 comments on commit 7e84926

Please sign in to comment.