Skip to content

Commit

Permalink
Make it easier to specify custom config files
Browse files Browse the repository at this point in the history
Right now this mainly makes end-to-end testing easier, as you can see
from this commit's contents. But in the future this will also make
it easier for end users to switch configuration files.
  • Loading branch information
benjaminwil committed Feb 4, 2024
1 parent aae69f8 commit 994b5e9
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 138 deletions.
4 changes: 2 additions & 2 deletions lib/lifer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module Lifer
] | IGNORE_DIRECTORIES.map { |d| "^(#{d})" }

class << self
def brain(root: Dir.pwd)
@@brain ||= Lifer::Brain.init(root: root)
def brain(root: Dir.pwd, config_file: nil)
@@brain ||= Lifer::Brain.init(root: root, config_file: config_file)
end

def build!
Expand Down
16 changes: 11 additions & 5 deletions lib/lifer/brain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
require_relative "config"

class Lifer::Brain
DEFAULT_CONFIG_FILE_URI = ".config/lifer.yaml"
DEFAULT_OUTPUT_DIRECTORY_NAME = "_build"

attr_reader :root

class << self
def init(root: Dir.pwd)
new(root: root)
def init(root: Dir.pwd, config_file: nil)
new(root: root, config_file: config_file)
end
end

Expand Down Expand Up @@ -57,17 +58,22 @@ def setting(*name, collection: nil)

private

def initialize(root:)
attr_reader :config_file_location

def initialize(root:, config_file:)
@root = root
@config_file_location = build_config_file_location(config_file)
end

def brainwash!
FileUtils.rm_r output_directory
FileUtils.mkdir_p output_directory
end

def config_file_location
File.join(root, ".config", "lifer.yaml")
def build_config_file_location(path)
return File.join(root, DEFAULT_CONFIG_FILE_URI) if path.nil?

path.start_with?("/") ? path : File.join(root, path)
end

# FIXME:
Expand Down
2 changes: 0 additions & 2 deletions spec/lifer/brain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@
brain.build!
end

let(:root) { temp_root support_file("root_with_entries") }

