From 6a3c67c8feb6b04e49d21749e2b6faed5bd398db Mon Sep 17 00:00:00 2001 From: Zakir Dzhamaliddinov Date: Tue, 20 Aug 2024 10:04:42 +0300 Subject: [PATCH] Use package name as basename for Thor classes (#223) --- lib/cpflow.rb | 20 +------------------- lib/patches/thor.rb | 26 ++++++++++++++++++++++++++ spec/patches/thor_spec.rb | 30 ++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 lib/patches/thor.rb create mode 100644 spec/patches/thor_spec.rb diff --git a/lib/cpflow.rb b/lib/cpflow.rb index be0f94f4..5ff095b3 100644 --- a/lib/cpflow.rb +++ b/lib/cpflow.rb @@ -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 diff --git a/lib/patches/thor.rb b/lib/patches/thor.rb new file mode 100644 index 00000000..dd6c3cda --- /dev/null +++ b/lib/patches/thor.rb @@ -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 diff --git a/spec/patches/thor_spec.rb b/spec/patches/thor_spec.rb new file mode 100644 index 00000000..0bd22ce4 --- /dev/null +++ b/spec/patches/thor_spec.rb @@ -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