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.



81
82
83
84
# File 'lib/kumi/dev/golden_v2.rb', line 81

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.



79
80
81
# File 'lib/kumi/dev/golden_v2.rb', line 79

def base_dir
  @base_dir
end

#ioObject (readonly)

Returns the value of attribute io.



79
80
81
# File 'lib/kumi/dev/golden_v2.rb', line 79

def io
  @io
end

Instance Method Details

#diff(names:, reprs:) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/kumi/dev/golden_v2.rb', line 160

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



90
91
92
93
94
95
96
97
98
# File 'lib/kumi/dev/golden_v2.rb', line 90

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



86
87
88
# File 'lib/kumi/dev/golden_v2.rb', line 86

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

#select_representations(tokens) ⇒ Object

Raises:

  • (ArgumentError)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/kumi/dev/golden_v2.rb', line 184

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



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
# File 'lib/kumi/dev/golden_v2.rb', line 100

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/kumi/dev/golden_v2.rb', line 137

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 result[:status] == :passed
    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