Skip to content

Commit

Permalink
allow passign env to cheetah (gh#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Dec 16, 2015
1 parent aef298c commit d84deb3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Features
* 100% secure (shell expansion is impossible by design)
* Raises exceptions on errors (no more manual status code checks)
* allow specification of valid exit codes
* allow temporary overwrite of env variables
* Optional logging for easy debugging

Non-features
Expand Down Expand Up @@ -151,6 +152,14 @@ status, input and both outputs to it:
Cheetah.run("ls -l", logger: logger)
```

### Overwritting env

If command need to have own specified ENV set, then passing :env option does it.

```ruby
Cheetah.run("env", env: { "LC_ALL" => "C" })
```

### Expecting Non-zero Exit Status

If command is expected to return valid a non-zero exit status like `grep` command
Expand Down
15 changes: 15 additions & 0 deletions lib/cheetah.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ class << self
# @option options [Fixnum, .include?, nil] :allowed_exitstatus (nil)
# Allows to specify allowed exit codes that do not cause exception. It
# adds as last element of result exitstatus.
# @option options [Hash] :env (nil)
# Allows to overwrite env for running command. if key have nil value it
# is deleted from env.
#
# @example
# Cheetah.run("tar", "xzf", "foo.tar.gz")
Expand Down Expand Up @@ -375,6 +378,7 @@ class << self

def run(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
return changed_env(options, *args) if options[:env]
options = BUILTIN_DEFAULT_OPTIONS.merge(@default_options).merge(options)

options[:stdin] ||= "" # allow passing nil stdin see issue gh#11
Expand Down Expand Up @@ -404,6 +408,17 @@ def run(*args)

private

def changed_env(options, *args)
old_env = ENV.to_hash
options = options.dup # do not modify original options
env = options.delete(:env)
ENV.update(env)
args.push(options)
run(*args)
ensure
ENV.replace(old_env)
end

# Parts of Cheetah.run

def compute_streamed(options)
Expand Down
20 changes: 20 additions & 0 deletions spec/cheetah_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,26 @@ def create_command(source, name: "command")
expect(Cheetah.run("/bin/false", stdout: :capture, allowed_exitstatus: 1)).to eq(["", 1])
end
end

describe "changing environment" do
let(:command) do
create_command(<<-EOT)
echo -n $CHEETAH_TEST
EOT
end

it "runs command with given env" do
expect(
Cheetah.run(command, stdout: :capture, env: { "CHEETAH_TEST" => "OK" })
).to eq "OK"
end

it "do not change env outside call" do
ENV["CHEETAH_TEST"] = "OK"
Cheetah.run(command, stdout: :capture, env: { "CHEETAH_TEST" => "fail" })
expect(ENV["CHEETAH_TEST"]).to eq "OK"
end
end
end
end
end

0 comments on commit d84deb3

Please sign in to comment.