Class: Generator

Inherits:
Object show all
Defined in:
lib/ceedling/generators/generator.rb

Instance Method Summary collapse

Instance Method Details

#generate_executable_file(tool, context, objects, flags, executable, map = '', libraries = [], libpaths = []) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/ceedling/generators/generator.rb', line 319

def generate_executable_file(tool, context, objects, flags, executable, map='', libraries=[], libpaths=[])
  shell_result = {}
  arg_hash = { :tool => tool,
               :context => context,
               :objects => objects,
               :flags => flags,
               :executable => executable,
               :map => map,
               :libraries => libraries,
               :libpaths => libpaths
             }

  @plugin_manager.pre_link_execute(arg_hash)

  msg = @reportinator.generate_progress("Linking #{File.basename(arg_hash[:executable])}")
  @loginator.log( msg )

  command =
    @tool_executor.build_command_line(
      arg_hash[:tool],
      # Extra arguments
      arg_hash[:flags],
      # Argument replacement
      arg_hash[:objects],
      arg_hash[:executable],
      arg_hash[:map],
      arg_hash[:libraries],
      arg_hash[:libpaths]
    )

  begin
    shell_result = @tool_executor.exec( command )
  rescue ShellException => ex
    shell_result = ex.shell_result
    raise ex
  ensure
    arg_hash[:shell_command] = command[:line]
    arg_hash[:shell_result] = shell_result
    @plugin_manager.post_link_execute(arg_hash)
  end
end

#generate_mock(context:, mock:, test:, input_filepath:, output_path:) ⇒ Object



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
# File 'lib/ceedling/generators/generator.rb', line 104

def generate_mock(context:, mock:, test:, input_filepath:, output_path:)
  arg_hash = {
    :header_file => input_filepath,
    :test => test,
    :context => context,
    :output_path => output_path }
  
  @plugin_manager.pre_mock_generate( arg_hash )

  begin
    # Below is a workaround that instantiates CMock anew:
    #  1. To allow dfferent output path per mock
    #  2. To avoid any thread safety complications

    # TODO:
    #  - Add option to CMock to generate mock to any destination path
    #  - Make CMock thread-safe

    # Get default config created by Ceedling and customize it
    config = @generator_mocks.build_configuration( output_path )

    # Generate mock
    msg = @reportinator.generate_module_progress(
      operation: "Generating mock for",
      module_name: test,
      filename: File.basename(input_filepath)
    )
    @loginator.log( msg )

    cmock = @generator_mocks.manufacture( config )
    cmock.setup_mocks( arg_hash[:header_file] )
  rescue StandardError => ex
    # Re-raise execption but decorate it with CMock to better identify it
    raise( ex, "CMock >> #{ex.message}", ex.backtrace )
  ensure
    @plugin_manager.post_mock_generate( arg_hash )
  end
end

#generate_object_file_asm(tool:, module_name:, context:, source:, object:, search_paths: [], flags: [], defines: [], list: '', dependencies: '', msg: nil) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/ceedling/generators/generator.rb', line 254

def generate_object_file_asm(
    tool:,
    module_name:,
    context:,
    source:,
    object:,
    search_paths:[],
    flags:[],
    defines:[],
    list:'',
    dependencies:'',
    msg:nil
  )

  shell_result = {}

  arg_hash = { :tool => tool,
               :module_name => module_name,
               :operation => OPERATION_ASSEMBLE_SYM,
               :context => context,
               :source => source,
               :object => object,
               :search_paths => search_paths,
               :flags => flags,
               :defines => defines,
               :list => list,
               :dependencies => dependencies
             }

  @plugin_manager.pre_compile_execute(arg_hash)

  msg = String(msg)
  msg = @reportinator.generate_module_progress(
    operation: "Assembling",
    module_name: module_name,
    filename: File.basename(arg_hash[:source])
    ) if msg.empty?
  @loginator.log( msg )

  command =
    @tool_executor.build_command_line( 
      arg_hash[:tool],
      # Extra arguments
      arg_hash[:flags],
      # Argument replacement
      arg_hash[:source],
      arg_hash[:object],
      arg_hash[:search_paths],
      arg_hash[:defines],
      arg_hash[:list],
      arg_hash[:dependencies]
    )

  begin
    shell_result = @tool_executor.exec( command )
  rescue ShellException => ex
    shell_result = ex.shell_result
    raise ex
  ensure
    arg_hash[:shell_command] = command[:line]
    arg_hash[:shell_result] = shell_result
    @plugin_manager.post_compile_execute(arg_hash)
  end
