repl_runner 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: 05eaef051153f7d7b06f9c68c4933a0955d58e6b
4
- data.tar.gz: a371ba6c53712e73296da79c69c3f604fa7c841e
3
+ metadata.gz: 6202f0b227dbf9c88ab52cf6f10b7294c2f97630
4
+ data.tar.gz: 0dad45ad6ab909015bda06202b69496a18c6260e
5
5
  SHA512:
6
- metadata.gz: 7d406e33b664960f9af50c4aea6a6ba3ebfbf3d848193f18732aad7c340f6d3e11c9125786b0b70fddcd72b482f57f90e2a432d6d6d7ea63c879b7739bc2a1a2
7
- data.tar.gz: ecaee16aaf62250a286ffb4cf1e0deeda8cbecdaabb7d1b8015a537c6f89e1f68fe067ca2df8e6eefa9e72c5115012a2071db34601b149629ced825dc317a7bb
6
+ metadata.gz: b54cd9a521ec9df9c82086d4b7441ea7d04c3a09ad5fd384dfb2200b922579a0269e05fb5a17195aef7cd4af99bc43a830eb6ac05c93705ccf4c5c9b3d3a21b9
7
+ data.tar.gz: a963bcb45e11f9b1b74016d996923994a49e2310fbbc8f6023f68e12b719313b85bb788401b257d44d01a09a34cfa5af86f8b38adb843dbd3814a7fb5c031bf7
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - ruby-head
6
+
7
+ matrix:
8
+ allow_failures:
9
+ - rvm: ruby-head
@@ -0,0 +1,10 @@
1
+ ## 0.0.2 (10/02/2013)
2
+
3
+ * Remove trailing prompts by default
4
+ * Add ReplRunner#zip
5
+ zip takes in a multi line string of commands, returns an array of arrays
6
+ [[command, result]]
7
+
8
+ ## 0.0.1
9
+
10
+ * Initial
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # REPL Runner
2
2
 
3
- Drive irb, bash, or another REPL like environment programatically.
3
+ Drive irb, bash, or another REPL like environment programmatically.
4
4
 
5
5
  ## Why?
6
6
 
