Class: SpecRbTransformer
- Inherits:
-
Object
- Object
- SpecRbTransformer
- Defined in:
- lib/spec_rb_transformer.rb
Overview
Transform a generated spec.rb file for running
Constant Summary collapse
- TEST_EXTENSION =
'_spec.rb'
- REMOVALS =
%w(blddev- bldinf- default)
- ENVIRONMENTS =
%w(non_prod prod)
Instance Method Summary collapse
-
#clean_end_of_file(file) ⇒ Object
Sort out indentation of last end statement.
-
#clear_last_run(root_dir) ⇒ Object
Clear out the last run.
-
#construct_test_context(basename) ⇒ Object
Create the context description.
-
#delete_test_block(pattern, file) ⇒ Object
Delete an entire test block if the description matches the pattern.
-
#initialize(search_directory, target_root_directory) ⇒ SpecRbTransformer
constructor
A new instance of SpecRbTransformer.
-
#insert_dynamic_resource(line) ⇒ Object
Insert the dynamic_resource call.
-
#insert_environment_tag(line) ⇒ Object
Insert any tags.
-
#parse_account_from_context_line(file) ⇒ Object
Parse the account (prod / non_prod) from the context line of a test.
-
#parse_environment_tag_from_test_file(file) ⇒ Object
What environment referenced in file.
-
#parse_object_type_from_file_name(file) ⇒ Object
Parse the object type from the file name.
-
#replace_ip_with_regex_in_line(line) ⇒ Object
Change private dns line.
-
#replace_private_ip_with_regex(line) ⇒ Object
Change private ip line.
-
#set_environment_tag(line) ⇒ Object
Set the class tag var.
-
#setup_generator_output(type, account, file) ⇒ Object
Set the output file for a generator and create sub dir if not set.
- #transform ⇒ Object
-
#transform_line(line) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity.
Constructor Details
#initialize(search_directory, target_root_directory) ⇒ SpecRbTransformer
Returns a new instance of SpecRbTransformer.
17 18 19 20 21 22 |
# File 'lib/spec_rb_transformer.rb', line 17 def initialize(search_directory, target_root_directory) @target_root_directory = File.absolute_path(target_root_directory) + File::SEPARATOR @sieve = FileSieve.new(search_directory, TEST_EXTENSION) @tag = '' @described = false end |
Instance Method Details
#clean_end_of_file(file) ⇒ Object
Sort out indentation of last end statement
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/spec_rb_transformer.rb', line 59 def clean_end_of_file(file) lines = File.readlines(file) (lines.length - 1).step(0, -1) do |i| if lines[i] == " end\n" lines[i] = "end\n" break end end File.open(file, 'w') do |f| lines.each do |line| f.write(line) end end end |
#clear_last_run(root_dir) ⇒ Object
Clear out the last run
25 26 27 28 29 30 31 |
# File 'lib/spec_rb_transformer.rb', line 25 def clear_last_run(root_dir) Pathname.new(root_dir).children.select { |c| c.directory? }.collect { |p| p.to_s }.each do |dir| next unless dir.include?("_tests_") puts "Deleting directory from previous run #{dir}" FileUtils.remove_dir(dir) end end |
#construct_test_context(basename) ⇒ Object
Create the context description
76 77 78 79 80 |
# File 'lib/spec_rb_transformer.rb', line 76 def construct_test_context(basename) vpc = basename.split('_')[2] partition = basename.split('_')[0].to_s.upcase partition + ' Tests on ' + vpc end |
#delete_test_block(pattern, file) ⇒ Object
Delete an entire test block if the description matches the pattern
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/spec_rb_transformer.rb', line 83 def delete_test_block(pattern, file) lines = File.readlines(file) remove = false File.delete(file) File.open(file, 'w') do |f| lines.each do |line| remove = true if line.include?('describe ') && line.include?(pattern) if remove remove = false if line.include?(' end') next end f.write(line) end end end |
#insert_dynamic_resource(line) ⇒ Object
Insert the dynamic_resource call
100 101 102 103 104 |
# File 'lib/spec_rb_transformer.rb', line 100 def insert_dynamic_resource(line) line.insert(line.index('(') + 1, 'dynamic_resource(') line.insert(line.index(')'), ')') line end |
#insert_environment_tag(line) ⇒ Object
Insert any tags
107 108 109 110 |
# File 'lib/spec_rb_transformer.rb', line 107 def insert_environment_tag(line) line = line.slice(0, line.index(' do')) line + @tag + ' do' + "\n" end |
#parse_account_from_context_line(file) ⇒ Object
Parse the account (prod / non_prod) from the context line of a test
113 114 115 116 117 118 |
# File 'lib/spec_rb_transformer.rb', line 113 def parse_account_from_context_line(file) File.readlines(file).each do |line| ENVIRONMENTS.each {|env| return env if line.include?(env)} end raise(RuntimeError, 'Failed to find account from context line in file (' + file + ')') end |
#parse_environment_tag_from_test_file(file) ⇒ Object
What environment referenced in file
121 122 123 124 125 126 127 128 |
# File 'lib/spec_rb_transformer.rb', line 121 def parse_environment_tag_from_test_file(file) File.foreach(file) do |line| if line.include?('describe') set_environment_tag(line) return end end end |
#parse_object_type_from_file_name(file) ⇒ Object
Parse the object type from the file name
131 132 133 |
# File 'lib/spec_rb_transformer.rb', line 131 def parse_object_type_from_file_name(file) file[0, file.index('_on')] end |
#replace_ip_with_regex_in_line(line) ⇒ Object
Change private dns line
136 137 138 139 140 141 142 143 |
# File 'lib/spec_rb_transformer.rb', line 136 def replace_ip_with_regex_in_line(line) first = line.slice(0, line.index('{') + 1) last = line.slice(line.index('.'), line.length) .gsub(/\./, '\.') .tr("'", '/') first + ' should match /ip-[\d]{1,3}-[\d]{1,3}-[\d]{1,3}-[\d]{1,3}' + last end |
#replace_private_ip_with_regex(line) ⇒ Object
Change private ip line
146 147 148 149 |
# File 'lib/spec_rb_transformer.rb', line 146 def replace_private_ip_with_regex(line) line = line.slice(0, line.index('{') + 1) line + ' should match /[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}/ }' + "\n" end |
#set_environment_tag(line) ⇒ Object
Set the class tag var
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/spec_rb_transformer.rb', line 152 def set_environment_tag(line) case line when MatchesInt @tag = ', int: true' when MatchesDevint @tag = ', devint: true' when MatchesFt @tag = ', ft: true' when MatchesInfradev @tag = ', infradev: true' when MatchesPpd @tag = ', ppd: true' when MatchesStg @tag = ', stg: true' else @tag = '' end end |
#setup_generator_output(type, account, file) ⇒ Object
Set the output file for a generator and create sub dir if not set
172 173 174 175 176 |
# File 'lib/spec_rb_transformer.rb', line 172 def setup_generator_output(type, account, file) directory = @target_root_directory + "#{type}_tests_#{account}" FileUtils.mkdir_p directory unless Dir.exists?(directory) File.absolute_path(directory) + File::SEPARATOR + file end |
#transform ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/spec_rb_transformer.rb', line 33 def transform @sieve.found_files.each {|file| parse_environment_tag_from_test_file(file) basename = File.basename(file) type = parse_object_type_from_file_name(basename) account = parse_account_from_context_line(file) target_file = setup_generator_output(type, account, basename) File.delete(target_file) if File.exist?(target_file) @describe = false File.open(target_file, 'w') do |wr| File.foreach(file) do |line| line = transform_line(line) wr.write(line) end end clean_end_of_file(target_file) REMOVALS.each {|pattern| delete_test_block(pattern, target_file)} File.delete(target_file) unless @described } end |
#transform_line(line) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/spec_rb_transformer.rb', line 179 def transform_line(line) line = ' ' + line case line when MatchesAclOwner line = '' when MatchesContextLine line.lstrip! line = insert_environment_tag(line) when MatchesDescribeLine line = insert_dynamic_resource(line) @described = true when MatchesHaveEbs return '' # Uncomment to ignore :image_id and :instance_id # when MatchesImageId # return '' # when MatchesInstanceId # return '' when MatchesNetworkInterfaceLine return '' when MatchesPrivateDnsNameLine line = replace_ip_with_regex_in_line(line) when MatchesPrivateIpAddressLine line = replace_private_ip_with_regex(line) when MatchesRequireLine line = line.strip! + "\n" end line end |