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 +4 -4
- data/.travis.yml +9 -0
- data/CHANGELOG.md +10 -0
- data/README.md +2 -2
- data/lib/repl_runner.rb +11 -0
- data/lib/repl_runner/multi_command_parser.rb +8 -5
- data/lib/repl_runner/version.rb +1 -1
- data/test/multi_command_parser_test.rb +11 -1
- data/test/pty_party_test.rb +5 -5
- data/test/repl_runner_test.rb +29 -5
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6202f0b227dbf9c88ab52cf6f10b7294c2f97630
|
4
|
+
data.tar.gz: 0dad45ad6ab909015bda06202b69496a18c6260e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b54cd9a521ec9df9c82086d4b7441ea7d04c3a09ad5fd384dfb2200b922579a0269e05fb5a17195aef7cd4af99bc43a830eb6ac05c93705ccf4c5c9b3d3a21b9
|
7
|
+
data.tar.gz: a963bcb45e11f9b1b74016d996923994a49e2310fbbc8f6023f68e12b719313b85bb788401b257d44d01a09a34cfa5af86f8b38adb843dbd3814a7fb5c031bf7
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# REPL Runner
|
2
2
|
|
3
|
-
Drive irb, bash, or another REPL like environment
|
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
|
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
|
|
data/lib/repl_runner.rb
CHANGED
@@ -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
|
24
|
-
|
25
|
-
|
26
|
-
raise NoResults.new(command, raw) if
|
27
|
-
|
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!
|
data/lib/repl_runner/version.rb
CHANGED
@@ -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
|
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
|
data/test/pty_party_test.rb
CHANGED
@@ -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
|
8
|
-
assert_equal "'hello' + 'world'\r\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\
|
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
|
data/test/repl_runner_test.rb
CHANGED
@@ -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
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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.
|
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-
|
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.
|
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
|