Sha256: 7738789eee65140688bec8bc9c44f37bb38ca09a713bf57833e348e566611367

Contents?: true

Size: 1.83 KB

Versions: 30

Compression:

Stored size: 1.83 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'openstax_kitchen'
require 'rspec'
require 'byebug'
require 'rainbow'

def parse_doc(filename)
  Nokogiri::XML(File.open(filename)) { |config| config.noblanks }
end

def parse_doc_and_prep_for_diff(filename)
  doc = parse_doc(filename)
  string = doc.to_xhtml
  # Get rid of whitespace at the start of lines (remove impact of indentation)
  string.gsub(/^\s*/, '')
end

if ARGV.empty?
  puts "Usage: check_it tutorial-folder-number [recipe-name]\n" \
       "  e.g. check_it 02"
  exit(0)
end

tutorial_number = ARGV[0]

Dir[File.expand_path(__dir__ + "/#{tutorial_number}/*.rb")].sort.each { |f| require f }

recipe_name = (ARGV[1] || 'my_recipe')
recipe_variable_name = "@#{recipe_name}".to_sym

unless instance_variables.include?(recipe_variable_name)
  puts Rainbow("There is no recipe named '#{recipe_variable_name}' defined in the " \
               "tutorials/#{tutorial_number} directory").red
  exit(1)
end

recipe = instance_variable_get(recipe_variable_name)

Kitchen::Oven.bake(
  input_file: File.expand_path("#{tutorial_number}/raw.html", __dir__),
  recipes: recipe,
  output_file: File.expand_path("#{tutorial_number}/actual_baked.html", __dir__)
)

actual = parse_doc_and_prep_for_diff("#{tutorial_number}/actual_baked.html")
expected = parse_doc_and_prep_for_diff("#{tutorial_number}/expected_baked.html")

differ = RSpec::Support::Differ.new(color: true)
diff = differ.diff(expected, actual)

# Get rid of some coloring junk at the start
diff.sub!(/\e\[0m\n\e\[0m/, '')

if diff.blank?
  puts Rainbow('The actual output matches the expected output! Way to go!').green
else
  puts Rainbow('The actual output does not match the expected output.').yellow
  puts "#{Rainbow('-').red} = actual output"
  puts "#{Rainbow('+').green} = expected output"
  puts "\n"
  puts diff
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
openstax_kitchen-19.0.0 tutorials/check_it
openstax_kitchen-18.0.0 tutorials/check_it
openstax_kitchen-17.1.0 tutorials/check_it
openstax_kitchen-17.0.0 tutorials/check_it
openstax_kitchen-16.0.0 tutorials/check_it
openstax_kitchen-15.0.0 tutorials/check_it
openstax_kitchen-14.0.0 tutorials/check_it
openstax_kitchen-13.0.0 tutorials/check_it
openstax_kitchen-12.2.0 tutorials/check_it
openstax_kitchen-12.1.0 tutorials/check_it
openstax_kitchen-12.0.0 tutorials/check_it
openstax_kitchen-11.2.0 tutorials/check_it
openstax_kitchen-11.1.0 tutorials/check_it
openstax_kitchen-11.0.0 tutorials/check_it
openstax_kitchen-10.0.0 tutorials/check_it
openstax_kitchen-9.2.0 tutorials/check_it
openstax_kitchen-9.1.0 tutorials/check_it
openstax_kitchen-9.0.0 tutorials/check_it
openstax_kitchen-8.0.1 tutorials/check_it
openstax_kitchen-8.0.0 tutorials/check_it