summaryrefslogtreecommitdiff
path: root/spec/unit/core/guess_spec.rb
blob: 058b4f6a53fba91a4c539aa3d6a8102f4a9bc2ae (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
# frozen_string_literal: true

RSpec.describe Spandx::Core::Guess do
  subject { described_class.new(catalogue) }

  let(:catalogue) { Spandx::Spdx::Catalogue.from_file(fixture_file('spdx/json/licenses.json')) }
  let(:active_licenses) { catalogue.find_all { |x| !x.deprecated_license_id? } }

  describe '#license_for' do
    context 'when detecting a license by id' do
      specify do
        active_licenses.each do |license|
          expect(subject.license_for(license.id)).to eql(license)
        end
      end
    end

    context 'when detecting a license by name' do
      pending do
        active_licenses.each do |license|
          expect(subject.license_for(license.name)).to eql(license)
        end
      end
    end

    context 'when detecting a license by text' do
      pending do
        active_licenses.each do |license|
          content = Spandx.git[:spdx].read("text/#{license.id}.txt")
          expect(subject.license_for(content)).to eql(license)
        end
      end
    end

    specify 'does not contain any duplicate names' do
      items = Hash.new { |hash, key| hash[key] = 0 }
      active_licenses.each { |license| items[license.name] += 1 }
      expect(items.find_all { |_x, y| y > 1 }).to be_empty
    end

    context 'when guessing the spandx license' do
      let!(:content) { IO.read('LICENSE.txt') }

      specify { expect(subject.license_for(content)&.id).to eql('MIT') }
    end

    specify { expect(subject.license_for(%w[MIT 0BSD]).id).to eql('MIT AND 0BSD') }
    specify { expect(subject.license_for('(0BSD OR MIT)')&.id).to eql('0BSD OR MIT') }
    specify { expect(subject.license_for('(BSD-2-Clause OR MIT OR Apache-2.0)')&.id).to eql('BSD-2-Clause OR MIT OR Apache-2.0') }
    specify { expect(subject.license_for('(BSD-3-Clause OR GPL-2.0)')&.id).to eql('BSD-3-Clause OR GPL-2.0') }
    specify { expect(subject.license_for('(MIT AND CC-BY-3.0)')&.id).to eql('MIT AND CC-BY-3.0') }
    specify { expect(subject.license_for('(MIT AND Zlib)')&.id).to eql('MIT AND Zlib') }
    specify { expect(subject.license_for('(MIT OR Apache-2.0)')&.id).to eql('MIT OR Apache-2.0') }
    specify { expect(subject.license_for('(MIT OR CC0-1.0)')&.id).to eql('MIT OR CC0-1.0') }
    specify { expect(subject.license_for('(MIT OR GPL-3.0)')&.id).to eql('MIT OR GPL-3.0') }
    specify { expect(subject.license_for('(WTFPL OR MIT)')&.id).to eql('WTFPL OR MIT') }
    specify { expect(subject.license_for('Apache 2.0')&.id).to eql('Apache-2.0') }
    specify { expect(subject.license_for('BSD-3-Clause OR MIT')&.id).to eql('BSD-3-Clause OR MIT') }
    specify { expect(subject.license_for('BSD-like')&.id).to eql('Nonstandard') }
    specify { expect(subject.license_for('Common Public License Version 1.0')&.id).to eql('CPL-1.0') }
    specify { expect(subject.license_for('MIT or GPLv3')&.id).to eql('MIT OR Nonstandard') }
    pending { expect(subject.license_for('MIT/X11')&.id).to eql('X11') }
  end
end