DEV: Move chrome binary check into a shared lib (#13451)

DEV: Move chrome binary check into a shared lib (#13451)

We had checks for the chrome binary in 3 different places for tests and only one of them checked for google-chrome-stable, which is problematic for Arch linux users (there are dozens of us!)

This PR moves all the code to one place and references it instead of copying and pasting.

diff --git a/lib/autospec/qunit_runner.rb b/lib/autospec/qunit_runner.rb
index ccffc5d..ec76a24 100644
--- a/lib/autospec/qunit_runner.rb
+++ b/lib/autospec/qunit_runner.rb
@@ -1,7 +1,7 @@
 # frozen_string_literal: true
 
 require "demon/rails_autospec"
-require 'rbconfig'
+require "chrome_installed_checker"
 
 module Autospec
 
@@ -38,10 +38,8 @@ module Autospec
 
     require "socket"
 
-    class ChromeNotInstalled < StandardError; end
-
     def initialize
-      ensure_chrome_is_installed
+      ChromeInstalledChecker.run
     end
 
     def start
@@ -117,21 +115,6 @@ module Autospec
 
     private
 
-    def ensure_chrome_is_installed
-      if RbConfig::CONFIG['host_os'][/darwin|mac os/]
-        binary = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
-      elsif system("command -v google-chrome-stable >/dev/null;")
-        binary = "google-chrome-stable"
-      end
-      binary ||= "google-chrome" if system("command -v google-chrome >/dev/null;")
-
-      raise ChromeNotInstalled.new if !binary
-
-      if Gem::Version.new(`\"#{binary}\" --version`.match(/[\d\.]+/)[0]) < Gem::Version.new("59")
-        raise "Chrome 59 or higher is required"
-      end
-    end
-
     def port_available?(port)
       TCPServer.open(port).close
       true
diff --git a/lib/chrome_installed_checker.rb b/lib/chrome_installed_checker.rb
new file mode 100644
index 0000000..bc89ff0
--- /dev/null
+++ b/lib/chrome_installed_checker.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+require "rbconfig"
+
+class ChromeInstalledChecker
+  class ChromeNotInstalled < StandardError; end
+  class ChromeVersionTooLow < StandardError; end
+
+  def self.run
+    if RbConfig::CONFIG['host_os'][/darwin|mac os/]
+      binary = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
+    elsif system("command -v google-chrome-stable >/dev/null;")
+      binary = "google-chrome-stable"
+    end
+    binary ||= "google-chrome" if system("command -v google-chrome >/dev/null;")
+
+    if !binary
+      raise ChromeNotInstalled.new("Chrome is not installed. Download from https://www.google.com/chrome/browser/desktop/index.html")
+    end
+
+    if Gem::Version.new(`\"#{binary}\" --version`.match(/[\d\.]+/)[0]) < Gem::Version.new("59")
+      raise ChromeVersionTooLow.new("Chrome 59 or higher is required")
+    end
+  end
+end
diff --git a/lib/tasks/qunit.rake b/lib/tasks/qunit.rake
index 34ac6c3..ab902a1 100644
--- a/lib/tasks/qunit.rake
+++ b/lib/tasks/qunit.rake
@@ -4,20 +4,12 @@ desc "Runs the qunit test suite"
 
 task "qunit:test", [:timeout, :qunit_path] do |_, args|
   require "socket"
-  require 'rbconfig'
+  require "chrome_installed_checker"
 
-  if RbConfig::CONFIG['host_os'][/darwin|mac os/]
-    google_chrome_cli = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
-  else
-    google_chrome_cli = "google-chrome"
-  end
-
-  unless system("command -v \"#{google_chrome_cli}\" >/dev/null")
-    abort "Chrome is not installed. Download from https://www.google.com/chrome/browser/desktop/index.html"
-  end
-
-  if Gem::Version.new(`\"#{google_chrome_cli}\" --version`.match(/[\d\.]+/)[0]) < Gem::Version.new("59")
-    abort "Chrome 59 or higher is required to run tests in headless mode."
+  begin
+    ChromeInstalledChecker.run
+  rescue ChromeNotInstalled, ChromeVersionTooLow => err
+    abort err.message
   end
 
   unless system("command -v yarn >/dev/null;")
diff --git a/lib/tasks/smoke_test.rake b/lib/tasks/smoke_test.rake
index 5535d6b..4f48dff 100644
--- a/lib/tasks/smoke_test.rake
+++ b/lib/tasks/smoke_test.rake
@@ -2,18 +2,12 @@
 
 desc "run chrome headless smoke tests on current build"
 task "smoke:test" do
-  if RbConfig::CONFIG['host_os'][/darwin|mac os/]
-    google_chrome_cli = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
-  else
-    google_chrome_cli = "google-chrome"
-  end
-
-  unless system("command -v \"#{google_chrome_cli}\" >/dev/null")
-    abort "Chrome is not installed. Download from https://www.google.com/chrome/browser/desktop/index.html"
-  end
+  require "chrome_installed_checker"
 
-  if Gem::Version.new(`\"#{google_chrome_cli}\" --version`.match(/[\d\.]+/)[0]) < Gem::Version.new("59")
-    abort "Chrome 59 or higher is required to run tests in headless mode."
+  begin
+    ChromeInstalledChecker.run
+  rescue ChromeNotInstalled, ChromeVersionTooLow => err
+    abort err.message
   end
 
   system("yarn install")

GitHub sha: 7b31d8a11b1b77899a4eb2ab99933058fd355fbc

This commit appears in #13451 which was approved by tgxworld. It was merged by martin.