Skip to content

Commit 6a3c67c

Browse files
authored
Use package name as basename for Thor classes (#223)
1 parent 82ff517 commit 6a3c67c

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-19
lines changed

lib/cpflow.rb

+1-19
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,7 @@
3131
end
3232
end
3333

34-
# Fix for https://github.com/erikhuda/thor/issues/398
35-
# Copied from https://github.com/rails/thor/issues/398#issuecomment-622988390
36-
class Thor
37-
module Shell
38-
class Basic
39-
def print_wrapped(message, options = {})
40-
indent = (options[:indent] || 0).to_i
41-
if indent.zero?
42-
stdout.puts(message)
43-
else
44-
message.each_line do |message_line|
45-
stdout.print(" " * indent)
46-
stdout.puts(message_line.chomp)
47-
end
48-
end
49-
end
50-
end
51-
end
52-
end
34+
require_relative "patches/thor"
5335

5436
module Cpflow
5537
class Error < StandardError; end

lib/patches/thor.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
class Thor
4+
# Fix for https://github.com/erikhuda/thor/issues/398
5+
# Copied from https://github.com/rails/thor/issues/398#issuecomment-622988390
6+
module Shell
7+
class Basic
8+
def print_wrapped(message, options = {})
9+
indent = (options[:indent] || 0).to_i
10+
if indent.zero?
11+
stdout.puts(message)
12+
else
13+
message.each_line do |message_line|
14+
stdout.print(" " * indent)
15+
stdout.puts(message_line.chomp)
16+
end
17+
end
18+
end
19+
end
20+
end
21+
22+
# Fix for https://github.com/rails/thor/issues/742
23+
def self.basename
24+
@package_name || super
25+
end
26+
end

spec/patches/thor_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require "thor"
4+
require "patches/thor"
5+
6+
describe Thor do
7+
describe ".basename" do
8+
subject(:basename) { klass.send(:basename) }
9+
10+
context "when class has defined package name" do
11+
let(:klass) do
12+
Class.new(described_class) do
13+
package_name "test_package_name"
14+
end
15+
end
16+
17+
it "returns package name" do
18+
expect(basename).to eq("test_package_name")
19+
end
20+
end
21+
22+
context "when class doesn't have defined package name" do
23+
let(:klass) { Class.new(described_class) }
24+
25+
it "returns basename of program invoking the class" do
26+
expect(basename).to eq("rspec")
27+
end
28+
end
29+
end
30+
end

0 commit comments

Comments
 (0)