cexpect 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ccc43da0141c2168408e3a9f931f58e37c0a71edab8b2bcd40066396ed8dfbe
4
- data.tar.gz: 543082cfb8016426f41aad970df5cadee7109f285aec39b8062cb097662ab49a
3
+ metadata.gz: 7d6f51d1822901ccaef277fd26017749f6d65ed3eaea50836fa2fbe5b7e90410
4
+ data.tar.gz: 9eade35e51126e2ce00f99a02f3f1052f09314ecd19b516cb51f0d7ea4438139
5
5
  SHA512:
6
- metadata.gz: faf35c685141bbe365000a483594932614fc9b5873b152a5fff7fdbdc961b769f62c91f69b8553f6265b31cf4d9beaf5913f0736e5d21c4f9e644d48b27c98cb
7
- data.tar.gz: '0383c442a569ac777e57f9bd38b2c63025ad2d1bfb2748ddc6adf0859f32cdc85c755a66d9dfc6c15a336ed5258bafd2566652299954d56d357d3fcb6d9f025f'
6
+ metadata.gz: 28a4cc9e4edfa0b18fa6d57f0c43f0fefae54b8872de8fe09c56595b31bf203a0aa5a24fe8005ebc737483c5a7d5e293fdd1d95276a57faccb717e786420ddc1
7
+ data.tar.gz: 180e097fe5134c7527264a9a464bb5d4c27a8317dd08aef3886a560e997a00f1836f6740987fe6ccf83e8f181be8e5509f6a0ad2849b5788f89d9ac60312c1d6
@@ -0,0 +1,33 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+
21
+ steps:
22
+ - uses: actions/checkout@v2
23
+ - name: Set up Ruby
24
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
25
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
26
+ # uses: ruby/setup-ruby@v1
27
+ uses: ruby/setup-ruby@v1.70.1
28
+ with:
29
+ ruby-version: 3.0
30
+ - name: Install dependencies
31
+ run: bundle install
32
+ - name: Run tests
33
+ run: bundle exec rake
data/.rspec CHANGED
@@ -1,3 +1,4 @@
1
1
  --format documentation
2
2
  --color
3
3
  --require spec_helper
4
+ -Ilib
data/.rubocop.yml ADDED
@@ -0,0 +1,43 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ Exclude:
5
+ - 'bin/{console,setup}' # not mine
6
+ - 'Gemfile' # maintained through "bundle" command
7
+
8
+ # TODO: Clean up tests
9
+ RSpec/ExpectInHook:
10
+ Enabled: false
11
+ RSpec/FilePath:
12
+ Exclude:
13
+ - spec/cexpect_spec.rb
14
+ RSpec/MessageSpies:
15
+ Enabled: false
16
+ RSpec/NestedGroups:
17
+ Enabled: false
18
+ RSpec/VerifiedDoubles:
19
+ Enabled: false
20
+
21
+ # Personal preferences
22
+ Layout/DotPosition:
23
+ EnforcedStyle: trailing
24
+ Style/PercentLiteralDelimiters:
25
+ PreferredDelimiters:
26
+ '%i': '()'
27
+
28
+ # New stuff in rubocop, not yet enabled/disabled by default
29
+ Layout/SpaceAroundMethodCallOperator:
30
+ Enabled: true
31
+ Lint/RaiseException:
32
+ Enabled: true
33
+ Lint/StructNewOverride:
34
+ Enabled: true
35
+ Style/HashEachMethods:
36
+ Enabled: true
37
+ Style/HashTransformKeys:
38
+ Enabled: true
39
+ Style/HashTransformValues:
40
+ Enabled: true
41
+
42
+ Style/ExponentialNotation:
43
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,2 +1,4 @@
1
1
  # Changelog for the `cexpect` gem
2
2
  * cexpect 0.1.0: First version
3
+ * cexpect 0.1.1: Different logging interface, general clean-up
4
+ * cexpect 0.1.2: Update CHANGELOG.md and fix gemspec
data/Gemfile CHANGED
@@ -5,3 +5,6 @@ gemspec
5
5
 
