Skip to content

Commit 7d5d586

Browse files
committed
Add hook option for configuring environment variables
Some command line utilities can only be configured via environment variables. Add a hook option that allows you to set additional environment variables that a hook should run with.
1 parent 0f3c4aa commit 7d5d586

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* Fix handling of paths with spaces in the name
88
* Fix `CaseConflicts` pre-commit hook to not fail on initial commit
99
* Fix handling of files removed or renamed in a commit amendment
10+
* Add `env` hook configuration option that allows you to set values for
11+
environment variables during the course of a particular hook's run
1012

1113
## 0.25.0
1214

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ Option | Description
192192
`required_library`/`required_libraries` | List of Ruby libraries to load with `Kernel.require` before the hook runs. This is specifically for hooks that integrate with external Ruby libraries.
193193
`command` | Array of arguments to use as the command. How each hook uses this is different, but it allows hooks to change the context with which they run. For example, you can change the command to be `['bundle', 'exec', 'rubocop']` instead of just `rubocop` so that you can use the gem versions specified in your local `Gemfile.lock`. This defaults to the name of the `required_executable`.
194194
`flags` | Array of arguments to append to the `command`. This is useful for customizing the behavior of a tool. It's also useful when a newer version of a tool removes/renames existing flags, so you can update the flags via your `.overcommit.yml` instead of waiting for an upstream fix in Overcommit.
195+
`env` | Hash of environment variables the hook should be run with. This is intended to be used as a last resort when an executable a hook runs is configured only via an environment variable. Any pre-existing environment variables with the same names as ones defined in `env` will have their original values restored after the hook runs.
195196
`install_command` | Command the user can run to install the `required_executable` (or alternately the specified `required_libraries`). This is intended for documentation purposes, as Overcommit does not install software on your behalf since there are too many edge cases where such behavior would result in incorrectly configured installations (e.g. installing a Python package in the global package space instead of in a virtual environment).
196197

197198
In addition to the built-in configuration options, each hook can expose its

lib/overcommit/hook/base.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def run_and_transform
4242
if output = check_for_requirements
4343
status = :fail
4444
else
45-
status, output = process_hook_return_value(run)
45+
result = Overcommit::Utils.with_environment(@config.fetch('env', {})) { run }
46+
status, output = process_hook_return_value(result)
4647
end
4748

4849
[transform_status(status), output]

spec/overcommit/hook/base_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::Base do
4+
let(:config) { double('config') }
5+
let(:context) { double('context') }
6+
let(:hook) { described_class.new(config, context) }
7+
8+
describe '#run_and_transform' do
9+
let(:var_name) { 'OVERCOMMIT_TEST_HOOK_VAR' }
10+
let(:hook_config) { {} }
11+
12+
before do
13+
config.stub(:for_hook).and_return(hook_config)
14+
hook.stub(:run) { ENV[var_name] == 'pass' ? :pass : :fail }
15+
end
16+
17+
subject { hook.run_and_transform }
18+
19+
context 'when no env configuration option is specified' do
20+
let(:hook_config) { {} }
21+
22+
it 'does not modify the environment' do
23+
subject.first.should == :fail
24+
end
25+
end
26+
27+
context 'when env configuration option is specified' do
28+
let(:hook_config) { { 'env' => { var_name => 'pass' } } }
29+
30+
it 'does not modify the environment' do
31+
subject.first.should == :pass
32+
end
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)