end

#generate_object_file_c(tool:, module_name:, context:, source:, object:, search_paths: [], flags: [], defines: [], list: '', dependencies: '', msg: nil) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/ceedling/generators/generator.rb', line 189

def generate_object_file_c(
    tool:,
    module_name:,
    context:,
    source:,
    object:,
    search_paths:[],
    flags:[],
    defines:[],
    list:'',
    dependencies:'',
    msg:nil
  )

  shell_result = {}
  arg_hash = { :tool => tool,
               :module_name => module_name,
               :operation => OPERATION_COMPILE_SYM,
               :context => context,
               :source => source,
               :object => object,
               :search_paths => search_paths,
               :flags => flags,
               :defines => defines,
               :list => list,
               :dependencies => dependencies,
               :msg => String(msg)
             }

  @plugin_manager.pre_compile_execute(arg_hash)

  msg = arg_hash[:msg]
  msg = @reportinator.generate_module_progress(
    operation: "Compiling",
    module_name: module_name,
    filename: File.basename(arg_hash[:source])
    ) if msg.empty?
  @loginator.log( msg )

  command =
    @tool_executor.build_command_line(
      arg_hash[:tool],
      # Extra arguments
      arg_hash[:flags],
      # Argument replacement
      arg_hash[:source],
      arg_hash[:object],
      arg_hash[:list],
      arg_hash[:dependencies],
      arg_hash[:search_paths],
      arg_hash[:defines]
    )

  begin
    shell_result = @tool_executor.exec( command )
  rescue ShellException => ex
    shell_result = ex.shell_result
    raise ex
  ensure
    arg_hash[:shell_command] = command[:line]
    arg_hash[:shell_result] = shell_result
    @plugin_manager.post_compile_execute(arg_hash)
  end
end

#generate_partial_implementation(test:, partial:, function_definitions:, source_includes:, header_includes:, c_module:, input_filepath:, output_path:) ⇒ Object



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
# File 'lib/ceedling/generators/generator.rb', line 73

def generate_partial_implementation(
    test:,
    partial:,
    function_definitions:,
    source_includes:,
    header_includes:,
    c_module:,
    input_filepath:,
    output_path:
  )

  msg = @reportinator.generate_module_progress(
    operation: "Generating Partial implementation for",
    module_name: test,
    filename: partial # Partial module name, not filename
  )
  @loginator.log( msg )

  arg_hash = {
    :function_definitions => function_definitions,
    :test => test,
    :name => partial,
    :source_includes => source_includes,
    :header_includes => header_includes,
    :c_module => c_module,
    :output_path => output_path
  }

  return @generator_partials.generate_implementation( **arg_hash )
end

#generate_partial_interface(test:, partial:, function_declarations:, includes:, c_module:, input_filepath:, output_path:) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ceedling/generators/generator.rb', line 53

def generate_partial_interface(test:, partial:, function_declarations:, includes:, c_module:, input_filepath:, output_path:)
  msg = @reportinator.generate_module_progress(
    operation: "Generating Partial mockable interface for",
    module_name: test,
    filename: partial # Partial module name, not filename
  )
  @loginator.log( msg )

  arg_hash = {
    :test => test,
    :name => partial,
    :function_declarations => function_declarations,
    :includes => includes,
    :c_module => c_module,
    :output_path => output_path
  }

  return @generator_partials.generate_interface( **arg_hash )
end

#generate_partial_types(name:, c_module:, output_path:) ⇒ Object

