Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.

Commit

Permalink
Cli#add comments-out new lines when the majority of lines is commented
Browse files Browse the repository at this point in the history
  • Loading branch information
Narnach committed Sep 16, 2009
1 parent aa4a94e commit 1c877f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/blocklist/cli.rb
Expand Up @@ -32,7 +32,10 @@ def add
block = Blocklist::Block.new(block_name)
@bl.blocks << block
end


commented_lines = block.lines.inject(0) {|sum, line| line.commented ? sum + 1 : sum}
uncommented_lines = block.lines.size - commented_lines
comment_new_lines = commented_lines > uncommented_lines

domains = block.lines.map {|line| line.domains}.flatten
saved_domains = @argv.map do |domain|
Expand All @@ -45,7 +48,9 @@ def add
subdomain = dom_no_tld.size == 1 ? nil : dom_no_tld[0...-1].join(".")
new_domains = [nil, 'www', subdomain].uniq.map {|sub| [sub, domain_base, tld].compact.join(".")} - domains
if new_domains.size > 0
block.lines << Blocklist::Line.new('127.0.0.1', *new_domains)
new_line = Blocklist::Line.new('127.0.0.1', *new_domains)
new_line.commented = comment_new_lines
block.lines << new_line
domains.concat(new_domains)
end
new_domains
Expand Down
14 changes: 14 additions & 0 deletions spec/blocklist/cli_spec.rb
Expand Up @@ -72,5 +72,19 @@ def run(cmd)
127.0.0.1 example.org www.example.org
STR
end


it 'should add a domain commented-out if there are more commented-out domains in the block' do
fake_hosts <<-STR
# localhost
# 127.0.0.1 localhost
STR
run 'add localhost example.org'
File.read('/etc/hosts').should == <<-STR
# localhost
# 127.0.0.1 localhost
# 127.0.0.1 example.org www.example.org
STR
end
end
end

0 comments on commit 1c877f7

Please sign in to comment.