Module: Adopter::ProjectDetector

Defined in:
lib/adopter/project_detector.rb

Constant Summary collapse

GEM_AUTOMATION_MAP =
{
  'site_prism' => 'capybara',
  'capybara' => 'capybara',
  'selenium-webdriver' => 'selenium',
  'watir' => 'watir',
  'appium_lib' => 'appium',
  'eyes_selenium' => 'selenium',
  'axe-core-selenium' => 'selenium',
  'axe-core-rspec' => 'selenium'
}.freeze
GEM_FRAMEWORK_MAP =
{
  'cucumber' => 'cucumber',
  'rspec' => 'rspec',
  'minitest' => 'minitest'
}.freeze
BROWSERS =
%w[chrome firefox safari edge].freeze

Class Method Summary collapse

Class Method Details

.detect(path = '.') ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/adopter/project_detector.rb', line 26

def detect(path = '.')
  {
    automation: detect_automation(path),
    framework: detect_framework(path),
    page_path: detect_page_path(path),
    spec_path: detect_spec_path(path),
    feature_path: detect_feature_path(path),
    helper_path: detect_helper_path(path),
    browser: detect_browser(path),
    url: detect_url(path),
    ci_platform: detect_ci_platform(path)
  }.compact
end

.detect_automation(path) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/adopter/project_detector.rb', line 40

def detect_automation(path)
  gems = parse_gemfile(path)
  GEM_AUTOMATION_MAP.each do |gem_name, automation|
    return automation if gems.include?(gem_name)
  end
  detect_automation_from_requires(path)
end

.detect_browser(path) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/adopter/project_detector.rb', line 80

def detect_browser(path)
  config_files = helper_and_config_files(path)
  config_files.each do |file|
    next unless File.exist?(file)

    content = File.read(file)
    BROWSERS.each do |browser|
      return browser if content.match?(/(?:browser|driver)\s*[:=]\s*[:'"]?#{browser}\b/i)
    end
  end
  nil
end

.detect_ci_platform(path) ⇒ Object



105
106
107
108
109
110
# File 'lib/adopter/project_detector.rb', line 105

def detect_ci_platform(path)
  return 'github' if Dir.exist?(File.join(path, '.github', 'workflows'))
  return 'gitlab' if File.exist?(File.join(path, '.gitlab-ci.yml'))

  nil
end

.detect_feature_path(path) ⇒ Object



70
71
72
73
# File 'lib/adopter/project_detector.rb', line 70

def detect_feature_path(path)
  candidates = %w[features features/scenarios]
  find_existing_dir(path, candidates)
end

.detect_framework(path) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/adopter/project_detector.rb', line 48

def detect_framework(path)
  gems = parse_gemfile(path)
  GEM_FRAMEWORK_MAP.each do |gem_name, framework|
    return framework if gems.include?(gem_name)
  end
  return 'rspec' if Dir.exist?(File.join(path, 'spec'))
  return 'cucumber' if Dir.exist?(File.join(path, 'features'))
  return 'minitest' if Dir.exist?(File.join(path, 'test'))

  nil
end

.detect_helper_path(path) ⇒ Object



75
76
77
78
# File 'lib/adopter/project_detector.rb', line 75

def detect_helper_path(path)
  candidates = %w[helpers support spec/support features/support]
  find_existing_dir(path, candidates)
end

.detect_page_path(path) ⇒ Object



60
61
62
63
# File 'lib/adopter/project_detector.rb', line 60

def detect_page_path(path)
  candidates = %w[page_objects/pages page_objects pages page]
  find_existing_dir(path, candidates)
end

.detect_spec_path(path) ⇒ Object



65
66
67
68
# File 'lib/adopter/project_detector.rb', line 65

def detect_spec_path(path)
  candidates = %w[spec spec/features spec/tests test tests]
  find_existing_dir(path, candidates)
end

.detect_url(path) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/adopter/project_detector.rb', line 93

def detect_url(path)
  config_files = helper_and_config_files(path)
  config_files.each do |file|
    next unless File.exist?(file)

    content = File.read(file)
    match = content.match(%r{(?:base_url|url|app_host)\s*[:=]\s*['"]?(https?://[^\s'"]+)})
    return match[1] if match
  end
  nil
end