6
6
  gem "rake", "~> 12.0"
7
7
  gem "rspec", "~> 3.0"
8
+
9
+ gem "rubocop", "~> 0.82.0"
10
+ gem "rubocop-rspec", "~> 1.38"
data/Gemfile.lock CHANGED
@@ -1,26 +1,45 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cexpect (0.1.0)
4
+ cexpect (0.1.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- diff-lcs (1.3)
9
+ ast (2.4.2)
10
+ diff-lcs (1.4.4)
11
+ jaro_winkler (1.5.4)
12
+ parallel (1.20.1)
13
+ parser (3.0.1.1)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.0.0)
10
16
  rake (12.3.3)
11
- rspec (3.9.0)
12
- rspec-core (~> 3.9.0)
13
- rspec-expectations (~> 3.9.0)
14
- rspec-mocks (~> 3.9.0)
15
- rspec-core (3.9.1)
16
- rspec-support (~> 3.9.1)
17
- rspec-expectations (3.9.1)
17
+ rexml (3.2.5)
18
+ rspec (3.10.0)
19
+ rspec-core (~> 3.10.0)
20
+ rspec-expectations (~> 3.10.0)
21
+ rspec-mocks (~> 3.10.0)
22
+ rspec-core (3.10.1)
23
+ rspec-support (~> 3.10.0)
24
+ rspec-expectations (3.10.1)
18
25
  diff-lcs (>= 1.2.0, < 2.0)
19
- rspec-support (~> 3.9.0)
20
- rspec-mocks (3.9.1)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-mocks (3.10.2)
21
28
  diff-lcs (>= 1.2.0, < 2.0)
22
- rspec-support (~> 3.9.0)
23
- rspec-support (3.9.2)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-support (3.10.2)
31
+ rubocop (0.82.0)
32
+ jaro_winkler (~> 1.5.1)
33
+ parallel (~> 1.10)
34
+ parser (>= 2.7.0.1)
35
+ rainbow (>= 2.2.2, < 4.0)
36
+ rexml
37
+ ruby-progressbar (~> 1.7)
38
+ unicode-display_width (>= 1.4.0, < 2.0)
39
+ rubocop-rspec (1.41.0)
40
+ rubocop (>= 0.68.1)
41
+ ruby-progressbar (1.11.0)
42
+ unicode-display_width (1.7.0)
24
43
 
25
44
  PLATFORMS
26
45
  ruby
@@ -29,6 +48,8 @@ DEPENDENCIES
29
48
  cexpect!
30
49
  rake (~> 12.0)
31
50
  rspec (~> 3.0)
51
+ rubocop (~> 0.82.0)
52
+ rubocop-rspec (~> 1.38)
32
53
 
33
54
  BUNDLED WITH
34
55
  2.1.4
data/README.md CHANGED
@@ -62,6 +62,16 @@ end
62
62
 
63
63
  wr.puts('cat very-large-file')
64
64
  contents = rd.fexpect('$ ')
