Commit bea43e50 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch...

Merge branch '299291-validated-uniqueness-of-the-key_path-on-usage-metric-definition-generator' into 'master'

Validate uniqueness of key_path in usage metric definition generator

See merge request gitlab-org/gitlab!55236
parents 79e19c5e 1df50643
......@@ -74,6 +74,7 @@ module Gitlab
def validate!
raise "--dir option is required" unless input_dir.present?
raise "Invalid dir #{input_dir}, allowed options are #{VALID_INPUT_DIRS.join(', ')}" unless directory.present?
raise "Metric definition with key path '#{key_path}' already exists" if metric_definition_exists?
end
def ee?
......@@ -88,11 +89,23 @@ module Gitlab
#
# 20210201124931_g_project_management_issue_title_changed_weekly.yml
def file_name
"#{Time.now.utc.strftime("%Y%m%d%H%M%S")}_#{key_path.split('.').last}"
"#{Time.now.utc.strftime("%Y%m%d%H%M%S")}_#{metric_name}"
end
def directory
@directory ||= TIME_FRAME_DIRS.find { |d| d.match?(input_dir) }
end
def metric_name
key_path.split('.').last
end
def metric_definitions
@definitions ||= Gitlab::Usage::MetricDefinition.definitions
end
def metric_definition_exists?
metric_definitions[key_path].present?
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.configure do |config|
# Redirect stdout so specs don't have so much noise
config.before(:all) do
$stdout = StringIO.new
end
# Reset stdout
config.after(:all) do
$stdout = STDOUT
end
end
# frozen_string_literal: true
require 'spec_helper'
require 'rails/generators/testing/behaviour'
require 'generator_helper'
RSpec.describe Gitlab::UsageMetricDefinitionGenerator do
describe 'Validation' do
let(:key_path) { 'counter.category.event' }
let(:dir) { '7d' }
let(:options) { [key_path, '--dir', dir, '--pretend'] }
subject { described_class.start(options) }
it 'does not raise an error' do
expect { subject }.not_to raise_error
end
context 'with a missing directory' do
let(:options) { [key_path, '--pretend'] }
it 'raises an error' do
expect { subject }.to raise_error(RuntimeError)
end
end
context 'with an invalid directory' do
let(:dir) { '8d' }
it 'raises an error' do
expect { subject }.to raise_error(RuntimeError)
end
end
context 'with an already existing metric with the same key_path' do
before do
allow(Gitlab::Usage::MetricDefinition).to receive(:definitions).and_return(Hash[key_path, 'definition'])
end
it 'raises an error' do
expect { subject }.to raise_error(RuntimeError)
end
end
end
describe 'Name suggestions' do
let(:temp_dir) { Dir.mktmpdir }
before do
......@@ -33,4 +71,5 @@ RSpec.describe Gitlab::UsageMetricDefinitionGenerator do
expect(YAML.safe_load(File.read(metric_definition_path)).keys).not_to include(:name)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment