Skip to content

Commit

Permalink
refactor worker system call to use bash -c and source the rvm script
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Apr 15, 2011
1 parent 294d03e commit 9424f5f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 37 deletions.
35 changes: 19 additions & 16 deletions lib/travis/buildable.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'shellwords'
require 'fileutils'
require 'uri'
require 'travis/buildable/config'
Expand Down Expand Up @@ -42,43 +43,41 @@ def configure!
end

def build!
status = (install? ? install && execute : execute) ? 0 : 1
status = (install? ? install && run_script : run_script) ? 0 : 1
puts "\nDone. Build script exited with: #{status}"
status
end

def checkout
exists? ? fetch : clone
system "git checkout -qf #{commit}" if commit
execute "git checkout -qf #{commit}" if commit
end

def clone
system "cd ..; git clone #{git_url}; cd -"
execute "cd ..; git clone #{git_url}; cd -"
end

def fetch
system 'git clean -fdx'
system 'git fetch'
execute 'git clean -fdx'
execute 'git fetch'
end

def install?
File.exists?('Gemfile')
end

def install
system prepend_env('bundle install')
execute prepend_env('bundle install')
end

def execute
system prepend_env(script)
def run_script
execute prepend_env(script)
end

def prepend_env(command)
if config['rvm']
"rvm #{config['rvm']} exec '#{(env + [command]).join(' ')}'"
else
(env + [command]).join(' ')
end
command = [(env + [command]).join(' ')]
command.unshift("rvm use #{config['rvm']}") if config['rvm']
command
end

def env
Expand Down Expand Up @@ -136,9 +135,13 @@ def extract_path(url)
end
end

def system(command)
puts "$ #{command}"
super("#{command} 2>&1")
def execute(cmd)
system "bash -c 'source ~/.rvm/scripts/rvm\n#{echoize(cmd)}' 2>&1"
end

