travis-ci/travis-core — pull request #249 by jumanjiman (2013-08-25 21:12:16+00:00)
Title: Enable per-build variables via Travis::CommitCommand

This PR is related to issue #161.

I would appreciate a helpful pointer to get me in the right direction
(assuming this looks like a PR you'll consider merging).

WRT the two work-in-process commits,
I'm not sure at this point how to delegate the data to the builder.

Atomic [1] commits in this PR with passing rspec [2]:

* WIP: enable to set granular vars for single build
* Add rspec shared context that is useful for testing env vars
* WIP: enable to set generic debug mode for single build
* Travis::CommitCommand: refactor implementation to be DRY
* Travis::CommitCommand: refactor rspec to be DRY

## WIP "granular build vars"

Extend Travis::CommitCommand with the concept of assignments.
For example, include this in the commit message...

# Escape the ci to avoid interpretation.
[ci fail_fast = true]

...to get interpreted as `[{ 'fail_fast' => 'true' }]` and
exported to the build environment as `fail_fast=true`.

Use case 1: Be able to set things like seeds for the test run
as mentioned at https://github.com/travis-ci/travis-core/issues/161

Use case 2: Be able to override default build configuration for
a single build. Example...

RSpec.configure do |c|
# Fail fast to reduce duration of test runs.
# IOW get out of the way so that the next pull request gets tested.
# Override for a single build using a Travis commit command.
c.fail_fast = ENV['fail_fast'] == 'true'
end

## WIP "generic debug mode"

Add keyword 'debug' to Travis::CommitCommand.
For example, include this in the commit message...

[ci debug]

...to `export TRAVIS_DEBUG=true` to build environment.

Use case: I can't see why my build is failing, and I need a WTF?! mode.
Example:

case ENV['TRAVIS_DEBUG'] == 'true'
when true
# Set numerous debug options that your tests use.
fail_fast = false
suppress_warnings = false
else
# Default to 'quiet' mode.
fail_fast = true
suppress_warnings = true
end

I'm pretty sure it needs to be delegated to `travis_vars` at
https://github.com/travis-ci/travis-build/blob/master/lib/travis/build/data/env.rb

## Notes

[1] For each commit, I follow these basic principles:

* rspec must pass before and after each commit
* If I refactor rspec, I must not touch the implementation
* If I refactor the implementation, I must not touch the rspec
* If I change implementation, I add new rspec but do not touch existing rspec

[2] rspec results as of creating this PR

```
$ bundle exec 'ruby -S rspec spec/travis/commit_command_spec.rb spec/travis/environment_context_spec.rb' 2>&1
```

output:

```
I Setting up Travis::Core
Run options: include {:focused=>true}

All examples were filtered out; ignoring {:focused=>true}

Travis::CommitCommand
unknown-command
behaves like an invalid keyword
method does not exist
behaves like an unused keyword
does not attempt to call 'unknown-command?'
debug
behaves like a valid keyword
method 'debug?' exists
is not invoked by default
is not invoked by a commit msg containing [ci unknown-command]
is invoked by the forward form [ci debug]
is invoked by the forward form [ci:debug]
is invoked by the backwards form [debug ci]
is invoked by the backwards form [debug:ci]
strips whitespace around 'debug' in both forward forms
strips whitespace around 'debug' in both backwards forms
is case-insensitive
skip
behaves like a valid keyword
method 'skip?' exists
is not invoked by default
is not invoked by a commit msg containing [ci unknown-command]
is invoked by the forward form [ci skip]
is invoked by the forward form [ci:skip]
is invoked by the backwards form [skip ci]
is invoked by the backwards form [skip:ci]
strips whitespace around 'skip' in both forward forms
strips whitespace around 'skip' in both backwards forms
is case-insensitive
assignments
behaves like a list of key-value pairs
is an array
is empty if message does not contain assignments
key-value pairs are hashes
contains the correct key-value pairs
does not override travis_vars
does not override environment variables
ignores multiple assignments within a single command
allows whitespace to surround =
strips whitespace around keys and values
ignores keys that contain whitespace
allows values that contain whitespace
allows the forward form with space separator
allows the forward form with colon separator
allows the backwards form with space separator
allows the backwards form with colon separator
key
behaves like an unused keyword
does not attempt to call 'debug?'
behaves like an unused keyword
does not attempt to call 'skip?'
value
behaves like an unused keyword
does not attempt to call 'debug?'
behaves like an unused keyword
does not attempt to call 'skip?'

Travis::EnvironmentContext
is accessible as Travis::EnvironmentContext
when included
preserves pre-existing env var
restores pre-existing env var if changed within example
discards env var if created within example
when not included (i.e., default rspec behavior)
preserves pre-existing env var
clobbers env var if changed within example
persists env var if created within example

Finished in 0.23387 seconds
48 examples, 0 failures
```