Skip to content
代码片段 群组 项目
提交 6f5aed4c 编辑于 作者: Gabriel Mazetto's avatar Gabriel Mazetto
浏览文件

Add Dependencies to Gitlab::Backup::Cli

上级 f4d572db
No related branches found
No related tags found
无相关合并请求
......@@ -7,6 +7,7 @@ module Cli
autoload :VERSION, 'gitlab/backup/cli/version'
autoload :Runner, 'gitlab/backup/cli/runner'
autoload :Utils, 'gitlab/backup/cli/utils'
autoload :Dependencies, 'gitlab/backup/cli/dependencies'
Error = Class.new(StandardError)
# Your code goes here...
......
# frozen_string_literal: true
module Gitlab
module Backup
module Cli
module Dependencies
# Search on PATH or default locations for provided binary and return its fullpath
#
# @param [String] binary name
# @return [String|False] full path to the binary file
def self.find_executable(binary)
executable_file = proc { |name| next name if File.file?(name) && File.executable?(name) }
# Retrieve PATH from ENV or use a fallback
path = ENV['PATH']&.split(File::PATH_SEPARATOR) || %w[/usr/local/bin /usr/bin /bin]
# check binary against each PATH
path.each do |dir|
file = File.expand_path(binary, dir)
return file if executable_file.call(file)
end
nil
end
# Check whether provided binary name exists on PATH or default locations
#
# @param [String] binary name
# @return [Boolean] whether binary exists
def self.executable_exist?(name)
!!find_executable(name)
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Backup::Cli::Dependencies do
let(:bin_path) { Dir.mktmpdir('dependencies', temp_path) }
before do
stub_env('PATH', bin_path)
end
after do
FileUtils.rmtree(bin_path)
end
describe '.find_executable' do
it 'returns the full path of the executable' do
executable = create_dummy_executable('dummy')
expect(described_class.find_executable('dummy')).to eq(executable)
end
it 'returns nil when executable cant be found' do
expect(described_class.find_executable('non-existent')).to be_nil
end
it 'also finds by absolute path' do
executable = create_dummy_executable('dummy')
expect(described_class.find_executable(executable)).to eq(executable)
end
end
describe '.executable_exist?' do
it 'returns true if an executable exists in the PATH' do
create_dummy_executable('dummy')
expect(described_class.executable_exist?('dummy')).to be_truthy
end
it 'returns false when no exectuable can be found' do
expect(described_class.executable_exist?('non-existent')).to be_falsey
end
end
def create_dummy_executable(name)
filepath = File.join(bin_path, name)
FileUtils.touch(filepath)
File.chmod(0o755, filepath)
filepath
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Backup::Cli do
it "has a version number" do
expect(Gitlab::Backup::Cli::VERSION).not_to be nil
......
# frozen_string_literal: true
require "gitlab/backup/cli"
require 'tmpdir'
require 'fileutils'
# Load spec support code
Dir['spec/support/**/*.rb'].each { |f| load f }
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
......
# frozen_string_literal: true
def spec_path
Pathname.new(__dir__).join('..').expand_path
end
def temp_path
spec_path.join('..', 'tmp').expand_path
end
def stub_env(var, return_value)
stub_const('ENV', ENV.to_hash.merge(var => return_value))
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册