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

Use package name as basename for Thor classes #223

Merged
merged 1 commit into from
Aug 20, 2024
Merged
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
20 changes: 1 addition & 19 deletions lib/cpflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,7 @@
end
end

# Fix for https://github.com/erikhuda/thor/issues/398
# Copied from https://github.com/rails/thor/issues/398#issuecomment-622988390
class Thor
module Shell
class Basic
def print_wrapped(message, options = {})
indent = (options[:indent] || 0).to_i
if indent.zero?
stdout.puts(message)
else
message.each_line do |message_line|
stdout.print(" " * indent)
stdout.puts(message_line.chomp)
end
end
end
end
end
end
require_relative "patches/thor"

module Cpflow
class Error < StandardError; end
Expand Down
26 changes: 26 additions & 0 deletions lib/patches/thor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class Thor
# Fix for https://github.com/erikhuda/thor/issues/398
# Copied from https://github.com/rails/thor/issues/398#issuecomment-622988390
module Shell
class Basic
def print_wrapped(message, options = {})
indent = (options[:indent] || 0).to_i
if indent.zero?
stdout.puts(message)
else
message.each_line do |message_line|
stdout.print(" " * indent)
stdout.puts(message_line.chomp)
end
end
end
end
end

# Fix for https://github.com/rails/thor/issues/742
def self.basename
@package_name || super
end
end
30 changes: 30 additions & 0 deletions spec/patches/thor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require "thor"
require "patches/thor"

describe Thor do
describe ".basename" do
subject(:basename) { klass.send(:basename) }

context "when class has defined package name" do
let(:klass) do
Class.new(described_class) do
package_name "test_package_name"
end
end

it "returns package name" do
expect(basename).to eq("test_package_name")
end
end

context "when class doesn't have defined package name" do
let(:klass) { Class.new(described_class) }

it "returns basename of program invoking the class" do
expect(basename).to eq("rspec")
end
end
end
end
Loading