Skip to content
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
21 changes: 1 addition & 20 deletions spec/integration/hanami/cli/commands/app/generate/part_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
RSpec.describe "Hanami generate part integration", :app do
let(:fs) { Hanami::CLI::Files.new(memory: false, out: out) }
let(:out) { StringIO.new }
let(:err) { StringIO.new }

subject(:command) do
Hanami::CLI::Commands::App::Generate::Part.new(fs: fs, out: out, err: err)
Hanami::CLI::Commands::App::Generate::Part.new(fs: fs, out: out)
end

def error_output = err.string.chomp

around do |example|
Dir.mktmpdir do |dir|
original_dir = Dir.pwd
Expand Down Expand Up @@ -65,20 +62,4 @@ class Routes < Hanami::Routes
expect(fs.exist?("app/views/parts/book.rb")).to be(true)
end
end

context "error handling" do
let(:file_path) { "app/views/parts/existing.rb" }

it "handles file conflicts" do
fs.mkdir("app/views/parts")
fs.write(file_path, "# existing content")

expect do
command.call(name: "existing")
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
64 changes: 1 addition & 63 deletions spec/unit/hanami/cli/commands/app/generate/action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
require "ostruct"

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

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 @@ -20,8 +19,6 @@ def output
out.rewind && out.read.chomp
end

def error_output = err.string.chomp

shared_context "with existing files" do
before do
allow(Hanami).to receive(:bundled?).and_call_original
Expand All @@ -40,65 +37,6 @@ def error_output = err.string.chomp
end
end
end

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

before do
within_application_directory do
fs.write(file_path, "")
end
end

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(file_path, "")
end
end

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(file_path, "")
end
end

it "exits with error message" do
expect do
within_application_directory do
generate_action
end
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

context "generate for app, without hanami view bundled" do
Expand Down
73 changes: 1 addition & 72 deletions spec/unit/hanami/cli/commands/app/generate/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
require "ostruct"

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

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 @@ -19,8 +18,6 @@ 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 @@ -40,23 +37,6 @@ class SendWelcomeEmail
expect(fs.read("app/operations/send_welcome_email.rb")).to eq(component)
expect(output).to include("Created app/operations/send_welcome_email.rb")
end

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

before do
fs.write(file_path, "existing content")
end

it "exits with error message" do
expect do
subject.call(name: "operations.send_welcome_email")
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

context "deeply nested" do
Expand All @@ -81,23 +61,6 @@ class SendWelcomeEmail
expect(fs.read("app/operations/user/mailing/send_welcome_email.rb")).to eq(component)
expect(output).to include("Created app/operations/user/mailing/send_welcome_email.rb")
end

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

before do
fs.write(file_path, "existing content")
end

it "exits with error message" do
expect do
subject.call(name: "operations.user.mailing.send_welcome_email")
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
end

Expand All @@ -121,23 +84,6 @@ class WelcomeEmail
expect(fs.read("slices/main/renderers/welcome_email.rb")).to eq(component)
expect(output).to include("Created slices/main/renderers/welcome_email.rb")
end

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

before do
fs.write(file_path, "existing content")
end

it "exits with error message" do
expect do
subject.call(name: "renderers.welcome_email", slice: "main")
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

context "deeply nested" do
Expand All @@ -163,23 +109,6 @@ class WelcomeEmail
expect(fs.read("slices/main/renderers/user/mailing/welcome_email.rb")).to eq(component)
expect(output).to include("Created slices/main/renderers/user/mailing/welcome_email.rb")
end

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

before do
fs.write(file_path, "existing content")
end

it "exits with error message" do
expect do
subject.call(name: "renderers.user.mailing.welcome_email", slice: "main")
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

context "with missing slice" do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "hanami"

RSpec.describe "file conflict handling", :app do
subject(:cmd) { Hanami::CLI::Commands::App::Generate::View.new(fs:, out:, err:) }

let(:out) { StringIO.new }
let(:err) { StringIO.new }
let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) }

def error_output = err.string.chomp

context "when file to be generated already exists" do
it "exits with error" do
subject.call(name: "users.new")

file_path = "app/views/users/new.rb"

expect do
subject.call(name: "users.new")
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
41 changes: 1 addition & 40 deletions spec/unit/hanami/cli/commands/app/generate/migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
require "hanami"

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

let(:fs) { Hanami::CLI::Files.new(memory: true, out: out) }
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 @@ -65,23 +62,6 @@ def error_output = err.string.chomp
"Name must contain only letters, numbers, and underscores."
))
end

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

before do
fs.write(file_path, "existing content")
end

it "exits with error message" do
expect do
subject.call(name: "create_posts")
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

context "generating for a slice" do
Expand All @@ -94,24 +74,5 @@ def error_output = err.string.chomp
expect(fs.read("slices/main/config/db/migrate/20240713140600_create_posts.rb")).to eq migration_file_contents
expect(output).to eq("Created slices/main/config/db/migrate/20240713140600_create_posts.rb")
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(file_path, "existing content")
end

it "exits with error message" do
expect do
subject.call(name: "create_posts", slice: "main")
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
end
end
Loading