reality-generators 1.11.0 → 1.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/reality/generators.rb +1 -0
- data/lib/reality/generators/runner.rb +233 -0
- data/reality-generators.gemspec +1 -1
- data/test/generators/gentest.rb +90 -0
- data/test/generators/test_runner.rb +229 -0
- data/test/helper.rb +7 -3
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6449615da8a7084e231edf77e9846601f1b8ac55
|
4
|
+
data.tar.gz: e15cf1793acaf16344595204197d8d34e1e6882d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a2641ffdf3ffffef5120a76debe8a462a98c5d79b3a271f48f56ca32c43a2406db01aaa3e738fe468d34668669494a9b07eac3450635b79e1085dd05e809a8c
|
7
|
+
data.tar.gz: 589ed998dfde9b98773a7ffb1d69ab212812af37db233975de9f3b0cecc4a115f5aed751a78250d5e70feaca5866808d5197a7dea35f3849147f4584346f31b1
|
data/lib/reality/generators.rb
CHANGED
@@ -30,5 +30,6 @@ require 'reality/generators/generator'
|
|
30
30
|
require 'reality/generators/standard_template_set'
|
31
31
|
require 'reality/generators/standard_artifact_dsl'
|
32
32
|
require 'reality/generators/rake_integration'
|
33
|
+
require 'reality/generators/runner'
|
33
34
|
|
34
35
|
require 'reality/generators/buildr_integration'
|
@@ -0,0 +1,233 @@
|
|
1
|
+
#
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
#
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
#
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
9
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
# See the License for the specific language governing permissions and
|
12
|
+
# limitations under the License.
|
13
|
+
#
|
14
|
+
|
15
|
+
module Reality #nodoc
|
16
|
+
module Generators #nodoc
|
17
|
+
# A base class for writing command line tasks that load one or more
|
18
|
+
# descriptors and run zer or more generators/template_sets
|
19
|
+
class BaseRunner
|
20
|
+
EXIT_CODE_SUCCESS = 0
|
21
|
+
EXIT_CODE_UNABLE_TO_PARSE_ARGS = 50
|
22
|
+
EXIT_CODE_UNEXPECTED_ARGS = 51
|
23
|
+
EXIT_CODE_DESCRIPTOR_NO_EXIST = 52
|
24
|
+
EXIT_CODE_NO_ELEMENT_NAME_SPECIFIED = 53
|
25
|
+
EXIT_CODE_ELEMENT_NAME_NO_EXIST = 54
|
26
|
+
|
27
|
+
def initialize
|
28
|
+
@descriptors = []
|
29
|
+
@generators = self.default_generators
|
30
|
+
@target_dir = nil
|
31
|
+
@element_name = nil
|
32
|
+
@element_type_name_char_code = nil
|
33
|
+
@verbose = false
|
34
|
+
@debug = false
|
35
|
+
@tool_name = File.basename($PROGRAM_NAME)
|
36
|
+
end
|
37
|
+
|
38
|
+
def default_generators
|
39
|
+
[]
|
40
|
+
end
|
41
|
+
|
42
|
+
def default_descriptor
|
43
|
+
raise 'default_descriptor not implemented'
|
44
|
+
end
|
45
|
+
|
46
|
+
def element_type_name
|
47
|
+
raise 'repository not implemented'
|
48
|
+
end
|
49
|
+
|
50
|
+
def log_container
|
51
|
+
raise 'log_container not implemented'
|
52
|
+
end
|
53
|
+
|
54
|
+
def instance_container
|
55
|
+
raise 'instance_container not implemented'
|
56
|
+
end
|
57
|
+
|
58
|
+
def template_set_container
|
59
|
+
raise 'template_set_container not implemented'
|
60
|
+
end
|
61
|
+
|
62
|
+
def additional_loggers
|
63
|
+
[]
|
64
|
+
end
|
65
|
+
|
66
|
+
def element_type_name_char_code
|
67
|
+
@element_type_name_char_code || self.element_type_name[0, 1]
|
68
|
+
end
|
69
|
+
|
70
|
+
def default_target_dir
|
71
|
+
'generated'
|
72
|
+
end
|
73
|
+
|
74
|
+
attr_writer :verbose
|
75
|
+
|
76
|
+
def verbose?
|
77
|
+
!!@verbose
|
78
|
+
end
|
79
|
+
|
80
|
+
attr_writer :debug
|
81
|
+
|
82
|
+
def debug?
|
83
|
+
!!@debug
|
84
|
+
end
|
85
|
+
|
86
|
+
attr_writer :target_dir
|
87
|
+
|
88
|
+
def target_dir
|
89
|
+
@target_dir || self.default_target_dir
|
90
|
+
end
|
91
|
+
|
92
|
+
attr_accessor :generators
|
93
|
+
|
94
|
+
attr_accessor :descriptors
|
95
|
+
|
96
|
+
attr_accessor :element_name
|
97
|
+
|
98
|
+
attr_accessor :tool_name
|
99
|
+
|
100
|
+
def run
|
101
|
+
require 'optparse'
|
102
|
+
|
103
|
+
opt_parser = OptionParser.new do |opt|
|
104
|
+
opt.banner = "Usage: #{tool_name} [OPTIONS]"
|
105
|
+
opt.separator ''
|
106
|
+
opt.separator 'Options'
|
107
|
+
|
108
|
+
opt.on('-d', '--descriptor FILENAME', "the filename of a descriptor to be loaded. Multiple descriptors may be loaded. Defaults to 'resources.rb' if none specified.") do |arg|
|
109
|
+
self.descriptors << arg
|
110
|
+
end
|
111
|
+
|
112
|
+
opt.on("-#{self.element_type_name_char_code}",
|
113
|
+
"--#{self.element_type_name} NAME",
|
114
|
+
"the name of the #{self.element_type_name} to load. Defaults to the the name of the only #{element_type_name} if there is only one #{self.element_type_name} defined by the descriptors, otherwise must be specified.") do |arg|
|
115
|
+
self.element_name = arg
|
116
|
+
end
|
117
|
+
|
118
|
+
opt.on('-g', '--generators GENERATORS', "the comma separated list of generators to run. Defaults to #{default_generators.inspect}") do |arg|
|
119
|
+
self.generators += arg.split(',').collect { |g| g.to_sym }
|
120
|
+
end
|
121
|
+
|
122
|
+
opt.on('-t', '--target-dir DIR', "the directory into which to generate artifacts. Defaults to '#{self.default_target_dir}'.") do |arg|
|
123
|
+
self.target_dir = arg
|
124
|
+
end
|
125
|
+
|
126
|
+
opt.on('-v', '--verbose', 'turn on verbose logging.') do
|
127
|
+
self.verbose = true
|
128
|
+
end
|
129
|
+
|
130
|
+
opt.on('--debug', 'turn on debug logging.') do
|
131
|
+
self.verbose = true
|
132
|
+
self.debug = true
|
133
|
+
end
|
134
|
+
|
135
|
+
opt.on('-h', '--help', 'help') do
|
136
|
+
puts opt_parser
|
137
|
+
exit(EXIT_CODE_SUCCESS)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
args = ARGV.dup
|
142
|
+
begin
|
143
|
+
opt_parser.parse!(args)
|
144
|
+
rescue => e
|
145
|
+
puts "Error: #{e.message}"
|
146
|
+
exit(EXIT_CODE_UNABLE_TO_PARSE_ARGS)
|
147
|
+
end
|
148
|
+
|
149
|
+
if args.length != 0
|
150
|
+
puts "Unexpected arguments #{args.inspect} passed to command"
|
151
|
+
puts opt_parser
|
152
|
+
exit(EXIT_CODE_UNEXPECTED_ARGS)
|
153
|
+
end
|
154
|
+
|
155
|
+
loggers = [self.log_container.const_get(:Logger), Reality::Generators::Logger] + self.additional_loggers
|
156
|
+
|
157
|
+
log_level = debug? ? ::Logger::DEBUG : verbose? ? ::Logger::INFO : ::Logger::WARN
|
158
|
+
Reality::Logging.set_levels(log_level, *loggers)
|
159
|
+
|
160
|
+
if 0 == self.descriptors.size
|
161
|
+
puts "No descriptor specified. Defaulting to #{default_descriptor}"
|
162
|
+
self.descriptors << default_descriptor
|
163
|
+
end
|
164
|
+
|
165
|
+
if verbose?
|
166
|
+
puts "#{Reality::Naming.humanize(self.element_type_name)} Name: #{self.element_name || 'Unspecified'}"
|
167
|
+
puts "Target Dir: #{self.target_dir}"
|
168
|
+
if self.descriptors.size == 1
|
169
|
+
puts "Descriptor: #{self.descriptors[0]}"
|
170
|
+
else
|
171
|
+
puts 'Descriptors:'
|
172
|
+
self.descriptors.each do |descriptor|
|
173
|
+
puts " * #{descriptor}"
|
174
|
+
end
|
175
|
+
end
|
176
|
+
puts 'Generators:'
|
177
|
+
self.generators.each do |generator|
|
178
|
+
puts " * #{generator}"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
self.descriptors.each do |descriptor|
|
183
|
+
puts "Loading descriptor: #{descriptor}" if verbose?
|
184
|
+
filename = File.expand_path(descriptor)
|
185
|
+
unless File.exist?(filename)
|
186
|
+
puts "Descriptor file #{filename} does not exist"
|
187
|
+
exit(EXIT_CODE_DESCRIPTOR_NO_EXIST)
|
188
|
+
end
|
189
|
+
load_descriptor(filename)
|
190
|
+
puts "Descriptor loaded: #{descriptor}" if verbose?
|
191
|
+
end
|
192
|
+
|
193
|
+
unless self.element_name
|
194
|
+
element_names = self.instance_container.send(Reality::Naming.pluralize(self.element_type_name).to_sym).collect { |r| r.name }
|
195
|
+
if element_names.size == 1
|
196
|
+
self.element_name = element_names[0]
|
197
|
+
puts "Derived default #{Reality::Naming.humanize(self.element_type_name)} name: #{self.element_name}" if verbose?
|
198
|
+
else
|
199
|
+
puts "No #{Reality::Naming.humanize(self.element_type_name).downcase} name specified and #{Reality::Naming.humanize(element_type_name).downcase} name could not be determined. Please specify one of the valid #{Reality::Naming.humanize(self.element_type_name).downcase} names: #{element_names.join(', ')}"
|
200
|
+
exit(EXIT_CODE_NO_ELEMENT_NAME_SPECIFIED)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
unless self.instance_container.send(:"#{self.element_type_name}_by_name?", self.element_name)
|
205
|
+
puts "Specified #{Reality::Naming.humanize(self.element_type_name).downcase} name '#{self.element_name}' does not exist in descriptors."
|
206
|
+
exit(EXIT_CODE_ELEMENT_NAME_NO_EXIST)
|
207
|
+
end
|
208
|
+
|
209
|
+
element = self.instance_container.send(:"#{self.element_type_name}_by_name", self.element_name)
|
210
|
+
|
211
|
+
self.template_set_container.generator.generate(element_type_name.to_sym,
|
212
|
+
element,
|
213
|
+
File.expand_path(self.target_dir),
|
214
|
+
self.generators,
|
215
|
+
nil)
|
216
|
+
|
217
|
+
exit EXIT_CODE_SUCCESS
|
218
|
+
end
|
219
|
+
|
220
|
+
def load_descriptor(filename)
|
221
|
+
pre_load(filename)
|
222
|
+
require filename
|
223
|
+
post_load(filename)
|
224
|
+
end
|
225
|
+
|
226
|
+
def pre_load(filename)
|
227
|
+
end
|
228
|
+
|
229
|
+
def post_load(filename)
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
data/reality-generators.gemspec
CHANGED
@@ -0,0 +1,90 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler/setup'
|
5
|
+
|
6
|
+
require 'reality/generators'
|
7
|
+
|
8
|
+
module GenTest
|
9
|
+
Reality::Logging.configure(GenTest, ::Logger::WARN)
|
10
|
+
|
11
|
+
class Repository < Reality.base_element(:name => true)
|
12
|
+
end
|
13
|
+
|
14
|
+
class RepositoryTemplate < Reality::Generators::SingleFileOutputTemplate
|
15
|
+
def render_to_string(context_binding)
|
16
|
+
eval('"Repository: #{repository.name}"', context_binding)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class RepositoryTemplate2 < Reality::Generators::SingleFileOutputTemplate
|
21
|
+
def render_to_string(context_binding)
|
22
|
+
eval('"Repository: #{repository.name} (Template2)"', context_binding)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module TestTemplateSetContainer
|
27
|
+
class << self
|
28
|
+
include Reality::Generators::TemplateSetContainer
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
TestTemplateSetContainer.target_manager.target(:repository)
|
33
|
+
|
34
|
+
TestTemplateSetContainer.template_set(:test) do |t|
|
35
|
+
RepositoryTemplate.new(t, [], :repository, 'repository.java', 'main/java/#{repository.name}.java')
|
36
|
+
end
|
37
|
+
|
38
|
+
TestTemplateSetContainer.template_set(:test2) do |t|
|
39
|
+
RepositoryTemplate2.new(t, [], :repository, 'repository_t2.java', 'main/java/#{repository.name}Template2.java')
|
40
|
+
end
|
41
|
+
|
42
|
+
class << self
|
43
|
+
def repositories
|
44
|
+
repository_map.values
|
45
|
+
end
|
46
|
+
|
47
|
+
def repository_map
|
48
|
+
@repository_map ||= {}
|
49
|
+
end
|
50
|
+
|
51
|
+
def repository(name)
|
52
|
+
repository = Repository.new(name)
|
53
|
+
self.repository_map[name.to_s] = repository
|
54
|
+
yield repository if block_given?
|
55
|
+
repository
|
56
|
+
end
|
57
|
+
|
58
|
+
def repository_by_name(name)
|
59
|
+
self.repository_map[name.to_s] || (raise "No such repository #{name}")
|
60
|
+
end
|
61
|
+
|
62
|
+
def repository_by_name?(name)
|
63
|
+
!!self.repository_map[name.to_s]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class Runner < Reality::Generators::BaseRunner
|
68
|
+
def default_descriptor
|
69
|
+
'repository.rb'
|
70
|
+
end
|
71
|
+
|
72
|
+
def element_type_name
|
73
|
+
'repository'
|
74
|
+
end
|
75
|
+
|
76
|
+
def log_container
|
77
|
+
GenTest
|
78
|
+
end
|
79
|
+
|
80
|
+
def instance_container
|
81
|
+
GenTest
|
82
|
+
end
|
83
|
+
|
84
|
+
def template_set_container
|
85
|
+
TestTemplateSetContainer
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
GenTest::Runner.new.run
|
@@ -0,0 +1,229 @@
|
|
1
|
+
require File.expand_path('../../helper', __FILE__)
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
class Reality::Generators::TestRunner < Reality::TestCase
|
5
|
+
def test_basic_runner_execution
|
6
|
+
descriptor = "#{temp_dir}/repository.rb"
|
7
|
+
File.write(descriptor, 'GenTest.repository(:MyRepo)')
|
8
|
+
|
9
|
+
target_directory = "#{temp_dir}/generated/erb_template"
|
10
|
+
|
11
|
+
output = run_runner((%W(--descriptor #{descriptor} --generators test --target-dir #{target_directory})))
|
12
|
+
|
13
|
+
repo_file = "#{target_directory}/main/java/MyRepo.java"
|
14
|
+
assert_equal true, File.directory?("#{target_directory}/main/java")
|
15
|
+
assert_equal true, File.exist?(repo_file)
|
16
|
+
assert_equal 'Repository: MyRepo', IO.read(repo_file)
|
17
|
+
assert_equal '', output
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_verbose_runner_execution
|
21
|
+
descriptor = "#{temp_dir}/repository.rb"
|
22
|
+
File.write(descriptor, 'GenTest.repository(:MyRepo)')
|
23
|
+
|
24
|
+
target_directory = "#{temp_dir}/generated/erb_template"
|
25
|
+
|
26
|
+
output = run_runner((%W(--descriptor #{descriptor} --generators test --target-dir #{target_directory} --verbose)))
|
27
|
+
|
28
|
+
target_file = "#{target_directory}/main/java/MyRepo.java"
|
29
|
+
assert_equal 'Repository: MyRepo', IO.read(target_file)
|
30
|
+
|
31
|
+
assert_equal <<OUTPUT, output
|
32
|
+
Repository Name: Unspecified
|
33
|
+
Target Dir: #{target_directory}
|
34
|
+
Descriptor: #{descriptor}
|
35
|
+
Generators:
|
36
|
+
* test
|
37
|
+
Loading descriptor: #{descriptor}
|
38
|
+
Descriptor loaded: #{descriptor}
|
39
|
+
Derived default Repository name: MyRepo
|
40
|
+
Generator completed
|
41
|
+
OUTPUT
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_debug_runner_execution
|
45
|
+
descriptor = "#{temp_dir}/repository.rb"
|
46
|
+
File.write(descriptor, 'GenTest.repository(:MyRepo)')
|
47
|
+
|
48
|
+
target_directory = "#{temp_dir}/generated/erb_template"
|
49
|
+
|
50
|
+
output = run_runner((%W(--descriptor #{descriptor} --generators test --target-dir #{target_directory} --debug)))
|
51
|
+
|
52
|
+
target_file = "#{target_directory}/main/java/MyRepo.java"
|
53
|
+
assert_equal 'Repository: MyRepo', IO.read(target_file)
|
54
|
+
|
55
|
+
assert_equal <<OUTPUT, output
|
56
|
+
Repository Name: Unspecified
|
57
|
+
Target Dir: #{target_directory}
|
58
|
+
Descriptor: #{descriptor}
|
59
|
+
Generators:
|
60
|
+
* test
|
61
|
+
Loading descriptor: #{descriptor}
|
62
|
+
Descriptor loaded: #{descriptor}
|
63
|
+
Derived default Repository name: MyRepo
|
64
|
+
Templates to process: ["test:repository.java"]
|
65
|
+
Evaluating template: test:repository.java
|
66
|
+
Generating test:repository.java for repository MyRepo
|
67
|
+
Generated test:repository.java for repository MyRepo to #{target_file}
|
68
|
+
Generator completed
|
69
|
+
OUTPUT
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_debug_runner_execution_multiple_descriptors_multiple_templates
|
73
|
+
descriptor = "#{temp_dir}/repository.rb"
|
74
|
+
File.write(descriptor, 'GenTest.repository(:MyRepo)')
|
75
|
+
descriptor2 = "#{temp_dir}/repository2.rb"
|
76
|
+
File.write(descriptor2, '')
|
77
|
+
|
78
|
+
target_directory = "#{temp_dir}/generated/erb_template"
|
79
|
+
|
80
|
+
output = run_runner((%W(--descriptor #{descriptor} --descriptor #{descriptor2} --generators test,test2 --target-dir #{target_directory} --debug)))
|
81
|
+
|
82
|
+
target_file = "#{target_directory}/main/java/MyRepo.java"
|
83
|
+
target_file2 = "#{target_directory}/main/java/MyRepoTemplate2.java"
|
84
|
+
assert_equal 'Repository: MyRepo', IO.read(target_file)
|
85
|
+
assert_equal 'Repository: MyRepo (Template2)', IO.read(target_file2)
|
86
|
+
|
87
|
+
expected = <<OUTPUT
|
88
|
+
Repository Name: Unspecified
|
89
|
+
Target Dir: #{target_directory}
|
90
|
+
Descriptors:
|
91
|
+
* #{descriptor}
|
92
|
+
* #{descriptor2}
|
93
|
+
Generators:
|
94
|
+
* test
|
95
|
+
* test2
|
96
|
+
Loading descriptor: #{descriptor}
|
97
|
+
Descriptor loaded: #{descriptor}
|
98
|
+
Loading descriptor: #{descriptor2}
|
99
|
+
Descriptor loaded: #{descriptor2}
|
100
|
+
Derived default Repository name: MyRepo
|
101
|
+
Templates to process: ["test:repository.java", "test2:repository_t2.java"]
|
102
|
+
Evaluating template: test:repository.java
|
103
|
+
Generating test:repository.java for repository MyRepo
|
104
|
+
Generated test:repository.java for repository MyRepo to #{target_file}
|
105
|
+
Evaluating template: test2:repository_t2.java
|
106
|
+
Generating test2:repository_t2.java for repository MyRepo
|
107
|
+
Generated test2:repository_t2.java for repository MyRepo to #{target_file2}
|
108
|
+
Generator completed
|
109
|
+
OUTPUT
|
110
|
+
assert_equal expected.to_s, output.to_s
|
111
|
+
|
112
|
+
FileUtils.rm_rf target_directory
|
113
|
+
output2 = run_runner((%W(--descriptor #{descriptor} --descriptor #{descriptor2} --generators test --generators test2 --target-dir #{target_directory} --debug)))
|
114
|
+
assert_equal expected, output2
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_multiple_repositories_without_explicit_repository
|
118
|
+
descriptor = "#{temp_dir}/repository.rb"
|
119
|
+
File.write(descriptor, 'GenTest.repository(:MyRepo)')
|
120
|
+
descriptor2 = "#{temp_dir}/repository2.rb"
|
121
|
+
File.write(descriptor2, 'GenTest.repository(:MyRepo2)')
|
122
|
+
|
123
|
+
target_directory = "#{temp_dir}/generated/erb_template"
|
124
|
+
|
125
|
+
output = run_runner(%W(--descriptor #{descriptor} --descriptor #{descriptor2} --target-dir #{target_directory}),
|
126
|
+
Reality::Generators::BaseRunner::EXIT_CODE_NO_ELEMENT_NAME_SPECIFIED)
|
127
|
+
|
128
|
+
expected = "No repository name specified and repository name could not be determined. Please specify one of the valid repository names: MyRepo, MyRepo2\n"
|
129
|
+
assert_equal expected, output
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_multiple_repositories_with_explicit_repository
|
133
|
+
descriptor = "#{temp_dir}/repository.rb"
|
134
|
+
File.write(descriptor, 'GenTest.repository(:MyRepo)')
|
135
|
+
descriptor2 = "#{temp_dir}/repository2.rb"
|
136
|
+
File.write(descriptor2, 'GenTest.repository(:MyRepo2)')
|
137
|
+
|
138
|
+
target_directory = "#{temp_dir}/generated/erb_template"
|
139
|
+
|
140
|
+
output = run_runner(%W(--descriptor #{descriptor} --descriptor #{descriptor2} --target-dir #{target_directory} --repository MyRepo))
|
141
|
+
|
142
|
+
assert_equal '', output
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_element_name_specified_but_no_exist
|
146
|
+
descriptor = "#{temp_dir}/repository.rb"
|
147
|
+
File.write(descriptor, 'GenTest.repository(:MyRepo)')
|
148
|
+
|
149
|
+
target_directory = "#{temp_dir}/generated/erb_template"
|
150
|
+
|
151
|
+
output = run_runner(%W(--descriptor #{descriptor} --target-dir #{target_directory} --repository SomeOtherMyRepo),
|
152
|
+
Reality::Generators::BaseRunner::EXIT_CODE_ELEMENT_NAME_NO_EXIST)
|
153
|
+
|
154
|
+
expected = "Specified repository name 'SomeOtherMyRepo' does not exist in descriptors.\n"
|
155
|
+
assert_equal expected, output
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_descriptor_no_exist
|
159
|
+
descriptor = "#{temp_dir}/repository.rb"
|
160
|
+
|
161
|
+
target_directory = "#{temp_dir}/generated/erb_template"
|
162
|
+
|
163
|
+
output = run_runner(%W(--descriptor #{descriptor} --target-dir #{target_directory}),
|
164
|
+
Reality::Generators::BaseRunner::EXIT_CODE_DESCRIPTOR_NO_EXIST)
|
165
|
+
|
166
|
+
expected = "Descriptor file #{descriptor} does not exist\n"
|
167
|
+
assert_equal expected, output
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_invalid_args
|
171
|
+
output = run_runner(%W(--bad-arg), Reality::Generators::BaseRunner::EXIT_CODE_UNABLE_TO_PARSE_ARGS)
|
172
|
+
|
173
|
+
expected = "Error: invalid option: --bad-arg\n"
|
174
|
+
assert_equal expected, output
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_unexpected_arg
|
178
|
+
output = run_runner(%W(bad-arg), Reality::Generators::BaseRunner::EXIT_CODE_UNEXPECTED_ARGS)
|
179
|
+
|
180
|
+
expected = <<OUTPUT
|
181
|
+
Unexpected arguments ["bad-arg"] passed to command
|
182
|
+
Usage: gentest.rb [OPTIONS]
|
183
|
+
|
184
|
+
Options
|
185
|
+
-d, --descriptor FILENAME the filename of a descriptor to be loaded. Multiple descriptors may be loaded. Defaults to 'resources.rb' if none specified.
|
186
|
+
-r, --repository NAME the name of the repository to load. Defaults to the the name of the only repository if there is only one repository defined by the descriptors, otherwise must be specified.
|
187
|
+
-g, --generators GENERATORS the comma separated list of generators to run. Defaults to []
|
188
|
+
-t, --target-dir DIR the directory into which to generate artifacts. Defaults to 'generated'.
|
189
|
+
-v, --verbose turn on verbose logging.
|
190
|
+
--debug turn on debug logging.
|
191
|
+
-h, --help help
|
192
|
+
OUTPUT
|
193
|
+
assert_equal expected, output
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_help_invocation
|
197
|
+
output = run_runner(%w(--help))
|
198
|
+
|
199
|
+
assert_equal <<OUTPUT, output
|
200
|
+
Usage: gentest.rb [OPTIONS]
|
201
|
+
|
202
|
+
Options
|
203
|
+
-d, --descriptor FILENAME the filename of a descriptor to be loaded. Multiple descriptors may be loaded. Defaults to 'resources.rb' if none specified.
|
204
|
+
-r, --repository NAME the name of the repository to load. Defaults to the the name of the only repository if there is only one repository defined by the descriptors, otherwise must be specified.
|
205
|
+
-g, --generators GENERATORS the comma separated list of generators to run. Defaults to []
|
206
|
+
-t, --target-dir DIR the directory into which to generate artifacts. Defaults to 'generated'.
|
207
|
+
-v, --verbose turn on verbose logging.
|
208
|
+
--debug turn on debug logging.
|
209
|
+
-h, --help help
|
210
|
+
OUTPUT
|
211
|
+
end
|
212
|
+
|
213
|
+
def run_gentest(descriptor, generators, target_directory, additional_args = '')
|
214
|
+
run_runner((%W(--descriptor #{descriptor} --generators #{generators.join(',')} --target-dir #{target_directory} #{additional_args})))
|
215
|
+
end
|
216
|
+
|
217
|
+
def run_runner(args, expected_exitcode = Reality::Generators::BaseRunner::EXIT_CODE_SUCCESS)
|
218
|
+
prefix = (defined?(JRUBY_VERSION) || Gem.win_platform?) ? 'ruby ' : ''
|
219
|
+
command = File.expand_path("#{File.dirname(__FILE__)}/gentest.rb")
|
220
|
+
run_command("#{prefix}#{command} #{args.join(' ')}", expected_exitcode)
|
221
|
+
end
|
222
|
+
|
223
|
+
def run_command(command, expected_exitcode = 0)
|
224
|
+
output = `#{command}`
|
225
|
+
exitcode = $?.exitstatus
|
226
|
+
raise "Command failed to exit with code #{expected_exitcode} but returned #{exitcode}: #{command}\nOutput: #{output}" unless exitcode == expected_exitcode
|
227
|
+
output
|
228
|
+
end
|
229
|
+
end
|
data/test/helper.rb
CHANGED
@@ -36,9 +36,13 @@ class Reality::TestCase < Minitest::Test
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def teardown
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
if passed?
|
40
|
+
unless @temp_dir.nil?
|
41
|
+
FileUtils.rm_rf @temp_dir unless ENV['NO_DELETE_DIR'] == 'true'
|
42
|
+
@temp_dir = nil
|
43
|
+
end
|
44
|
+
else
|
45
|
+
$stderr.puts "Test #{self.class.name}.#{name} Failed. Leaving working directory #{@temp_dir}"
|
42
46
|
end
|
43
47
|
end
|
44
48
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reality-generators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Donald
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: reality-core
|
@@ -103,6 +103,7 @@ files:
|
|
103
103
|
- lib/reality/generators/rake_integration.rb
|
104
104
|
- lib/reality/generators/render_context.rb
|
105
105
|
- lib/reality/generators/ruby_template.rb
|
106
|
+
- lib/reality/generators/runner.rb
|
106
107
|
- lib/reality/generators/standard_artifact_dsl.rb
|
107
108
|
- lib/reality/generators/standard_template_set.rb
|
108
109
|
- lib/reality/generators/target_manager.rb
|
@@ -110,6 +111,7 @@ files:
|
|
110
111
|
- lib/reality/generators/template_set.rb
|
111
112
|
- lib/reality/generators/template_set_container.rb
|
112
113
|
- reality-generators.gemspec
|
114
|
+
- test/generators/gentest.rb
|
113
115
|
- test/generators/jpa/model.rb
|
114
116
|
- test/generators/jpa/templates/mytemplate.java.erb
|
115
117
|
- test/generators/jpa/templates/rubytemplate.java.rb
|
@@ -117,6 +119,7 @@ files:
|
|
117
119
|
- test/generators/test_generator.rb
|
118
120
|
- test/generators/test_render_context.rb
|
119
121
|
- test/generators/test_ruby_template.rb
|
122
|
+
- test/generators/test_runner.rb
|
120
123
|
- test/generators/test_standard_artifact_dsl.rb
|
121
124
|
- test/generators/test_standard_template_set.rb
|
122
125
|
- test/generators/test_target_manager.rb
|