Skip to content

Commit

Permalink
Merge pull request #28 from ninoseki/improve-custom-list-error-handling
Browse files Browse the repository at this point in the history
refactor: improve custom-list error handling
  • Loading branch information
ninoseki committed Feb 21, 2020
2 parents 7c63a5c + 16573af commit 9d8c6b0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
9 changes: 5 additions & 4 deletions lib/rogue_one/detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def inspect
end

def domains
@domains ||= custom_domains || top_100_domains
@domains ||= custom_list ? custom_domains : top_100_domains
end

def custom_domains
Expand All @@ -105,14 +105,15 @@ def top_100_domains
read_domains File.expand_path("./data/alexa_100.yml", __dir__)
when "fortune"
read_domains File.expand_path("./data/fortune_100.yml", __dir__)
else
raise ArgumentError, "A list for #{default_list} is not existing"
end
end

def read_domains(path)
list = DomainList.new(path)
list.valid? ? list.domains : nil
return list.domains if list.valid?

raise ArgumentError, "Inputted an invalid list. #{path} is not eixst." unless list.exists?
raise ArgumentError, "Inputted an invalid list. Please input a list as an YAML file." unless list.valid_format?
end

def normal_resolver
Expand Down
2 changes: 0 additions & 2 deletions lib/rogue_one/domain_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ def domains
@domains ||= exists? ? YAML.safe_load(File.read(path)) : nil
end

private

def exists?
File.exist?(path)
end
Expand Down
8 changes: 8 additions & 0 deletions spec/detector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,13 @@
expect(report.dig(:results)).to be_a(Hash)
end
end

context "when given an invalid --custom-list" do
subject { described_class.new(target: "1.1.1.1", custom_list: "/tmp/foobar") }

it do
expect { subject.report }.to raise_error(ArgumentError)
end
end
end
end

0 comments on commit 9d8c6b0

Please sign in to comment.