Skip to content

Commit

Permalink
Use package name as basename for Thor classes (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzaakiirr authored Aug 20, 2024
1 parent 82ff517 commit 6a3c67c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
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

0 comments on commit 6a3c67c

Please sign in to comment.