Skip to content

Commit

Permalink
Fix uncomment_lines method to preserve indentation correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorianer committed Feb 15, 2024
1 parent f1ba900 commit 7cb2b07
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
7 changes: 3 additions & 4 deletions lib/thor/actions/file_manipulation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>:: path of the file to be changed
Expand All @@ -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
Expand Down
22 changes: 14 additions & 8 deletions spec/actions/file_manipulation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/doc/COMMENTER
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ orange
purple
ind#igo
# ind#igo
# spaces_between
__end__

0 comments on commit 7cb2b07

Please sign in to comment.