Skip to content

Commit

Permalink
Add basic Cucumber tests to assess rendering tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmaroli committed Sep 14, 2018
1 parent 187a46d commit f8dc987
Show file tree
Hide file tree
Showing 7 changed files with 432 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .travis.yml
@@ -1,3 +1,5 @@
language: ruby
rvm:
- 2.3.3
- 2.3.3
script:
- bundle exec cucumber
224 changes: 224 additions & 0 deletions features/step_definitions.rb
@@ -0,0 +1,224 @@
# frozen_string_literal: true

Before do
FileUtils.rm_rf(Paths.test_dir) if Paths.test_dir.exist?
FileUtils.mkdir_p(Paths.test_dir) unless Paths.test_dir.directory?
Dir.chdir(Paths.test_dir)
end

#

After do
FileUtils.rm_rf(Paths.test_dir) if Paths.test_dir.exist?
Paths.output_file.delete if Paths.output_file.exist?
Paths.status_file.delete if Paths.status_file.exist?
Dir.chdir(Paths.test_dir.parent)
end

#

Given(%r!^I do not have a "(.*)" directory$!) do |path|
Paths.test_dir.join(path).directory?
end

#

Given(%r!^I have an? "(.*)" page(?: with (.*) "(.*)")? that contains "(.*)"$!) do |file, key, value, text|
File.write(file, Jekyll::Utils.strip_heredoc(<<-DATA))
---
#{key || "layout"}: #{value || "none"}
---
#{text}
DATA
end

#

Given(%r!^I have an? "(.*)" file that contains "(.*)"$!) do |file, text|
File.write(file, text)
end

#

Given(%r!^I have an? (.*) (layout|theme) that contains "(.*)"$!) do |name, type, text|
folder = type == "layout" ? "_layouts" : "_theme"

destination_file = Pathname.new(File.join(folder, "#{name}.html"))
FileUtils.mkdir_p(destination_file.parent) unless destination_file.parent.directory?
File.write(destination_file, text)
end

#

Given(%r!^I have an? "(.*)" file with content:$!) do |file, text|
File.write(file, text)
end

#

Given(%r!^I have an? (.*) directory$!) do |dir|
unless File.directory?(dir)
then FileUtils.mkdir_p(dir)
end
end

#

Given(%r!^I have the following (draft|page|post)s?(?: (in|under) "([^"]+)")?:$!) do |status, direction, folder, table|
table.hashes.each do |input_hash|
title = slug(input_hash["title"])
ext = input_hash["type"] || "markdown"
filename = "#{title}.#{ext}" if %w(draft page).include?(status)
before, after = location(folder, direction)
dest_folder = "_drafts" if status == "draft"
dest_folder = "_posts" if status == "post"
dest_folder = "" if status == "page"

if status == "post"
parsed_date = Time.xmlschema(input_hash["date"]) rescue Time.parse(input_hash["date"])
input_hash["date"] = parsed_date
filename = "#{parsed_date.strftime("%Y-%m-%d")}-#{title}.#{ext}"
end

path = File.join(before, dest_folder, after, filename)
File.write(path, file_content_from_hash(input_hash))
end
end

#

Given(%r!^I have the following (draft|post)s? within the "(.*)" directory:$!) do |type, folder, table|
table.hashes.each do |input_hash|
title = slug(input_hash["title"])
parsed_date = Time.xmlschema(input_hash["date"]) rescue Time.parse(input_hash["date"])

filename = type == "draft" ? "#{title}.markdown" : "#{parsed_date.strftime("%Y-%m-%d")}-#{title}.markdown"

path = File.join(folder, "_#{type}s", filename)
File.write(path, file_content_from_hash(input_hash))
end
end

#

Given(%r!^I have a fixture configuration file$!) do
File.write("_config.yml", YAML.dump(WEBMENTIONS_DEFAULT_CONF))
end

#

