Simplify Cucumber helper

This commit is contained in:
Ashwin Maroli 2019-03-17 16:38:50 +05:30
parent 26b7d6a94d
commit e522f54e53
1 changed files with 22 additions and 32 deletions

View File

@ -1,12 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require "fileutils"
require "jekyll" require "jekyll"
require "time"
require "safe_yaml/load"
class Paths class Paths
SOURCE_DIR = Pathname.new(File.expand_path("../..", __dir__)) SOURCE_DIR = Pathname.new(File.expand_path("../..", __dir__))
def self.test_dir; source_dir.join("tmp", "jekyll"); end def self.test_dir; source_dir.join("tmp", "jekyll"); end
def self.theme_gem_dir; source_dir.join("tmp", "jekyll", "my-cool-theme"); end def self.theme_gem_dir; source_dir.join("tmp", "jekyll", "my-cool-theme"); end
@ -24,24 +22,19 @@ end
def file_content_from_hash(input_hash) def file_content_from_hash(input_hash)
matter_hash = input_hash.reject { |k, _v| k == "content" } matter_hash = input_hash.reject { |k, _v| k == "content" }
matter = matter_hash.map do |k, v| matter = matter_hash.map { |k, v| "#{k}: #{v}\n" }.join
"#{k}: #{v}\n" matter.chomp!
content = if input_hash["input"] && input_hash["filter"]
"{{ #{input_hash["input"]} | #{input_hash["filter"]} }}"
else
input_hash["content"]
end end
matter = matter.join.chomp <<~EOF
content = \ ---
if !input_hash["input"] || !input_hash["filter"] #{matter}
then input_hash["content"] ---
else "{{ #{input_hash["input"]} | " \
"#{input_hash["filter"]} }}"
end
Jekyll::Utils.strip_heredoc(<<-EOF)
---
#{matter.gsub(
%r!\n!, "\n "
)}
---
#{content} #{content}
EOF EOF
end end
@ -49,7 +42,7 @@ end
# #
def source_dir(*files) def source_dir(*files)
return Paths.test_dir(*files) Paths.test_dir(*files)
end end
# #
@ -70,17 +63,13 @@ end
# #
def jekyll_run_output def jekyll_run_output
if Paths.output_file.file? Paths.output_file.read if Paths.output_file.file?
then return Paths.output_file.read
end
end end
# #
def jekyll_run_status def jekyll_run_status
if Paths.status_file.file? Paths.status_file.read if Paths.status_file.file?
then return Paths.status_file.read
end
end end
# #
@ -104,6 +93,7 @@ def run_jekyll(args)
end end
# #
def run_in_shell(*args) def run_in_shell(*args)
p, output = Jekyll::Utils::Exec.run(*args) p, output = Jekyll::Utils::Exec.run(*args)
@ -121,9 +111,10 @@ end
# #
def slug(title = nil) def slug(title = nil)
if !title if title
then Time.now.strftime("%s%9N") # nanoseconds since the Epoch title.downcase.gsub(%r![^\w]!, " ").strip.gsub(%r!\s+!, "-")
else title.downcase.gsub(%r![^\w]!, " ").strip.gsub(%r!\s+!, "-") else
Time.now.strftime("%s%9N") # nanoseconds since the Epoch
end end
end end
@ -142,7 +133,7 @@ end
# #
def file_contents(path) def file_contents(path)
return Pathname.new(path).read Pathname.new(path).read
end end
# #
@ -155,8 +146,7 @@ def seconds_agnostic_datetime(datetime = Time.now)
Regexp.escape(date), Regexp.escape(date),
"#{time}:\\d{2}", "#{time}:\\d{2}",
Regexp.escape(zone), Regexp.escape(zone),
] \ ].join("\\ ")
.join("\\ ")
end end
# #