summaryrefslogtreecommitdiff
path: root/spec/finders/packages/go/module_finder_spec.rb
blob: a93fd85552964863323491c5b718ad9cd6b6745c (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Packages::Go::ModuleFinder do
  let_it_be(:project) { create :project }
  let_it_be(:other_project) { create :project }

  let(:finder) { described_class.new project, module_name }

  shared_examples 'an invalid path' do
    describe '#module_name' do
      it 'returns the expected name' do
        expect(finder.module_name).to eq(expected_name)
      end
    end

    describe '#execute' do
      it 'returns nil' do
        expect(finder.execute).to be_nil
      end
    end
  end

  describe '#execute' do
    context 'with module name equal to project name' do
      let(:module_name) { base_url(project) }

      it 'returns a module with empty path' do
        mod = finder.execute
        expect(mod).not_to be_nil
        expect(mod.path).to eq('')
      end
    end

    context 'with module name starting with project name and slash' do
      let(:module_name) { base_url(project) + '/mod' }

      it 'returns a module with non-empty path' do
        mod = finder.execute
        expect(mod).not_to be_nil
        expect(mod.path).to eq('mod')
      end
    end

    context 'with a module name not equal to and not starting with project name' do
      let(:module_name) { base_url(other_project) }

      it 'returns nil' do
        expect(finder.execute).to be_nil
      end
    end
  end

  context 'with relative path component' do
    it_behaves_like 'an invalid path' do
      let(:module_name) { base_url(project) + '/../xyz' }
      let(:expected_name) { base_url(project.namespace) + '/xyz' }
    end
  end

  context 'with many relative path components' do
    it_behaves_like 'an invalid path' do
      let(:module_name) { base_url(project) + ('/..' * 10) + '/xyz' }
      let(:expected_name) { ('../' * 7) + 'xyz' }
    end
  end

  def base_url(project)
    "#{Settings.build_gitlab_go_url}/#{project.full_path}"
  end
end