Skip to content

Commit

Permalink
Merge pull request #54 from jkamenik/master
Browse files Browse the repository at this point in the history
Fixing Munge of ENV
  • Loading branch information
queso committed Jun 22, 2011
2 parents 46c559e + 91441d5 commit 7c7ea5d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/cijoe.rb
Expand Up @@ -176,9 +176,14 @@ def run_hook(hook)
{}
end

orig_ENV = ENV.to_hash
ENV.clear
data.each{ |k, v| ENV[k] = v }
`cd #{@project_path} && sh #{file}`
output = `cd #{@project_path} && sh #{file}`

ENV.clear
orig_ENV.to_hash.each{ |k, v| ENV[k] = v}
output
end
end

Expand Down
81 changes: 81 additions & 0 deletions test/test_hooks.rb
@@ -0,0 +1,81 @@
require 'helper'

# mock files to true
class File
class << self
alias orig_exists? exists?
alias orig_executable? executable?

def exists?(f)
return true if $hook_override
orig_exists? f
end
def executable?(f)
return true if $hook_override
orig_executable? f
end
end
end

# #mock file to be the file I want
class CIJoe
attr_writer :last_build
alias orig_path_in_project path_in_project
alias orig_git_user_and_project git_user_and_project

def path_in_project(f)
return '/tmp/test' if $hook_override
orig_path_in_project
end

def git_user_and_project
return ['mine','yours'] if $hook_override
orig_git_user_and_project
end
end

class CIJoe::Commit
attr_writer :raw_commit
end



class TestHooks < Test::Unit::TestCase
def teardown
$hook_override = nil
end

def setup
$hook_override = true
open("/tmp/test",'w') do |file|
file.write "echo $test\n"
file.write "echo $AUTHOR\n"
file.write "export test=foo\n"
end
File.chmod(0777,'/tmp/test')

@cijoe = CIJoe.new('/tmp')
@cijoe.last_build = CIJoe::Build.new "path", "user", "project", Time.now, Time.now,
"deadbeef", :failed, "output", nil
@cijoe.last_build.commit.raw_commit = "Author: commit author\nDate: now"
end

def test_leave_env_intact
ENV['AUTHOR'] = 'foo'
@cijoe.run_hook("/tmp/test")
assert ENV.size != 0, 'ENV is empty but should not be'
assert ENV['AUTHOR'] == 'foo', 'ENV munged a value'
end

def test_empty_hook_env
ENV["test"] = 'should not be shown'
output = @cijoe.run_hook("/tmp/test")
assert_equal "\ncommit author\n", output
end

def test_hook_munge_env
ENV['test'] = 'bar'
output = @cijoe.run_hook("/tmp/test")
assert ENV['test'] == 'bar'
end
end

0 comments on commit 7c7ea5d

Please sign in to comment.