summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/file_detector_spec.rb
blob: 208acf28cc4c4e2453539924a02303a4ab720bc8 (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
# frozen_string_literal: true

require 'fast_spec_helper'

RSpec.describe Gitlab::FileDetector do
  describe '.types_in_paths' do
    it 'returns the file types for the given paths' do
      expect(described_class.types_in_paths(%w(README.md CHANGELOG VERSION VERSION)))
        .to eq(%i{readme changelog version})
    end

    it 'does not include unrecognized file paths' do
      expect(described_class.types_in_paths(%w(README.md foo.txt)))
        .to eq(%i{readme})
    end
  end

  describe '.type_of' do
    it 'returns the type of a README without extension' do
      expect(described_class.type_of('README')).to eq(:readme)
      expect(described_class.type_of('INDEX')).to eq(:readme)
    end

    it 'returns the type of a README file with a recognized extension' do
      extensions = ['txt', *Gitlab::MarkupHelper::EXTENSIONS]

      extensions.each do |ext|
        %w(index readme).each do |file|
          expect(described_class.type_of("#{file}.#{ext}")).to eq(:readme)
        end
      end
    end

    it 'returns nil for a README with unrecognized extension' do
      expect(described_class.type_of('README.rb')).to be_nil
    end

    it 'is case insensitive' do
      expect(described_class.type_of('ReadMe')).to eq(:readme)
      expect(described_class.type_of('index.TXT')).to eq(:readme)
    end

    it 'returns nil for a README file in a directory' do
      expect(described_class.type_of('foo/README.md')).to be_nil
    end

    it 'returns the type of a changelog file' do
      %w(CHANGELOG HISTORY CHANGES NEWS).each do |file|
        expect(described_class.type_of(file)).to eq(:changelog)
      end
    end

    it 'returns the type of a license file' do
      %w(LICENSE LICENCE COPYING UNLICENSE UNLICENCE).each do |file|
        expect(described_class.type_of(file)).to eq(:license)
      end
    end

    it 'returns nil for an UNCOPYING file' do
      expect(described_class.type_of('UNCOPYING')).to be_nil
    end

    it 'returns the type of a version file' do
      expect(described_class.type_of('VERSION')).to eq(:version)
    end

    it 'returns the type of a .gitignore file' do
      expect(described_class.type_of('.gitignore')).to eq(:gitignore)
    end

    it 'returns the type of a GitLab CI config file' do
      expect(described_class.type_of('.gitlab-ci.yml')).to eq(:gitlab_ci)
    end

    it 'returns the type of an avatar' do
      %w(logo.gif logo.png logo.jpg).each do |file|
        expect(described_class.type_of(file)).to eq(:avatar)
      end
    end

    it 'returns the type of an issue template' do
      expect(described_class.type_of('.gitlab/issue_templates/foo.md')).to eq(:issue_template)
    end

    it 'returns the type of a merge request template' do
      expect(described_class.type_of('.gitlab/merge_request_templates/foo.md')).to eq(:merge_request_template)
    end

    it 'returns nil for an unknown file' do
      expect(described_class.type_of('foo.txt')).to be_nil
    end

    it 'returns the type of an OpenAPI spec if file name is correct' do
      openapi_types = [
        'openapi.yml', 'openapi.yaml', 'openapi.json',
        'swagger.yml', 'swagger.yaml', 'swagger.json',
        'gitlab_swagger.yml', 'openapi_gitlab.yml',
        'OpenAPI.YML', 'openapi.Yaml', 'openapi.JSON',
        'openapi.gitlab.yml', 'gitlab.openapi.yml',
        'attention/openapi.yml', 'attention/swagger.yml',
        'attention/gitlab_swagger.yml', 'attention/openapi_gitlab.yml',
        'openapi/openapi.yml', 'openapi/swagger.yml',
        'openapi/my_openapi.yml', 'openapi/swagger_one.yml'
      ]

      openapi_types.each do |type_name|
        expect(described_class.type_of(type_name)).to eq(:openapi)
      end

      openapi_bad_types = [
        'openapiyml',
        'openapi/attention.yaml', 'swagger/attention.yaml'
      ]

      openapi_bad_types.each do |type_name|
        expect(described_class.type_of(type_name)).to be_nil
      end
    end
  end
end