From 09bf85f8c67a3e697a48ac80db0b4985a92a0782 Mon Sep 17 00:00:00 2001 From: Edwin Vargas Navarro Date: Sun, 22 Sep 2024 13:26:23 -0700 Subject: [PATCH] Revert "hack?" This reverts commit 72ba8818f26add5504cbe3c8048bcf41e8d619c2. --- spec/support/jekyll.rb | 64 +++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/spec/support/jekyll.rb b/spec/support/jekyll.rb index 05d8b4f..e288d08 100644 --- a/spec/support/jekyll.rb +++ b/spec/support/jekyll.rb @@ -1,20 +1,15 @@ # frozen_string_literal: true require 'jekyll' -require 'yaml' # Tools to build / compile the Jekyll site and extract the sitemap def site_config - # Consider appending jekyll-sitemap to the plugins here, instead of in _config. + # TODO(template): We should standardize the build for specs + # Consider simplifying baseurl + # Consider forcing the desination folder + # Override the local URL too? Would it break the sitemap? # Note: Config keys must be strings and thus use => style hashes. - @site_config ||= Jekyll.configuration({ - 'destination' => DESTINATION, - 'url' => '', - 'baseurl' => '', - 'future' => true, - 'SKIP_CONFIG_VALIDATION' => 'true', - 'sass' => { 'quiet_deps' => true } - }) + @site_config ||= Jekyll.configuration({ 'sass' => { 'quiet_deps' => true } }) end def build_jekyll_site! @@ -24,24 +19,47 @@ def build_jekyll_site! puts 'Site build complete.' end -# Turn a page path into a symbol to used in specs. -# e.g. rspec --tag index -def path_to_sym(path) - return :index if path == '/' - - path.gsub(/^\//, '').gsub(/\/$/, '').gsub('/', '-').to_sym -end - def load_sitemap - # Ensure that you have called build_jekyll_site! first. - sitemap_text = File.read("#{DESTINATION}/sitemap.xml") - sitemap_links = sitemap_text.scan(%r{(.+)}) - sitemap_links.filter_map do |link, *_unused| + # Ensure that you have called + sitemap_text = File.read('_site/sitemap.xml') + sitemap_links = sitemap_text.scan(%r{.+}) + sitemap_links.filter_map do |link| + link = link.gsub("#{site_config['url']}", '').gsub('', '') # Skip non-html pages - # Are there other pages that should be audited for accessibility? + # (FUTURE?) Are there other pages that should be audited for accessibility? # (e.g. PDFs, documents. They'd need a different checker.) next unless link.end_with?('.html') || link.end_with?('/') link end.sort end + +# Start a local Rack server +# https://nts.strzibny.name/how-to-test-static-sites-with-rspec-capybara-and-webkit/ +class StaticSite + attr_reader :root, :server + + def initialize(root) + @root = root + @server = Rack::Files.new(root) + end + + def call(env) + # Remove the /baseurl prefix, which is present in all URLs, but not in the file system. + path = "_site#{env['PATH_INFO'].gsub(site_config['baseurl'], '/')}" + + env['PATH_INFO'] = if path.end_with?('/') && exists?("#{path}index.html") + "#{path}index.html" + elsif !exists?(path) && exists?("#{path}.html") + "#{path}.html" + else + path + end + + server.call(env) + end + + def exists?(path) + File.exist?(File.join(root, path)) + end +end