-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run bundle-stats command with custom Gemfile path (#30)
- Loading branch information
1 parent
2763c66
commit 3186c5d
Showing
5 changed files
with
160 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module Bundler | ||
module Stats | ||
class FilePathResolver | ||
FILES_MAP = { | ||
"gems.rb" => "gems.locked", | ||
"Gemfile" => "Gemfile.lock" | ||
} | ||
|
||
def initialize(specific_gemfile_path = nil) | ||
@specific_gemfile_path = specific_gemfile_path | ||
end | ||
|
||
def gemfile_path | ||
resolve_file_path(FILES_MAP.keys, specific_gemfile_path) | ||
end | ||
|
||
def lockfile_path | ||
resolve_file_path(FILES_MAP.values, resolve_lockfile_path) | ||
end | ||
|
||
private | ||
|
||
attr_reader :specific_gemfile_path | ||
|
||
def resolve_lockfile_path | ||
return unless specific_gemfile_path | ||
|
||
file_path = Pathname.new(specific_gemfile_path) | ||
file_name = file_path.basename.to_s | ||
locked_file_name = FILES_MAP[file_name] | ||
raise ArgumentError, "Invalid file name: #{file_name}" if locked_file_name.nil? | ||
file_path.dirname.join(locked_file_name).to_s | ||
end | ||
|
||
def resolve_file_path(file_names, custom_path) | ||
if custom_path | ||
raise ArgumentError, "Couldn't find #{custom_path} file path" unless File.exist?(custom_path) | ||
return custom_path | ||
end | ||
|
||
find_file_path(file_names) | ||
end | ||
|
||
def find_file_path(file_names) | ||
cwd = Pathname.new(".") | ||
until cwd.realdirpath.root? do | ||
file_names.each do |file| | ||
return (cwd + file).to_s if File.exist?(cwd + file) | ||
end | ||
cwd = cwd.parent | ||
end | ||
raise ArgumentError, "Couldn't find #{file_names.join(" nor ")} in this directory or parents" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
require 'spec_helper' | ||
|
||
describe Bundler::Stats::FilePathResolver do | ||
let(:file_path_resolver) { described_class.new(specific_gemfile_path) } | ||
|
||
context "with undefined specific gemfile path" do | ||
let(:specific_gemfile_path) { nil } | ||
|
||
before do | ||
allow(File).to receive(:exist?).and_return(false) | ||
end | ||
|
||
context "without files found" do | ||
it do | ||
expect { file_path_resolver.gemfile_path }.to raise_error( | ||
ArgumentError, "Couldn't find gems.rb nor Gemfile in this directory or parents" | ||
) | ||
end | ||
|
||
it do | ||
expect { file_path_resolver.lockfile_path }.to raise_error( | ||
ArgumentError, "Couldn't find gems.locked nor Gemfile.lock in this directory or parents" | ||
) | ||
end | ||
end | ||
|
||
context "with files found" do | ||
before do | ||
allow(File).to receive(:exist?).with(Pathname.new("../Gemfile")).and_return(true) | ||
allow(File).to receive(:exist?).with(Pathname.new("../../gems.locked")).and_return(true) | ||
end | ||
|
||
it { expect(file_path_resolver.gemfile_path).to eq("../Gemfile") } | ||
it { expect(file_path_resolver.lockfile_path).to eq("../../gems.locked") } | ||
end | ||
end | ||
|
||
context "with specific gemfile path" do | ||
let(:specific_gemfile_path) { "some-project/Gemfile" } | ||
|
||
context "with valid file path" do | ||
before do | ||
allow(File).to receive(:exist?).and_return(true) | ||
end | ||
|
||
it { expect(file_path_resolver.gemfile_path).to eq("some-project/Gemfile") } | ||
it { expect(file_path_resolver.lockfile_path).to eq("some-project/Gemfile.lock") } | ||
|
||
context "with gems.rb file" do | ||
let(:specific_gemfile_path) { "some-project/gems.rb" } | ||
|
||
it { expect(file_path_resolver.gemfile_path).to eq("some-project/gems.rb") } | ||
it { expect(file_path_resolver.lockfile_path).to eq("some-project/gems.locked") } | ||
end | ||
|
||
context "with invalid file name" do | ||
let(:specific_gemfile_path) { "some-project/yeimfile" } | ||
|
||
it do | ||
expect { file_path_resolver.lockfile_path }.to raise_error( | ||
ArgumentError, "Invalid file name: yeimfile" | ||
) | ||
end | ||
end | ||
end | ||
|
||
context "with invalid file path" do | ||
before do | ||
allow(File).to receive(:exist?).and_return(false) | ||
end | ||
|
||
it do | ||
expect { file_path_resolver.gemfile_path }.to raise_error( | ||
ArgumentError, "Couldn't find some-project/Gemfile file path" | ||
) | ||
end | ||
|
||
it do | ||
expect { file_path_resolver.lockfile_path }.to raise_error( | ||
ArgumentError, "Couldn't find some-project/Gemfile.lock file path" | ||
) | ||
end | ||
end | ||
end | ||
end |