let(:directory_entries) {
Dir
.glob("#{root}/**/*")
Expand Down
19 changes: 8 additions & 11 deletions spec/lifer/builder/html/layout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
subject { described_class.build entry: entry }

let(:collection) {
Lifer::Collection.generate name: "Collection",
directory: File.dirname(file)
Lifer::Collection.generate name: "subdirectory_one",
directory: "#{spec_lifer.root}/subdirectory_one"
}
let(:entry) {
Lifer::Entry::Markdown.new file: file, collection: collection
}
let(:file) { support_file "root_with_entries/tiny_entry.md" }

context "when not assigning a template file" do
before do
spec_lifer!
end

it "renders a valid HTML document using the default template" do
expect(subject).to fuzzy_match <<~RESULT
<html>
Expand All @@ -29,16 +33,9 @@
end

context "when the collection has its own layout file" do
let(:config) {
Lifer::Config.build(
file: support_file(
"root_with_entries/.config/custom-root-layout-lifer.yaml"
)
)
}

before do
allow(Lifer::Config).to receive(:build).and_return(config)
spec_lifer! config_file: "root_with_entries/.config/" \
"custom-root-layout-lifer.yaml"
end

it "renders a valid HTML document using any other template" do
Expand Down
89 changes: 39 additions & 50 deletions spec/lifer/builder/html_spec.rb
Original file line number Diff line number Diff line change
@@ -1,92 +1,81 @@
require "spec_helper"

RSpec.describe Lifer::Builder::HTML do
let(:root) { temp_root support_file("root_with_entries") }

before do
Lifer.brain root: root
spec_lifer!
end

describe ".execute" do
subject { described_class.execute(root: root) }
subject { described_class.execute(root: spec_lifer.root) }

it "generates HTML for each entry" do
entry_files = Dir.glob("#{root}/**/*.*")
entry_files = Dir.glob("#{spec_lifer.root}/**/*.*")
.select { |entry| Lifer::Entry.supported? entry }

expect { subject }
.to change { Dir.glob("#{root}/_build/**/*.html").count }
.to change {
Dir.glob("#{spec_lifer.output_directory}/**/*.html").count
}
.from(0)
.to(entry_files.count)
end

it "generates HTML in the correct subdirectories" do
entry_count = Dir.glob("#{root}/subdirectory_one/**/*.*").count
entry_count = Dir.glob("#{spec_lifer.root}/subdirectory_one/**/*.*").count

expect { subject }
.to change {
Dir.glob("#{root}/_build/subdirectory_one/**/*.html").count
Dir.glob("#{spec_lifer.output_directory}/subdirectory_one/**/*.html")
.count
}
.from(0)
.to(entry_count)
end

context "when a custom layout is configured in the root settings" do
let(:config) {
Lifer::Config.build(
file: support_file(
"root_with_entries/.config/custom-root-layout-lifer.yaml"
)
)
}
let(:layout_file) {
File.join root, ".config/layouts/layout_with_greeting.html.erb"
}
let(:collection_entry_file) {
File.read File.join(
root,
"_build/subdirectory_one/entry_in_subdirectory.html"
)
}
let(:root_collection_entry_file) {
File.read File.join(root, "_build/tiny_entry.html")
}
before do
spec_lifer! config_file: "root_with_entries/.config/" \
"custom-root-layout-lifer.yaml"
end

it "builds using the correct layout" do
allow(Lifer::Config).to receive(:build).and_return(config)
allow(config)
.to receive(:settings)
.and_return({layout_file: layout_file})

subject

expect(collection_entry_file).to include "Greetings!"
expect(root_collection_entry_file).to include "Greetings!"
collection_entry =
File.read File.join(
spec_lifer.output_directory,
"subdirectory_one/entry_in_subdirectory/index.html"
)
root_collection_entry =
File.read File.join(spec_lifer.output_directory, "tiny_entry.html")

expect(collection_entry).to include "Greetings!"
expect(root_collection_entry).to include "Greetings!"
end

context "when a custom layout is configured for a collection" do
let(:collection_layout_file) {
File.join root,
".config/layouts/layout_for_subdirectory_one_collection.html.erb"
}
before do
spec_lifer! config_file: "root_with_entries/.config/" \
"all-custom-layouts.yaml"
end

it "builds using all the correct layouts" do
allow(Lifer::Config).to receive(:build).and_return(config)
allow(config)
.to receive(:settings)
.and_return({
layout_file: layout_file,
subdirectory_one: {layout_file: collection_layout_file}
})

subject

expect(collection_entry_file).not_to include "Greetings!"
expect(collection_entry_file)
collection_entry =
File.read File.join(
spec_lifer.output_directory,
"subdirectory_one/entry_in_subdirectory/index.html"
)
root_collection_entry =
File.read File.join(spec_lifer.output_directory, "tiny_entry.html")

expect(collection_entry).not_to include "Greetings!"
expect(collection_entry)
.to include "Layout for Subdirectory One"

expect(root_collection_entry_file).to include "Greetings!"
expect(root_collection_entry_file)
expect(root_collection_entry).to include "Greetings!"
expect(root_collection_entry)
.not_to include "Layout for Subdirectory One"
end
end
Expand Down
51 changes: 19 additions & 32 deletions spec/lifer/builder/rss_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,34 @@
require "spec_helper"

RSpec.describe Lifer::Builder::RSS do
let(:config) {
Lifer::Config.build file: support_file(
File.join "root_with_entries",
".config",
"custom-config-with-root-rss-feed.yaml"
)
}
let(:directory) { temp_root support_file("root_with_entries") }

before do
allow(Lifer)
.to receive(:brain)
.and_return(Lifer::Brain.init(root: directory))

allow(Lifer::Config).to receive(:build).and_return(config)
spec_lifer! config_file: "root_with_entries/.config/" \
"custom-config-with-root-rss-feed.yaml"
end

describe ".execute" do
subject { described_class.execute root: directory }
subject { described_class.execute root: spec_lifer.root }

it "generates a single RSS feed" do
expect { subject }
.to change { Dir.glob("#{directory}/_build/**/feed.xml").count }
.to change {
Dir.glob("#{spec_lifer.output_directory}/**/feed.xml").count
}
.from(0)
.to(1)
end

it "generates the correct amount of feed items" do
# We're excluding the entries in `subdirectory_one` here.
#
article_count = Dir.glob("#{directory}/*.md").count
article_count = Dir.glob("#{spec_lifer.root}/*.md").count

subject

generated_feed =
File.open(Dir.glob("#{directory}/_build/**/feed.xml").first) {
Nokogiri::XML _1
}
File.open(
Dir.glob("#{spec_lifer.output_directory}/**/feed.xml").first
) { Nokogiri::XML _1 }
feed_items = generated_feed.xpath "//item"

expect(feed_items.count).to eq article_count
Expand All @@ -50,9 +40,9 @@
subject

generated_feed =
File.open(Dir.glob("#{directory}/_build/**/feed.xml").first) {
Nokogiri::XML _1
}
File.open(
Dir.glob("#{spec_lifer.output_directory}/**/feed.xml").first
) { Nokogiri::XML _1 }
entry = generated_feed.xpath("//item").css("link")
.detect { _1.text == "https://example.com/tiny_entry.html" }
.parent
Expand All @@ -72,7 +62,7 @@
subject

feed_contents =
File.read Dir.glob("#{directory}/_build/**/feed.xml").first
File.read Dir.glob("#{spec_lifer.output_directory}/**/feed.xml").first

expect(feed_contents).to include "<content:encoded>" \
"&lt;h1 id=&quot;tiny&quot;&gt;Tiny&lt;/h1&gt;\n\n" \
Expand All @@ -81,16 +71,13 @@
end

context "when many collections are configured" do
let(:config) {
Lifer::Config.build file: support_file(
File.join "root_with_entries",
".config",
"custom-config-with-multiple-rss-feeds.yaml"
)
}
before do
spec_lifer! config_file: "root_with_entries/.config/" \
"custom-config-with-multiple-rss-feeds.yaml"
end

it "generates more than one RSS feed" do
pattern = "#{directory}/_build/**/*.xml"
pattern = "#{spec_lifer.output_directory}/**/*.xml"
expect { subject }
.to change { Dir.glob(pattern).count }.from(0).to(2)

Expand Down
Loading

0 comments on commit 994b5e9

Please sign in to comment.