rube 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +1 -0
- data/VERSION +1 -1
- data/bin/rube +10 -2
- data/lib/rube.rb +97 -185
- data/lib/rube/cli.rb +105 -0
- data/test/test_helper.rb +1 -0
- data/test/test_rube.rb +35 -121
- metadata +3 -2
data/Manifest.txt
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/bin/rube
CHANGED
@@ -1,2 +1,10 @@
|
|
1
|
-
#!/bin/
|
2
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Created on 2009-7-20.
|
4
|
+
# Copyright (c) 2009. All rights reserved.
|
5
|
+
|
6
|
+
require File.expand_path(File.dirname(__FILE__) + "/../lib/rube")
|
7
|
+
|
8
|
+
require "rube/cli"
|
9
|
+
|
10
|
+
Rube::CLI.execute(STDOUT, ARGV)
|
data/lib/rube.rb
CHANGED
@@ -30,8 +30,7 @@
|
|
30
30
|
# The equivalent code for the above, when invoked from within a Ruby script, would be:
|
31
31
|
#
|
32
32
|
# require 'rube'
|
33
|
-
# rube = Rube.new
|
34
|
-
# rube.trim_level = 2
|
33
|
+
# rube = Rube.new($stdout, :trim_level=>2)
|
35
34
|
# rube.add_task(:require, 'active_support')
|
36
35
|
# rube.add_task(:require, 'yaml')
|
37
36
|
# rube.add_task(:eval, "document=YAML.load(IO.read('document.yml))" )
|
@@ -47,16 +46,14 @@
|
|
47
46
|
# rube --help
|
48
47
|
#
|
49
48
|
# at the command line for more information.
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
# rube.to_string = true Causes rube.generate to output results to a string
|
54
|
-
# rube.to_file "name" Causes rube.generate to output results to the named file
|
49
|
+
|
50
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
51
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
55
52
|
|
56
53
|
require 'optparse'
|
57
54
|
require 'erb'
|
58
55
|
|
59
|
-
|
56
|
+
module Rube
|
60
57
|
VERSION = %x{cat #{File.dirname(__FILE__)+'/../VERSION'}}.chomp
|
61
58
|
|
62
59
|
# Class and procedure to provide context for evaluation of tasks: ruby inline code, requires, and erb templates
|
@@ -67,212 +64,127 @@ class Rube
|
|
67
64
|
end
|
68
65
|
end
|
69
66
|
|
70
|
-
class
|
71
|
-
def self.exit_code
|
72
|
-
1
|
73
|
-
end
|
67
|
+
class RubeError < RuntimeError
|
74
68
|
end
|
75
69
|
|
76
|
-
class BadArgumentError <
|
77
|
-
def self.exit_code
|
78
|
-
2
|
79
|
-
end
|
70
|
+
class BadArgumentError < RubeError
|
80
71
|
end
|
81
72
|
|
82
|
-
class MissingRequireError <
|
83
|
-
def self.exit_code
|
84
|
-
3
|
85
|
-
end
|
73
|
+
class MissingRequireError < RubeError
|
86
74
|
end
|
87
75
|
|
88
|
-
class MissingTemplateError <
|
89
|
-
def self.exit_code
|
90
|
-
4
|
91
|
-
end
|
76
|
+
class MissingTemplateError < RubeError
|
92
77
|
end
|
93
78
|
|
94
|
-
class ScriptError <
|
95
|
-
def self.exit_code
|
96
|
-
5
|
97
|
-
end
|
79
|
+
class ScriptError < RubeError
|
98
80
|
end
|
99
81
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
def initialize
|
104
|
-
@tasks = []
|
105
|
-
@trim_level = nil
|
106
|
-
self.disable_percent = false
|
107
|
-
self.trim_level = nil
|
108
|
-
@from_command_line = nil
|
109
|
-
@to_string = false
|
110
|
-
@to_file = nil
|
111
|
-
@safety = nil
|
112
|
-
@explicit = false
|
113
|
-
end
|
82
|
+
class Generator
|
83
|
+
attr_accessor :tasks, :safety, :from_command_line
|
84
|
+
attr_reader :trim_level, :trim_mode, :disable_percent
|
114
85
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
@op.separator "Each task may be a template, require or eval (see below). These are processed in the order given,"
|
125
|
-
@op.separator "so results from prior tasks are available to subsequent ones. All variables and constants, including"
|
126
|
-
@op.separator "local variables, are preserved, so their values are available to subsequent tasks."
|
127
|
-
@op.separator ''
|
128
|
-
@op.separator "Tasks:"
|
129
|
-
@op.separator " path/to/template/file Process the specified erb template file"
|
130
|
-
@op.on('-i', '--stdin', "Process the template provided in stdin") do |val|
|
131
|
-
template_count += 1
|
132
|
-
@tasks << [:template, '/dev/stdin']
|
86
|
+
def initialize(stdout=$stdout, options={})
|
87
|
+
@stdout = stdout
|
88
|
+
@tasks = options[:tasks] || []
|
89
|
+
@trim_level = nil
|
90
|
+
self.disable_percent = options[:disable_percent]
|
91
|
+
self.trim_level = options[:trim_level]
|
92
|
+
@from_command_line = options[:from_command_line]
|
93
|
+
@safety = options[:safety]
|
94
|
+
@explicit = options[:explicit]
|
133
95
|
end
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
@op.on('-E', '--[no-]explicit', "All templates must be explicitly provided. Default is false -- rube assumes it should read",
|
139
|
-
"a template from stdin if no templates are specified among the tasks") {|val| @explicit = val }
|
140
|
-
@op.on('-S', '--safe SAFE_LEVEL', Integer, "Set $SAFE (0..4). Default off") do |val|
|
141
|
-
error BadArgumentError, "Invalid --safe level #{val}. Should be 0..4" unless (0..4).include?(val)
|
142
|
-
@safety = val
|
143
|
-
end
|
144
|
-
@op.on('-T', '--trim TRIM_LEVEL', "Set trim level (0..2, or '-'). Default 0") {|trim| tr_level = trim }
|
145
|
-
@op.on('-P', '--[no-]disable-percent', "Disable '%' prefix for erb code. Default false") {|val| self.disable_percent = val }
|
146
|
-
@op.on_tail('-h', '--help', "Produce this help list") {|val| help }
|
147
|
-
@op.on_tail('-v', '--version', "Show version") {|val| puts VERSION; exit 0 }
|
148
|
-
begin
|
149
|
-
@templates = @op.order!(args) do |template|
|
150
|
-
template_count += 1
|
151
|
-
@tasks << [:template, template]
|
152
|
-
end
|
153
|
-
rescue OptionParser::InvalidOption, OptionParser::InvalidArgument => e
|
154
|
-
$stderr.puts e.to_s
|
155
|
-
help BadArgumentError
|
96
|
+
|
97
|
+
def disable_percent=(disable_percent)
|
98
|
+
@disable_percent = disable_percent
|
99
|
+
@trim_mode = trim_mode_opt(@trim_level)
|
156
100
|
end
|
157
|
-
@tasks << [:template, '/dev/stdin'] if !@explicit && template_count == 0
|
158
|
-
self.trim_level = tr_level
|
159
|
-
end
|
160
|
-
|
161
|
-
def disable_percent=(disable_percent)
|
162
|
-
@disable_percent = disable_percent
|
163
|
-
@trim_mode = trim_mode_opt(@trim_level)
|
164
|
-
end
|
165
|
-
|
166
|
-
def trim_level=(trim_level)
|
167
|
-
@trim_level = (trim_level || '0').to_s
|
168
|
-
@trim_mode = trim_mode_opt(@trim_level)
|
169
|
-
end
|
170
101
|
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
mode += case trim_mode.to_s
|
175
|
-
when '0','' then ''
|
176
|
-
when '1' then '>'
|
177
|
-
when '2' then '<>'
|
178
|
-
when '-' then '-'
|
179
|
-
else error BadArgumentError, "Invalid trim mode #{trim_mode}. Should be 0, 1, 2, or -"
|
102
|
+
def trim_level=(trim_level)
|
103
|
+
@trim_level = (trim_level || '0').to_s
|
104
|
+
@trim_mode = trim_mode_opt(@trim_level)
|
180
105
|
end
|
181
|
-
end
|
182
106
|
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
107
|
+
# Convert command line trim_mode to a form erb will understand
|
108
|
+
def trim_mode_opt(trim_mode)
|
109
|
+
mode = disable_percent ? '' : '%'
|
110
|
+
mode += case trim_mode.to_s
|
111
|
+
when '0','' then ''
|
112
|
+
when '1' then '>'
|
113
|
+
when '2' then '<>'
|
114
|
+
when '-' then '-'
|
115
|
+
else error BadArgumentError, "Invalid trim mode #{trim_mode}. Should be 0, 1, 2, or -"
|
116
|
+
end
|
193
117
|
end
|
194
|
-
self # Allow chaining
|
195
|
-
end
|
196
118
|
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
elsif @to_file
|
205
|
-
$stdout = File.new @to_file, 'w'
|
119
|
+
# Add a task
|
120
|
+
def add_task(type, task)
|
121
|
+
case type
|
122
|
+
when :require, :eval, :template then @tasks << [type, task]
|
123
|
+
else raise "Invalid task type #{type.inspect}"
|
124
|
+
end
|
125
|
+
self # Allow chaining
|
206
126
|
end
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
$stdout
|
127
|
+
|
128
|
+
# Run all the tasks
|
129
|
+
def generate
|
130
|
+
@eval_context = EvalContext.new
|
131
|
+
@binding = @eval_context.sandbox
|
132
|
+
saved_stdout, $stdout = $stdout, @stdout
|
133
|
+
@tasks.each {|p| execute p }
|
134
|
+
nil
|
135
|
+
ensure
|
136
|
+
$stdout = saved_stdout
|
213
137
|
end
|
214
|
-
res
|
215
|
-
ensure
|
216
|
-
$stdout = saved_stdout
|
217
|
-
end
|
218
138
|
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
139
|
+
# Execute a single task
|
140
|
+
def execute(p)
|
141
|
+
case p.first
|
142
|
+
when :require
|
143
|
+
protected_require(p.last)
|
144
|
+
when :eval
|
145
|
+
protected_eval(p.last)
|
146
|
+
when :template
|
147
|
+
protected_erb(p.last)
|
148
|
+
else
|
149
|
+
raise "Unexpected task #{p.inspect}"
|
150
|
+
end
|
230
151
|
end
|
231
|
-
end
|
232
152
|
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
153
|
+
# Load a ruby file or library
|
154
|
+
def protected_require(r)
|
155
|
+
@eval_context.instance_eval {require r}
|
156
|
+
rescue LoadError
|
157
|
+
error MissingRequireError, "Can't find require file #{r}"
|
158
|
+
end
|
239
159
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
160
|
+
# Evaluate inline ruby code
|
161
|
+
def protected_eval(src)
|
162
|
+
bind_at = @binding
|
163
|
+
@eval_context.instance_eval{eval src, bind_at}
|
164
|
+
rescue => e
|
165
|
+
error ScriptError, "Error executing source:\n#{src}\n\n#{e.to_s}"
|
166
|
+
end
|
247
167
|
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
168
|
+
# Process an erb template
|
169
|
+
def protected_erb(template)
|
170
|
+
error MissingTemplateError, "Can't find template file #{template}" unless File.exists?(template)
|
171
|
+
source = File.read(template)
|
172
|
+
@stdout.puts ERB.new(source, @safety, @trim_mode).result(@binding)
|
173
|
+
end
|
254
174
|
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
175
|
+
# Issue an error message or exception
|
176
|
+
def self.error(exception, msg)
|
177
|
+
raise exception, msg
|
178
|
+
end
|
179
|
+
|
180
|
+
def error(exception, msg)
|
181
|
+
self.class.error exception, msg
|
182
|
+
end
|
261
183
|
end
|
262
184
|
|
263
185
|
# Convenience method: create a Rube object and use it to execute command line-style arguments
|
264
|
-
def self.generate(
|
265
|
-
rube = new
|
266
|
-
options = {}
|
267
|
-
if args.last.is_a?(Hash)
|
268
|
-
options = args.pop
|
269
|
-
end
|
270
|
-
rube.from_command_line = options[:from_command_line]
|
271
|
-
rube.process_options(*args)
|
186
|
+
def self.generate(stdout, options)
|
187
|
+
rube = Generator.new(stdout, options)
|
272
188
|
rube.generate
|
273
189
|
end
|
274
190
|
end
|
275
|
-
|
276
|
-
if $0 == __FILE__
|
277
|
-
Rube.generate(*(ARGV << {:from_command_line=>true}))
|
278
|
-
end
|
data/lib/rube/cli.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
module Rube
|
4
|
+
class ExitStatus
|
5
|
+
HELP_REQUESTED = 1
|
6
|
+
BAD_ARGUMENT = 2
|
7
|
+
MISSING_REQUIRE = 3
|
8
|
+
MISSING_TEMPLATE = 4
|
9
|
+
SCRIPT_ERROR = 5
|
10
|
+
end
|
11
|
+
|
12
|
+
class CLI
|
13
|
+
def self.execute(stdout, arguments=[])
|
14
|
+
|
15
|
+
# NOTE: the option -p/--path= is given as an example, and should be replaced in your application.
|
16
|
+
|
17
|
+
options = {
|
18
|
+
:tasks => [],
|
19
|
+
:explicit => false,
|
20
|
+
:trim_level => '0',
|
21
|
+
:disable_percent => false,
|
22
|
+
:safe => nil,
|
23
|
+
:from_command_line => true
|
24
|
+
}
|
25
|
+
|
26
|
+
mandatory_options = %w( )
|
27
|
+
template_count = 0
|
28
|
+
tr_level = nil
|
29
|
+
parser = OptionParser.new do |opts|
|
30
|
+
opts = OptionParser.new
|
31
|
+
|
32
|
+
opts.banner = <<-BANNER.gsub(/^ /,'')
|
33
|
+
Process erb templates along with other ruby tasks
|
34
|
+
|
35
|
+
Usage: #{File.basename($0)} [options] tasks
|
36
|
+
|
37
|
+
Each task may be a template, require or eval (see below). These are processed in the order given,
|
38
|
+
so results from prior tasks are available to subsequent ones. All variables and constants, including
|
39
|
+
local variables, are preserved, so their values are available to subsequent tasks.
|
40
|
+
BANNER
|
41
|
+
|
42
|
+
opts.separator ''
|
43
|
+
opts.separator "Tasks:"
|
44
|
+
opts.separator " path/to/template/file Process the specified erb template file"
|
45
|
+
opts.on('-i', '--stdin', "Process the template provided in stdin") do |val|
|
46
|
+
template_count += 1
|
47
|
+
options[:tasks] << [:template, '/dev/stdin']
|
48
|
+
end
|
49
|
+
opts.on('-r', '--require path/to/ruby/file', "Load a ruby library or source code file") {|val| options[:tasks] << [:require, val] }
|
50
|
+
opts.on('-e', '--eval "ruby code"', "Evaluate some inline ruby code"){|src| options[:tasks] << [:eval, src] }
|
51
|
+
opts.separator ''
|
52
|
+
opts.separator "Options:"
|
53
|
+
opts.on('-E', '--[no-]explicit', "All templates must be explicitly provided. Default is false -- rube assumes it should read",
|
54
|
+
"a template from stdin if no templates are specified among the tasks") {|val| options[:explicit] = val }
|
55
|
+
opts.on('-S', '--safe SAFE_LEVEL', Integer, "Set $SAFE (0..4). Default off") do |val|
|
56
|
+
help stdout, opts, "Invalid --safe level #{val}. Should be 0..4", ExitStatus::BAD_ARGUMENT unless (0..4).include?(val)
|
57
|
+
options[:safety] = val
|
58
|
+
end
|
59
|
+
opts.on('-T', '--trim TRIM_LEVEL', "Set trim level (0..2, or '-'). Default 0") {|trim| tr_level = trim }
|
60
|
+
opts.on('-P', '--[no-]disable-percent', "Disable '%' prefix for erb code. Default false") {|val| options[:disable_percent] = val }
|
61
|
+
opts.on_tail('-h', '--help', "Produce this help list") {|val| help stdout, opts }
|
62
|
+
opts.on_tail('-v', '--version', "Show version") {|val| puts VERSION; exit 0 }
|
63
|
+
begin
|
64
|
+
@templates = opts.order!(arguments) do |template|
|
65
|
+
template_count += 1
|
66
|
+
options[:tasks] << [:template, template]
|
67
|
+
end
|
68
|
+
rescue OptionParser::InvalidOption, OptionParser::InvalidArgument => e
|
69
|
+
help stdout, opts, e.to_s, ExitStatus::BAD_ARGUMENT
|
70
|
+
end
|
71
|
+
options[:tasks] << [:template, '/dev/stdin'] if !options[:explicit] && template_count == 0
|
72
|
+
options[:trim_level] = tr_level
|
73
|
+
|
74
|
+
if mandatory_options && mandatory_options.find { |option| options[option.to_sym].nil? }
|
75
|
+
help stdout, opts
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
begin
|
80
|
+
Rube.generate(stdout, options)
|
81
|
+
rescue BadArgumentError => e
|
82
|
+
quit stdout, e.to_s, ExitStatus::BAD_ARGUMENT
|
83
|
+
rescue MissingRequireError => e
|
84
|
+
quit stdout, e.to_s, ExitStatus::MISSING_REQUIRE
|
85
|
+
rescue MissingTemplateError => e
|
86
|
+
quit stdout, e.to_s, ExitStatus::MISSING_TEMPLATE
|
87
|
+
rescue ScriptError => e
|
88
|
+
quit stdout, e.to_s, ExitStatus::SCRIPT_ERROR
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.help(stdout, opt_parser, msg = nil, exit_code = ExitStatus::HELP_REQUESTED)
|
93
|
+
m = msg.to_s
|
94
|
+
m += "\n" unless m == ''
|
95
|
+
m += opt_parser.to_s
|
96
|
+
quit stdout, m, exit_code
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.quit(stdout, msg=nil,exit_code = 0)
|
100
|
+
stdout.puts msg
|
101
|
+
exit exit_code
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
data/test/test_helper.rb
CHANGED
data/test/test_rube.rb
CHANGED
@@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/test_helper.rb'
|
|
3
3
|
class TestRubeBasic < Test::Unit::TestCase
|
4
4
|
|
5
5
|
def setup
|
6
|
-
@new_rube = Rube.new
|
6
|
+
@new_rube = Rube::Generator.new
|
7
7
|
end
|
8
8
|
|
9
9
|
def test_truth
|
@@ -11,7 +11,7 @@ class TestRubeBasic < Test::Unit::TestCase
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_creates_rube_object
|
14
|
-
assert_instance_of Rube, @new_rube, "Rube.new does not create Rube object"
|
14
|
+
assert_instance_of Rube::Generator, @new_rube, "Rube:Generator.new does not create Rube::Generator object"
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_rube_object_has_tasks
|
@@ -65,14 +65,6 @@ class TestRubeBasic < Test::Unit::TestCase
|
|
65
65
|
def test_rube_object_trim_mode
|
66
66
|
assert_respond_to @new_rube, :trim_mode
|
67
67
|
end
|
68
|
-
|
69
|
-
def test_rube_object_to_string
|
70
|
-
assert_respond_to @new_rube, :to_string
|
71
|
-
end
|
72
|
-
|
73
|
-
def test_rube_object_to_file
|
74
|
-
assert_respond_to @new_rube, :to_file
|
75
|
-
end
|
76
68
|
|
77
69
|
def test_rube_object_go
|
78
70
|
assert_respond_to @new_rube, :generate
|
@@ -86,7 +78,7 @@ end
|
|
86
78
|
class TestRubeTaskManipulation < Test::Unit::TestCase
|
87
79
|
|
88
80
|
def setup
|
89
|
-
@empty_rube = Rube.new
|
81
|
+
@empty_rube = Rube::Generator.new
|
90
82
|
end
|
91
83
|
|
92
84
|
def test_empty_rube_object_has_no_tasks
|
@@ -143,7 +135,7 @@ end
|
|
143
135
|
class TestRubeSettings < Test::Unit::TestCase
|
144
136
|
|
145
137
|
def setup
|
146
|
-
@empty_rube = Rube.new
|
138
|
+
@empty_rube = Rube::Generator.new
|
147
139
|
end
|
148
140
|
|
149
141
|
def test_rube_default_disable_percent
|
@@ -172,31 +164,13 @@ class TestRubeSettings < Test::Unit::TestCase
|
|
172
164
|
@empty_rube.from_command_line = 'foo'
|
173
165
|
assert_equal 'foo', @empty_rube.from_command_line
|
174
166
|
end
|
175
|
-
|
176
|
-
def test_rube_default_to_string
|
177
|
-
assert_equal false, !!@empty_rube.to_string
|
178
|
-
end
|
179
|
-
|
180
|
-
def test_rube_object_preserves_to_string
|
181
|
-
@empty_rube.to_string = 'foo'
|
182
|
-
assert_equal 'foo', @empty_rube.to_string
|
183
|
-
end
|
184
|
-
|
185
|
-
def test_rube_default_to_file
|
186
|
-
assert_equal nil, @empty_rube.to_file
|
187
|
-
end
|
188
|
-
|
189
|
-
def test_rube_object_preserves_to_file
|
190
|
-
@empty_rube.to_file = 'foo'
|
191
|
-
assert_equal 'foo', @empty_rube.to_file
|
192
|
-
end
|
193
167
|
end
|
194
168
|
|
195
169
|
class TestRubeTrimLevel < Test::Unit::TestCase
|
196
170
|
|
197
171
|
def setup
|
198
|
-
@empty_rube = Rube.new
|
199
|
-
@disabled_rube = Rube.new
|
172
|
+
@empty_rube = Rube::Generator.new
|
173
|
+
@disabled_rube = Rube::Generator.new
|
200
174
|
@disabled_rube.disable_percent = true
|
201
175
|
end
|
202
176
|
|
@@ -274,113 +248,78 @@ end
|
|
274
248
|
class TestRubeProcessing < Test::Unit::TestCase
|
275
249
|
|
276
250
|
def setup
|
277
|
-
@
|
278
|
-
@
|
279
|
-
end
|
280
|
-
|
281
|
-
def teardown
|
282
|
-
$stdout = @save_stdout
|
283
|
-
end
|
284
|
-
|
285
|
-
def test_simple_eval_to_stdout
|
286
|
-
StringIO.open do |out|
|
287
|
-
$stdout = out
|
288
|
-
@rube.add_task :eval, 'puts "foo"'
|
289
|
-
res = @rube.generate
|
290
|
-
assert_equal "foo\n", out.string, "Failed to produce expected output on $stdout"
|
291
|
-
assert_equal nil, res, "Without redirection, rube.generate should return nil"
|
292
|
-
end
|
251
|
+
@out = StringIO.new
|
252
|
+
@rube = Rube::Generator.new(@out)
|
293
253
|
end
|
294
254
|
|
295
|
-
def
|
296
|
-
@rube.to_string = true
|
297
|
-
@rube.add_task :eval, 'puts "foo"'
|
298
|
-
out = @rube.generate
|
299
|
-
assert_equal "foo\n", out, "Failed to output to string"
|
300
|
-
end
|
301
|
-
|
302
|
-
def test_simple_eval_to_file
|
303
|
-
out = Tempfile.new('testout')
|
304
|
-
@rube.to_file = out.path
|
255
|
+
def test_simple_eval
|
305
256
|
@rube.add_task :eval, 'puts "foo"'
|
306
257
|
res = @rube.generate
|
307
|
-
out.
|
308
|
-
output = File.read(out.path)
|
309
|
-
assert_equal "foo\n", output, "Failed to produce expected output on file #{out.path}"
|
258
|
+
assert_equal "foo\n", @out.string, "Failed to produce expected output on $stdout"
|
310
259
|
assert_equal nil, res, "Without redirection, rube.generate should return nil"
|
311
260
|
end
|
312
261
|
|
313
262
|
def test_process_require_task
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
# If English doesn't load properly, then $PID is nil; if it does, then it's a numeric process id
|
320
|
-
assert_match(/^\d+$/, out.string.chomp, "Failed to load require file")
|
321
|
-
end
|
263
|
+
@rube.add_task :require, 'English'
|
264
|
+
@rube.add_task :eval, 'puts $PID'
|
265
|
+
@rube.generate
|
266
|
+
# If English doesn't load properly, then $PID is nil; if it does, then it's a numeric process id
|
267
|
+
assert_match(/^\d+$/, @out.string.chomp, "Failed to load require file")
|
322
268
|
end
|
323
269
|
|
324
270
|
def test_process_template_task
|
325
|
-
@rube.to_string = true
|
326
271
|
@rube.add_task :template, 'templates/test1'
|
327
|
-
|
328
|
-
assert_equal "foo\nblurg\nblurble\n\nfarb\n\n", out, "Failed to produce expected result from test1 template"
|
272
|
+
@rube.generate
|
273
|
+
assert_equal "foo\nblurg\nblurble\n\nfarb\n\n", @out.string, "Failed to produce expected result from test1 template"
|
329
274
|
end
|
330
275
|
|
331
276
|
def test_process_template_task_with_disabled_percent
|
332
|
-
@rube.to_string = true
|
333
277
|
@rube.disable_percent = true
|
334
278
|
@rube.add_task :template, 'templates/test1'
|
335
|
-
|
279
|
+
@rube.generate
|
336
280
|
# Note: test1 template produces different results with % lines disabled
|
337
|
-
assert_equal "foo\nblurg\nblurble\n\n% z = 'farb'\nbarf\n\n", out, "Failed to produce expected result from test1 template with percentage lines disabled"
|
281
|
+
assert_equal "foo\nblurg\nblurble\n\n% z = 'farb'\nbarf\n\n", @out.string, "Failed to produce expected result from test1 template with percentage lines disabled"
|
338
282
|
end
|
339
283
|
|
340
284
|
def test_process_template_task_with_trim_level_1
|
341
|
-
@rube.to_string = true
|
342
285
|
@rube.trim_level = 1
|
343
286
|
@rube.add_task :template, 'templates/test1'
|
344
|
-
|
345
|
-
assert_equal "foo\nblurgblurblefarb\n", out, "Failed to produce expected result from test1 template"
|
287
|
+
@rube.generate
|
288
|
+
assert_equal "foo\nblurgblurblefarb\n", @out.string, "Failed to produce expected result from test1 template"
|
346
289
|
end
|
347
290
|
|
348
291
|
def test_process_template_task_with_trim_level_2
|
349
|
-
@rube.to_string = true
|
350
292
|
@rube.trim_level = 2
|
351
293
|
@rube.add_task :template, 'templates/test1'
|
352
294
|
out = @rube.generate
|
353
|
-
assert_equal "foo\nblurgblurble\nfarb\n", out, "Failed to produce expected result from test1 template"
|
295
|
+
assert_equal "foo\nblurgblurble\nfarb\n", @out.string, "Failed to produce expected result from test1 template"
|
354
296
|
end
|
355
297
|
|
356
298
|
def test_process_template_with_persistent_variable
|
357
|
-
@rube.to_string = true
|
358
299
|
@rube.add_task :eval, 'b="foo"'
|
359
300
|
@rube.add_task :template, 'templates/test2'
|
360
|
-
|
361
|
-
assert_equal "bar\nfoo*2\n", out, "Failed to produce expected result from test2 template with passed variable value"
|
301
|
+
@rube.generate
|
302
|
+
assert_equal "bar\nfoo*2\n", @out.string, "Failed to produce expected result from test2 template with passed variable value"
|
362
303
|
end
|
363
304
|
end
|
364
305
|
|
365
306
|
class TestRubeErrors < Test::Unit::TestCase
|
366
307
|
def setup
|
367
|
-
@
|
308
|
+
@out = StringIO.new
|
309
|
+
@rube = Rube::Generator.new(@out)
|
368
310
|
end
|
369
311
|
|
370
312
|
def test_bad_eval
|
371
|
-
@rube.to_string = true
|
372
313
|
@rube.add_task :eval, 'foo'
|
373
314
|
assert_raises(Rube::ScriptError) { @rube.generate }
|
374
315
|
end
|
375
316
|
|
376
317
|
def test_bad_require
|
377
|
-
@rube.to_string = true
|
378
318
|
@rube.add_task :require, 'foo'
|
379
319
|
assert_raises(Rube::MissingRequireError) { @rube.generate }
|
380
320
|
end
|
381
321
|
|
382
322
|
def test_bad_template
|
383
|
-
@rube.to_string = true
|
384
323
|
@rube.add_task :template, 'foo'
|
385
324
|
assert_raises(Rube::MissingTemplateError) { @rube.generate }
|
386
325
|
end
|
@@ -427,30 +366,17 @@ class TestRubeCommandLineIndirectly < Test::Unit::TestCase
|
|
427
366
|
#
|
428
367
|
# Not really an exhaustive test, but if this works, things should be okay
|
429
368
|
#
|
430
|
-
|
431
|
-
def setup
|
432
|
-
@rube = Rube.new
|
433
|
-
@save_stdout = $stdout
|
434
|
-
end
|
435
|
-
|
436
|
-
def teardown
|
437
|
-
$stdout = @save_stdout
|
438
|
-
end
|
439
369
|
|
440
370
|
def test_eval
|
441
371
|
StringIO.open do |out|
|
442
|
-
|
443
|
-
@rube.process_options('-E', '-e', 'puts "foo"')
|
444
|
-
res = @rube.generate
|
372
|
+
res = Rube::CLI.execute(out, ['-E', '-e', 'puts "foo"'])
|
445
373
|
assert_equal "foo\n", out.string, "Failed to produce expected output on $stdout"
|
446
374
|
end
|
447
375
|
end
|
448
376
|
|
449
377
|
def test_process_require_task
|
450
378
|
StringIO.open do |out|
|
451
|
-
|
452
|
-
@rube.process_options('-E', '-r', 'English', '-e', 'puts $PID')
|
453
|
-
@rube.generate
|
379
|
+
res = Rube::CLI.execute(out, ['-E', '-r', 'English', '-e', 'puts $PID'])
|
454
380
|
# If English doesn't load properly, then $PID is nil; if it does, then it's a numeric process id
|
455
381
|
assert_match(/^\d+$/, out.string.chomp, "Failed to load require file")
|
456
382
|
end
|
@@ -458,9 +384,7 @@ class TestRubeCommandLineIndirectly < Test::Unit::TestCase
|
|
458
384
|
|
459
385
|
def test_process_require_task_long_form
|
460
386
|
StringIO.open do |out|
|
461
|
-
|
462
|
-
@rube.process_options('--explicit', '--require', 'English', '--eval', 'puts $PID')
|
463
|
-
@rube.generate
|
387
|
+
res = Rube::CLI.execute(out, ['--explicit', '--require', 'English', '--eval', 'puts $PID'])
|
464
388
|
# If English doesn't load properly, then $PID is nil; if it does, then it's a numeric process id
|
465
389
|
assert_match(/^\d+$/, out.string.chomp, "Failed to load require file")
|
466
390
|
end
|
@@ -468,18 +392,14 @@ class TestRubeCommandLineIndirectly < Test::Unit::TestCase
|
|
468
392
|
|
469
393
|
def test_process_template_task
|
470
394
|
StringIO.open do |out|
|
471
|
-
|
472
|
-
@rube.process_options('templates/test1')
|
473
|
-
@rube.generate
|
395
|
+
res = Rube::CLI.execute(out, ['templates/test1'])
|
474
396
|
assert_equal "foo\nblurg\nblurble\n\nfarb\n\n", out.string, "Failed to produce expected result from test1 template"
|
475
397
|
end
|
476
398
|
end
|
477
399
|
|
478
400
|
def test_process_template_task_with_disabled_percent
|
479
401
|
StringIO.open do |out|
|
480
|
-
|
481
|
-
@rube.process_options('-P', 'templates/test1')
|
482
|
-
@rube.generate
|
402
|
+
res = Rube::CLI.execute(out, ['-P', 'templates/test1'])
|
483
403
|
# Note: test1 template produces different results with % lines disabled
|
484
404
|
assert_equal "foo\nblurg\nblurble\n\n% z = 'farb'\nbarf\n\n", out.string, "Failed to produce expected result from test1 template with percentage lines disabled"
|
485
405
|
end
|
@@ -487,27 +407,21 @@ class TestRubeCommandLineIndirectly < Test::Unit::TestCase
|
|
487
407
|
|
488
408
|
def test_process_template_task_with_trim_level_1
|
489
409
|
StringIO.open do |out|
|
490
|
-
|
491
|
-
@rube.process_options('--trim', '1', 'templates/test1')
|
492
|
-
@rube.generate
|
410
|
+
res = Rube::CLI.execute(out, ['--trim', '1', 'templates/test1'])
|
493
411
|
assert_equal "foo\nblurgblurblefarb\n", out.string, "Failed to produce expected result from test1 template"
|
494
412
|
end
|
495
413
|
end
|
496
414
|
|
497
415
|
def test_process_template_task_with_trim_level_2
|
498
416
|
StringIO.open do |out|
|
499
|
-
|
500
|
-
@rube.process_options('--trim', '2', 'templates/test1')
|
501
|
-
@rube.generate
|
417
|
+
res = Rube::CLI.execute(out, ['--trim', '2', 'templates/test1'])
|
502
418
|
assert_equal "foo\nblurgblurble\nfarb\n", out.string, "Failed to produce expected result from test1 template"
|
503
419
|
end
|
504
420
|
end
|
505
421
|
|
506
422
|
def test_process_template_with_persistent_variable
|
507
423
|
StringIO.open do |out|
|
508
|
-
|
509
|
-
@rube.process_options('--eval', 'b="foo"', 'templates/test2')
|
510
|
-
@rube.generate
|
424
|
+
res = Rube::CLI.execute(out, ['--eval', 'b="foo"', 'templates/test2'])
|
511
425
|
assert_equal "bar\nfoo*2\n", out.string, "Failed to produce expected result from test2 template with passed variable value"
|
512
426
|
end
|
513
427
|
end
|
@@ -529,7 +443,7 @@ class TestRubeCommandLineDirectly < Test::Unit::TestCase
|
|
529
443
|
end
|
530
444
|
|
531
445
|
def test_process_template_task
|
532
|
-
res = `rube
|
446
|
+
res = `rube templates/test1` # Not sure why it insists on the '.rb' here, but it does...
|
533
447
|
assert_equal "foo\nblurg\nblurble\n\nfarb\n\n", res, "Failed to produce expected result from test1 template"
|
534
448
|
end
|
535
449
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rube
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard LeBer
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-07-
|
12
|
+
date: 2009-07-23 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -59,6 +59,7 @@ files:
|
|
59
59
|
- VERSION
|
60
60
|
- bin/rube
|
61
61
|
- lib/rube.rb
|
62
|
+
- lib/rube/cli.rb
|
62
63
|
- script/console
|
63
64
|
- script/destroy
|
64
65
|
- script/generate
|