summaryrefslogtreecommitdiff
path: root/tooling/lib/tooling/test_file_finder.rb
blob: d4a972f759a9d2dc1580fba5578339dda22e96ed (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
# frozen_string_literal: true

require 'ostruct'
require 'set'

module Tooling
  class TestFileFinder
    RUBY_EXTENSION = '.rb'
    EE_PREFIX = 'ee/'

    def initialize(file, foss_test_only: false)
      @file = file
      @foss_test_only = foss_test_only
      @result = Set.new
    end

    def test_files
      contexts = [ee_context, foss_context]
      contexts.flat_map do |context|
        match_test_files_for(context)
      end

      result.to_a
    end

    private

    attr_reader :file, :foss_test_only, :result

    def ee_context
      OpenStruct.new.tap do |ee|
        ee.app = %r{^#{EE_PREFIX}app/(.+)\.rb$} unless foss_test_only
        ee.lib = %r{^#{EE_PREFIX}lib/(.+)\.rb$} unless foss_test_only
        ee.spec = %r{^#{EE_PREFIX}spec/(.+)_spec.rb$} unless foss_test_only
        ee.spec_dir = "#{EE_PREFIX}spec" unless foss_test_only
        ee.ee_modules = %r{^#{EE_PREFIX}(?!spec)(.*\/)ee/(.+)\.rb$}
        ee.ee_module_spec = %r{^#{EE_PREFIX}spec/(.*\/)ee/(.+)\.rb$}
        ee.foss_spec_dir = 'spec'
      end
    end

    def foss_context
      OpenStruct.new.tap do |foss|
        foss.app = %r{^app/(.+)\.rb$}
        foss.lib = %r{^lib/(.+)\.rb$}
        foss.tooling = %r{^(tooling/lib/.+)\.rb$}
        foss.spec = %r{^spec/(.+)_spec.rb$}
        foss.spec_dir = 'spec'
      end
    end

    def match_test_files_for(context)
      if (match = context.app&.match(file))
        result << "#{context.spec_dir}/#{match[1]}_spec.rb"
      end

      if (match = context.lib&.match(file))
        result << "#{context.spec_dir}/lib/#{match[1]}_spec.rb"
      end

      if (match = context.tooling&.match(file))
        result << "#{context.spec_dir}/#{match[1]}_spec.rb"
      end

      if context.spec&.match(file)
        result << file
      end

      if (match = context.ee_modules&.match(file))
        result << "#{context.foss_spec_dir}/#{match[1]}#{match[2]}_spec.rb"
      end

      if (match = context.ee_module_spec&.match(file))
        result << "#{context.foss_spec_dir}/#{match[1]}#{match[2]}.rb"
      end
    end
  end
end