Given(%r!^I have a fixture configuration file with:$!) do |table|
table.hashes.each do |row|
step %(I have a fixture configuration file with "#{row["key"]}" set to "#{row["value"]}")
end
end

#

Given(%r!^I have a fixture configuration file with "(.*)" set to "(.*)"$!) do |key, value|
config = WEBMENTIONS_DEFAULT_CONF
config["webmentions"][key] = SafeYAML.load(value)
File.write("_config.yml", YAML.dump(config))
end

#

Given(%r!^I have a fixture configuration file with subkey "(.*)" set to:$!) do |key, table|
config = WEBMENTIONS_DEFAULT_CONF
table.hashes.each do |row|
value = if row["value"] == "false"
false
elsif row["value"] == "true"
true
else
row["value"]
end
config["webmentions"][key] ||= {}
config["webmentions"][key][row["key"]] = value
end
File.write("_config.yml", YAML.dump(config))
end

#

Given(%r!^I have a fixture configuration file with "([^\"]*)" set to:$!) do |key, table|
plugin_setting = %w(jekyll-webmention_io)
File.open("_config.yml", "w") do |f|
f.write("plugins: #{plugin_setting}")
f.write("#{key}:\n")
table.hashes.each do |row|
f.write("- #{row["value"]}\n")
end
end
end

#

When(%r!^I run jekyll(.*)$!) do |args|
run_jekyll(args)
if args.include?("--verbose") || ENV["DEBUG"]
warn "\n#{jekyll_run_output}\n"
end
end

#

Then(%r!^the (.*) directory should +(not )?exist$!) do |dir, negative|
if negative.nil?
expect(Pathname.new(dir)).to exist
else
expect(Pathname.new(dir)).to_not exist
end
end

#

Then(%r!^I should (not )?see "(.*)" in "(.*)"$!) do |negative, text, file|
step %(the "#{file}" file should exist)
regexp = Regexp.new(text, Regexp::MULTILINE)
if negative.nil? || negative.empty?
expect(file_contents(file)).to match regexp
else
expect(file_contents(file)).not_to match regexp
end
end

#

Then(%r!^I should see exactly "(.*)" in "(.*)"$!) do |text, file|
step %(the "#{file}" file should exist)
expect(file_contents(file).strip).to eq text
end

#

Then(%r!^the "(.*)" file should +(not )?exist$!) do |file, negative|
if negative.nil?
expect(Pathname.new(file)).to exist
else
expect(Pathname.new(file)).to_not exist
end
end

#

Then(%r!^I should (not )?see "(.*)" in the build output$!) do |negative, text|
if negative.nil? || negative.empty?
expect(jekyll_run_output).to match Regexp.new(text)
else
expect(jekyll_run_output).not_to match Regexp.new(text)
end
end

#

Then(%r!^I should get a zero exit(?:\-| )status$!) do
step %(I should see "EXIT STATUS: 0" in the build output)
end

#

Then(%r!^I should get a non-zero exit(?:\-| )status$!) do
step %(I should not see "EXIT STATUS: 0" in the build output)
end
134 changes: 134 additions & 0 deletions features/support/helpers.rb
@@ -0,0 +1,134 @@
# frozen_string_literal: true

require "fileutils"
require "jekyll"
require "time"
require "safe_yaml/load"

class Paths
SOURCE_DIR = Pathname.new(File.expand_path("../..", __dir__))

def self.test_dir
source_dir.join("tmp", "jekyll")
end

def self.output_file
test_dir.join("jekyll_output.txt")
end

def self.status_file
test_dir.join("jekyll_status.txt")
end

def self.source_dir
SOURCE_DIR
end
end

WEBMENTIONS_DEFAULT_CONF = YAML.load(<<~CONFIG)
baseurl: ""
url: https://www.aaron-gustafson.com
plugins: ["jekyll-webmention_io"]
webmentions:
debug: false
username: aarongustafson
legacy_domains:
- http://aaron-gustafson.com
- http://www.aaron-gustafson.com
cache_bad_urls_for: 1
pages: true
CONFIG

