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

Exit code 1 when file already exists #279

Open
wants to merge 2 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
3 changes: 3 additions & 0 deletions lib/hanami/cli/commands/app/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def call(*args, **opts)
Hanami::Env.load

super
rescue FileAlreadyExistsError => error
err.puts(error.message)
exit(1)
end
end

Expand Down
8 changes: 6 additions & 2 deletions lib/hanami/cli/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ def initialize(path)

# @api public
class FileAlreadyExistsError < Error
def initialize(path)
super("Cannot overwrite existing file: `#{path}`")
ERROR_MESSAGE = <<~ERROR.chomp
The file `%{file_path}` could not be generated because it already exists in your application.
ERROR

def initialize(file_path)
super(ERROR_MESSAGE % {file_path:})
end
end

Expand Down
56 changes: 34 additions & 22 deletions spec/unit/hanami/cli/commands/app/generate/action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
require "ostruct"

RSpec.describe Hanami::CLI::Commands::App::Generate::Action, :app do
subject { described_class.new(fs: fs, inflector: inflector, generator: generator) }
subject { described_class.new(fs: fs, inflector: inflector, generator: generator, err: err) }

let(:out) { StringIO.new }
let(:err) { StringIO.new }
let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) }
let(:inflector) { Dry::Inflector.new }
let(:generator) { Hanami::CLI::Generators::App::Action.new(fs: fs, inflector: inflector) }
Expand All @@ -20,6 +21,8 @@ def output
out.rewind && out.read.chomp
end

def error_output = err.string.chomp

shared_context "with existing files" do
context "with existing route file" do
it "generates action without error" do
Expand All @@ -35,50 +38,59 @@ def output
end

context "with existing action file" do
let(:file_path) { "app/actions/#{controller}/#{action}.rb" }

before do
within_application_directory do
fs.write("app/actions/#{controller}/#{action}.rb", "")
fs.write(file_path, "")
end
end

it "raises error" do
expect {
within_application_directory do
generate_action
end
}.to raise_error(Hanami::CLI::FileAlreadyExistsError, "Cannot overwrite existing file: `app/actions/#{controller}/#{action}.rb`")
it "exits with error message" do
expect do
within_application_directory { generate_action }
end.to raise_error SystemExit do |exception|
expect(exception.status).to eq 1
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
end
end
end

context "with existing view file" do
let(:file_path) { "app/views/#{controller}/#{action}.rb" }

before do
within_application_directory do
fs.write("app/views/#{controller}/#{action}.rb", "")
fs.write(file_path, "")
end
end

it "raises error" do
expect {
within_application_directory do
generate_action
end
}.to raise_error(Hanami::CLI::FileAlreadyExistsError, "Cannot overwrite existing file: `app/views/#{controller}/#{action}.rb`")
it "exits with error message" do
expect do
within_application_directory { generate_action }
end.to raise_error SystemExit do |exception|
expect(exception.status).to eq 1
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
end
end
end

context "with existing template file" do
let(:file_path) { "app/templates/#{controller}/#{action}.html.erb" }

before do
within_application_directory do
fs.write("app/templates/#{controller}/#{action}.html.erb", "")
fs.write(file_path, "")
end
end

it "raises error" do
expect {
within_application_directory do
generate_action
end
}.to raise_error(Hanami::CLI::FileAlreadyExistsError, "Cannot overwrite existing file: `app/templates/#{controller}/#{action}.html.erb`")
it "exits with error message" do
expect do
within_application_directory { generate_action }
end.to raise_error SystemExit do |exception|
expect(exception.status).to eq 1
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
end
end
end
end
Expand Down
57 changes: 40 additions & 17 deletions spec/unit/hanami/cli/commands/app/generate/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
require "ostruct"

RSpec.describe Hanami::CLI::Commands::App::Generate::Component, :app do
subject { described_class.new(fs: fs, inflector: inflector, out: out) }
subject { described_class.new(fs: fs, inflector: inflector, out: out, err: err) }

let(:out) { StringIO.new }
let(:err) { StringIO.new }
let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) }
let(:inflector) { Dry::Inflector.new }
let(:app) { Hanami.app.namespace }
Expand All @@ -18,6 +19,8 @@ def output
out.rewind && out.read.chomp
end

def error_output = err.string.chomp

context "generating for app" do
context "shallowly nested" do
it "generates the component" do
Expand All @@ -39,14 +42,19 @@ class SendWelcomeEmail
end

context "with existing file" do
let(:file_path) { "app/operations/send_welcome_email.rb" }

before do
fs.write("app/operations/send_welcome_email.rb", "existing content")
fs.write(file_path, "existing content")
end

