Module: Tng::Utils

Defined in:
lib/tng/utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.center_text_static(text, width = 80) ⇒ Object



442
443
444
445
446
447
448
449
450
# File 'lib/tng/utils.rb', line 442

def self.center_text_static(text, width = 80)
  lines = text.split("\n")
  lines.map do |line|
    # Remove ANSI color codes for length calculation
    clean_line = line.gsub(/\e\[[0-9;]*m/, "")
    padding = [(width - clean_line.length) / 2, 0].max
    " " * padding + line
  end.join("\n")
end

.count_tests_in_file(file_path) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/tng/utils.rb', line 279

def self.count_tests_in_file(file_path)
  return 0 unless File.exist?(file_path)

  begin
    content = File.read(file_path)
    result = Prism.parse(content)
    return 0 unless result.success?

    count_test_nodes(result.value)
  rescue StandardError => e
    puts "⚠️  Warning: Could not parse #{file_path}: #{e.message}" if ENV["DEBUG"]
    0
  end
end

.display_test_counts(passed, failed, skipped, total, success, pastel, terminal_width) ⇒ Object



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
# File 'lib/tng/utils.rb', line 414

def self.display_test_counts(passed, failed, skipped, total, success, pastel, terminal_width)
  passed_icon = pastel.decorate(Tng::UI::Theme.icon(:success), Tng::UI::Theme.color(:primary))
  failed_icon = pastel.decorate(Tng::UI::Theme.icon(:error), Tng::UI::Theme.color(:primary))
  skipped_icon = pastel.decorate("⏭️", Tng::UI::Theme.color(:accent))
  total_icon = pastel.decorate(Tng::UI::Theme.icon(:marker), Tng::UI::Theme.color(:primary))

  passed_text = pastel.decorate("#{passed_icon} #{passed} passed", Tng::UI::Theme.color(:success))
  failed_text = pastel.decorate("#{failed_icon} #{failed} failed", Tng::UI::Theme.color(:error))
  skipped_text = if skipped.positive?
                   pastel.decorate("#{skipped_icon} #{skipped} skipped",
                                   Tng::UI::Theme.color(:warning))
                 else
                   nil
                 end
  total_text = pastel.decorate("#{total_icon} #{total} total", Tng::UI::Theme.color(:secondary))

  results = [passed_text, failed_text, skipped_text, total_text].compact.join(", ")
  puts center_text_static(results, terminal_width)

  # Overall result
  overall_msg = if success
                  pastel.decorate("#{Tng::UI::Theme.icon(:success)} All tests passed!", Tng::UI::Theme.color(:success))
                else
                  pastel.decorate("#{Tng::UI::Theme.icon(:error)} Some tests failed", Tng::UI::Theme.color(:error))
                end
  puts center_text_static(overall_msg, terminal_width)
end

.fixture_contentObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/tng/utils.rb', line 153

def self.fixture_content
  factory_library = Tng.factory_library || "active_record"

  case factory_library.downcase
  when "fixtures"
    load_all_fixtures_data
  when "fabrication", "fabricator"
    load_all_fabricator_data
  when "factory_girl", "factory_bot"
    load_all_factory_data
  when "active_record"
    load_all_active_record_data
  else
    puts "⚠️  Warning: Unknown factory library '#{factory_library}'. Using Active Record object creation as fallback."
    load_all_fixtures_data
  end
end

.format_generation_time(seconds) ⇒ Object



452
453
454
455
456
457
458
459
460
461
462
# File 'lib/tng/utils.rb', line 452

def self.format_generation_time(seconds)
  if seconds < 1
    "#{(seconds * 1000).round}ms"
  elsif seconds < 60
    "#{seconds.round(1)}s"
  else
    minutes = (seconds / 60).floor
    remaining_seconds = (seconds % 60).round
    "#{minutes}m #{remaining_seconds}s"
  end
end

.has_gem?(gem_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.has_gem?(gem_name)
  return false unless defined?(Bundler)

  gemfile_specs = Bundler.load.specs
  gemfile_specs.any? { |spec| spec.name == gem_name }
rescue StandardError
  false
end

.load_all_active_record_dataObject



275
276
277
# File 'lib/tng/utils.rb', line 275

def self.load_all_active_record_data
  {}
end

.load_all_fabricator_dataObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/tng/utils.rb', line 194

def self.load_all_fabricator_data
  fabricator_data = {}
  fabricators_dir = Rails.root.join("spec", "fabricators")

  return fabricator_data unless Dir.exist?(fabricators_dir)

  Dir.glob("#{fabricators_dir}/*_fabricator.rb").each do |fabricator_file|
    model_name = File.basename(fabricator_file, "_fabricator.rb")

    begin
      content = File.read(fabricator_file)
      fabricator_data[model_name] = parse_fabricator_structure(content, model_name)
    rescue StandardError => e
      puts "⚠️  Warning: Could not load fabricator file #{fabricator_file}: #{e.message}"
    end
  end

  fabricator_data
end

.load_all_factory_dataObject



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
# File 'lib/tng/utils.rb', line 214

def self.load_all_factory_data
  factory_data = {}
  factory_dirs = [
    Rails.root.join("spec", "factories"),
    Rails.root.join("test", "factories")
  ]

  factory_dirs.each do |factory_dir|
    next unless Dir.exist?(factory_dir)

    Dir.glob("#{factory_dir}/*.rb").each do |factory_file|
      content = File.read(factory_file)

      begin
        # Extract all factory definitions from the file
        content.scan(/factory\s+:(\w+)\s+do/) do |match|
          model_name = match[0]
          factory_data[model_name] = parse_factory_structure(content, model_name)
        end
      rescue StandardError => e
        puts "⚠️  Warning: Could not load factory file #{factory_file}: #{e.message}"
      end
    end
  end

  factory_data
end

.load_all_fixtures_dataObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/tng/utils.rb', line 171

def self.load_all_fixtures_data
  fixture_data = {}
  # TODO: Load proper folder for Rspec. This is only valid for minitest.
  fixtures_dir = Rails.root.join("test", "fixtures")

  return fixture_data unless Dir.exist?(fixtures_dir)

  fixture_files = Dir.glob("#{fixtures_dir}/*.yml")

  fixture_files.each do |fixture_file|
    model_name = File.basename(fixture_file, ".yml")

    begin
      fixtures = YAML.load_file(fixture_file)
      fixture_data[model_name] = fixtures
    rescue StandardError => e
      puts "⚠️  Warning: Could not load fixture file #{fixture_file}: #{e.message}"
    end
  end

  fixture_data
end

.parse_attribute_value(value) ⇒ Object



270
271
272
273
# File 'lib/tng/utils.rb', line 270

def self.parse_attribute_value(value)
  # Clean up and parse attribute values
  value.strip.gsub(/^["']|["']$/, "")
end

.parse_fabricator_structure(content, model_name) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/tng/utils.rb', line 242

def self.parse_fabricator_structure(content, model_name)
  # Parse fabricator file to extract attribute structure
  attributes = {}

  # Look for Fabricator definitions
  content.scan(/Fabricator\(:#{model_name}\) do \|f\|(.*?)end/m) do |block|
    block[0].scan(/f\.(\w+)\s+(.+)/) do |attr, value|
      attributes[attr] = parse_attribute_value(value)
    end
  end

  { "attributes" => attributes, "type" => "fabricator" }
end

.parse_factory_structure(content, model_name) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/tng/utils.rb', line 256

def self.parse_factory_structure(content, model_name)
  # Parse factory file to extract attribute structure
  attributes = {}

  # Look for factory definitions
  content.scan(/factory :#{model_name} do(.*?)end/m) do |block|
    block[0].scan(/(\w+)\s+(.+)/) do |attr, value|
      attributes[attr] = parse_attribute_value(value)
    end
  end

  { "attributes" => attributes, "type" => "factory" }
end

.parse_minitest_results(output, exit_code, pastel, terminal_width) ⇒ Object



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
# File 'lib/tng/utils.rb', line 377

def self.parse_minitest_results(output, exit_code, pastel, terminal_width)
  # Minitest output example: "7 tests, 15 assertions, 2 failures, 1 errors, 0 skips"
  # or "Run options: --seed 12345"
  # "7 tests, 2 assertions, 0 failures, 0 errors, 0 skips"

  lines = output.lines
  # Look for summary line - could be "X tests" or "X runs"
  summary_line = lines.find { |line| line.match?(/\d+ (?:tests?|runs?),/) }

  if summary_line
    # Extract numbers from summary - handle both "tests" and "runs" format
    match = summary_line.match(/(\d+) (?:tests?|runs?), (\d+) assertions?, (\d+) failures?, (\d+) errors?, (\d+) skips?/)
    if match
      total = match[1].to_i
      failures = match[3].to_i
      errors = match[4].to_i
      skips = match[5].to_i
      passed = total - failures - errors - skips

      display_test_counts(passed, failures + errors, skips, total, exit_code.zero?, pastel, terminal_width)
    else
      puts center_text_static(pastel.decorate("Could not parse Minitest results", Tng::UI::Theme.color(:error)),
                              terminal_width)
      puts center_text_static("Expected format: 'X tests, Y assertions, Z failures, A errors, B skips'",
                              terminal_width)
      puts center_text_static("Got: #{summary_line.strip}", terminal_width)
    end
  else
    puts center_text_static(pastel.decorate("No test results found", Tng::UI::Theme.color(:warning)),
                            terminal_width)
    puts center_text_static("Looking for line with test counts...", terminal_width)
    # Show last few lines for debugging
    last_lines = output.lines.last(3).map(&:strip).join(" | ")
    puts center_text_static("Last lines: #{last_lines}", terminal_width) if output.lines.any?
  end
end

.parse_rspec_json_results(output, exit_code, pastel, terminal_width) ⇒ Object

Test result parsing methods



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/tng/utils.rb', line 326

def self.parse_rspec_json_results(output, exit_code, pastel, terminal_width)
  # Parse RSpec JSON output
  json_data = JSON.parse(output)
  summary = json_data["summary"] || json_data

  total = summary["example_count"] || summary["total_examples"] || 0
  failures = summary["failure_count"] || summary["failures"] || 0
  pending = summary["pending_count"] || summary["pending"] || 0
  errors = summary["errors_outside_of_examples_count"] || 0

  passed = total - failures - pending - errors

  display_test_counts(passed, failures + errors, pending, total, exit_code.zero?, pastel, terminal_width)
rescue JSON::ParserError
  # Fallback to text parsing if JSON fails
  puts center_text_static(
    pastel.decorate("JSON parsing failed, falling back to text parsing",
                    Tng::UI::Theme.color(:warning)), terminal_width
  )
  parse_rspec_results(output, exit_code, pastel, terminal_width)
end

.parse_rspec_results(output, exit_code, pastel, terminal_width) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/tng/utils.rb', line 348

def self.parse_rspec_results(output, exit_code, pastel, terminal_width)
  # RSpec output example: "7 examples, 2 failures"
  # or "Finished in 0.12345 seconds (files took 0.01234 seconds to load)"
  # "7 examples, 2 failures, 1 pending"

  lines = output.lines
  summary_line = lines.find { |line| line.match?(/\d+ examples?,/) }

  if summary_line
    # Extract numbers from summary
    match = summary_line.match(/(\d+) examples?, (\d+) failures?(?:, (\d+) pending)?/)
    if match
      total = match[1].to_i
      failures = match[2].to_i
      pending = match[3].to_i || 0
      passed = total - failures - pending

      display_test_counts(passed, failures, pending, total, exit_code == 0, pastel, terminal_width)
    else
      puts center_text_static(pastel.decorate("Could not parse RSpec results", Tng::UI::Theme.color(:error)),
                              terminal_width)
      puts center_text_static(output.lines.last.strip, terminal_width) if output.lines.any?
    end
  else
    puts center_text_static(pastel.decorate("No test results found", Tng::UI::Theme.color(:warning)),
                            terminal_width)
  end
end

.save_test_file(test_content) ⇒ Object



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
# File 'lib/tng/utils.rb', line 99

def self.save_test_file(test_content)
  puts "📋 Raw API response: #{test_content[0..200]}..." if ENV["DEBUG"]
  parsed_response = JSON.parse(test_content)

  if parsed_response["error"]
    puts "❌ API responded with an error: #{parsed_response["error"]}"
    return
  end
  # Validate required fields
  unless parsed_response["file_content"]
    puts "❌ API response missing file_content field"
    puts "📋 Response keys: #{parsed_response.keys.inspect}"
    return
  end

  # Handle both possible field names for file path
  file_path = parsed_response["test_file_path"] || parsed_response["file_path"] || parsed_response["file_name"] || parsed_response["file"]
  unless file_path
    puts "❌ API response missing test_file_path or file_path field"
    puts "📋 Response keys: #{parsed_response.keys.inspect}"
    return
  end

  begin
    File.write(file_path, parsed_response["file_content"])
  rescue Errno::ENOENT
    # Create directory if it doesn't exist
    FileUtils.mkdir_p(File.dirname(file_path))
    File.write(file_path, parsed_response["file_content"])
  end
  puts "✅ Test generated successfully!"
  absolute_path = File.expand_path(file_path)
  puts "Please review the generated tests at \e]8;;file://#{absolute_path}\e\\#{file_path}\e]8;;\e\\"

  # Count tests in the generated file
  test_count = count_tests_in_file(file_path)

  # Determine run command based on test framework
  run_command = if file_path.include?("/spec/")
                  "bundle exec rspec #{file_path}"
                else
                  "bundle exec rails test #{file_path}"
                end

  # Return file information for CLI to use
  {
    file_path: file_path,
    absolute_path: absolute_path,
    run_command: run_command,
    test_class_name: parsed_response["test_class_name"],
    test_count: test_count
  }
end

Instance Method Details

#camelize(str, first_letter = :upper) ⇒ Object



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
# File 'lib/tng/utils.rb', line 63

def camelize(str, first_letter = :upper)
  # Use Rails' camelize method if available, otherwise use custom implementation
  if defined?(ActiveSupport::Inflector) && ActiveSupport::Inflector.respond_to?(:camelize)
    case first_letter
    when :upper
      ActiveSupport::Inflector.camelize(str, true)
    when :lower
      ActiveSupport::Inflector.camelize(str, false)
    else
      raise ArgumentError, "Invalid option, use either :upper or :lower."
    end
  elsif str.respond_to?(:camelize)
    str.camelize(first_letter)
  else
    # Custom implementation
    result = str.gsub(/(?:^|_)([a-z])/) { Regexp.last_match(1).upcase }
    case first_letter
    when :upper
      result
    when :lower
      result[0].downcase + result[1..] if result.length.positive?
    else
      raise ArgumentError, "Invalid option, use either :upper or :lower."
    end
  end
end

#center_text(text, width = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/tng/utils.rb', line 12

def center_text(text, width = nil)
  width ||= @terminal_width || 80 # Use fallback if still nil
  lines = text.split("\n")
  lines.map do |line|
    # Remove ANSI color codes for length calculation
    clean_line = line.gsub(/\e\[[0-9;]*m/, "")
    padding = [(width - clean_line.length) / 2, 0].max
    " " * padding + line
  end.join("\n")
end

#clear_screenObject



8
9
10
# File 'lib/tng/utils.rb', line 8

def clear_screen
  system("clear") || system("cls")
end

#copy_to_clipboard(text) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tng/utils.rb', line 23

def copy_to_clipboard(text)
  # Try to copy to clipboard
  if system("which pbcopy > /dev/null 2>&1")
    system("echo '#{text}' | pbcopy")
    success_msg = @pastel.green("✅ Command copied to clipboard!")
  elsif system("which xclip > /dev/null 2>&1")
    system("echo '#{text}' | xclip -selection clipboard")
    success_msg = @pastel.green("✅ Command copied to clipboard!")
  else
    success_msg = @pastel.yellow("📋 Copy this command:")
    puts center_text(success_msg)
    puts center_text(@pastel.bright_white(text))
    @prompt.keypress(center_text(@pastel.dim("Press any key to continue...")))
    return
  end

  puts center_text(success_msg)
  @prompt.keypress(center_text(@pastel.dim("Press any key to continue...")))
end

#find_rails_rootObject



53
54
55
56
57
58
59
60
61
# File 'lib/tng/utils.rb', line 53

def find_rails_root
  current_dir = Dir.pwd
  while current_dir != "/"
    return current_dir if File.exist?(File.join(current_dir, "config", "application.rb"))

    current_dir = File.dirname(current_dir)
  end
  nil
end

#load_rails_environmentObject



43
44
45
46
47
48
49
50
51
# File 'lib/tng/utils.rb', line 43

def load_rails_environment
  # Use bundler environment to avoid gem conflicts
  require "bundler/setup"
  require "./config/environment"
  true
rescue LoadError => e
  puts "Failed to load Rails: #{e.message}"
  false
end