From 7cb2b07dc2ef57357f4a207b442d0ab45529e824 Mon Sep 17 00:00:00 2001 From: Viktor Schmidt Date: Thu, 15 Feb 2024 11:59:19 +0100 Subject: [PATCH] Fix uncomment_lines method to preserve indentation correctly --- lib/thor/actions/file_manipulation.rb | 7 +++---- spec/actions/file_manipulation_spec.rb | 22 ++++++++++++++-------- spec/fixtures/doc/COMMENTER | 1 + 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/thor/actions/file_manipulation.rb b/lib/thor/actions/file_manipulation.rb index e4aa8e890..78ae8d975 100644 --- a/lib/thor/actions/file_manipulation.rb +++ b/lib/thor/actions/file_manipulation.rb @@ -275,9 +275,8 @@ def gsub_file(path, flag, *args, &block) end end - # Uncomment all lines matching a given regex. It will leave the space - # which existed before the comment hash in tact but will remove any spacing - # between the comment hash and the beginning of the line. + # Uncomment all lines matching a given regex. Preserves indentation before + # the comment hash and removes the hash and any immediate following space. # # ==== Parameters # path:: path of the file to be changed @@ -291,7 +290,7 @@ def gsub_file(path, flag, *args, &block) def uncomment_lines(path, flag, *args) flag = flag.respond_to?(:source) ? flag.source : flag - gsub_file(path, /^(\s*)#[[:blank:]]*(.*#{flag})/, '\1\2', *args) + gsub_file(path, /^(\s*)#[[:blank:]]?(.*#{flag})/, '\1\2', *args) end # Comment all lines matching a given regex. It will leave the space diff --git a/spec/actions/file_manipulation_spec.rb b/spec/actions/file_manipulation_spec.rb index a1b902af3..72919e09b 100644 --- a/spec/actions/file_manipulation_spec.rb +++ b/spec/actions/file_manipulation_spec.rb @@ -475,20 +475,26 @@ def file File.join(destination_root, "doc", "COMMENTER") end - unmodified_comments_file = /__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n purple\n ind#igo\n # ind#igo\n__end__/ + unmodified_comments_file = /__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n purple\n ind#igo\n # ind#igo\n # spaces_between\n__end__/ describe "#uncomment_lines" do it "uncomments all matching lines in the file" do action :uncomment_lines, "doc/COMMENTER", "green" - expect(File.binread(file)).to match(/__start__\n greenblue\n#\n# yellowblue\n#yellowred\n greenred\norange\n purple\n ind#igo\n # ind#igo\n__end__/) + expect(File.binread(file)).to match(/__start__\n greenblue\n#\n# yellowblue\n#yellowred\n greenred\norange\n purple\n ind#igo\n # ind#igo\n # spaces_between\n__end__/) action :uncomment_lines, "doc/COMMENTER", "red" - expect(File.binread(file)).to match(/__start__\n greenblue\n#\n# yellowblue\nyellowred\n greenred\norange\n purple\n ind#igo\n # ind#igo\n__end__/) + expect(File.binread(file)).to match(/__start__\n greenblue\n#\n# yellowblue\nyellowred\n greenred\norange\n purple\n ind#igo\n # ind#igo\n # spaces_between\n__end__/) end it "correctly uncomments lines with hashes in them" do action :uncomment_lines, "doc/COMMENTER", "ind#igo" - expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n purple\n ind#igo\n ind#igo\n__end__/) + expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n purple\n ind#igo\n ind#igo\n # spaces_between\n__end__/) + end + + it "will leave the space which existed before the comment hash in tact" do + action :uncomment_lines, "doc/COMMENTER", "ind#igo" + action :uncomment_lines, "doc/COMMENTER", "spaces_between" + expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n purple\n ind#igo\n ind#igo\n spaces_between\n__end__/) end it "does not modify already uncommented lines in the file" do @@ -499,22 +505,22 @@ def file it "does not uncomment the wrong line when uncommenting lines preceded by blank commented line" do action :uncomment_lines, "doc/COMMENTER", "yellow" - expect(File.binread(file)).to match(/__start__\n # greenblue\n#\nyellowblue\nyellowred\n #greenred\norange\n purple\n ind#igo\n # ind#igo\n__end__/) + expect(File.binread(file)).to match(/__start__\n # greenblue\n#\nyellowblue\nyellowred\n #greenred\norange\n purple\n ind#igo\n # ind#igo\n # spaces_between\n__end__/) end end describe "#comment_lines" do it "comments lines which are not commented" do action :comment_lines, "doc/COMMENTER", "orange" - expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\n# orange\n purple\n ind#igo\n # ind#igo\n__end__/) + expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\n# orange\n purple\n ind#igo\n # ind#igo\n # spaces_between\n__end__/) action :comment_lines, "doc/COMMENTER", "purple" - expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\n# orange\n # purple\n ind#igo\n # ind#igo\n__end__/) + expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\n# orange\n # purple\n ind#igo\n # ind#igo\n # spaces_between\n__end__/) end it "correctly comments lines with hashes in them" do action :comment_lines, "doc/COMMENTER", "ind#igo" - expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n purple\n # ind#igo\n # ind#igo\n__end__/) + expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n purple\n # ind#igo\n # ind#igo\n # spaces_between\n__end__/) end it "does not modify already commented lines" do diff --git a/spec/fixtures/doc/COMMENTER b/spec/fixtures/doc/COMMENTER index 384cb3a50..84e62144a 100644 --- a/spec/fixtures/doc/COMMENTER +++ b/spec/fixtures/doc/COMMENTER @@ -8,4 +8,5 @@ orange purple ind#igo # ind#igo + # spaces_between __end__