diff --git a/.gitignore b/.gitignore index 1c8eb8a..3ac1b65 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ results/visual config/tmp* +config/spider_paths* node_modules/ diff --git a/app.rb b/app.rb index 07e8467..537c1ca 100644 --- a/app.rb +++ b/app.rb @@ -13,6 +13,11 @@ end environment = payload['deployment']['environment'] - paths = GovukVisualRegression::VisualDiff::DocumentTypes.new.type_paths('statistics') - GovukVisualRegression::VisualDiff::HerokuRunner.new(paths: paths, environment: environment).run + review_domain = "https://#{environment}.herokuapp.com" + live_domain = "https://#{environment.gsub(/\-pr\-\d+$/, '')}.herokuapp.com" + surge_domain = "#{environment}.surge.sh" + + GovukVisualRegression::VisualDiff::Runner.new(review_domain: review_domain).spider_component_guide + paths = GovukVisualRegression::VisualDiff::SpiderPaths.component_preview_paths + GovukVisualRegression::VisualDiff::HerokuRunner.new(paths: paths, review_domain: review_domain, live_domain: live_domain, surge_domain: surge_domain).run end diff --git a/config/wraith.yaml b/config/wraith.yaml index 674546d..eb8227c 100644 --- a/config/wraith.yaml +++ b/config/wraith.yaml @@ -5,7 +5,6 @@ browser: directory: results/visual screen_widths: - 320x5000 - - 600x4000 - 1080x3000 resize_or_reload: 'resize' mode: diffs_first diff --git a/lib/govuk_visual_regression.rb b/lib/govuk_visual_regression.rb index 3ae626c..54273c5 100644 --- a/lib/govuk_visual_regression.rb +++ b/lib/govuk_visual_regression.rb @@ -13,6 +13,10 @@ def self.wraith_config_template config_file 'wraith.yaml' end + def self.spider_paths + config_file 'spider_paths.yml' + end + def self.config_file(filename) File.expand_path(root_dir + "/config/#{filename}") end diff --git a/lib/govuk_visual_regression/tasks/rakefile.rake b/lib/govuk_visual_regression/tasks/rakefile.rake index 6b2257e..d586eaf 100644 --- a/lib/govuk_visual_regression/tasks/rakefile.rake +++ b/lib/govuk_visual_regression/tasks/rakefile.rake @@ -6,6 +6,14 @@ def load_paths YAML.load(open(ENV.fetch("URI"))) end +def review_domain + ENV.fetch("REVIEW_DOMAIN") +end + +def live_domain + ENV.fetch("LIVE_DOMAIN") +end + namespace :diff do desc 'Set env var `URI` with location of a yaml file containing paths to diff' task visual: ['config:pre_flight_check'] do |_t, args| @@ -20,6 +28,16 @@ namespace :diff do GovukVisualRegression::VisualDiff::Runner.new(paths: paths).run end + desc 'Set env var `REVIEW_DOMAIN` to the domain hosting the newer component guide and `LIVE_DOMAIN` to compare with' + task component_guide: ['spider:component_guide'] do |_t, args| + paths = GovukVisualRegression::VisualDiff::SpiderPaths.component_preview_paths + if paths.any? + GovukVisualRegression::VisualDiff::Runner.new(paths: paths, review_domain: review_domain, live_domain: live_domain).run + else + puts "No paths found" + end + end + desc "clears the results directory" task :clear_results do puts "---> Clearing results directory" @@ -28,6 +46,13 @@ namespace :diff do end end +namespace :spider do + desc 'Set env var `REVIEW_DOMAIN` to the domain hosting the component guide' + task component_guide: ['config:pre_flight_check'] do |_t, args| + GovukVisualRegression::VisualDiff::Runner.new(review_domain: review_domain).spider_component_guide + end +end + namespace :config do desc "Checks that dependencies are in place" task :pre_flight_check do diff --git a/lib/govuk_visual_regression/visual_diff.rb b/lib/govuk_visual_regression/visual_diff.rb index 60ab5f4..9174c7e 100644 --- a/lib/govuk_visual_regression/visual_diff.rb +++ b/lib/govuk_visual_regression/visual_diff.rb @@ -3,6 +3,8 @@ module VisualDiff autoload :Runner, 'govuk_visual_regression/visual_diff/runner' autoload :HerokuRunner, 'govuk_visual_regression/visual_diff/heroku_runner' autoload :WraithConfig, 'govuk_visual_regression/visual_diff/wraith_config' + autoload :WraithSpiderComponentGuideConfig, 'govuk_visual_regression/visual_diff/wraith_spider_component_guide_config' autoload :DocumentTypes, 'govuk_visual_regression/visual_diff/document_types' + autoload :SpiderPaths, 'govuk_visual_regression/visual_diff/spider_paths' end end diff --git a/lib/govuk_visual_regression/visual_diff/heroku_runner.rb b/lib/govuk_visual_regression/visual_diff/heroku_runner.rb index b74308a..421291a 100644 --- a/lib/govuk_visual_regression/visual_diff/heroku_runner.rb +++ b/lib/govuk_visual_regression/visual_diff/heroku_runner.rb @@ -1,13 +1,20 @@ module GovukVisualRegression module VisualDiff class HerokuRunner < Runner + def initialize(paths: [], review_domain: nil, live_domain: nil, surge_domain: nil, kernel: Kernel) + @paths = paths + @kernel = kernel + @review_domain = review_domain + @live_domain = live_domain + @surge_domain = surge_domain + end + def install_surge @kernel.system "yarn global add surge" end def upload_to_surge - surge_domain = @environment ? @environment : "govuk-vr" - @kernel.system "surge --project results/visual/ --domain #{surge_domain}.surge.sh" + @kernel.system "surge --project results/visual/ --domain #{@surge_domain}" end def run diff --git a/lib/govuk_visual_regression/visual_diff/runner.rb b/lib/govuk_visual_regression/visual_diff/runner.rb index 2929a5d..5a2d264 100644 --- a/lib/govuk_visual_regression/visual_diff/runner.rb +++ b/lib/govuk_visual_regression/visual_diff/runner.rb @@ -1,15 +1,25 @@ module GovukVisualRegression module VisualDiff class Runner - def initialize(paths:, environment: nil, kernel: Kernel) + def initialize(paths: [], review_domain: nil, live_domain: nil, kernel: Kernel) @paths = paths @kernel = kernel - @environment = environment + @review_domain = review_domain + @live_domain = live_domain + end + + def spider_component_guide + wraith_config = WraithSpiderComponentGuideConfig.new(review_domain: @review_domain) + wraith_config.write + + cmd = "wraith spider #{wraith_config.location}" + puts "---> Running component guide spider on #{@review_domain}" + puts "running: #{cmd}" + @kernel.system cmd end def run - review_domain = @environment ? "https://#{@environment}.herokuapp.com" : nil - wraith_config = WraithConfig.new(paths: @paths, review_domain: review_domain) + wraith_config = WraithConfig.new(paths: @paths, review_domain: @review_domain, live_domain: @live_domain) wraith_config.write cmd = "wraith capture #{wraith_config.location}" diff --git a/lib/govuk_visual_regression/visual_diff/spider_paths.rb b/lib/govuk_visual_regression/visual_diff/spider_paths.rb new file mode 100644 index 0000000..06046bc --- /dev/null +++ b/lib/govuk_visual_regression/visual_diff/spider_paths.rb @@ -0,0 +1,16 @@ +require 'yaml' + +module GovukVisualRegression + module VisualDiff + module SpiderPaths + def self.paths + YAML.load_file(GovukVisualRegression.spider_paths)['paths'] + end + + # Only test component previews, not the guide itself + def self.component_preview_paths + paths.values.select { |v| v.match(/preview$/) } + end + end + end +end diff --git a/lib/govuk_visual_regression/visual_diff/wraith_config.rb b/lib/govuk_visual_regression/visual_diff/wraith_config.rb index e770302..6dfb6f7 100644 --- a/lib/govuk_visual_regression/visual_diff/wraith_config.rb +++ b/lib/govuk_visual_regression/visual_diff/wraith_config.rb @@ -1,5 +1,4 @@ require 'yaml' -require 'securerandom' module GovukVisualRegression module VisualDiff @@ -9,11 +8,11 @@ class WraithConfig attr_reader :review_domain attr_reader :live_domain - def initialize(paths:, review_domain: nil, live_domain: nil) + def initialize(paths: [], review_domain: nil, live_domain: nil) @paths = paths @live_domain = live_domain @review_domain = review_domain - @location = GovukVisualRegression.config_file("tmp_wraith_config.yaml") + @location = GovukVisualRegression.config_file(temporary_config_file_name) end def config @@ -51,6 +50,10 @@ def path_would_break_wraith?(path) def path_config_name(path) path.gsub('/', '_') end + + def temporary_config_file_name + "tmp_wraith_config.yaml" + end end end end diff --git a/lib/govuk_visual_regression/visual_diff/wraith_spider_component_guide_config.rb b/lib/govuk_visual_regression/visual_diff/wraith_spider_component_guide_config.rb new file mode 100644 index 0000000..156d471 --- /dev/null +++ b/lib/govuk_visual_regression/visual_diff/wraith_spider_component_guide_config.rb @@ -0,0 +1,23 @@ +module GovukVisualRegression + module VisualDiff + class WraithSpiderComponentGuideConfig < WraithConfig + def config + config = YAML.load_file(GovukVisualRegression.wraith_config_template) + config["domains"].delete("live") + config["domains"]["review"] = "#{review_domain}/component-guide" if review_domain + + # http://bbc-news.github.io/wraith/#Spiderfunctionality + config["imports"] = "spider_paths.yml" + + # Skip pages that don't begin with /component-guide + config["spider_skips"] = [/^\/(?!component\-guide)/] + config.delete("paths") + config + end + + def temporary_config_file_name + "tmp_wraith_spider_component_guide_config.yaml" + end + end + end +end diff --git a/spec/govuk_visual_regression/visual_diff/runner_spec.rb b/spec/govuk_visual_regression/visual_diff/runner_spec.rb index 601eb24..b5eeab2 100644 --- a/spec/govuk_visual_regression/visual_diff/runner_spec.rb +++ b/spec/govuk_visual_regression/visual_diff/runner_spec.rb @@ -12,7 +12,7 @@ end it "executes wraith with the appropriate config" do - expect(config_handler_klass).to receive(:new).with(paths: ["/government/stats/foo", "/government/stats/bar"], review_domain: nil) + expect(config_handler_klass).to receive(:new).with(paths: ["/government/stats/foo", "/government/stats/bar"], review_domain: nil, live_domain: nil) expect(config_handler).to receive(:write) expect(kernel).to receive(:system).with("wraith capture #{config_handler.location}") expect(config_handler).to receive(:delete)