Class: Kumi::Dev::GoldenV2::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/kumi/dev/golden_v2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_dir: "golden", io: $stdout) ⇒ Runner

Returns a new instance of Runner.



97
98
99
100
# File 'lib/kumi/dev/golden_v2.rb', line 97

def initialize(base_dir: "golden", io: $stdout)
  @base_dir = base_dir
  @io = io
end

Instance Attribute Details

#base_dirObject (readonly)

Returns the value of attribute base_dir.



95
96
97
# File 'lib/kumi/dev/golden_v2.rb', line 95

def base_dir
  @base_dir
end

#ioObject (readonly)

Returns the value of attribute io.



95
96
97
# File 'lib/kumi/dev/golden_v2.rb', line 95

def io
  @io
end

Instance Method Details

#diff(names:, reprs:) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/kumi/dev/golden_v2.rb', line 176

def diff(names:, reprs:)
  success = true

  each_schema(names) do |schema_name, schema_path|
    each_representation(reprs) do |repr|
      result = verify_representation(schema_name, schema_path, repr)
      next if result[:status] == :passed

      success = false
      io.puts("=== #{schema_name}/#{repr.filename} (#{result[:status]}) ===")
      if result[:diff]
        io.puts(result[:diff])
      elsif result[:error]
        io.puts(result[:error])
      else
        io.puts(format_failure(result))
      end
      io.puts
    end
  end

  success
end

#list_representationsObject



106
107
108
109
110
111
112
113
114
# File 'lib/kumi/dev/golden_v2.rb', line 106

def list_representations
  io.puts "Representations:"
  representation_names.each { |name| io.puts("  #{name}") }
  io.puts
  io.puts "Groups:"
  GROUPS.each do |name, members|
    io.puts("  #{name}=#{members.join(',')}")
  end
end

#list_schemasObject



102
103
104
# File 'lib/kumi/dev/golden_v2.rb', line 102

def list_schemas
  schema_names.each { |name| io.puts(name) }
end

#select_representations(tokens) ⇒ Object

Raises:

  • (ArgumentError)


200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/kumi/dev/golden_v2.rb', line 200

def select_representations(tokens)
  selected = GoldenV2.normalize_repr_tokens(tokens)
  selected = ["all"] if selected.empty?

  names = selected.flat_map do |token|
    GROUPS.fetch(token, [token])
  end

  known = representations_by_name
  unknown = names.uniq.reject { |name| known.key?(name) }
  raise ArgumentError, "unknown representations: #{unknown.join(', ')}" if unknown.any?

  names.uniq.map { |name| known.fetch(name) }
end

#update(names:, reprs:) ⇒ Object



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/kumi/dev/golden_v2.rb', line 116

def update(names:, reprs:)
  any_changed = false
  any_error = false

  each_schema(names) do |schema_name, schema_path|
    expected_dir = File.join(base_dir, schema_name, "expected")
    FileUtils.mkdir_p(expected_dir)

    each_representation(reprs) do |repr|
      result = generate(repr, schema_path)
      if result[:status] == :error
        any_error = true
        io.puts("#{schema_name}/#{repr.filename} (error: #{result[:error]})")
        next
      end

      next if result[:output].nil?

      expected_file = File.join(expected_dir, repr.filename)
      old = File.exist?(expected_file) ? File.read(expected_file) : nil

      if old && normalize(old) == normalize(result[:output])
        io.puts("= #{schema_name}/#{repr.filename}")
        next
      end

      File.write(expected_file, result[:output])
      any_changed = true
      label = old.nil? ? "created" : "updated"
      io.puts("#{label == 'created' ? '+' : '~'} #{schema_name}/#{repr.filename} (#{label})")
    end
  end

  io.puts(any_changed ? "Updated golden files" : "No golden changes")
  !any_error
end

#verify(names:, reprs:) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/kumi/dev/golden_v2.rb', line 153

def verify(names:, reprs:)
  success = true

  each_schema(names) do |schema_name, schema_path|
    failures = []

    each_representation(reprs) do |repr|
      result = verify_representation(schema_name, schema_path, repr)
      failures << result unless %i[passed skipped].include?(result[:status])
    end

    if failures.empty?
      io.puts("#{schema_name}")
    else
      success = false
      summary = failures.map { format_failure(_1) }.join(", ")
      io.puts("#{schema_name} (#{summary})")
    end
  end

  success
end