From 62a019f4a163c353d628ca3af481b4c133df9031 Mon Sep 17 00:00:00 2001 From: Marco Costa Date: Mon, 29 Mar 2021 12:23:43 -0400 Subject: [PATCH] Migrate ActionView::Utils tests to RSpec (#1432) --- .../ddtrace/contrib/action_view/utils_spec.rb | 49 +++++++++++++++++++ test/contrib/rails/utils_test.rb | 47 ------------------ 2 files changed, 49 insertions(+), 47 deletions(-) create mode 100644 spec/ddtrace/contrib/action_view/utils_spec.rb delete mode 100644 test/contrib/rails/utils_test.rb diff --git a/spec/ddtrace/contrib/action_view/utils_spec.rb b/spec/ddtrace/contrib/action_view/utils_spec.rb new file mode 100644 index 00000000000..266b95ccab3 --- /dev/null +++ b/spec/ddtrace/contrib/action_view/utils_spec.rb @@ -0,0 +1,49 @@ +RSpec.describe Datadog::Contrib::ActionView::Utils do + describe '#normalize_template_name' do + subject(:normalize_template_name) { described_class.normalize_template_name(name) } + + after { Datadog.configuration[:action_view].reset! } + + context 'with template path' do + let(:name) { '/rails/app/views/welcome/index.html.erb' } + + it { is_expected.to eq('welcome/index.html.erb') } + end + + context 'with nil template' do + let(:name) { nil } + + it { is_expected.to be(nil) } + end + + context 'with file name only' do + let(:name) { 'index.html.erb' } + + it { is_expected.to eq('index.html.erb') } + end + + context 'with template outside of `views/` directory' do + let(:name) { '/rails/app/other/welcome/index.html.erb' } + + it { is_expected.to eq('index.html.erb') } + end + + context 'with a custom template base path' do + before { Datadog.configuration[:action_view][:template_base_path] = 'custom/' } + + context 'with template outside of `views/` directory' do + let(:name) { '/rails/app/custom/welcome/index.html.erb' } + + it { is_expected.to eq('welcome/index.html.erb') } + end + end + + context 'with a non-string-like argument' do + let(:name) { :not_a_string } + + it 'stringifies arguments' do + is_expected.to eq('not_a_string') + end + end + end +end diff --git a/test/contrib/rails/utils_test.rb b/test/contrib/rails/utils_test.rb deleted file mode 100644 index 5b72ae73bd3..00000000000 --- a/test/contrib/rails/utils_test.rb +++ /dev/null @@ -1,47 +0,0 @@ -require 'helper' -require 'ddtrace' - -class UtilsTest < Minitest::Test - def setup - @default_base_template = Datadog.configuration[:rails][:template_base_path] - end - - def teardown - Datadog.configuration[:rails][:template_base_path] = @default_base_template - end - - def test_normalize_template_name - full_template_name = '/opt/rails/app/views/welcome/index.html.erb' - template_name = Datadog::Contrib::ActionView::Utils.normalize_template_name(full_template_name) - assert_equal(template_name, 'welcome/index.html.erb') - end - - def test_normalize_template_name_nil - template_name = Datadog::Contrib::ActionView::Utils.normalize_template_name(nil) - assert_nil(template_name) - end - - def test_normalize_template_name_not_a_path - full_template_name = 'index.html.erb' - template_name = Datadog::Contrib::ActionView::Utils.normalize_template_name(full_template_name) - assert_equal(template_name, 'index.html.erb') - end - - def test_normalize_template_name_without_views_prefix - full_template_name = '/opt/rails/app/custom/welcome/index.html.erb' - template_name = Datadog::Contrib::ActionView::Utils.normalize_template_name(full_template_name) - assert_equal(template_name, 'index.html.erb') - end - - def test_normalize_template_name_with_custom_prefix - Datadog.configuration[:rails][:template_base_path] = 'custom/' - full_template_name = '/opt/rails/app/custom/welcome/index.html.erb' - template_name = Datadog::Contrib::ActionView::Utils.normalize_template_name(full_template_name) - assert_equal(template_name, 'welcome/index.html.erb') - end - - def test_normalize_template_wrong_usage - template_name = Datadog::Contrib::ActionView::Utils.normalize_template_name({}) - assert_equal(template_name, '{}') - end -end