alternator 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in alternator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright 2011 Thibault Jouan. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions are met:
5
+ * Redistributions of source code must retain the above copyright notice, this
6
+ list of conditions and the following disclaimer.
7
+ * Redistributions in binary form must reproduce the above copyright notice,
8
+ this list of conditions and the following disclaimer in the documentation
9
+ and/or other materials provided with the distribution.
10
+ * Neither the name of the software nor the names of its contributors may be
11
+ used to endorse or promote products derived from this software without
12
+ specific prior written permission.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS "AS IS" AND ANY
15
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY
18
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,68 @@
1
+ Alternator
2
+ ==========
3
+
4
+ Alternator helps you alternating between alternatives, so that you can control
5
+ wich files are executed by a program. It generates wrapper scripts to "stub"
6
+ executed files.
7
+
8
+
9
+ Usage
10
+ -----
11
+
12
+ If you want ruby19 to be run when `ruby` is called, execute alternator
13
+ like this:
14
+
15
+ $ alternator wrap ruby ruby19
16
+
17
+ The following shell script will be written in `~/bin/ruby`:
18
+
19
+ #!/usr/bin/env sh
20
+
21
+ exec ruby19 $@
22
+
23
+ Then you prepend `~/bin` to your search path.
24
+
25
+
26
+ Requirements
27
+ ------------
28
+
29
+ * ruby
30
+ * rubygems
31
+ * env
32
+ * sh (any POSIX shell should do it)
33
+
34
+
35
+ Installation
36
+ ------------
37
+
38
+ $ gem install alternator
39
+
40
+
41
+ FAQ
42
+ ---
43
+
44
+ Q: Why?
45
+ A: I wanted to run various ruby code under various ruby interpreters
46
+ easily.
47
+
48
+ Q: Why not using RVM?
49
+ A: First, the installation procedure don't look acceptable to me and as
50
+ far as I know RVM is not available in any package management system.
51
+ RVM requires that I change my shell configuration in such a way
52
+ that I never needed to do for any other software. RVM depends on bash
53
+ and I don't want to install a new shell on all my environments,
54
+ especially not bash. RVM is aimed at managing rubies, I wanted my
55
+ tool not to have this restriction.
56
+
57
+ Q: Why not using rbenv?
58
+ A: I had great hope in rbenv, and I like the fact that it don't require
59
+ to modify the shell configuration. But all the other things I don't
60
+ like about RVM also apply to rbenv :-(
61
+
62
+
63
+ Notes
64
+ -----
65
+
66
+ If you only want to alternate between different rubies, here is a light
67
+ solution based on symlinks:
68
+ <http://chris.mowforth.com/si-because-rvm-and-rbenv-are-overkill>
@@ -0,0 +1,6 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'spec'
5
+ t.pattern = 'spec/**/*_spec.rb'
6
+ end
@@ -0,0 +1,25 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '/lib')
2
+ require 'alternator/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'alternator'
6
+ s.version = Alternator::VERSION
7
+ s.summary = "alternator-#{Alternator::VERSION}"
8
+ s.description = <<-eoh.gsub(/^ +/, '')
9
+ Alternator generates alternative wrapper scripts; it helps you stubbing
10
+ executed files.
11
+ eoh
12
+
13
+ s.author = 'Thibault Jouan'
14
+ s.email = 'tj@a13.fr'
15
+
16
+ s.files = `git ls-files`.split "\n"
17
+ s.test_files = `git ls-files -- {test,features}/*`.split "\n"
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map do |f|
19
+ File.basename(f)
20
+ end
21
+
22
+ s.add_development_dependency 'aruba'
23
+ s.add_development_dependency 'minitest'
24
+ s.add_development_dependency 'rake'
25
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
4
+ require 'alternator'
5
+
6
+ cli = Alternator::CLI.new ARGV
7
+ cli.run
@@ -0,0 +1,4 @@
1
+ Given /^my home directory is the current working directory$/ do
2
+ # Use aruba temporary directory for $HOME.
3
+ ENV['HOME'] = '.'
4
+ end
@@ -0,0 +1,9 @@
1
+ Then /^the file "([^"]*)" must be executable$/ do |file_path|
2
+ prep_for_fs_check do
3
+ File.executable?(file_path).should be_true
4
+ end
5
+ end
6
+
7
+ Then /^the file "([^"]*)" must contain exactly:$/ do |file_path, file_content|
8
+ Then %{the file "#{file_path}" should contain exactly:}, file_content
9
+ end
@@ -0,0 +1 @@
1
+ require 'aruba/cucumber'
@@ -0,0 +1,19 @@
1
+ Feature: Create a wrapper
2
+
3
+ So that I can control files executed by a program
4
+ As a user of the program
5
+ I want to create a wrapper in my search path
6
+
7
+ Scenario: Wrapper creation
8
+ Given my home directory is the current working directory
9
+ And a directory named "bin"
10
+ When I run `alternator wrap ruby ruby19`
11
+ Then the file "bin/ruby" must be executable
12
+ And the file "bin/ruby" must contain exactly:
13
+ """
14
+ #!/usr/bin/env sh
15
+
16
+ exec ruby19 $@
17
+
18
+ """
19
+ And the exit status should be 0
@@ -0,0 +1,3 @@
1
+ require 'alternator/action'
2
+ require 'alternator/actions/wrap'
3
+ require 'alternator/cli'
@@ -0,0 +1,9 @@
1
+ module Alternator
2
+ module Action
3
+ attr_accessor :arguments
4
+
5
+ def initialize(arguments)
6
+ @arguments = arguments
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,29 @@
1
+ module Alternator
2
+ module Actions
3
+ class Wrap
4
+ include Alternator::Action
5
+
6
+ def filename
7
+ @arguments.first
8
+ end
9
+
10
+ def file_path
11
+ "#{ENV['HOME']}/bin/#{filename}"
12
+ end
13
+
14
+ def target_binary
15
+ @arguments[1]
16
+ end
17
+
18
+ def execute
19
+ File.open file_path, 'w', 0700 do |f|
20
+ f.write <<-eoh
21
+ #!/usr/bin/env sh
22
+
23
+ exec #{target_binary} $@
24
+ eoh
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ module Alternator
2
+ class CLI
3
+ attr_accessor :arguments, :action
4
+
5
+ def initialize(arguments)
6
+ @arguments = arguments
7
+ end
8
+
9
+ def action
10
+ @action ||= action_class_const.new action_arguments
11
+ end
12
+
13
+ def action_class_const
14
+ Alternator::Actions.const_get(arguments.first.capitalize)
15
+ end
16
+
17
+ def action_arguments
18
+ arguments.drop(1)
19
+ end
20
+
21
+ def run
22
+ action.execute
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Alternator
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Alternator::Action do
4
+ before do
5
+ class Action
6
+ include Alternator::Action
7
+ end
8
+ end
9
+
10
+ let(:arguments) { ['some', 'arguments'] }
11
+ let(:action) { Action.new arguments }
12
+
13
+ describe '#arguments' do
14
+ it 'return the arguments' do
15
+ action.arguments.must_equal arguments
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+ require 'support/home_directory_helpers'
3
+
4
+ describe Alternator::Actions::Wrap do
5
+ include HomeDirectoryHelpers
6
+
7
+ let(:arguments) { ['ruby', 'ruby19'] }
8
+ let(:action) { Alternator::Actions::Wrap.new arguments }
9
+
10
+ it 'is an action' do
11
+ action.must_be_kind_of Alternator::Action
12
+ end
13
+
14
+ describe '#filename' do
15
+ it 'returns the first argument' do
16
+ action.filename.must_equal arguments.first
17
+ end
18
+ end
19
+
20
+ describe '#file_path' do
21
+ it 'returns the path to the wrapper script' do
22
+ action.file_path.must_equal "#{ENV['HOME']}/bin/#{action.filename}"
23
+ end
24
+ end
25
+
26
+ describe '#target_binary' do
27
+ it 'returns the second argument' do
28
+ action.target_binary.must_equal arguments[1]
29
+ end
30
+ end
31
+
32
+ describe '#execute' do
33
+ it 'writes the wrapper script content' do
34
+ with_fake_home_directory do
35
+ action.execute
36
+ File.read(action.file_path).must_equal <<-eoh
37
+ #!/usr/bin/env sh
38
+
39
+ exec ruby19 $@
40
+ eoh
41
+ end
42
+ end
43
+
44
+ it 'makes the wrapper script executable' do
45
+ with_fake_home_directory do
46
+ action.execute
47
+ File.executable?(action.file_path).must_equal true
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Alternator::CLI do
4
+ let(:arguments) { ['action', 'some', 'arguments'] }
5
+ let(:cli) { Alternator::CLI.new arguments }
6
+
7
+ before do
8
+ module Alternator
9
+ module Actions
10
+ class Action
11
+ include Alternator::Action
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ describe '#arguments' do
18
+ it 'returns all the arguments' do
19
+ cli.arguments.must_equal arguments
20
+ end
21
+ end
22
+
23
+ describe '#action' do
24
+ it 'returns the action instance' do
25
+ cli.action.must_be_instance_of Alternator::Actions::Action
26
+ end
27
+
28
+ it 'returns the action with action arguments assigned' do
29
+ cli.action.arguments.must_equal cli.action_arguments
30
+ end
31
+ end
32
+
33
+ describe '#action_class_const' do
34
+ it 'returns the action class const, named after the first argument' do
35
+ cli.action_class_const.must_equal Alternator::Actions::Action
36
+ end
37
+ end
38
+
39
+ describe '#action_arguments' do
40
+ it 'returns the action arguments' do
41
+ cli.action_arguments.must_equal arguments.drop(1)
42
+ end
43
+ end
44
+
45
+ describe '#run' do
46
+ it 'sends :execute to the action' do
47
+ action = MiniTest::Mock.new
48
+ action.expect :execute, nil
49
+ cli.action = action
50
+ cli.run
51
+ action.verify
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,10 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+
4
+ require 'alternator'
5
+
6
+ class MiniTest::Spec < MiniTest::Unit::TestCase
7
+ class << self
8
+ alias :context :describe
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ require 'tmpdir'
2
+
3
+ module HomeDirectoryHelpers
4
+ def with_fake_home_directory
5
+ Dir.mktmpdir do |tmp_directory|
6
+ real_home_directory = ENV['HOME']
7
+ ENV['HOME'] = tmp_directory
8
+ # FIXME: should not create the bin directory, write a feature for
9
+ # behavior when directory is missing.
10
+ Dir.mkdir("#{ENV['HOME']}/bin")
11
+ begin
12
+ yield
13
+ ensure
14
+ ENV['HOME'] = real_home_directory
15
+ end
16
+ end
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alternator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Thibault Jouan
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-28 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: aruba
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: minitest
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ version_requirements: *id003
62
+ description: |
63
+ Alternator generates alternative wrapper scripts; it helps you stubbing
64
+ executed files.
65
+
66
+ email: tj@a13.fr
67
+ executables:
68
+ - alternator
69
+ extensions: []
70
+
71
+ extra_rdoc_files: []
72
+
73
+ files:
74
+ - .gitignore
75
+ - Gemfile
76
+ - LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - alternator.gemspec
80
+ - bin/alternator
81
+ - features/steps/env_step.rb
82
+ - features/steps/wrapper_step.rb
83
+ - features/support/env.rb
84
+ - features/wrapper.feature
85
+ - lib/alternator.rb
86
+ - lib/alternator/action.rb
87
+ - lib/alternator/actions/wrap.rb
88
+ - lib/alternator/cli.rb
89
+ - lib/alternator/version.rb
90
+ - spec/action_spec.rb
91
+ - spec/actions/wrap_spec.rb
92
+ - spec/cli_spec.rb
93
+ - spec/spec_helper.rb
94
+ - spec/support/home_directory_helpers.rb
95
+ homepage:
96
+ licenses: []
97
+
98
+ post_install_message:
99
+ rdoc_options: []
100
+
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ requirements: []
122
+
123
+ rubyforge_project:
124
+ rubygems_version: 1.8.10
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: alternator-0.0.2
128
+ test_files:
129
+ - features/steps/env_step.rb
130
+ - features/steps/wrapper_step.rb
131
+ - features/support/env.rb
132
+ - features/wrapper.feature