Class: CreateTests
- Inherits:
-
Object
- Object
- CreateTests
- Defined in:
- lib/create_tests.rb
Class Method Summary collapse
-
.from(requests_file, type: :request_hash, test: :rspec, mode: :append) ⇒ Object
Generate tests from a file that contains Request Hashes.
Class Method Details
.from(requests_file, type: :request_hash, test: :rspec, mode: :append) ⇒ Object
Generate tests from a file that contains Request Hashes. More info about Request Hashes: https://github.com/MarioRuiz/Request-Hash
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/create_tests.rb', line 20 def self.from(requests_file, type: :request_hash, test: :rspec, mode: :append) begin f = File.new("#{requests_file}_create_tests.log", "w") f.sync = true @logger = Logger.new f puts "- Logs: #{requests_file}_create_tests.log" rescue StandardError => e warn "** Not possible to create the Logger file" warn e @logger = Logger.new nil end @logger.info "requests_file: #{requests_file}, type: #{type}, test: #{test}, mode: #{mode}" requests_file_orig = requests_file requests_file = if requests_file["./"].nil? requests_file else Dir.pwd.to_s + "/" + requests_file.gsub("./", "") end unless File.exist?(requests_file) = "** The file #{requests_file} doesn't exist" @logger.fatal raise end unless [:request_hash].include?(type) = "** Wrong type parameter: #{type}" @logger.fatal raise end unless [:rspec].include?(test) = "** Wrong test parameter: #{test}" @logger.fatal raise end unless [:overwrite, :dont_overwrite, :append].include?(mode) = "** Wrong mode parameter: #{mode}" @logger.fatal raise end if mode == :overwrite = "** Pay attention, if any of the files exist, will be overwritten" @logger.warn warn elsif mode == :append = "** Pay attention, if any of the test files exist or the help file exist only will be added the tests, methods that are missing." @logger.warn warn end @params = Array.new Dir.mkdir "./spec" unless test != :rspec or Dir.exist?("./spec") add_settings = true settings_file = "./settings/general.rb" helper_file = './spec/helper.rb' Dir.mkdir "./settings" unless Dir.exist?("./settings") if File.exist?(settings_file) and mode!=:overwrite = "** The file #{settings_file} already exists so no content will be added to it.\n" += " Remove the settings file to be able to be generated by create_tests or set mode: :overwrite" @logger.warn warn add_settings = false end add_helper = true helper_txt = "" if File.exist?(helper_file) if mode == :dont_overwrite = "** The file #{helper_file} already exists so no content will be added to it.\n" += " Remove the helper file to be able to be generated by create_tests or set mode: :overwrite or :append" @logger.warn warn add_helper = false elsif mode == :append helper_txt = File.read(helper_file) end end begin eval("require '#{requests_file}'") rescue Exception => stack = "\n\n** Error evaluating the ruby file containing the requests: \n" + stack.to_s @logger.fatal raise end if Kernel.const_defined?(:Swagger) first_module = Swagger elsif Kernel.const_defined?(:OpenApi) first_module = OpenApi elsif Kernel.const_defined?(:Requests) first_module = Requests else = "** The requests need to be inside a module named Swagger, OpenApi or Requests. For example:\n" += " module Swagger\n module UberApi\n module Products\n def self.list_products\n" @logger.fatal raise end modules = get_modules(first_module) modules.uniq! if add_settings mods_to_include = [] modules.each do |m| mods_to_include << m.scan(/^(.+)::/).join end mods_to_include.uniq! File.open(settings_file, "w") { |file| file.write(create_settings(requests_file_orig, mods_to_include)) } = "- Settings: #{settings_file}" @logger.info puts `rufo #{settings_file}` end modules.each do |mod_txt| mod_name = mod_txt.scan(/::(\w+)$/).join folder = "./spec/#{mod_name}" unless Dir.exist?(folder) Dir.mkdir folder @logger.info "Created folder: #{folder}" end mod_obj = eval("#{mod_txt}") mod_methods_txt = eval ("#{mod_txt}.methods(false)") mod_methods_txt.each do |method_txt| test_file = "#{folder}/#{method_txt}_spec.rb" if File.exist?(test_file) and mode==:dont_overwrite = "** The file #{test_file} already exists so no content will be added to it.\n" += " Remove the test file to be able to be generated by create_tests or set mode: :overwrite, or mode: :append" @logger.warn warn else if File.exist?(test_file) and mode == :append test_txt = File.read(test_file) else test_txt = '' end modified, txt = create_test(mod_txt, method_txt, mod_obj.method(method_txt),test_txt) File.open(test_file, "w") { |file| file.write(txt) } `rufo #{test_file}` if test_txt == "" = "- Test created: #{test_file}" elsif modified = "- Test updated: #{test_file}" else = "- Test without changes: #{test_file}" end @logger.info unless .include?("without changes") puts end end end end if add_helper @params.uniq! File.open(helper_file, "w") { |file| file.write(create_helper(@params, helper_txt)) } = "- Helper: #{helper_file}" @logger.info puts `rufo #{helper_file}` end end |