class SpecRbTransformer
Transform a generated spec.rb file for running
Constants
- ENVIRONMENTS
- REMOVALS
- TEST_EXTENSION
Public Class Methods
new(search_directory, target_root_directory)
click to toggle source
# 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
Public Instance Methods
clean_end_of_file(file)
click to toggle source
Sort out indentation of last end statement
# 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)
click to toggle source
Clear out the last run
# 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)
click to toggle source
Create the context description
# 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)
click to toggle source
Delete an entire test block if the description matches the pattern
# 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)
click to toggle source
Insert the dynamic_resource call
# 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)
click to toggle source
Insert any tags
# 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)
click to toggle source
Parse the account (prod / non_prod) from the context line of a test
# 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)
click to toggle source
What environment referenced in file
# 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)
click to toggle source
Parse the object type from the file name
# 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)
click to toggle source
Change private dns line
# 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)
click to toggle source
Change private ip line
# 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)
click to toggle source
Set the class tag var
# 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)
click to toggle source
Set the output file for a generator and create sub dir if not set
# 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()
click to toggle source
# 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)
click to toggle source
rubocop:disable Metrics/CyclomaticComplexity
# 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