Welcome to mirror list, hosted at ThFree Co, Russian Federation.

normalizer_spec.rb « package_url « sbom « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bbc2bd3ca13143736e82b101e0cd0e662a802c7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rspec-parameterized'

require_relative '../../../support/shared_contexts/lib/sbom/package_url_shared_contexts'

RSpec.describe Sbom::PackageUrl::Normalizer do
  shared_examples 'name normalization' do
    context 'with bitbucket url' do
      let(:type) { 'bitbucket' }
      let(:text) { 'Purl_Spec' }

      it 'downcases text' do
        is_expected.to eq('purl_spec')
      end
    end

    context 'with github url' do
      let(:type) { 'github' }
      let(:text) { 'Purl_Spec' }

      it 'downcases text' do
        is_expected.to eq('purl_spec')
      end
    end

    context 'with pypi url' do
      let(:type) { 'pypi' }
      let(:text) { 'Purl_Spec' }

      it 'downcases text and replaces underscores' do
        is_expected.to eq('purl-spec')
      end
    end

    context 'with other urls' do
      let(:type) { 'npm' }
      let(:text) { 'Purl_Spec' }

      it 'does not change the text' do
        is_expected.to eq(text)
      end
    end
  end

  describe '#normalize_name' do
    subject(:normalize_name) { described_class.new(type: type, text: text).normalize_name }

    it_behaves_like 'name normalization'

    context 'when text is nil' do
      let(:type) { 'npm' }
      let(:text) { nil }

      it 'raises an error' do
        expect { normalize_name }.to raise_error(ArgumentError, 'Name is required')
      end
    end
  end

  describe '#normalize_namespace' do
    subject(:normalize_namespace) { described_class.new(type: type, text: text).normalize_namespace }

    it_behaves_like 'name normalization'

    context 'when text is nil' do
      let(:type) { 'npm' }
      let(:text) { nil }

      it 'allows nil values' do
        expect(normalize_namespace).to be_nil
      end
    end
  end
end