Module: DromedaryInitializer

Included in:
Dromedary
Defined in:
lib/dromedary_initializer.rb

Class Method Summary collapse

Class Method Details

.already_initialized?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/dromedary_initializer.rb', line 51

def self.already_initialized?
  File.exist?('config/dromedary.yml') || File.exist?('features/support/dromedary_hooks.rb')
end

.create_directory(dir_name) ⇒ Object



16
17
18
# File 'lib/dromedary_initializer.rb', line 16

def self.create_directory(dir_name)
  create_directory_or_file dir_name, true
end

.create_directory_or_file(file_name, directory) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dromedary_initializer.rb', line 24

def self.create_directory_or_file(file_name, directory)
  file_type = if directory
                :mkdir_p
              else
                :touch
              end

  report_exists(file_name) || return if File.exist?(file_name)

  report_creating(file_name)
  FileUtils.send file_type, file_name
end

.create_file(file_name) ⇒ Object



20
21
22
# File 'lib/dromedary_initializer.rb', line 20

def self.create_file(file_name)
  create_directory_or_file file_name, false
end

.cucumber_initialized?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/dromedary_initializer.rb', line 47

def self.cucumber_initialized?
  !File.exist?('features/support/env.rb')
end

.dromedary_after_hooksObject



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
# File 'lib/dromedary_initializer.rb', line 136

def self.dromedary_after_hooks
  ['After do |scenario|',
   '  # updating results hash for TestRail after each scenario',
   '  feature_name = scenario.feature.name',
   '  scenario_name = scenario.name',
   '',
   '  scenario.test_steps.each do |step|',
   "    if step.text != 'Before hook' && step.text != 'AfterStep hook'",
   "      step_name = File.open(step.location.file).readlines[step.location.line-1].lstrip",
   "      full_description = (feature_name + ' ' + scenario_name + ' ' + step_name).rstrip",
   '      status_id = []',
   '      status_id =  @passed_steps_count > 0 ? (status_id.push 1) : (status_id.push 5)',
   '      if @results[ full_description ]',
   '        @results[ full_description ].push(status_id).flatten!',
   '      else',
   '        @results[ full_description ] = status_id',
   '      end',
   '      @passed_steps_count -= 1',
   '    end',
   '  end',
   '',
   '  # setting TestRail to generate reports at specific folder',
   "  unless @local == 'true'",
   '    File.open("artifacts/test_rail_results/file_#{UUID.generate(:compact).to_s}.json", "w") do |file|',
   '      file.puts @results.to_json',
   '    end',
   '  end',
   'end']
end

.dromedary_afterstep_hooksObject



130
131
132
133
134
# File 'lib/dromedary_initializer.rb', line 130

def self.dromedary_afterstep_hooks
  ['AfterStep do |step|',
   '  @passed_steps_count += 1 if step.passed?',
   'end']
end

.dromedary_before_hooksObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dromedary_initializer.rb', line 116

def self.dromedary_before_hooks
  ['Before do',
   "  # setting environment to 'local' in order not to generate reporting",
   "  # if you need individual reports, just put this variable to 'false'",
   "  ENV['local'] ||= 'true'",
   "  @local = ENV['local']",
   '',
   '  # creating results hash for TestRail',
   '  @results = {}',
   '  # and resetting passed steps count',
   '  @passed_steps_count = 0',
   'end']
end

.dromedary_config_contentObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dromedary_initializer.rb', line 78

def self.dromedary_config_content
  ['# This file was generated by Dromedary gem',
   '# It contains required settings to support TestRail integration',
   '# Fill in each line with credentials of your TestRail account',
   '',
   dromedary_config_structure,
   ' # By default Dromedary creates Test Runs on TestRail this way:',
   ' # test_run_default_name + Suite Type + on Environment',
   ' # So at the end you will get something like this:',
   ' # "My_project Regression on Staging"']
end

.dromedary_config_structureObject



90
91
92
93
94
95
96
97
98
# File 'lib/dromedary_initializer.rb', line 90

def self.dromedary_config_structure
  ['testrail:',
   ' url:',
   ' user:',
   ' password:',
   ' project_id:',
   ' suite_id:',
   ' test_run_default_name:']
end

.dromedary_hooks_contentObject



100
101
102
103
104
105
106
# File 'lib/dromedary_initializer.rb', line 100

def self.dromedary_hooks_content
  ['# This file was generated by Dromedary gem',
   '# It contains required Cucumber hooks to ensure that all reporting works',
   '# Do not edit this file',
   '',
   dromedary_hooks_structure]
end

.dromedary_hooks_structureObject



108
109
110
111
112
113
114
# File 'lib/dromedary_initializer.rb', line 108

def self.dromedary_hooks_structure
  [dromedary_before_hooks,
   '',
   dromedary_afterstep_hooks,
   '',
   dromedary_after_hooks]
end

.report_already_initializedObject



73
74
75
76
# File 'lib/dromedary_initializer.rb', line 73

def self.report_already_initialized
  puts '  Our suspicious Dromedary says that you have already initialized it'
  puts "  There is no need to run 'dromedary -- init' command more than once"
end

.report_creating(file) ⇒ Object



59
60
61
# File 'lib/dromedary_initializer.rb', line 59

def self.report_creating(file)
  puts "  creating   #{file}"
end

.report_exists(file) ⇒ Object



55
56
57
# File 'lib/dromedary_initializer.rb', line 55

def self.report_exists(file)
  puts "     exist   #{file}"
end

.report_no_cucumber_foundObject



67
68
69
70
71
# File 'lib/dromedary_initializer.rb', line 67

def self.report_no_cucumber_found
  puts "  Dromedary had searched all Sahara desert for Cucumber, but didn't found it"
  puts '  Are you sure that you had initialized Cucumber project?'
  puts "  If not, try to run 'cucumber --init' first"
end

.report_updating(file) ⇒ Object



63
64
65
# File 'lib/dromedary_initializer.rb', line 63

def self.report_updating(file)
  puts "  updating   #{file}"
end

.runObject



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/dromedary_initializer.rb', line 2

def self.run
  if cucumber_initialized?
    report_no_cucumber_found
  elsif already_initialized?
    report_already_initialized
  else
    create_file 'config/dromedary.yml'
    create_file 'features/support/dromedary_hooks.rb'

    update_file 'config/dromedary.yml', dromedary_config_content
    update_file 'features/support/dromedary_hooks.rb', dromedary_hooks_content
  end
end

.update_file(file_name, content) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/dromedary_initializer.rb', line 37

def self.update_file(file_name, content)
  open(file_name, 'a') do |file|
    content.flatten!.each do |line|
      file.puts line
    end
  end

  report_updating(file_name)
end