2021-04-23 12:09:52 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-06-08 15:10:00 +00:00
|
|
|
require 'spec_helper'
|
2021-04-23 12:09:52 +00:00
|
|
|
|
2021-06-08 15:10:00 +00:00
|
|
|
RSpec.describe Gitlab::SnowplowEventDefinitionGenerator, :silence_stdout do
|
2021-04-23 12:09:52 +00:00
|
|
|
let(:ce_temp_dir) { Dir.mktmpdir }
|
|
|
|
let(:ee_temp_dir) { Dir.mktmpdir }
|
2022-01-27 18:14:37 +00:00
|
|
|
let(:timestamp) { Time.current.to_i }
|
2021-04-23 12:09:52 +00:00
|
|
|
let(:generator_options) { { 'category' => 'Groups::EmailCampaignsController', 'action' => 'click' } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_const("#{described_class}::CE_DIR", ce_temp_dir)
|
|
|
|
stub_const("#{described_class}::EE_DIR", ee_temp_dir)
|
|
|
|
end
|
|
|
|
|
2022-01-27 18:14:37 +00:00
|
|
|
around do |example|
|
|
|
|
freeze_time { example.run }
|
|
|
|
end
|
|
|
|
|
2021-04-23 12:09:52 +00:00
|
|
|
after do
|
|
|
|
FileUtils.rm_rf([ce_temp_dir, ee_temp_dir])
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Creating event definition file' do
|
|
|
|
before do
|
|
|
|
stub_const('Gitlab::VERSION', '13.11.0-pre')
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:sample_event_dir) { 'lib/generators/gitlab/snowplow_event_definition_generator' }
|
2022-01-27 18:14:37 +00:00
|
|
|
let(:file_name) { Dir.children(ce_temp_dir).first }
|
2021-04-23 12:09:52 +00:00
|
|
|
|
|
|
|
it 'creates CE event definition file using the template' do
|
|
|
|
sample_event = ::Gitlab::Config::Loader::Yaml.new(fixture_file(File.join(sample_event_dir, 'sample_event.yml'))).load_raw!
|
|
|
|
|
|
|
|
described_class.new([], generator_options).invoke_all
|
|
|
|
|
2022-01-27 18:14:37 +00:00
|
|
|
event_definition_path = File.join(ce_temp_dir, file_name)
|
2021-04-23 12:09:52 +00:00
|
|
|
expect(::Gitlab::Config::Loader::Yaml.new(File.read(event_definition_path)).load_raw!).to eq(sample_event)
|
|
|
|
end
|
|
|
|
|
2022-01-27 18:14:37 +00:00
|
|
|
describe 'generated filename' do
|
|
|
|
it 'includes timestamp' do
|
|
|
|
described_class.new([], generator_options).invoke_all
|
|
|
|
|
|
|
|
expect(file_name).to include(timestamp.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes special characters' do
|
|
|
|
generator_options = { 'category' => '"`ui:[mavenpackages | t5%348()-=@ ]`"', 'action' => 'click' }
|
|
|
|
|
|
|
|
described_class.new([], generator_options).invoke_all
|
|
|
|
|
|
|
|
expect(file_name).to include('uimavenpackagest')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cuts name if longer than 100 characters' do
|
|
|
|
generator_options = { 'category' => 'a' * 100, 'action' => 'click' }
|
|
|
|
|
|
|
|
described_class.new([], generator_options).invoke_all
|
|
|
|
|
|
|
|
expect(file_name.length).to eq(100)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-05 09:10:02 +00:00
|
|
|
context 'event definition already exists' do
|
|
|
|
before do
|
|
|
|
stub_const('Gitlab::VERSION', '12.11.0-pre')
|
|
|
|
described_class.new([], generator_options).invoke_all
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'overwrites event definition --force flag set to true' do
|
|
|
|
sample_event = ::Gitlab::Config::Loader::Yaml.new(fixture_file(File.join(sample_event_dir, 'sample_event.yml'))).load_raw!
|
|
|
|
|
|
|
|
stub_const('Gitlab::VERSION', '13.11.0-pre')
|
|
|
|
described_class.new([], generator_options.merge('force' => true)).invoke_all
|
|
|
|
|
2022-01-27 18:14:37 +00:00
|
|
|
event_definition_path = File.join(ce_temp_dir, file_name)
|
2021-05-05 09:10:02 +00:00
|
|
|
event_data = ::Gitlab::Config::Loader::Yaml.new(File.read(event_definition_path)).load_raw!
|
|
|
|
|
|
|
|
expect(event_data).to eq(sample_event)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises error when --force flag set to false' do
|
|
|
|
expect { described_class.new([], generator_options.merge('force' => false)).invoke_all }
|
|
|
|
.to raise_error(StandardError, /Event definition already exists at/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-27 18:14:37 +00:00
|
|
|
describe 'EE' do
|
|
|
|
let(:file_name) { Dir.children(ee_temp_dir).first }
|
2021-04-23 12:09:52 +00:00
|
|
|
|
2022-01-27 18:14:37 +00:00
|
|
|
it 'creates EE event definition file using the template' do
|
|
|
|
sample_event = ::Gitlab::Config::Loader::Yaml.new(fixture_file(File.join(sample_event_dir, 'sample_event_ee.yml'))).load_raw!
|
2021-04-23 12:09:52 +00:00
|
|
|
|
2022-01-27 18:14:37 +00:00
|
|
|
described_class.new([], generator_options.merge('ee' => true)).invoke_all
|
|
|
|
|
|
|
|
event_definition_path = File.join(ee_temp_dir, file_name)
|
|
|
|
expect(::Gitlab::Config::Loader::Yaml.new(File.read(event_definition_path)).load_raw!).to eq(sample_event)
|
|
|
|
end
|
2021-04-23 12:09:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|