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

Returns a new instance of Publisher.



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

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.



24
25
26
# File 'lib/hiptest-publisher.rb', line 24

def reporter
  @reporter
end

Instance Method Details

#add_listener(listener) ⇒ Object



125
126
127
# File 'lib/hiptest-publisher.rb', line 125

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

#analyze_project_dataObject



180
181
182
183
184
185
186
187
# File 'lib/hiptest-publisher.rb', line 180

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

#compute_actionwords_diffObject



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/hiptest-publisher.rb', line 162

def compute_actionwords_diff
  old = nil
  reporter.with_status_message I18n.t('actionwords_diff.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



251
252
253
254
255
256
257
258
259
# File 'lib/hiptest-publisher.rb', line 251

def display_empty_push_help
  command = @cli_options.command_line_used(exclude: [:push, :push_format, :execution_environment])
  enhanced_command = "#{command} --without=actionwords"
  if @cli_options.test_run_id.nil? || @cli_options.test_run_id.empty?
    enhanced_command += " --test-run-id=<#{I18n.t('push.test_run_id')}>"
  end

  puts I18n.t('push.empty_results', enhanced_command: enhanced_command)
end

#exportObject



189
190
191
192
193
194
195
# File 'lib/hiptest-publisher.rb', line 189

def export
  return if @project.nil?

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

#export_actionword_signatureObject



152
153
154
155
156
157
158
159
160
# File 'lib/hiptest-publisher.rb', line 152

def export_actionword_signature
  analyze_project_data

  write_to_file(
    "#{@cli_options.output_directory}/actionwords_signature.yaml",
    I18n.t('actionwords_diff.exporting_title'),
    ask_overwrite: true
  ) { Hiptest::SignatureExporter.export_actionwords(@project).to_yaml }
end

#export_filesObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/hiptest-publisher.rb', line 135

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,
        I18n.t(:exporting_file, name: node_rendering_context.description),
        ask_overwrite: ask_overwrite
      )
    end
  end
end

#fetch_xml_fileObject



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

def fetch_xml_file
  @client.fetch_project_export
rescue ClientError => err
  # This should not be an error that needs reporting to an exception monitoring app
  reporter.show_error(err.message)
  if @exit_on_bad_arguments == false # means we are embedded in hiptest-publisher
    raise
  end
rescue => err
  reporter.show_failure(I18n.t("errors.default"))
  reporter.dump_error(err)
end

#get_project(xml) ⇒ Object



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

def get_project(xml)
  reporter.with_status_message I18n.t('build_data.title') do
    parser = Hiptest::XMLParser.new(xml, reporter)
    return parser.build_project
  end
rescue => err
  reporter.dump_error(err)
end

#get_xml_fileObject



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

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

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/hiptest-publisher.rb', line 112

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

  if $stdout.tty?
    answer = reporter.ask(I18n.t('overwrite.ask_confirmation', path: path))
    return ['y', 'yes'].include?(answer)
  else
    reporter.warning_message(I18n.t('overwrite.warning_message', path: path))
    return false
  end
end

#post_resultsObject



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/hiptest-publisher.rb', line 207

def post_results
  response = nil
  reporter.with_status_message I18n.t('push.posting_results', file: @cli_options.push, site: @cli_options.site) do
    response = @client.push_results
  end
  if valid_hiptest_api_response?(response)
    report_imported_results(response)
  else
    report_hiptest_api_error(response)
  end
rescue => err
  reporter.dump_error(err)
end


197
198
199
200
201
202
203
204
205
# File 'lib/hiptest-publisher.rb', line 197

def print_categories
  language_config = LanguageConfigParser.new(@cli_options)
  group_names = language_config.group_names
  puts I18n.t('help.categories.title', language: @cli_options.language)
  group_names.each do |group_name|
    puts "  - #{group_name}"
  end
  puts I18n.t('help.categories.usage_example', language: @cli_options.language, first: group_names.first, second: group_names[1])
end

#report_hiptest_api_error(response) ⇒ Object



242
243
244
245
246
247
248
249
# File 'lib/hiptest-publisher.rb', line 242

def report_hiptest_api_error(response)
  reporter.failure_message(I18n.t('errors.api_error', code: response.code))
  if response.code == "422" && response.body.start_with?("Unknown format")
    STDERR.print response.body.chomp + "\n"
  elsif response.code == "404"
    STDERR.print I18n.t('errors.project_not_found')
  end
end

#report_imported_results(response) ⇒ Object



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

def report_imported_results(response)
  json = JSON.parse(response.body)

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

  reporter.with_status_message I18n.t('push.tests_imported_summary', count: passed_count) do
    if @cli_options.verbose
      reported_tests.each do |imported_test|
        puts I18n.t('push.test_imported', name: imported_test['name'])
      end
    end
  end

  display_empty_push_help if passed_count == 0
end

#runObject



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

def run
  if @cli_options.check_version
    return VersionChecker.check_version
  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



174
175
176
177
178
# File 'lib/hiptest-publisher.rb', line 174

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

#valid_hiptest_api_response?(response) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/hiptest-publisher.rb', line 221

def valid_hiptest_api_response?(response)
  response.is_a?(Net::HTTPSuccess)
end

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



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

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



107
108
109
110
# File 'lib/hiptest-publisher.rb', line 107

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