Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gel add command #94

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/gel/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,4 @@ def self.extract_word(arguments)
require_relative "command/config"
require_relative "command/shell_setup"
require_relative "command/open"
require_relative "command/add"
18 changes: 18 additions & 0 deletions lib/gel/command/add.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

class Gel::Command::Add < Gel::Command
def run(argv)
name = argv[0]
raise Gel::Error::NoGemError if name.nil? || name.empty?

line = %(gem "#{name}"\n)
old_gemfile = IO.read(Gel::Environment.find_gemfile)
new_gemfile = old_gemfile + line

IO.write(Gel::Environment.find_gemfile, new_gemfile)
Gel::Environment.write_lock(output: $stdout)
rescue StandardError => exception
File.write("Gemfile", old_gemfile)
puts exception.message
end
end
10 changes: 10 additions & 0 deletions lib/gel/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ def message
end
end

class NoGemError < Gel::UserError
def initialize
super
end

def message
"Please specify gem to add"
end
end

class UnknownGemError < Gel::UserError
def initialize(gem_name:)
super
Expand Down
42 changes: 42 additions & 0 deletions test/command/add_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require "test_helper"

class AddTest < Minitest::Test
# def test_add_without_gem
# Dir.mktmpdir do |dir|
# dir = File.realpath(dir)
# gemfile = "#{dir}/Gemfile"
# File.write(gemfile, sample_gemfile)

# Dir.chdir(dir) do
# error = capture_stderr {
# Gel::Command.run(["add"])
# }
# assert_equal "ERROR: Please specify gem to add", error.chomp!
# end
# end
# end

# def test_add_a_gem
# Dir.mktmpdir do |dir|
# dir = File.realpath(dir)
# gemfile = "#{dir}/Gemfile"
# File.write(gemfile, sample_gemfile)

# Dir.chdir(dir) do
# Gel::Command.run(["add", "rack"])
# end

# assert IO.read(gemfile).include? %(gem "rack"\n)
# end
# end

# def sample_gemfile
# <<~GEMFILE
# source "https://rubygems.org"

# gemspec
# GEMFILE
# end
end
10 changes: 10 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ def capture_stdout
$stdout = original_stdout
end

def capture_stderr
original_stderr = $stderr
$stderr = StringIO.new
yield
rescue SystemExit
$stderr.string
ensure
$stderr = original_stderr
end

def assert_nothing_raised
yield
end