bash-session 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4056da143568e57bd5614284b04bae039535297e
4
+ data.tar.gz: 97ba82055cfb37fa3fdfc2cf6525d0cf30c55a9a
5
+ SHA512:
6
+ metadata.gz: f489dbb73045762cb9051030cbf81dd9b4053c484f4d5e2a22ff8eb6cfac7c53247872a8b784b28cd334bd7838b69619cd8927685024765add71d5e2c85cb6c5
7
+ data.tar.gz: a063c08d0c5fea4f68c0609642487abae631ee6e6d7e26d8c6b8a39d50dcec87396575a4144a105cde37727d5efe43d7674828310006a4f614e4be469f30a72c
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bash-session.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Akshay Karle
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,33 @@
1
+ [![Build Status](https://snap-ci.com/snap-ci/bash-session/branch/master/build_image)](https://snap-ci.com/snap-ci/bash-session/branch/master)
2
+
3
+ # Bash::Session
4
+
5
+ TODO: Write a gem description
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'bash-session'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bash-session
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it ( https://github.com/[my-github-username]/bash-session/fork )
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bash/session/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bash-session"
8
+ spec.version = Bash::Session::VERSION
9
+ spec.authors = ["Akshay Karle"]
10
+ spec.email = ["akshay.a.karle@gmail.com"]
11
+ spec.summary = %q{A minimalistic gem for a persistent bash session.}
12
+ spec.description = ""
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,64 @@
1
+ require "bash/session/version"
2
+ require 'securerandom'
3
+ require 'open3'
4
+
5
+ module Bash
6
+ class Session
7
+ class TimeoutError < StandardError; end
8
+
9
+ def initialize(default_timeout=nil)
10
+ start_session
11
+ @default_timeout = default_timeout
12
+ @separator = SecureRandom.hex
13
+ end
14
+
15
+ def execute(command, options={}, &callback)
16
+ exit_status = nil
17
+ out = options[:out]
18
+ timeout = options[:timeout] || @default_timeout
19
+
20
+ cmd = command.dup
21
+ cmd << ";" if cmd !~ /[;&]$/
22
+ cmd << "\n" if cmd =~ /#/
23
+ cmd << %Q{DONTEVERUSETHIS=$?; echo "\n#{@separator} $DONTEVERUSETHIS"; echo "exit $DONTEVERUSETHIS"|sh}
24
+
25
+ @stdin.puts(cmd)
26
+
27
+ until exit_status do
28
+ begin
29
+ data = @outstr.read_nonblock(160000)
30
+ if data.strip =~ /^#{@separator} (\d+)\s*/
31
+ exit_status = $1
32
+ data.gsub!(/\n^#{@separator} (\d+)\s*$/, '')
33
+ end
34
+ callback.call(data) if callback
35
+ out.write data if out
36
+ rescue IO::WaitReadable
37
+ ready = IO.select([@outstr], nil, nil, timeout)
38
+ unless ready
39
+ raise TimeoutError.new("No output received for the last #{timeout} seconds. Timing out...")
40
+ else
41
+ retry
42
+ end
43
+ end
44
+ end
45
+
46
+ exit_status.to_i
47
+ end
48
+
49
+ def close
50
+ return unless @wait_thr.alive?
51
+ return if (Process.kill('TERM', @wait_thr.pid) rescue nil)
52
+ return unless @wait_thr.alive?
53
+ return if (Process.kill('KILL', @wait_thr.pid) rescue nil)
54
+ return unless @wait_thr.alive?
55
+ raise "Could not kill process(PID #{@wait_thr.pid})"
56
+ end
57
+
58
+ private
59
+
60
+ def start_session
61
+ @stdin, @outstr, @wait_thr = Open3.popen2e("bash")
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+ module Bash
2
+ class Session
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bash::Session do
4
+ before(:each) do
5
+ @session = Bash::Session.new
6
+ end
7
+
8
+ after(:each) do
9
+ @session.close
10
+ end
11
+
12
+ it 'should run commands and show out put when it does not end in a new line' do
13
+ @session.execute("echo -n hello") { |output| expect(output).to eq("hello") }
14
+ end
15
+
16
+ it 'should return the exit status of the command' do
17
+ exit_status = @session.execute("true")
18
+ expect(exit_status).to eq(0)
19
+
20
+ exit_status = @session.execute("false")
21
+ expect(exit_status).to eq(1)
22
+ end
23
+
24
+ it 'should run commands ending with comments' do
25
+ exit_status = @session.execute("true # this is a comment") { |output| expect(output).to eq("") }
26
+ expect(exit_status).to eq(0)
27
+ end
28
+
29
+ it 'should preserve the state of bash internal commands' do
30
+ @session.execute("cd #{Dir.pwd}/spec")
31
+ @session.execute("pwd") { |output| expect(output).to include("#{Dir.pwd}/spec") }
32
+ @session.execute("cd ~")
33
+ @session.execute("pwd") { |output| expect(output).to include(Dir.home) }
34
+ @session.execute("echo $FOO") { |output| expect(output).to_not include("bar") }
35
+ @session.execute("export FOO=bar")
36
+ @session.execute("echo $FOO") { |output| expect(output).to include("bar") }
37
+ end
38
+
39
+ it 'should run multiple commands separated by a newline' do
40
+ reader, writer = IO.pipe
41
+ @session.execute("echo hi\necho bye", out: writer)
42
+ writer.close
43
+ expect(reader.read).to eq("hi\nbye\n")
44
+ end
45
+
46
+ it 'should wait for long running commands to complete and then exit' do
47
+ reader, writer = IO.pipe
48
+ exit_status = @session.execute("for i in {1..5}; do echo -n 'hello world '; sleep 1; done", out: writer)
49
+ expect(exit_status).to eq(0)
50
+ writer.close
51
+ expect(reader.read).to eq("hello world "*5)
52
+ end
53
+
54
+ it 'should raise error when command does not generate any output within a default timeout period' do
55
+ @session = Bash::Session.new(3)
56
+ reader, writer = IO.pipe
57
+
58
+ begin_time = Time.now
59
+ expect do
60
+ exit_status = @session.execute("echo hi; sleep 300; echo bye", out: writer)
61
+ end.to raise_error(Bash::Session::TimeoutError, 'No output received for the last 3 seconds. Timing out...')
62
+ writer.close
63
+ end_time = Time.now
64
+
65
+ expect(end_time - begin_time).to be_within(0.1).of(3)
66
+ expect(reader.read_nonblock(1000)).to eq("hi\n")
67
+ end
68
+
69
+ it 'should raise error when command does not generate any output command specific timeout period' do
70
+ @session = Bash::Session.new(1)
71
+ reader, writer = IO.pipe
72
+
73
+ begin_time = Time.now
74
+
75
+ expect do
76
+ exit_status = @session.execute("echo hi; sleep 300; echo bye", out: writer, timeout: 3)
77
+ end.to raise_error(Bash::Session::TimeoutError, 'No output received for the last 3 seconds. Timing out...')
78
+ end_time = Time.now
79
+
80
+ writer.close
81
+
82
+ expect(end_time - begin_time).to be_within(0.1).of(3)
83
+ expect(reader.read_nonblock(1000)).to eq("hi\n")
84
+ end
85
+
86
+ it 'should not raise error when long running command is constantly generating output with default timeout' do
87
+ @session = Bash::Session.new(3)
88
+ reader, writer = IO.pipe
89
+
90
+ exit_status = @session.execute("echo -n hi; for i in {1..6}; do sleep 1; echo -n .; done; echo bye", out: writer)
91
+ writer.close
92
+
93
+ expect(reader.read_nonblock(1000)).to eq("hi......bye\n")
94
+ end
95
+
96
+ it 'should not raise error when long running command is constantly generating output with a command specific timeout' do
97
+ @session = Bash::Session.new(3)
98
+ reader, writer = IO.pipe
99
+
100
+ exit_status = @session.execute("echo -n hi; for i in {1..6}; do sleep 4; echo -n .; done; echo bye", out: writer, timeout: 5)
101
+ writer.close
102
+
103
+ expect(reader.read_nonblock(1000)).to eq("hi......bye\n")
104
+ end
105
+ end
@@ -0,0 +1,90 @@
1
+ require 'bash/session'
2
+ # This file was generated by the `rspec --init` command. Conventionally, all
3
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
5
+ # file to always be loaded, without a need to explicitly require it in any files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need it.
13
+ #
14
+ # The `.rspec` file also contains a few flags that are not defaults but that
15
+ # users commonly want.
16
+ #
17
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
18
+ RSpec.configure do |config|
19
+ # rspec-expectations config goes here. You can use an alternate
20
+ # assertion/expectation library such as wrong or the stdlib/minitest
21
+ # assertions if you prefer.
22
+ config.expect_with :rspec do |expectations|
23
+ # This option will default to `true` in RSpec 4. It makes the `description`
24
+ # and `failure_message` of custom matchers include text for helper methods
25
+ # defined using `chain`, e.g.:
26
+ # be_bigger_than(2).and_smaller_than(4).description
27
+ # # => "be bigger than 2 and smaller than 4"
28
+ # ...rather than:
29
+ # # => "be bigger than 2"
30
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
31
+ end
32
+
33
+ # rspec-mocks config goes here. You can use an alternate test double
34
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
35
+ config.mock_with :rspec do |mocks|
36
+ # Prevents you from mocking or stubbing a method that does not exist on
37
+ # a real object. This is generally recommended, and will default to
38
+ # `true` in RSpec 4.
39
+ mocks.verify_partial_doubles = true
40
+ end
41
+
42
+ # The settings below are suggested to provide a good initial experience
43
+ # with RSpec, but feel free to customize to your heart's content.
44
+ =begin
45
+ # These two settings work together to allow you to limit a spec run
46
+ # to individual examples or groups you care about by tagging them with
47
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
48
+ # get run.
49
+ config.filter_run :focus
50
+ config.run_all_when_everything_filtered = true
51
+
52
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
53
+ # For more details, see:
54
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
55
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
56
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
57
+ config.disable_monkey_patching!
58
+
59
+ # This setting enables warnings. It's recommended, but in some cases may
60
+ # be too noisy due to issues in dependencies.
61
+ config.warnings = true
62
+
63
+ # Many RSpec users commonly either run the entire suite or an individual
64
+ # file, and it's useful to allow more verbose output when running an
65
+ # individual spec file.
66
+ if config.files_to_run.one?
67
+ # Use the documentation formatter for detailed output,
68
+ # unless a formatter has already been configured
69
+ # (e.g. via a command-line flag).
70
+ config.default_formatter = 'doc'
71
+ end
72
+
73
+ # Print the 10 slowest examples and example groups at the
74
+ # end of the spec run, to help surface which specs are running
75
+ # particularly slow.
76
+ config.profile_examples = 10
77
+
78
+ # Run specs in random order to surface order dependencies. If you find an
79
+ # order dependency and want to debug it, you can fix the order by providing
80
+ # the seed, which is printed after each run.
81
+ # --seed 1234
82
+ config.order = :random
83
+
84
+ # Seed global randomization in this process using the `--seed` CLI option.
85
+ # Setting this allows you to use `--seed` to deterministically reproduce
86
+ # test failures related to randomization by passing the same `--seed` value
87
+ # as the one that triggered the failure.
88
+ Kernel.srand config.seed
89
+ =end
90
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bash-session
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Akshay Karle
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: ''
56
+ email:
57
+ - akshay.a.karle@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bash-session.gemspec
68
+ - lib/bash/session.rb
69
+ - lib/bash/session/version.rb
70
+ - spec/session_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: ''
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.0.14
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: A minimalistic gem for a persistent bash session.
96
+ test_files:
97
+ - spec/session_spec.rb
98
+ - spec/spec_helper.rb