Skip to content

Commit 126667c

Browse files
committed
Add missing specs for Scanner#initialize
Adds comprehensive specs for Scanner#initialize covering: - Default root (Dir.pwd) when no arguments given - Setting #root to the expanded directory path - Setting #database to a Database instance - Setting #lockfile by parsing Gemfile.lock - Raising Bundler::GemfileLockNotFound when lock file is missing - Error message includes lock file name - Custom gemfile_lock name (valid and invalid) - Custom database parameter - Loading .bundler-audit.yml config when present - Absolute config_dot_file path - Relative config_dot_file path - Default empty Configuration when no config file exists Closes #326
1 parent f8b06eb commit 126667c

1 file changed

Lines changed: 125 additions & 0 deletions

File tree

spec/scanner_spec.rb

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,131 @@
77

88
subject { described_class.new(directory) }
99

10+
describe "#initialize" do
11+
context "when given no arguments" do
12+
subject { described_class }
13+
14+
context "when a Gemfile.lock exists in Dir.pwd" do
15+
it "must default root to Dir.pwd" do
16+
Dir.chdir(directory) do
17+
scanner = subject.new
18+
expect(scanner.root).to eq(File.expand_path(Dir.pwd))
19+
end
20+
end
21+
end
22+
end
23+
24+
context "when given a root directory" do
25+
let(:root) { directory }
26+
27+
subject { described_class.new(root) }
28+
29+
it "must set #root to the expanded directory path" do
30+
expect(subject.root).to eq(File.expand_path(root))
31+
end
32+
33+
it "must set #database" do
34+
expect(subject.database).to be_kind_of(Database)
35+
end
36+
37+
it "must set #lockfile by parsing the Gemfile.lock" do
38+
expect(subject.lockfile).to be_kind_of(Bundler::LockfileParser)
39+
end
40+
41+
it "must set #config to a default Configuration when no config file exists" do
42+
expect(subject.config).to be_kind_of(Configuration)
43+
expect(subject.config.ignore).to be_empty
44+
end
45+
end
46+
47+
context "when the Gemfile.lock does not exist in the root directory" do
48+
let(:bad_dir) { File.join('spec','bundle','nonexistent') }
49+
50+
it "must raise Bundler::GemfileLockNotFound" do
51+
expect {
52+
described_class.new(bad_dir)
53+
}.to raise_error(Bundler::GemfileLockNotFound)
54+
end
55+
56+
it "must include the lock file name and root in the error message" do
57+
expect {
58+
described_class.new(bad_dir)
59+
}.to raise_error(Bundler::GemfileLockNotFound, /Gemfile\.lock/)
60+
end
61+
end
62+
63+
context "when given a custom gemfile_lock name" do
64+
it "must raise Bundler::GemfileLockNotFound if the custom lock file does not exist" do
65+
expect {
66+
described_class.new(directory, 'NoSuchLockFile.lock')
67+
}.to raise_error(Bundler::GemfileLockNotFound)
68+
end
69+
70+
it "must use the custom gemfile_lock name" do
71+
scanner = described_class.new(directory, 'Gemfile.lock')
72+
expect(scanner.lockfile).to be_kind_of(Bundler::LockfileParser)
73+
end
74+
end
75+
76+
context "when given a custom database" do
77+
let(:custom_db) { Database.new }
78+
79+
subject { described_class.new(directory, 'Gemfile.lock', custom_db) }
80+
81+
it "must set #database to the custom database" do
82+
expect(subject.database).to be(custom_db)
83+
end
84+
end
85+
86+
context "when a .bundler-audit.yml config file exists" do
87+
let(:bundle) { 'unpatched_gems_with_dot_configuration' }
88+
89+
subject { described_class.new(directory) }
90+
91+
it "must load the configuration from the config file" do
92+
expect(subject.config).to be_kind_of(Configuration)
93+
expect(subject.config.ignore).to include('OSVDB-89025')
94+
end
95+
end
96+
97+
context "when given a custom config_dot_file" do
98+
let(:config_path) { File.join('spec','bundle','unpatched_gems_with_dot_configuration','.bundler-audit.yml') }
99+
100+
context "when the config_dot_file is an absolute path" do
101+
let(:absolute_config_path) { File.absolute_path(config_path) }
102+
103+
subject { described_class.new(directory, 'Gemfile.lock', Database.new, absolute_config_path) }
104+
105+
it "must load the configuration from the absolute path" do
106+
expect(subject.config).to be_kind_of(Configuration)
107+
expect(subject.config.ignore).to include('OSVDB-89025')
108+
end
109+
end
110+
111+
context "when the config_dot_file is a relative path" do
112+
let(:relative_config_path) { File.join('..','unpatched_gems_with_dot_configuration','.bundler-audit.yml') }
113+
114+
subject { described_class.new(directory, 'Gemfile.lock', Database.new, relative_config_path) }
115+
116+
it "must load the configuration from the relative path" do
117+
expect(subject.config).to be_kind_of(Configuration)
118+
expect(subject.config.ignore).to include('OSVDB-89025')
119+
end
120+
end
121+
end
122+
123+
context "when no .bundler-audit.yml config file exists" do
124+
let(:bundle) { 'secure' }
125+
126+
subject { described_class.new(directory) }
127+
128+
it "must set #config to a default empty Configuration" do
129+
expect(subject.config).to be_kind_of(Configuration)
130+
expect(subject.config.ignore).to be_empty
131+
end
132+
end
133+
end
134+
10135
describe "#scan" do
11136
it "should yield results" do
12137
results = []

0 commit comments

Comments
 (0)