diff --git a/lib/lifer.rb b/lib/lifer.rb index 06a89e6..7c1548e 100644 --- a/lib/lifer.rb +++ b/lib/lifer.rb @@ -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! diff --git a/lib/lifer/brain.rb b/lib/lifer/brain.rb index 06c61e0..0975a60 100644 --- a/lib/lifer/brain.rb +++ b/lib/lifer/brain.rb @@ -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 @@ -57,8 +58,11 @@ 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! @@ -66,8 +70,10 @@ def brainwash! 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: diff --git a/spec/lifer/brain_spec.rb b/spec/lifer/brain_spec.rb index 417e8ab..66ffe87 100644 --- a/spec/lifer/brain_spec.rb +++ b/spec/lifer/brain_spec.rb @@ -103,8 +103,6 @@ brain.build! end - let(:root) { temp_root support_file("root_with_entries") } - let(:directory_entries) { Dir .glob("#{root}/**/*") diff --git a/spec/lifer/builder/html/layout_spec.rb b/spec/lifer/builder/html/layout_spec.rb index ebb1b39..49c0635 100644 --- a/spec/lifer/builder/html/layout_spec.rb +++ b/spec/lifer/builder/html/layout_spec.rb @@ -5,8 +5,8 @@ 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 @@ -14,6 +14,10 @@ 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 @@ -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 diff --git a/spec/lifer/builder/html_spec.rb b/spec/lifer/builder/html_spec.rb index 4430f04..519efbe 100644 --- a/spec/lifer/builder/html_spec.rb +++ b/spec/lifer/builder/html_spec.rb @@ -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 diff --git a/spec/lifer/builder/rss_spec.rb b/spec/lifer/builder/rss_spec.rb index dd45c59..cccb259 100644 --- a/spec/lifer/builder/rss_spec.rb +++ b/spec/lifer/builder/rss_spec.rb @@ -3,29 +3,19 @@ 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 @@ -33,14 +23,14 @@ 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 @@ -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 @@ -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 "" \ "<h1 id="tiny">Tiny</h1>\n\n" \ @@ -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) diff --git a/spec/lifer/collection_spec.rb b/spec/lifer/collection_spec.rb index f07be8d..26953f5 100644 --- a/spec/lifer/collection_spec.rb +++ b/spec/lifer/collection_spec.rb @@ -1,10 +1,14 @@ require "spec_helper" RSpec.describe Lifer::Collection do + before do + spec_lifer! + end + let(:collection) { described_class.generate name: "name", directory: directory } - let(:directory) { support_file "root_with_entries/subdirectory_one" } + let(:directory) { "#{spec_lifer.root}/subdirectory_one" } describe ".generate" do subject { @@ -39,20 +43,19 @@ context "when the setting is an absolute path" do context "containing the gem root" do before do - allow(Lifer).to receive(:gem_root).and_return("fake/gem_root") + allow(Lifer).to receive(:gem_root).and_return("/fake/gem_root") allow(Lifer) .to receive(:setting) .with(:layout_file, collection: collection) - .and_return("fake/gem_root") + .and_return("/fake/gem_root") end - it { is_expected.to eq "fake/gem_root" } + it { is_expected.to eq "/fake/gem_root" } end context "containing the Lifer root" do let(:absolute_path_to_layout_file) { - support_file "root_with_entries/.config/layouts/" \ - "layout_with_greeting.html.erb" + "#{spec_lifer.root}/.config/layouts/layout_with_greeting.html.erb" } before do @@ -67,20 +70,15 @@ end context "when the setting is a relative path" do - let(:relative_path_to_layout_file) { - support_file( - "root_with_entries/.config/layouts/layout_with_greeting.html.erb" - ).gsub(support_file("root_with_entries/.config/"), "") - } let(:absolute_path_to_layout_file) { - support_file "root_with_entries/.config/layouts/" \ + "#{spec_lifer.config.file}/.config/layouts/" \ "layout_with_greeting.html.erb" } + let(:relative_path_to_layout_file) { + absolute_path_to_layout_file.gsub("#{spec_lifer.root}/", "") + } before do - allow(Lifer).to receive(:config_file).and_return( - support_file "root_with_entries/.config/lifer.yaml" - ) allow(Lifer) .to receive(:setting) .with(:layout_file, collection: collection) diff --git a/spec/lifer/entry_spec.rb b/spec/lifer/entry_spec.rb index bca6a6c..25f8fad 100644 --- a/spec/lifer/entry_spec.rb +++ b/spec/lifer/entry_spec.rb @@ -1,11 +1,11 @@ require "spec_helper" RSpec.describe Lifer::Entry do + let(:collection) { nil } + describe ".generate" do subject { described_class.generate file: file, collection: collection } - let(:collection) { nil } - context "when the file doesn't exist" do let(:file) { "doesnt-exist" } diff --git a/spec/lifer_spec.rb b/spec/lifer_spec.rb index c9ca782..1fe50f3 100644 --- a/spec/lifer_spec.rb +++ b/spec/lifer_spec.rb @@ -3,6 +3,10 @@ require "spec_helper" RSpec.describe Lifer do + before do + Lifer.class_variable_set "@@brain", nil + end + it "has a version number" do expect(Lifer::VERSION).not_to be nil end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 92b153a..076d752 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,10 +1,6 @@ # frozen_string_literal: true -require "lifer" - require "debug" -require "fileutils" -require "tmpdir" require_relative "shared_examples" require_relative "support" @@ -13,10 +9,6 @@ # Use the built-in `documentation` formatter for runs. config.add_formatter :documentation - config.after(:each) do - lose_support_config - end - # If RSpec runs on somethign other than TTY, try to display colours. config.color_mode = :on @@ -30,5 +22,8 @@ c.syntax = :expect end + # Provide helper methods for initializing test Lifer projects without + # littering garbage files all over the developer's filesystem. + # config.include Support::LiferTestHelpers::Files end diff --git a/spec/support/lifer_test_helpers/files.rb b/spec/support/lifer_test_helpers/files.rb index f8e7fc0..18d25b6 100644 --- a/spec/support/lifer_test_helpers/files.rb +++ b/spec/support/lifer_test_helpers/files.rb @@ -1,23 +1,29 @@ +require "fileutils" +require "tmpdir" + +require "lifer" + # frozen_string_literal: true module Support::LiferTestHelpers::Files SPEC_ROOT = "%s/spec" % Lifer.gem_root - def support_file(path_to_file) - "#{SPEC_ROOT}/support/#{path_to_file}" + def spec_lifer!( + root: "root_with_entries", + config_file: "root_with_entries/.config/lifer.yaml" + ) + Lifer.class_variable_set "@@brain", nil + + @@spec_lifer = Lifer.brain root: temp_root(support_file root), + config_file: support_file(config_file) end - def use_support_config(path_to_root) - Lifer.class_variable_set( - "@@config", - Lifer::Config.build(file: support_file(path_to_root)) - ) + def spec_lifer + @@spec_lifer end - def lose_support_config - Lifer.class_variables.each do |class_variable| - Lifer.class_variable_set class_variable.to_s, nil - end + def support_file(path_to_file) + "#{SPEC_ROOT}/support/#{path_to_file}" end def temp_root(root_directory) diff --git a/spec/support/root_with_entries/.config/all-custom-layouts.yaml b/spec/support/root_with_entries/.config/all-custom-layouts.yaml new file mode 100644 index 0000000..e8b081a --- /dev/null +++ b/spec/support/root_with_entries/.config/all-custom-layouts.yaml @@ -0,0 +1,5 @@ +layout_file: ./layouts/layout_with_greeting.html.erb +uri_strategy: simple +subdirectory_one: + layout_file: ./layouts/layout_for_subdirectory_one_collection.html.erb + uri_strategy: pretty