kuromi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.2"
4
+ - "1.9.3"
5
+ - "2.0.0"
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in boxy.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'mocha', require: false
8
+ gem 'minitest', require: false
9
+ gem 'simplecov', require: false
10
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 On The Beach LTD
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ # Kuromi
2
+
3
+ [![Build Status](https://travis-ci.org/onthebeach/kuromi.png)](https://travis-ci.org/onthebeach/kuromi)
4
+ [![Code Climate](https://codeclimate.com/github/onthebeach/kuromi.png)](https://codeclimate.com/github/onthebeach/kuromi)
5
+
6
+ Kuromi is a command runner and a pretty DSL around your command line. It's basically a nice friendly way to interact with `Open3#popen3` from the standard library
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'kuromi', github: 'onthebeach/kuromi'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install kuromi
21
+
22
+ ## Usage
23
+
24
+ Commands are built using Runners. and can be built up in chains until they are finally executed with `#run`.
25
+
26
+ output = Kuromi::Runner.for('hostname').run
27
+
28
+ You will end up with a Kuromi::Output object that you can use.
29
+
30
+ output.code # => 0, the exit code of the process
31
+ output.stdout # => Optional information from STDOUT
32
+ output.stderr # => Optional information from STDERR
33
+
34
+ like most OnTheBeach projects, Kuromi is allergic to `nil` so where applicable (currently `stdout` and `stderr` do this) you will either get `Some[value]` or `None`. For the implementation of Optional Values see [the Optional gem here](http://github.com/rsslldnphy/optional)
35
+
36
+ You can pass extra arguments and build up command line chains with Kuromi using `#command`, `#with` and `#and`. For example
37
+
38
+ Kuromi::Runner.for('git').
39
+ command('rebase').
40
+ with('--interactive').
41
+ and('origin/master').
42
+ run
43
+
44
+ ## TODO
45
+
46
+ * Kuromi is synchronous - it will block while your command is being executed.
47
+
48
+ ## Contributing
49
+
50
+ Pull requests are welcome. Please ensure they're tested or we may reject them.
51
+
52
+ Run the tests as follows
53
+
54
+ bundle exec rake
55
+
56
+ or with coverage
57
+
58
+ COVERAGE=true bundle exec rake
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kuromi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kuromi"
8
+ spec.version = Kuromi::VERSION
9
+ spec.authors = ["Matt House"]
10
+ spec.email = ["matt@eightbitraptor.com"]
11
+ spec.description = %q{Kuromi is an application runner}
12
+ spec.summary = %q{Kuromi provides a DSL for running command lines in Ruby}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency "optional"
24
+ end
@@ -0,0 +1,7 @@
1
+ require "optional"
2
+
3
+ require "kuromi/runner"
4
+ require "kuromi/version"
5
+
6
+ module Kuromi
7
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'runner/all'
2
+
3
+ module Kuromi
4
+ class Runner
5
+
6
+ def self.for(binary_name)
7
+ Binary.new(binary_name)
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,8 @@
1
+ require_relative 'runnable'
2
+ require_relative 'options'
3
+
4
+ require_relative 'binary'
5
+ require_relative 'command'
6
+ require_relative 'option_value'
7
+
8
+ require_relative 'output'
@@ -0,0 +1,24 @@
1
+ module Kuromi
2
+ class Runner
3
+ class Binary
4
+ include Runnable
5
+ include Options
6
+
7
+ def initialize(name)
8
+ @name = name
9
+ end
10
+
11
+ def command(com)
12
+ Command.new(self, com)
13
+ end
14
+
15
+ def to_s
16
+ name
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :name
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ module Kuromi
2
+ class Runner
3
+ class Command
4
+ include Runnable
5
+ include Options
6
+
7
+ def initialize(binary, command)
8
+ @binary = binary
9
+ @command = command
10
+ end
11
+
12
+ def to_s
13
+ "#{binary} #{command}"
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :binary, :command
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ module Kuromi
2
+ class Runner
3
+ class OptionValue
4
+ include Runnable
5
+ include Options
6
+
7
+ def initialize(binary, option, value)
8
+ @binary = binary
9
+ @option = option
10
+ @value = value
11
+ end
12
+
13
+ def to_s
14
+ "#{binary} #{option} #{value}".strip
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :binary, :option, :value
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ module Kuromi
2
+ class Runner
3
+ module Options
4
+ def with(option, value=nil)
5
+ OptionValue.new(self, option, value)
6
+ end
7
+
8
+ alias_method :and, :with
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module Kuromi
2
+ class Runner
3
+ class Output
4
+ attr_reader :stdout, :stderr, :code
5
+
6
+ def initialize(code, stdout, stderr)
7
+ @code = code
8
+ @stdout = Option[stdout]
9
+ @stderr = Option[stderr]
10
+ end
11
+
12
+ def success?
13
+ @code == 0
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ require 'open3'
2
+
3
+ module Kuromi
4
+ class Runner
5
+ module Runnable
6
+ def run
7
+ Open3.popen3(self.to_s) do |sin, sout, serr, thr|
8
+ return Output.new(thr.value, sout.read, serr.read)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Kuromi
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,66 @@
1
+ require 'test_helper'
2
+
3
+ module Kuromi
4
+ describe "Runner Output" do
5
+ it "is returned from a Runner object" do
6
+ Open3.expects(:popen3).yields(stub,
7
+ stub(read: nil),
8
+ stub(read: nil),
9
+ stub(value: 0))
10
+
11
+ output = Runner.for('ps').run
12
+ output.must_be_instance_of Runner::Output
13
+ end
14
+ end
15
+
16
+ describe "An Output object" do
17
+ let(:stdout){ stub }
18
+ let(:stderr){ stub }
19
+ let(:stdin){ stub }
20
+
21
+ subject{ Runner.for('hostname').run }
22
+
23
+ it "puts the stdout stream in the output object" do
24
+ stdout.expects(:read).returns('hitomi')
25
+ stderr.stubs(:read)
26
+ Open3.expects(:popen3).yields(stdin, stdout, stderr, stub(:value => 0))
27
+
28
+ subject.stdout.must_equal Some['hitomi']
29
+ end
30
+
31
+ it "puts the exit code in the output object" do
32
+ stdout.stubs(:read)
33
+ stderr.stubs(:read)
34
+ Open3.expects(:popen3).yields(stdin, stdout, stderr, stub(:value => 0))
35
+
36
+ subject.code.must_equal 0
37
+ end
38
+
39
+ it "puts the stderr stream in the output object" do
40
+ stdout.stubs(:read)
41
+ stderr.expects(:read).returns('ERR: Panic')
42
+ Open3.expects(:popen3).yields(stdin, stdout, stderr, stub(:value => 1))
43
+
44
+ subject.stderr.must_equal Some['ERR: Panic']
45
+ end
46
+
47
+ it "reports success and failure" do
48
+ Open3.expects(:popen3).yields(stub,
49
+ stub(read: nil),
50
+ stub(read: nil),
51
+ stub(value: 0))
52
+
53
+ subject.success?.must_equal true
54
+ end
55
+
56
+ it "reports failure" do
57
+ Open3.expects(:popen3).yields(stub,
58
+ stub(read: nil),
59
+ stub(read: nil),
60
+ stub(value: 1))
61
+
62
+ subject.success?.must_equal false
63
+ end
64
+ end
65
+
66
+ end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ module Kuromi
4
+ describe "running a binary" do
5
+ subject { Runner.for('hostname')}
6
+
7
+ it "runs" do
8
+ Open3.expects(:popen3).with("hostname")
9
+ subject.run
10
+ end
11
+ end
12
+
13
+ describe "running a binary with a command" do
14
+ subject { Runner.for('git').command('st') }
15
+
16
+ it "runs" do
17
+ Open3.expects(:popen3).with("git st")
18
+ subject.run
19
+ end
20
+ end
21
+
22
+ describe "running a binary with options" do
23
+ let(:runner) { Runner.for('ps').with('-ef') }
24
+
25
+ it "runs with a single option" do
26
+ Open3.expects(:popen3).with("ps -ef")
27
+ runner.run
28
+ end
29
+
30
+ it "runs with multiple options" do
31
+ Open3.expects(:popen3).with("ps -ef foo")
32
+ runner.and("foo").run
33
+ end
34
+ end
35
+
36
+ describe "chaining all the things" do
37
+ let(:runner) { Runner.for('git').
38
+ command('rebase').
39
+ with('--interactive').
40
+ and('origin/safe') }
41
+
42
+ it "runs with everything" do
43
+ Open3.expects(:popen3).with("git rebase --interactive origin/safe")
44
+ runner.run
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,16 @@
1
+ require 'simplecov'
2
+ if ENV['COVERAGE']
3
+ SimpleCov.start do
4
+ add_filter "/test/"
5
+ end
6
+ end
7
+
8
+ require 'minitest/autorun'
9
+ require 'mocha/setup'
10
+
11
+ require 'kuromi'
12
+
13
+ Mocha::Configuration.prevent(:stubbing_non_existent_method)
14
+ Mocha::Configuration.prevent(:stubbing_method_unnecessarily)
15
+ Mocha::Configuration.prevent(:stubbing_non_public_method)
16
+
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kuromi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt House
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: optional
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Kuromi is an application runner
63
+ email:
64
+ - matt@eightbitraptor.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .travis.yml
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - kuromi.gemspec
76
+ - lib/kuromi.rb
77
+ - lib/kuromi/runner.rb
78
+ - lib/kuromi/runner/all.rb
79
+ - lib/kuromi/runner/binary.rb
80
+ - lib/kuromi/runner/command.rb
81
+ - lib/kuromi/runner/option_value.rb
82
+ - lib/kuromi/runner/options.rb
83
+ - lib/kuromi/runner/output.rb
84
+ - lib/kuromi/runner/runnable.rb
85
+ - lib/kuromi/version.rb
86
+ - test/lib/kuromi/output_test.rb
87
+ - test/lib/kuromi/runner_test.rb
88
+ - test/test_helper.rb
89
+ homepage: ''
90
+ licenses:
91
+ - MIT
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 1.8.25
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Kuromi provides a DSL for running command lines in Ruby
114
+ test_files:
115
+ - test/lib/kuromi/output_test.rb
116
+ - test/lib/kuromi/runner_test.rb
117
+ - test/test_helper.rb