Class: ConjureShield::TestGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/conjure_shield/test_generator.rb

Constant Summary collapse

EXCLUDED_FACTORY_COLUMNS =
%w[
  id created_at updated_at encrypted_password reset_password_token
  reset_password_sent_at remember_created_at
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, suggestions) ⇒ TestGenerator

Returns a new instance of TestGenerator.



7
8
9
10
11
12
# File 'lib/conjure_shield/test_generator.rb', line 7

def initialize(code, suggestions)
  @code = code
  @suggestions = suggestions
  @codebase_path = Dir.pwd
  @framework = nil
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/conjure_shield/test_generator.rb', line 5

def code
  @code
end

#suggestionsObject (readonly)

Returns the value of attribute suggestions.



5
6
7
# File 'lib/conjure_shield/test_generator.rb', line 5

def suggestions
  @suggestions
end

Class Method Details

.generate(code, suggestions) ⇒ Object



14
15
16
# File 'lib/conjure_shield/test_generator.rb', line 14

def self.generate(code, suggestions)
  new(code, suggestions).generate_all
end

.generate_for_all_frameworks(code, suggestions, codebase_path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/conjure_shield/test_generator.rb', line 25

def self.generate_for_all_frameworks(code, suggestions, codebase_path)
  frameworks = []
  frameworks << :rspec if Dir.exist?(File.join(codebase_path, "spec"))
  if Dir.exist?(File.join(codebase_path, "test"))
    frameworks << :minitest
  else
    FileUtils.mkdir_p(File.join(codebase_path, "test"))
    frameworks << :minitest
    puts "Created test/ directory for Minitest"
  end

  frameworks.each_with_index do |fw, idx|
    new(code, suggestions).tap do |g|
      g.instance_variable_set(:@codebase_path, codebase_path)
      g.instance_variable_set(:@framework, fw)
    end.generate_all(skip_factories: idx > 0)
  end
end

.generate_with_path(code, suggestions, codebase_path) ⇒ Object



18
19
20
21
22
23
# File 'lib/conjure_shield/test_generator.rb', line 18

def self.generate_with_path(code, suggestions, codebase_path)
  new(code, suggestions).tap do |g|
    g.instance_variable_set(:@codebase_path, codebase_path)
    g.instance_variable_set(:@framework, nil)
  end.generate_all
end

Instance Method Details

#collect_factory_specsObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/conjure_shield/test_generator.rb', line 66

def collect_factory_specs
  specs = {}
  @suggestions.each do |missing_test|
    inner = missing_test[:suggestions] || []
    context = missing_test.reject { |k, _| k == :suggestions }
    inner.each do |suggestion|
      next unless suggestion[:type] == :factories

      merged = context.merge(suggestion)
      specs[merged[:model]] = {
        columns: merged[:columns] || {},
        associations: merged[:associations] || []
      }
    end
  end
  specs
end

#factory_association_lines(associations) ⇒ Object



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
165
166
167
168
# File 'lib/conjure_shield/test_generator.rb', line 137

def factory_association_lines(associations)
  associations.flat_map do |assoc|
    name = assoc[:name]
    target = assoc[:target]
    case assoc[:type]
    when :belongs_to
      "    association :#{name}"
    when :has_one, :has_one_through
      [
        "    trait :with_#{name} do",
        "      association :#{name}",
        "    end",
      ]
    when :has_many
      [
        "    trait :with_#{name} do",
        "      association :#{name}",
        "    end",
      ]
    when :has_and_belongs_to_many, :has_many_through
      [
        "    trait :with_#{name} do",
        "      after(:create) do |obj, evaluator|",
        "        obj.#{name} << create(:#{target.underscore})",
        "      end",
        "    end",
      ]
    else
      nil
    end
  end.compact
end

#factory_attribute_line(col_name, type) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/conjure_shield/test_generator.rb', line 117

def factory_attribute_line(col_name, type)
  case col_name
  when "email", /_email$/
    "sequence(:#{col_name}) { |n| \"user_\#{n}@example.com\" }"
  when /_url$/
    "#{col_name} { \"https://example.com/#{col_name}\" }"
  else
    case type
    when :string then "#{col_name} { \"#{col_name.humanize.downcase}\" }"
    when :text then "#{col_name} { \"sample text\" }"
    when :integer then "#{col_name} { 1 }"
    when :boolean then "#{col_name} { false }"
    when :datetime, :date then "#{col_name} { Time.current }"
    when :decimal, :float then "#{col_name} { 1.0 }"
    else
      nil
    end
  end
end

#factory_attribute_lines(columns) ⇒ Object



110
111
112
113
114
115
# File 'lib/conjure_shield/test_generator.rb', line 110

def factory_attribute_lines(columns)
  columns.reject { |name, _| EXCLUDED_FACTORY_COLUMNS.include?(name) }.flat_map do |col_name, col_info|
    line = factory_attribute_line(col_name, col_info[:type])
    line ? "    #{line}" : nil
  end.compact
end

#factory_content(model_name, columns, associations) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/conjure_shield/test_generator.rb', line 84

def factory_content(model_name, columns, associations)
  factory_decl = if model_name.include?("::")
    "  factory :#{factory_name(model_name)}, class: \"#{model_name}\" do"
  else
    "  factory :#{factory_name(model_name)} do"
  end

  lines = [
    "# frozen_string_literal: true",
    "",
    "FactoryBot.define do",
    factory_decl,
    factory_attribute_lines(columns),
    factory_association_lines(associations),
    "  end",
    "end",
    "",
  ]
  lines.flatten.join("\n")
end

#generate_all(framework: nil, skip_factories: false) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/conjure_shield/test_generator.rb', line 44

def generate_all(framework: nil, skip_factories: false)
  @framework = framework if framework
  generate_factories unless skip_factories

  @suggestions.each do |missing_test|
    inner = missing_test[:suggestions] || []
    context = missing_test.reject { |k, _| k == :suggestions }

    inner.each do |suggestion|
      merged = context.merge(suggestion)
      @current_type = merged[:type]
      generate_test(merged)
    end
  end
end

#generate_factoriesObject



60
61
62
63
64
# File 'lib/conjure_shield/test_generator.rb', line 60

def generate_factories
  collect_factory_specs.each do |model_name, info|
    write_factory_file(model_name, factory_content(model_name, info[:columns], info[:associations]))
  end
end

#write_factory_file(model_name, content) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/conjure_shield/test_generator.rb', line 170

def write_factory_file(model_name, content)
  base = @codebase_path || Dir.pwd
  base_dir = File.join(base, rspec? ? "spec" : "test", "factories")
  FileUtils.mkdir_p(base_dir)
  path = File.join(base_dir, "#{factory_name(model_name)}.rb")
  if File.exist?(path)
    puts "Skipped existing factory: #{path}"
    return
  end
  File.write(path, content)
  puts "Generated factory: #{path}"
end