it "raises error" do
expect {
it "exits with error message" do
expect do
subject.call(name: "operations.send_welcome_email")
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
end.to raise_error SystemExit do |exception|
expect(exception.status).to eq 1
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
end
end
end
end
Expand Down Expand Up @@ -75,14 +83,19 @@ class SendWelcomeEmail
end

context "with existing file" do
let(:file_path) { "app/operations/user/mailing/send_welcome_email.rb" }

before do
fs.write("app/operations/user/mailing/send_welcome_email.rb", "existing content")
fs.write(file_path, "existing content")
end

it "raises error" do
expect {
it "exits with error message" do
expect do
subject.call(name: "operations.user.mailing.send_welcome_email")
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
end.to raise_error SystemExit do |exception|
expect(exception.status).to eq 1
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
end
end
end
end
Expand Down Expand Up @@ -110,14 +123,19 @@ class WelcomeEmail
end

context "with existing file" do
let(:file_path) { "slices/main/renderers/welcome_email.rb" }

before do
fs.write("slices/main/renderers/welcome_email.rb", "existing content")
fs.write(file_path, "existing content")
end

it "raises error" do
expect {
it "exits with error message" do
expect do
subject.call(name: "renderers.welcome_email", slice: "main")
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
end.to raise_error SystemExit do |exception|
expect(exception.status).to eq 1
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
end
end
end
end
Expand Down Expand Up @@ -147,14 +165,19 @@ class WelcomeEmail
end

context "with existing file" do
let(:file_path) { "slices/main/renderers/user/mailing/welcome_email.rb" }

before do
fs.write("slices/main/renderers/user/mailing/welcome_email.rb", "existing content")
fs.write(file_path, "existing content")
end

it "raises error" do
expect {
it "exits with error message" do
expect do
subject.call(name: "renderers.user.mailing.welcome_email", slice: "main")
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
end.to raise_error SystemExit do |exception|
expect(exception.status).to eq 1
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
end
end
end
end
Expand Down
31 changes: 22 additions & 9 deletions spec/unit/hanami/cli/commands/app/generate/migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
require "hanami"

RSpec.describe Hanami::CLI::Commands::App::Generate::Migration, :app do
subject { described_class.new(fs: fs, inflector: inflector) }
subject { described_class.new(fs: fs, inflector: inflector, err: err) }

let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) }
let(:inflector) { Dry::Inflector.new }

let(:out) { StringIO.new }
let(:err) { StringIO.new }

def output
out.string.strip
end

def error_output = err.string.chomp

let(:app) { Hanami.app.namespace }

let(:migration_file_contents) {
Expand Down Expand Up @@ -66,14 +69,19 @@ def output
end

context "with existing file" do
let(:file_path) { "config/db/migrate/20240713140600_create_posts.rb" }

before do
fs.write("config/db/migrate/20240713140600_create_posts.rb", "existing content")
fs.write(file_path, "existing content")
end

it "raises error" do
expect {
it "exits with error message" do
expect do
subject.call(name: "create_posts")
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
end.to raise_error SystemExit do |exception|
expect(exception.status).to eq 1
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
end
end
end
end
Expand All @@ -90,15 +98,20 @@ def output
end

context "with existing file" do
let(:file_path) { "slices/main/config/db/migrate/20240713140600_create_posts.rb" }

context "with existing file" do
before do
fs.write("slices/main/config/db/migrate/20240713140600_create_posts.rb", "existing content")
fs.write(file_path, "existing content")
end

it "raises error" do
expect {
it "exits with error message" do
expect do
subject.call(name: "create_posts", slice: "main")
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
end.to raise_error SystemExit do |exception|
expect(exception.status).to eq 1
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
end
end
end
end
Expand Down
31 changes: 22 additions & 9 deletions spec/unit/hanami/cli/commands/app/generate/operation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

RSpec.describe Hanami::CLI::Commands::App::Generate::Operation, :app do
subject { described_class.new(fs: fs, inflector: inflector, out: out) }
subject { described_class.new(fs: fs, inflector: inflector, out: out, err: err) }

let(:out) { StringIO.new }
let(:err) { StringIO.new }
let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) }
let(:inflector) { Dry::Inflector.new }
let(:app) { Hanami.app.namespace }
Expand All @@ -12,6 +13,8 @@ def output
out.string.chomp
end

def error_output = err.string.chomp

context "generating for app" do
it "generates an operation without a namespace, with a recommendation" do
subject.call(name: "add_book")
Expand Down Expand Up @@ -80,14 +83,19 @@ def call
end

context "with existing file" do
let(:file_path) { "app/admin/books/add.rb" }

before do
fs.write("app/admin/books/add.rb", "existing content")
fs.write(file_path, "existing content")
end

it "raises error" do
expect {
it "exits with error message" do
expect do
subject.call(name: "admin.books.add")
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
end.to raise_error SystemExit do |exception|
expect(exception.status).to eq 1
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
end
end
end
end
Expand Down Expand Up @@ -140,15 +148,20 @@ def call
end

context "with existing file" do
let(:file_path) { "slices/main/admin/books/add.rb" }

before do
fs.mkdir("slices/main")
fs.write("slices/main/admin/books/add.rb", "existing content")
fs.write(file_path, "existing content")
end

it "raises error" do
expect {
it "exits with error message" do
expect do
subject.call(name: "admin.books.add", slice: "main")
}.to raise_error(Hanami::CLI::FileAlreadyExistsError)
end.to raise_error SystemExit do |exception|
expect(exception.status).to eq 1
expect(error_output).to eq Hanami::CLI::FileAlreadyExistsError::ERROR_MESSAGE % {file_path:}
end
end
end
end
Expand Down
Loading