def file_content_from_hash(input_hash)
matter_hash = input_hash.reject { |k, _v| k == "content" }
matter = matter_hash.map { |k, v| "#{k}: #{v}\n" }
matter = matter.join.chomp
content =
if !input_hash["input"] || !input_hash["filter"]
input_hash["content"]
else
"{{ #{input_hash["input"]} | #{input_hash["filter"]} }}"
end

Jekyll::Utils.strip_heredoc(<<-EOF)
---
#{matter.gsub(%r!\n!, "\n ")}
---
#{content}
EOF
end

def source_dir(*files)
Paths.test_dir(*files)
end

def all_steps_to_path(path)
dest = Pathname.new(path).expand_path
paths = []

dest.ascend do |f|
break if f == source_dir
paths.unshift f.to_s
end

paths
end

def jekyll_run_output
Paths.output_file.read if Paths.output_file.file?
end

def jekyll_run_status
Paths.status_file.read if Paths.status_file.file?
end

def run_bundle(args)
run_in_shell("bundle", *args.strip.split(" "))
end

def run_rubygem(args)
run_in_shell("gem", *args.strip.split(" "))
end

def run_jekyll(args)
args = args.strip.split(" ") # Shellwords?
process = run_in_shell("bundle", "exec", "jekyll", *args, "--trace")
process.exitstatus.zero?
end

def run_in_shell(*args)
p, output = Jekyll::Utils::Exec.run(*args)

File.write(Paths.status_file, p.exitstatus)
File.open(Paths.output_file, "wb") do |f|
f.print "$ "
f.puts args.join(" ")
f.puts output
f.puts "EXIT STATUS: #{p.exitstatus}"
end
p
end

def slug(title = nil)
if title
title.downcase.gsub(%r![^\w]!, " ").strip.gsub(%r!\s+!, "-")
else
Time.now.strftime("%s%9N") # nanoseconds since the Epoch
end
end

def location(folder, direction)
if folder
before = folder if direction == "in"
after = folder if direction == "under"
end

[
before || ".",
after || ".",
]
end

def file_contents(path)
Pathname.new(path).read
end
25 changes: 25 additions & 0 deletions features/webmention_head_tag.feature
@@ -0,0 +1,25 @@
Feature: Webmentions Head Tag

Scenario: Rendering the Webmention Head Tag with default configuration
Given I have an "index.md" page that contains "{% webmentions_head %}"
And I have a fixture configuration file
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
And I should see "<link rel=\"dns-prefetch\" href=\"https://webmention.io\" />" in "_site/index.html"
And I should see "<link rel=\"preconnect\" href=\"https://webmention.io\" />" in "_site/index.html"
And I should see "<link rel=\"preconnect\" href=\"ws://webmention.io:8080\" />" in "_site/index.html"

Scenario: Rendering the Webmention Head Tag with custom configuration
Given I have an "index.md" page that contains "{% webmentions_head %}"
And I have a fixture configuration file with:
| key | value |
| username | John Doe |
When I run jekyll build
Then I should get a zero exit status
And the _site directory should exist
And I should see "<link rel=\"dns-prefetch\" href=\"https://webmention.io\" />" in "_site/index.html"
And I should see "<link rel=\"preconnect\" href=\"https://webmention.io\" />" in "_site/index.html"
And I should see "<link rel=\"preconnect\" href=\"ws://webmention.io:8080\" />" in "_site/index.html"
And I should see "<link rel=\"pingback\" href=\"https://webmention.io/John Doe/xmlrpc\" />" in "_site/index.html"
And I should see "<link rel=\"webmention\" href=\"https://webmention.io/John Doe/webmention\" />" in "_site/index.html"

0 comments on commit f8dc987

Please sign in to comment.