Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions spec/modularization_statistics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,6 @@ module ModularizationStatistics # rubocop:disable RSpec/DescribedClassModuleWrap
end

describe 'ModularizationStatistics.get_metrics' do
RSpec::Matchers.define(:include_metric) do |expected_metric|
match do |actual_metrics|
@actual_metrics = actual_metrics
@expected_metric = expected_metric
@matching_metrics = actual_metrics.select { |actual_metric| actual_metric.name == expected_metric.name }
@actual_metric = @matching_metrics.find { |matching_metric| matching_metric.count == expected_metric.count && expected_metric.tags.sort_by(&:key) == matching_metric.tags.sort_by(&:key) }
@matching_metrics.any? && !@actual_metric.nil?
end

description do
"to have a metric named `#{expected_metric.name}` with count of #{expected_metric.count} and tags of #{expected_metric.tags.map(&:to_s)}"
end

failure_message do
if @matching_metrics.none?
"Could not find metric with name `#{expected_metric.name}` Could only find metrics with names: #{@actual_metrics.map(&:name)}"
else
count_diff = "Actual count: #{@matching_metrics.map(&:count)}\nExpected count: #{expected_metric.count}"
actual_tags = @matching_metrics.map { |matching_metric| matching_metric.tags.map(&:to_s) }
expected_tags = expected_metric.tags.map(&:to_s)
tags_diff = "Actual tags (not in expected): #{actual_tags.map { |actual| actual - expected_tags }}\nExpected tags (not in actual): #{expected_tags - actual_tags}"
<<~FAILURE_MESSAGE
Expected and actual metric `#{expected_metric.name}` are not equal. Found #{@matching_metrics.count} metrics with matching name `#{@expected_metric.name}`, but the properties are different
#{count_diff}
#{tags_diff}
FAILURE_MESSAGE
end
end
end

let(:subject) do
ModularizationStatistics.get_metrics(
app_name: 'MyApp',
Expand Down
40 changes: 40 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,43 @@ def sorbet_double(stubbed_class, attr_map = {})
allow(CodeOwnership).to receive(:for_file).and_return(sorbet_double(CodeTeams::Team, name: 'Some team'))
end
end

RSpec::Matchers.define(:include_metric) do |expected_metric|
match do |actual_metrics|
@actual_metrics = actual_metrics
@expected_metric = expected_metric
@metrics_with_same_name = actual_metrics.select { |actual_metric| actual_metric.name == expected_metric.name }
@matching_metric = @metrics_with_same_name.find { |matching_metric| matching_metric.count == expected_metric.count && expected_metric.tags.sort_by(&:key) == matching_metric.tags.sort_by(&:key) }
@metrics_with_same_name.any? && !@matching_metric.nil?
end

description do
"to have a metric named `#{expected_metric.name}` with count of #{expected_metric.count} and tags of #{expected_metric.tags.map(&:to_s)}"
end

failure_message do
if @metrics_with_same_name.none?
"Could not find metric:\n\n#{expected_metric}\n\nCould only find metrics with names: \n\n#{@actual_metrics.sort_by(&:name).uniq.join("\n")}"
else

# We colorize each part of the output red (if there is no match) or green (if there is a partial match)
colorized_metrics = @metrics_with_same_name.sort_by(&:name).map do |actual_metric|
count_equal = actual_metric.count == expected_metric.count
with_count = "with count #{actual_metric.count}"
colorized_with_count = count_equal ? Rainbow(with_count).green : Rainbow(with_count).red

tags = actual_metric.tags.sort_by(&:key).map do |tag|
if expected_metric.tags.include?(tag)
Rainbow(tag.to_s).green
else
Rainbow(tag.to_s).red
end
end

"#{Rainbow(actual_metric.name).green} #{colorized_with_count}, with tags #{tags.join(', ')}"
end

"Could not find metric:\n\n#{expected_metric}\n\nCould only find metrics: \n\n#{colorized_metrics.join("\n")}"
end
end
end