Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FreeBSD pkg addon #1780

Merged
merged 11 commits into from Mar 4, 2020
2 changes: 1 addition & 1 deletion Gemfile.lock
Expand Up @@ -111,7 +111,7 @@ GEM
coderay (~> 1.1.0)
method_source (~> 0.9.0)
public_suffix (3.0.3)
puma (3.12.3)
puma (3.12.4)
pusher-client (0.6.2)
json
websocket (~> 1.0)
Expand Down
1 change: 1 addition & 0 deletions lib/travis/build/addons.rb
Expand Up @@ -22,6 +22,7 @@
require 'travis/build/addons/sonarqube'
require 'travis/build/addons/browserstack'
require 'travis/build/addons/srcclr'
require 'travis/build/addons/pkg'

module Travis
module Build
Expand Down
69 changes: 69 additions & 0 deletions lib/travis/build/addons/pkg.rb
@@ -0,0 +1,69 @@
require 'travis/build/addons/base'
require 'shellwords'

module Travis
module Build
class Addons
class Pkg < Base
SUPPORTED_OPERATING_SYSTEMS = %w[
freebsd
].freeze

def before_prepare?
SUPPORTED_OPERATING_SYSTEMS.any? do |os_match|
data[:config][:os].to_s == os_match
end
end

def before_prepare
return if config_pkg.empty?
sh.newline
sh.fold('pkg') do
install_pkg
end
sh.newline
end

def before_configure?
config
end

def before_configure
sh.echo "Configuring default pkg options", ansi: :yellow
tmp_dest = "${TRAVIS_TMPDIR}/99-travis-pkg-conf"
sh.file tmp_dest, <<~PKG_CONF
ASSUME_ALWAYS_YES=YES
FETCH_RETRY=5
FETCH_TIMEOUT=30
PKG_CONF
sh.cmd %Q{su -m root -c "mv #{tmp_dest} ${TRAVIS_ROOT}/usr/local/etc/pkg.conf"}
if config[:branch] && config[:branch].to_s.downcase != 'quarterly'
sed_find = 'pkg+http://pkg.FreeBSD.org/\([^/]*\)/quarterly'
sed_replace = 'pkg+http://pkg.FreeBSD.org/\1/' + config[:branch]
sed_cmd = %Q{sed -i'' -e 's,#{sed_find},#{sed_replace},' /etc/pkg/FreeBSD.conf}
sh.cmd %Q{su -m root -c "#{sed_cmd}"}
end
end

def config
@config ||= Hash(super)
end

def install_pkg
sh.echo "Installing #{config_pkg.count} packages", ansi: :yellow

packages = config_pkg.map{|v| Shellwords.escape(v)}.join(' ')
sh.cmd "su -m root -c 'pkg install #{packages}'", echo: true, timing: true, assert: true
end

def config_pkg
@config_pkg ||= Array(config[:packages]).flatten.compact
rescue TypeError => e
if e.message =~ /no implicit conversion of Symbol into Integer/
raise Travis::Build::PkgConfigError.new
end
end
end
end
end
end
10 changes: 10 additions & 0 deletions lib/travis/build/errors.rb
Expand Up @@ -72,6 +72,16 @@ def doc_path
end
end

class PkgConfigError < CompilationError
def initialize(msg = "\\`pkg\\` should be a list.")
super
end

def doc_path
'/user/installing-dependencies'
end
end

class GithubAppsTokenFetchError < CompilationError
def initialize(msg = "Unable to fetch GitHub Apps Token. GitHub may be unavailable. " \
"Check https://githubstatus.com. If GitHub is available, restart may resolve the " \
Expand Down
40 changes: 40 additions & 0 deletions spec/build/addons/pkg_spec.rb
@@ -0,0 +1,40 @@
require "spec_helper"

describe Travis::Build::Addons::Pkg, :sexp do
let(:script) { stub('script') }
#let(:pkg_config) { ['travis', { name: 'aws-cli', no_deps: true }] }
let(:pkg_config) { {} }
let(:data) { payload_for(:push, :ruby, config: { os: 'freebsd', addons: { pkg: pkg_config } }) }
let(:sh) { Travis::Shell::Builder.new }
let(:addon) { described_class.new(script, sh, Travis::Build::Data.new(data), pkg_config) }
subject { sh.to_sexp }
before { addon.before_prepare }

context 'when on linux' do
let(:data) { payload_for(:push, :ruby, config: { os: 'linux' }) }

it 'will not run' do
expect(addon.before_prepare?).to eql false
end
end

context 'when on freebsd' do
let(:data) { payload_for(:push, :ruby, config: { os: 'freebsd' }) }

it 'will run' do
expect(addon.before_prepare?).to eql true
end
end

context 'with multiple packages' do
let(:pkg_config) { { packages: ['git', 'curl'] } }

it { should include_sexp [:cmd, "su -m root -c 'pkg install git curl'", echo: true, timing: true, assert: true] }
end

context 'with single packages' do
let(:pkg_config) { { packages: ['git'] } }

it { should include_sexp [:cmd, "su -m root -c 'pkg install git'", echo: true, timing: true, assert: true] }
end
end