Class: Hiptest::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/hiptest-publisher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, listeners: nil, exit_on_bad_arguments: true) ⇒ Publisher



24
25
26
27
28
29
30
31
32
# File 'lib/hiptest-publisher.rb', line 24

def initialize(args, listeners: nil, exit_on_bad_arguments: true)
  @reporter = Reporter.new(listeners)
  @cli_options = OptionsParser.parse(args, reporter)
  @client = Hiptest::Client.new(@cli_options, reporter)
  @file_writer = Hiptest::FileWriter.new(@reporter)

  # pass false to prevent hiptest-publisher from exiting, useful when used embedded
  @exit_on_bad_arguments = exit_on_bad_arguments
end

Instance Attribute Details

#reporterObject (readonly)

Returns the value of attribute reporter.



22
23
24
# File 'lib/hiptest-publisher.rb', line 22

def reporter
  @reporter
end

Instance Method Details

#add_listener(listener) ⇒ Object



129
130
131
# File 'lib/hiptest-publisher.rb', line 129

def add_listener(listener)
  reporter.add_listener(listener)
end

#analyze_project_dataObject



184
185
186
187
188
189
190
191
# File 'lib/hiptest-publisher.rb', line 184

def analyze_project_data
  return if @project_data_analyzed
  reporter.with_status_message "Analyzing data" do
    @language_config = LanguageConfigParser.new(@cli_options)
    Hiptest::NodeModifiers.add_all(@project, @cli_options.sort)
  end
  @project_data_analyzed = true
end

#check_versionObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/hiptest-publisher.rb', line 220

def check_version
  latest = nil
  reporter.with_status_message "Checking latest version on Rubygem" do
    latest_gem = Gem.latest_version_for('hiptest-publisher')

    raise RuntimeError, "Unable to connect to Rubygem" if latest_gem.nil?

    latest = latest_gem.version
  end

  return if latest.nil?

  current = hiptest_publisher_version

  if latest == current
    puts "Your current install of hiptest-publisher (#{current}) is up-to-date."
  else
    puts [
      "Your current install of hiptest-publisher (#{current}) is outdated, version #{latest} is available",
      "run 'gem install hiptest-publisher' to get the latest version."
      ].join("\n")
  end
end

#compute_actionwords_diffObject



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/hiptest-publisher.rb', line 166

def compute_actionwords_diff
  old = nil
  reporter.with_status_message "Loading previous definition" do
    old = YAML.load_file("#{@cli_options.output_directory}/actionwords_signature.yaml")
  end

  analyze_project_data

  current = Hiptest::SignatureExporter.export_actionwords(@project, true)
  diff =  Hiptest::SignatureDiffer.diff(old, current, library_name: @cli_options.library_name)
end

#display_empty_push_helpObject



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/hiptest-publisher.rb', line 267

def display_empty_push_help
  command = @cli_options.command_line_used(exclude: [:push, :push_format])
  enhanced_command = "#{command} --without=actionwords"
  if @cli_options.test_run_id.nil? || @cli_options.test_run_id.empty?
    enhanced_command += " --test-run-id=<the ID of the test run you want to push the results to>"
  end

  puts [
    "Possible causes for the lack of imported tests:",
    "",
    "  * Did you run the following command before executing your tests ?",
    "    #{enhanced_command}",
    "",
    "  * Did you specify the correct push format ?",
    "    Use push_format=<format> in your config file or option --push-format=<format> in the command line",
    "    Available formats are: cucumber-json, junit, nunit, robot, tap",
    ""
  ].join("\n")
end

#exportObject



193
194
195
196
197
198
199
# File 'lib/hiptest-publisher.rb', line 193

def export
  return if @project.nil?

  analyze_project_data
  export_files
  export_actionword_signature if @language_config.include_group?("actionwords")
end

#export_actionword_signatureObject



156
157
158
159
160
161
162
163
164
# File 'lib/hiptest-publisher.rb', line 156

def export_actionword_signature
  analyze_project_data

  write_to_file(
    "#{@cli_options.output_directory}/actionwords_signature.yaml",
    "Exporting actionword signature",
    ask_overwrite: true
  ) { Hiptest::SignatureExporter.export_actionwords(@project).to_yaml }
end

#export_filesObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/hiptest-publisher.rb', line 139

def export_files
  @language_config.language_group_configs.each do |language_group_config|
    next if ['library', 'libraries'].include?(language_group_config[:group_name]) && !@project.has_libraries?
    ask_overwrite = ['actionwords', 'libraries'].include?(language_group_config[:group_name])

    language_group_config.each_node_rendering_context(@project) do |node_rendering_context|
      write_node_to_file(
        node_rendering_context.path,
        node_rendering_context.node,
        node_rendering_context,
        "Exporting #{node_rendering_context.description}",
        ask_overwrite: ask_overwrite
      )
    end
  end
end