A module's typedefs and aggregate definitions are identical regardless of whether a test file tests it, mocks it, or both, so they're generated once into their own header here rather than duplicated by generate_partial_implementation and generate_partial_interface. Returns the bare generated filename (for the caller to add to each header's own includes list) or nil when the module has no typedefs or aggregate definitions to share.



43
44
45
46
47
48
49
50
51
# File 'lib/ceedling/generators/generator.rb', line 43

def generate_partial_types(name:, c_module:, output_path:)
  arg_hash = {
    :name => name,
    :c_module => c_module,
    :output_path => output_path
  }

  return @generator_partials.generate_types( **arg_hash )
end

#generate_test_results(tool:, context:, test_name:, test_filepath:, executable:, result:) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/ceedling/generators/generator.rb', line 361

def generate_test_results(tool:, context:, test_name:, test_filepath:, executable:, result:)
  arg_hash = {
    :tool => tool,
    :context => context,
    :test_name => test_name,
    :test_filepath => test_filepath,
    :executable => executable,
    :result_file => result,
    :msg => @reportinator.generate_progress( "Running #{File.basename(executable)}" )
  }

  @plugin_manager.pre_test_fixture_execute( arg_hash )

  @loginator.log( arg_hash[:msg] )

  # Unity's exit code is equivalent to the number of failed tests.
  # We tell @tool_executor not to fail out if there are failures
  # so that we can run all tests and collect all results.
  command = 
    @tool_executor.build_command_line(
      arg_hash[:tool],
      # Extra arguments: Additional test case filters 
      @test_runner_manager.collect_cmdline_args(),
      # Argument replacement
      arg_hash[:executable]
    )

  # Run the test executable itself
  # We allow it to fail without an exception.
  # We'll analyze its results apart from tool_executor
  command[:options][:boom] = false
  shell_result = @tool_executor.exec( command )
  filename = File.basename( test_filepath )

  # Handle crashes
  if @helper.test_crash?( filename, executable, shell_result )
    @helper.log_test_results_crash(
      executable,
      shell_result,
      @configurator.project_config_hash[:project_use_backtrace]
    )

    # Lookup test cases and filter based on any matchers specified for the build task
    test_cases = @test_context_extractor.lookup_test_cases( test_filepath )
    test_cases = @generator_test_results.filter_test_cases( test_cases )

    case @configurator.project_config_hash[:project_use_backtrace]
    # If we have the options and tools to learn more, dig into the details
    when :gdb
      shell_result =
        @backtrace.do_gdb( filename, executable, shell_result, test_cases, context: context )

    # Simple test-case-by-test-case exercise
    when :simple
      shell_result =
        @backtrace.do_simple( filename, executable, shell_result, test_cases, context: context )

    else # :none
      # Otherwise, call a crash a single failure so it shows up in the report
      shell_result = @generator_test_results.create_crash_failure(
        filename,
        shell_result,
        test_cases
      )
    end
  end
  
  shell_result[:executable] = executable
  shell_result[:result_file] = arg_hash[:result_file]
  shell_result[:test_file] = @file_finder.find_test_file_from_filepath( arg_hash[:executable] )
  processed = @generator_test_results.process_and_write_results( shell_result )

  arg_hash[:result_file]  = processed[:result_file]
  arg_hash[:results]      = processed[:results]
  # For raw output display if no plugins enabled for nice display
  arg_hash[:shell_result] = shell_result

  @plugin_manager.post_test_fixture_execute( arg_hash )

  shell_result
end

#generate_test_runner(context:, mocks:, includes:, test_filepath:, input_filepath:, runner_filepath:) ⇒ Object

Parameters:

  • mocks:

    Array of Mocks to include in the runner

  • includes:

    Array of Includes without mocks to include in the runner



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
# File 'lib/ceedling/generators/generator.rb', line 145

def generate_test_runner(context:, mocks:, includes:, test_filepath:, input_filepath:, runner_filepath:)
  arg_hash = {
    :context => context,
    :test_file => test_filepath,
    :input_file => input_filepath,
    :runner_file => runner_filepath}

  @plugin_manager.pre_runner_generate( arg_hash )

  # Collect info we need
  module_name = File.basename( arg_hash[:test_file] )

  msg = @reportinator.generate_progress("Generating runner for #{module_name}")
  @loginator.log( msg )

  unity_test_runner_generator = 
    @test_context_extractor.lookup_test_runner_generator( test_filepath )

  if unity_test_runner_generator.nil?
    msg = "Could not find test runner generator for #{test_filepath}"
    raise CeedlingException.new( msg )
  end

  # Further filter others to remove vendor files
  others = includes.reject do |include|
    VENDORS_FILES.include?( include.filename.ext() )
  end

  # Build runner file
  begin
    unity_test_runner_generator.generate(
      module_name: module_name,
      runner_filepath: runner_filepath,
      mocks: mocks,
      includes: others
    )
  rescue StandardError => ex
    # Re-raise execption but decorate it to better identify it in Ceedling output
    raise( ex, "Unity Runner Generator >> #{ex.message}", ex.backtrace )
  ensure
    @plugin_manager.post_runner_generate(arg_hash)
  end
end

#setupObject



32
33
34
35
36
# File 'lib/ceedling/generators/generator.rb', line 32

def setup()
  # Aliases
  @helper = @generator_helper
  @backtrace = @generator_test_results_backtrace
end