65
+
66
+ # If CExpect.spawn doesn't suit you
67
+ orig_rd, wr = IO.pipe
68
+ rd = CExpect::Reader.new(orig_rd)
69
+
70
+ wr.puts "Hello, World!\n"
71
+ greeting = rd.fexpect("\n")
72
+
73
+ # If you want logging
74
+
65
75
  ```
66
76
 
67
77
  ## Development
data/Rakefile CHANGED
@@ -1,6 +1,10 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
3
6
 
4
7
  RSpec::Core::RakeTask.new(:spec)
8
+ RuboCop::RakeTask.new
5
9
 
6
- task :default => :spec
10
+ task default: %i(spec rubocop)
data/cexpect.gemspec CHANGED
@@ -1,27 +1,32 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'lib/cexpect/version'
2
4
 
3
5
  Gem::Specification.new do |spec|
4
- spec.name = "cexpect"
5
- spec.version = Cexpect::VERSION
6
- spec.authors = ["Christer Jansson"]
7
- spec.email = ["christer@janssons.org"]
6
+ spec.name = 'cexpect'
7
+ spec.version = CExpect::VERSION
8
+ spec.authors = ['Christer Jansson']
9
+ spec.email = ['christer@janssons.org']
8
10
 
9
- spec.summary = %q{An improved expect method}
10
- spec.description = %q{An expect method with more reasonable return values and logging functionality}
11
- spec.homepage = "https://github.com/kondensatorn/cexpect"
12
- spec.license = "BSD 2-clause"
13
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
11
+ spec.summary = 'An improved expect method'
12
+ spec.description = 'An expect method with more reasonable return' \
13
+ ' values and logging functionality'
14
+ spec.homepage = 'https://github.com/kondensatorn/cexpect'
15
+ spec.license = 'BSD-2-Clause'
16
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
17
 
15
- spec.metadata["homepage_uri"] = spec.homepage
16
- spec.metadata["source_code_uri"] = spec.homepage
17
- spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = spec.homepage
20
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/CHANGELOG.md"
18
21
 
19
22
  # Specify which files should be added to the gem when it is released.
20
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ # The `git ls-files -z` loads the files in the RubyGem that have
24
+ # been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.
27
+ split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
28
  end
24
- spec.bindir = "exe"
29
+ spec.bindir = 'exe'
25
30
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
- spec.require_paths = ["lib"]
31
+ spec.require_paths = ['lib']
27
32
  end
data/lib/cexpect.rb CHANGED
@@ -1,31 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cexpect/module_methods'
1
4
  require 'delegate'
2
5
  require 'observer'
3
- require 'pty'
4
6
 
7
+ #
8
+ # A module for supplying a different expect method
9
+ #
5
10
  module CExpect
6
- def self.spawn(*args)
7
- original_rd, wr, pid = PTY.spawn(*args)
8
- rd = CExpect::Reader.new(original_rd)
9
- if block_given?
10
- yield(rd, wr, pid)
11
- else
12
- [rd, wr, pid]
13
- end
14
- end
11
+ extend CExpect::ModuleMethods
15
12
 
13
+ #
14
+ # A class delegating normal operations to a wrapped IO, adding an
15
+ # expect method
16
+ #
16
17
  class Reader < SimpleDelegator
17
- include Observable
18
-
19
- def initialize(_original)
20
- @leftovers = ''
21
- super
18
+ def initialize(io, observers = nil)
19
+ extend(LoggingReader) if observers
20
+ super(io)
22
21
  end
23
22
 
24
- def expect(pat, timeout = nil, match_method: :_expect_re_match)
23
+ def expect(pat, timeout = nil, match_method: :re_match)
25
24
  buf = +''
26
25
 
27
26
  result = catch(:result) do
28
- loop { _expect_try(pat, buf, timeout, match_method) }
27
+ loop { expect_try(pat, buf, timeout, match_method) }
29
28
  end
30
29
 
31
30
  if block_given?
@@ -36,48 +35,52 @@ module CExpect
36
35
  end
37
36
 
38
37
  def fexpect(pat, timeout = nil)
39
- expect(pat, timeout, match_method: :_expect_string_match)
38
+ expect(pat, timeout, match_method: :string_match)
40
39
  end
41
40
 
42
41
  private
43
42
 
44
- def _expect_try(pat, buf, timeout, match_method)
45
- c = _expect_getc(timeout)
43
+ def expect_try(pat, buf, timeout, match_method)
44
+ c = getc(timeout)
46
45
 
47
- if c.nil?
48
- @leftovers = buf
49
- throw(:result, nil)
50
- end
46
+ throw(:result, nil) if c.nil?
51
47
 
52
48
  buf << c
53
49
 
54
- _expect_log(pat, buf)
50
+ log(pat, buf) if respond_to?(:log)
55
51
 
56
52
  result = send(match_method, buf, pat)
57
53
  throw(:result, result) if result
58
54
  end
59
55
 
60
- def _expect_getc(timeout)
61
- return @leftovers.slice!(0).chr unless @leftovers.empty?
56
+ def getc(timeout)
57
+ rd = __getobj__
62
58
 
63
- return nil if !IO.select([self], nil, nil, timeout) || eof?
59
+ return nil if !IO.select([rd], nil, nil, timeout) || eof?
64
60
 
65
- getc.chr
61
+ rd.getc.chr
66
62
  end
67
63
 
68
- def _expect_log(pat, buf)
69
- return if count_observers.zero?
70
-
71
- changed
72
- notify_observers("pat: #{pat.inspect}\nbuf: #{buf.inspect}\n")
73
- end
74
-
75
- def _expect_re_match(buf, pat)
64
+ def re_match(buf, pat)
76
65
  buf.match(pat)
77
66
  end
78
67
 
79
- def _expect_string_match(buf, pat)
68
+ def string_match(buf, pat)
80
69
  buf[0, buf.size - pat.size] if buf.end_with?(pat)
81
70
  end
82
71
  end
72
+
73
+ #
74
+ # Adds logging capability when observers are given to constructor
75
+ #
76
+ module LoggingReader
77
+ include Observable
78
+
79
+ def log(pat, buf)
80
+ return if count_observers.zero?
81
+
82
+ changed
83
+ notify_observers(pat, buf)
84
+ end
85
+ end
83
86
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pty'
4
+
5
+ module CExpect
6
+ #
7
+ # Module methods for CExpect
8
+ #
9
+ module ModuleMethods
10
+ #
11
+ # Assume first element benefits from using CExpect
12
+ #
13
+ # This generalization is a bit iffy; it works for class methods
14
+ # that returns and yields an IO reader first, followed by any
15
+ # other parameters. But hey, if we need to wrap things that behave
16
+ # differently, we can just give this one (and its shared examples
17
+ # in the specs) a more specific name and implement another.
18
+ #
19
+ def cexpect_wrap(array, &block)
20
+ array[0] = CExpect::Reader.new(array[0])
21
+ if block_given?
22
+ block.call(*array)
23
+ else
24
+ array
25
+ end
26
+ end
27
+
28
+ def pipe(*args, &block)
29
+ cexpect_wrap(IO.pipe(*args), &block)
30
+ end
31
+
32
+ def spawn(*args, &block)
33
+ cexpect_wrap(PTY.spawn(*args), &block)
34
+ end
35
+ end
36
+ end
@@ -1,3 +1,5 @@
1
- module Cexpect
2
- VERSION = "0.1.0"
1
+ # frozen_string_literal: true
2
+
3
+ module CExpect
4
+ VERSION = '0.2.0'
3
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cexpect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christer Jansson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-28 00:00:00.000000000 Z
11
+ date: 2021-05-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: An expect method with more reasonable return values and logging functionality
14
14
  email:
@@ -17,8 +17,10 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - ".github/workflows/ruby.yml"
20
21
  - ".gitignore"
21
22
  - ".rspec"
23
+ - ".rubocop.yml"
22
24
  - ".travis.yml"
23
25
  - CHANGELOG.md
24
26
  - Gemfile
@@ -30,10 +32,11 @@ files:
30
32
  - bin/setup
31
33
  - cexpect.gemspec
32
34
  - lib/cexpect.rb
35
+ - lib/cexpect/module_methods.rb
33
36
  - lib/cexpect/version.rb
34
37
  homepage: https://github.com/kondensatorn/cexpect
35
38
  licenses:
36
- - BSD 2-clause
39
+ - BSD-2-Clause
37
40
  metadata:
38
41
  homepage_uri: https://github.com/kondensatorn/cexpect
39
42
  source_code_uri: https://github.com/kondensatorn/cexpect
@@ -53,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
56
  - !ruby/object:Gem::Version
54
57
  version: '0'
55
58
  requirements: []
56
- rubygems_version: 3.1.2
59
+ rubygems_version: 3.1.6
57
60
  signing_key:
58
61
  specification_version: 4
59
62
  summary: An improved expect method