summaryrefslogtreecommitdiff
path: root/spec/integration/core/cache_spec.rb
blob: a3c0ed62b25c03253aa72b23cd63f222cc39b157 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# frozen_string_literal: true

RSpec.describe Spandx::Core::Cache do
  RSpec.shared_examples 'each data file' do |package_manager, key|
    describe "#licenses_for(#{package_manager})" do
      subject { described_class.new(package_manager, root: root_dir) }

      let(:root_dir) { "#{Spandx.git[key].root}/.index" }

      it 'finds each package quickly' do
        subject.take(10).each do |item|
          expect do
            subject.licenses_for(item[0], item[1])
          end.to perform_under(0.005).sample(10)
        end
      end

      it 'finds each package correctly' do
        subject.take(100).each do |item|
          result = subject.licenses_for(item[0], item[1])
          expect(result).to match_array([item[2]].reject(&:nil?).reject(&:empty?)), "Could not find #{item[0]}:#{item[1]}"
        end
      end

      it 'returns an empty list of unknown packages' do
        expect(subject.licenses_for('x', 'x')).to be_empty
      end
    end
  end

  include_examples 'each data file', 'npm', :cache
  include_examples 'each data file', 'nuget', :cache
  include_examples 'each data file', 'rubygems', :rubygems
  include_examples 'each data file', 'yarn', :cache

  describe '#insert!' do
    subject { described_class.new('rubygems', root: root_dir) }

    let(:root_dir) { Dir.mktmpdir }

    after do
      FileUtils.remove_entry(root_dir)
    end

    context 'when inserting a new record' do
      let(:dependency_name) { SecureRandom.uuid }
      let(:version) { "#{rand(10)}.#{rand(10)}.#{rand(10)}" }

      before do
        subject.insert!(dependency_name, version, ['MIT'])
      end

      specify { expect(subject.licenses_for(dependency_name, version)).to match_array(['MIT']) }
    end

    context 'when attempting to insert invalid entries' do
      specify do
        subject.insert!(nil, '1.1.1', ['MIT'])
        expect(subject.licenses_for(nil, '1.1.1')).to be_empty
      end

      specify do
        subject.insert!('', '1.1.1', ['MIT'])
        expect(subject.licenses_for('', '1.1.1')).to be_empty
      end

      specify do
        subject.insert!('spandx', nil, ['MIT'])
        expect(subject.licenses_for('spandx', nil)).to be_empty
      end

      specify do
        subject.insert!('spandx', nil, ['MIT'])
        expect(File.exist?(File.join(root_dir, 'cf', subject.package_manager))).to be(false)
      end

      specify do
        subject.insert!('spandx', '', ['MIT'])
        expect(subject.licenses_for('spandx', '')).to be_empty
      end

      specify do
        subject.insert!('spandx', '', ['MIT'])
        expect(File.exist?(File.join(root_dir, 'cf', subject.package_manager))).to be(false)
      end
    end
  end

  describe '#rebuild_index' do
    subject { described_class.new('rubygems', root: root_dir) }

    let(:root_dir) { Dir.mktmpdir }

    after do
      FileUtils.remove_entry(root_dir)
    end

    context 'when new items are added to the catalogue' do
      let(:items) do
        [
          ['spandx', '0.0.0', ['MIT']],
          ['bolt', '0.2.0', ['Apache-2.0']],
          ['spandx', '0.1.0', ['MIT']]
        ]
      end
      let(:sorted_items) { items.sort }

      before do
        items.each do |item|
          subject.insert(item[0], item[1], item[2])
        end

        subject.rebuild_index
      end

      it 'sorts each datafile' do
        lines = subject.datafile_for('spandx').absolute_path.readlines
        expect(lines).to eql(lines.sort)
      end

      it 'builds an index that contains the seek position for the start of each line' do
        data_file = subject.datafile_for('spandx')
        data_file.open_file do |io|
          i = 0
          data_file.index.each do |position|
            io.seek(position)
            expect(io.gets).to eql("\"#{sorted_items[i].flatten.join('","')}\"\n")
            i += 1
          end
        end
      end

      specify { expect(subject.licenses_for('bolt', '0.2.0')).to match_array(['Apache-2.0']) }
      specify { expect(subject.licenses_for('spandx', '0.0.0')).to match_array(['MIT']) }
      specify { expect(subject.licenses_for('spandx', '0.1.0')).to match_array(['MIT']) }
    end
  end

  describe '#each' do
    context 'when a single item is present in the cache' do
      subject { described_class.new('rubygems', root: root_dir) }

      let(:root_dir) { Dir.mktmpdir }

      before do
        subject.insert!('spandx', '0.0.0', ['MIT'])
      end

      after do
        FileUtils.remove_entry(root_dir)
      end

      it 'yields each item in the cache' do
        collect = []

        subject.each do |item|
          collect << item
        end

        expect(collect).to match_array([['spandx', '0.0.0', 'MIT']])
      end
    end

    context 'when multiple items are in multiple datafiles' do
      subject { described_class.new('rubygems', root: root_dir) }

      let(:root_dir) { "#{Spandx.git[:rubygems].root}/.index" }

      it 'yields each item in the cache' do
        expect(subject.count).to be > 800_000
      end

      it 'yields each item quickly' do
        expect { subject.take(100_000).count }.to perform_under(0.1).sample(10)
      end
    end
  end
end