def echoize(cmd)
cmd = [cmd].flatten.join("\n").split("\n")
cmd.map { |cmd| "echo #{Shellwords.escape("$ #{cmd}")}\n#{cmd}" }.join("\n")
end
end
end
27 changes: 27 additions & 0 deletions play/worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'session'
require 'shellwords'

cmd = <<-cmd
source ~/.rvm/scripts/rvm
rvm use 1.9.2
ruby play/worker_test.rb
cmd

cmd = cmd.strip.split("\n").map do |line|
"echo #{Shellwords.escape("$ #{line}")}\n#{line}"
end.join("\n")

puts "-- using system('bash -c ...')"
result = system("bash -c '#{cmd}'")
puts "\n"
p result

puts "\n-- using bash.execute(...)"
bash = Session::Bash.new
bash.execute(cmd) do |out, err|
STDOUT << out if out
STDOUT << err if err
end

puts "\n"
p bash.exit_status
11 changes: 11 additions & 0 deletions play/worker_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
STDOUT.sync = true;

puts "Using RUBY_VERSION: #{RUBY_VERSION}"

1.upto(100) do
sleep(0.01)
putc '.'
STDOUT.flush
end

exit 0
50 changes: 29 additions & 21 deletions test/functional/travis/buildable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setup
FileUtils.mkdir_p(Buildable.base_dir)
Buildable.any_instance.stubs(:system)
@stdout = $stdout
$stdout = StringIO.new
# $stdout = StringIO.new
end

def teardown
Expand Down Expand Up @@ -51,22 +51,22 @@ def teardown
test "prepend_env: equals the build script if no config is given" do
buildable = Buildable.new
buildable.stubs(:config).returns(config)
assert_equal 'rake ci', buildable.prepend_env('rake ci')
assert_equal ['rake ci'], buildable.prepend_env('rake ci')
end

test "prepend_env: prepends an rvm command if configured" do
buildable = Buildable.new(:config => { 'rvm' => '1.9.2' })
assert_equal %(rvm 1.9.2 exec 'rake ci'), buildable.prepend_env('rake ci')
assert_equal ['rvm use 1.9.2', 'rake ci'], buildable.prepend_env('rake ci')
end

test "prepend_env: prepends an env var if configured" do
buildable = Buildable.new(:config => { 'gemfile' => 'gemfiles/rails-2.3.x' })
assert_equal %(BUNDLE_GEMFILE=gemfiles/rails-2.3.x rake ci), buildable.prepend_env('rake ci')
assert_equal ['BUNDLE_GEMFILE=gemfiles/rails-2.3.x rake ci'], buildable.prepend_env('rake ci')
end

test "prepend_env: prepends both rvm command and env var if configured" do
buildable = Buildable.new(:config => { 'rvm' => '1.9.2', 'gemfile' => 'gemfiles/rails-2.3.x' })
assert_equal %(rvm 1.9.2 exec 'BUNDLE_GEMFILE=gemfiles/rails-2.3.x rake ci'), buildable.prepend_env('rake ci')
assert_equal ['rvm use 1.9.2', 'BUNDLE_GEMFILE=gemfiles/rails-2.3.x rake ci'], buildable.prepend_env('rake ci')
end

test 'build_dir: given a local filesystem url it returns a valid path' do
Expand Down Expand Up @@ -108,49 +108,57 @@ def teardown

test 'install: runs bundle install' do
buildable = Buildable.new(:config => { :not => :blank })
buildable.expects(:system).with('bundle install')
buildable.expects(:execute).with(['bundle install'])
buildable.install
end

test 'install: runs bundle install w/ env vars prepended' do
buildable = Buildable.new(:config => { 'env' => 'FOO=bar' })
buildable.expects(:system).with('FOO=bar bundle install')
buildable.expects(:execute).with(['FOO=bar bundle install'])
buildable.install
end

test 'install: runs bundle install w/ rvm command prepended' do
buildable = Buildable.new(:config => { 'rvm' => '1.8.7' })
buildable.expects(:system).with("rvm 1.8.7 exec 'bundle install'")
buildable.expects(:execute).with(['rvm use 1.8.7', 'bundle install'])
buildable.install
end

test 'install: runs bundle install w/ rvm command and env vars prepended' do
buildable = Buildable.new(:config => { 'script' => 'rake ci', 'rvm' => '1.8.7', 'env' => 'FOO=bar' })
buildable.expects(:system).with("rvm 1.8.7 exec 'FOO=bar bundle install'")
buildable.expects(:execute).with(['rvm use 1.8.7', 'FOO=bar bundle install'])
buildable.install
end

test 'execute: executes the build script' do
test 'run_script: executes the build script' do
buildable = Buildable.new(:config => { 'script' => 'rake ci' })
buildable.expects(:system).with('rake ci')
buildable.execute
buildable.expects(:execute).with(['rake ci'])
buildable.run_script
end

test 'execute: executes the build script w/ env vars prepended' do
test 'run_script: executes the build script w/ env vars prepended' do
buildable = Buildable.new(:config => { 'script' => 'rake ci', 'env' => 'FOO=bar' })
buildable.expects(:system).with('FOO=bar rake ci')
buildable.execute
buildable.expects(:execute).with(['FOO=bar rake ci'])
buildable.run_script
end

test 'execute: executes the build script w/ rvm command prepended' do
test 'run_script: executes the build script w/ rvm command prepended' do
buildable = Buildable.new(:config => { 'script' => 'rake ci', 'rvm' => '1.8.7' })
buildable.expects(:system).with("rvm 1.8.7 exec 'rake ci'")
buildable.execute
buildable.expects(:execute).with(['rvm use 1.8.7', 'rake ci'])
buildable.run_script
end

test 'execute: executes the build script w/ rvm command and env vars prepended' do
test 'run_script: executes the build script w/ rvm command and env vars prepended' do
buildable = Buildable.new(:config => { 'script' => 'rake ci', 'rvm' => '1.8.7', 'env' => 'FOO=bar' })
buildable.expects(:system).with("rvm 1.8.7 exec 'FOO=bar rake ci'")
buildable.execute
buildable.expects(:execute).with(['rvm use 1.8.7', 'FOO=bar rake ci'])
buildable.run_script
end

test 'echoize: echo the command before executing it (1)' do
assert_equal "echo \\$\\ rake\nrake", Buildable.new.echoize('rake')
end

test 'echoize: echo the command before executing it (2)' do
assert_equal "echo \\$\\ rvm\\ use\\ 1.9.2\nrvm use 1.9.2\necho \\$\\ FOO\\=bar\\ rake\\ ci\nFOO=bar rake ci", Buildable.new.echoize(['rvm use 1.9.2', 'FOO=bar rake ci'])
end
end

0 comments on commit 9424f5f

Please sign in to comment.