7
- I needed to be able to run `rails console` commands programatically for testing buildpacks with [Hatchet](http://github.com/heroku/hatchet).
7
+ I needed to be able to run `rails console` commands programmatically for testing buildpacks with [Hatchet](http://github.com/heroku/hatchet).
8
8
 
9
9
  ## How?
10
10
 
@@ -59,6 +59,17 @@ class ReplRunner
59
59
  repl.close
60
60
  end
61
61
 
62
+ def zip(string)
63
+ results = []
64
+ lines = string.lines.map(&:rstrip)
65
+ self.run do |repl|
66
+ lines.each do |line|
67
+ repl.run(line) {|result| results << result }
68
+ end
69
+ end
70
+ lines.zip(results)
71
+ end
72
+
62
73
  def run(&block)
63
74
  repl = start
64
75
  yield repl
@@ -1,5 +1,6 @@
1
1
  class ReplRunner
2
2
  class MultiCommandParser
3
+ STRIP_TRAILING_PROMPT_REGEX = /(\r|\n)+/
3
4
  attr_accessor :commands, :raw
4
5
 
5
6
  def initialize(commands, terminate_command = nil)
@@ -20,11 +21,13 @@ class ReplRunner
20
21
  string = string.gsub(command_to_regex(@terminate_command), '') if @terminate_command
21
22
  # attack the string from the end
22
23
  commands.reverse.each do |command|
23
- regex = command_to_regex(command)
24
- result_array = string.split(regex)
25
- @parsed_result << result_array.pop
26
- raise NoResults.new(command, raw) if @parsed_result.last.blank?
27
- string = result_array.join('')
24
+ regex = command_to_regex(command)
25
+ before, match, result = string.rpartition(regex)
26
+
27
+ raise NoResults.new(command, raw) if result.empty?
28
+
29
+ string = before
30
+ @parsed_result << result.rpartition(STRIP_TRAILING_PROMPT_REGEX).first
28
31
  end
29
32
 
30
33
  @parsed_result.reverse!
@@ -1,3 +1,3 @@
1
1
  class ReplRunner
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -4,9 +4,19 @@ class MultiCommandParserTest < Test::Unit::TestCase
4
4
  def test_removes_command_from_string
5
5
  hash = {commands: ["1+1", "'hello' + 'world'"],
6
6
  string: "1+1\r\n => 2 \r\n'hello' + 'world'\r\n => \"helloworld\" \r\n",
7
- expect: [" => 2 \r\n", " => \"helloworld\" \r\n"]
7
+ expect: [" => 2 \r", " => \"helloworld\" \r"]
8
8
  }
9
9
  cp = ReplRunner::MultiCommandParser.new(hash[:commands])
10
10
  assert_equal hash[:expect], cp.parse(hash[:string])
11
11
  end
12
+
13
+ def test_removes_trailing_prompt
14
+ string = "1+1\r\n => 2 \r\n>"
15
+ regex = ReplRunner::MultiCommandParser::STRIP_TRAILING_PROMPT_REGEX
16
+ expect = "1+1\r\n => 2"
17
+ before, match, after = string.rpartition(regex)
18
+ assert_equal "1+1\r\n => 2 \r", before
19
+ assert_equal "\n", match
20
+ assert_equal ">", after
21
+ end
12
22
  end
@@ -2,17 +2,17 @@ require 'test_helper'
2
2
 
3
3
  class StreamExecTest < Test::Unit::TestCase
4
4
  def test_local_irb_stream
5
- repl = ReplRunner::PtyParty.new("irb")
5
+ repl = ReplRunner::PtyParty.new("irb --simple-prompt")
6
6
  repl.run("STDOUT.sync = true\n")
7
- assert_equal "1+1\r\n => 2 \r\n", repl.run("1+1\n")
8
- assert_equal "'hello' + 'world'\r\n => \"helloworld\" \r\n", repl.run("'hello' + 'world'\n")
7
+ assert_equal "1+1\r\n=> 2\r\n", repl.run("1+1\n")
8
+ assert_equal "'hello' + 'world'\r\n=> \"helloworld\"\r\n", repl.run("'hello' + 'world'\n")
9
9
  end
10
10
 
11
11
  def test_multi_command_read
12
- repl = ReplRunner::PtyParty.new("irb")
12
+ repl = ReplRunner::PtyParty.new("irb --simple-prompt")
13
13
  repl.write("STDOUT.sync = true\n")
14
14
  repl.write("1+1\n")
15
15
  repl.write("exit\n")
16
- assert_equal "STDOUT.sync = true\r\n1+1\r\nexit\r\n2.0.0-p0 :001 > STDOUT.sync = true\r\n => true \r\n2.0.0-p0 :002 > 1+1\r\n => 2 \r\n2.0.0-p0 :003 > exit\r\n", repl.read
16
+ assert_equal "STDOUT.sync = true\r\n1+1\r\nexit\r\n>> STDOUT.sync = true\r\n=> true\r\n>> 1+1\r\n=> 2\r\n>> exit\r\n", repl.read
17
17
  end
18
18
  end
@@ -9,6 +9,20 @@ class ReplRunnerTest < Test::Unit::TestCase
9
9
  repl.run("b = 'bar'") {} # test empty block doesn't throw exceptions
10
10
  repl.run("a * 5") {|r| assert_match 'foofoofoofoofoo', r }
11
11
  end
12
+
13
+ ReplRunner.new(:irb, "irb").run do |repl|
14
+ repl.run('111+111') {|r| assert_match '222', r }
15
+ repl.run("'hello' + 'world'") {|r| assert_match 'helloworld', r }
16
+ repl.run("a = 'foo'")
17
+ repl.run("b = 'bar'") {} # test empty block doesn't throw exceptions
18
+ repl.run("a * 5") {|r| assert_match 'foofoofoofoofoo', r }
19
+ end
20
+ end
21
+
22
+ def test_does_not_have_trailing_prompts
23
+ ReplRunner.new(:irb, "irb ").run do |repl|
24
+ repl.run('111+111') {|r| assert_equal "=> 222", r.strip }
25
+ end
12
26
  end
13
27
 
14
28
  def test_ensure_exit
@@ -31,10 +45,20 @@ class ReplRunnerTest < Test::Unit::TestCase
31
45
  end
32
46
  end
33
47
 
34
- def test_heroku
35
- # ReplRunner.new('rails console', "heroku run rails console -a test-app-1372231309-0734751").run do |repl|
36
- # repl.run("'foo' * 5") {|r| assert_match /foofoofoofoofoo/, r }
37
- # repl.run("'hello ' + 'world'") {|r| assert_match /hello world/, r }
38
- # end
48
+ def test_zipping_commands
49
+ commands = "a = 3\nb = 'foo' * a\nputs b"
50
+ zip = ReplRunner.new(:irb).zip(commands)
51
+ actual = [["a = 3", "=> 3\r"],
52
+ ["b = 'foo' * a", "=> \"foofoofoo\"\r"],
53
+ ["puts b", "foofoofoo\r\n=> nil\r"]]
54
+ assert_equal actual, zip
55
+
56
+ expected = ["a = 3",
57
+ "=> 3\r",
58
+ "b = 'foo' * a",
59
+ "=> \"foofoofoo\"\r",
60
+ "puts b",
61
+ "foofoofoo\r\n=> nil\r"]
62
+ assert_equal expected, zip.flatten
39
63
  end
40
64
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: repl_runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Schneeman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-26 00:00:00.000000000 Z
11
+ date: 2013-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -60,6 +60,8 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - .gitignore
63
+ - .travis.yml
64
+ - CHANGELOG.md
63
65
  - Gemfile
64
66
  - LICENSE
65
67
  - README.md
@@ -99,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
101
  version: '0'
100
102
  requirements: []
101
103
  rubyforge_project:
102
- rubygems_version: 2.0.2
104
+ rubygems_version: 2.0.3
103
105
  signing_key:
104
106
  specification_version: 4
105
107
  summary: Run your REPL like interfaces like never before