#fetch_xml_fileObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hiptest-publisher.rb', line 84

def fetch_xml_file
  reporter.with_status_message "Fetching data from Hiptest" do
    @client.fetch_project_export
  end
rescue ClientError => err
  # This should not be an error that needs reporting to an exception monitoring app
  puts err.message.yellow
  if @exit_on_bad_arguments == false # means we are embedded in hiptest-publisher
    raise
  end
rescue => err
  puts ("An error has occured, sorry for the inconvenience.\n" +
    "Try running the command again with --verbose for detailed output").red
  reporter.dump_error(err)
end

#get_project(xml) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/hiptest-publisher.rb', line 100

def get_project(xml)
  reporter.with_status_message "Extracting data" do
    parser = Hiptest::XMLParser.new(xml, reporter)
    return parser.build_project
  end
rescue => err
  reporter.dump_error(err)
end

#get_xml_fileObject



76
77
78
79
80
81
82
# File 'lib/hiptest-publisher.rb', line 76

def get_xml_file
  if @cli_options.xml_file
    IO.read(@cli_options.xml_file)
  else
    fetch_xml_file
  end
end

#overwrite_file?(path) ⇒ Boolean



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/hiptest-publisher.rb', line 114

def overwrite_file?(path)
  return true unless File.file?(path)
  return true if @cli_options.force_overwrite

  if $stdout.isatty
    STDOUT.print "\n"
    STDOUT.print "[#{"?".yellow}] File #{path} exists, do you want to overwrite it? [y/N] "
    answer = $stdin.gets.chomp.downcase.strip
    return ['y', 'yes'].include?(answer)
  else
    reporter.notify(:show_status_message, "File #{path} already exists, skipping. Use --force to overwrite it.", :warning)
    return false
  end
end

#post_resultsObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/hiptest-publisher.rb', line 244

def post_results
  response = nil
  reporter.with_status_message "Posting #{@cli_options.push} to #{@cli_options.site}" do
    response = @client.push_results
  end
  json = JSON.parse(response.body)

  reported_tests = json.has_key?('test_import') ? json['test_import'] : []
  passed_count = reported_tests.size

  reporter.with_status_message "#{pluralize(passed_count, "test")} imported" do
    if @cli_options.verbose
      reported_tests.each do |imported_test|
        puts "  Test '#{imported_test['name']}' imported"
      end
    end
  end

  display_empty_push_help if passed_count == 0
rescue => err
  reporter.dump_error(err)
end


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/hiptest-publisher.rb', line 201

def print_categories
  language_config = LanguageConfigParser.new(@cli_options)
  group_names = language_config.group_names
  puts "For language #{@cli_options.language}, available file groups are"
  group_names.each do |group_name|
    puts "  - #{group_name}"
  end
  puts [
    "",
    "Usage examples:",
    "",
    "To export only #{group_names.first} files:",
    "    hiptest-publisher --language=#{@cli_options.language} --only=#{group_names.first}",
    "",
    "To export both #{group_names.first} and #{group_names[1]} files:",
    "    hiptest-publisher --language=#{@cli_options.language} --only=#{group_names.take(2).join(",")}"
  ].join("\n")
end

#runObject



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
# File 'lib/hiptest-publisher.rb', line 34

def run
  if @cli_options.check_version
    check_version
    return
  end

  begin
    CliOptionsChecker.new(@cli_options, reporter).check!
  rescue CliOptionError => e
    puts e.message
    exit 1 if @exit_on_bad_arguments
    raise
  end

  if @cli_options.only == 'list'
    print_categories
    return
  end

  if @cli_options.push?
    post_results
    return
  end

  xml = get_xml_file
  return if xml.nil?

  @project = get_project(xml)

  if @cli_options.actionwords_signature
    export_actionword_signature
    return
  end

  if @cli_options.actionwords_diff?
    show_actionwords_diff
    return
  end

  export
end

#show_actionwords_diffObject



178
179
180
181
182
# File 'lib/hiptest-publisher.rb', line 178

def show_actionwords_diff
  Hiptest::DiffDisplayer.new(compute_actionwords_diff, @cli_options, @language_config, @file_writer).display
rescue => err
  reporter.dump_error(err)
end

#write_node_to_file(path, node, context, message, ask_overwrite: false) ⇒ Object



133
134
135
136
137
# File 'lib/hiptest-publisher.rb', line 133

def write_node_to_file(path, node, context, message, ask_overwrite: false)
  write_to_file(path, message, ask_overwrite: ask_overwrite) do
    Hiptest::Renderer.render(node, context)
  end
end

#write_to_file(path, message, ask_overwrite: false) ⇒ Object



109
110
111
112
# File 'lib/hiptest-publisher.rb', line 109

def write_to_file(path, message, ask_overwrite: false)
  return if ask_overwrite && !overwrite_file?(path)
  @file_writer.write_to_file(path, message) { yield }
end