From f4e8655f526baee69a1274e631ff740234e12f7a Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Tue, 9 Oct 2018 17:01:00 +0530 Subject: [PATCH] Test support for collections_dir --- spec/draft_spec.rb | 32 +++++++++++++++++++++++++++++--- spec/post_spec.rb | 23 +++++++++++++++++++++++ spec/publish_spec.rb | 13 ++++++------- spec/unpublish_spec.rb | 39 ++++++++++++++++++++++++++++++++------- 4 files changed, 90 insertions(+), 17 deletions(-) diff --git a/spec/draft_spec.rb b/spec/draft_spec.rb index 1d572ef..f2db8cf 100644 --- a/spec/draft_spec.rb +++ b/spec/draft_spec.rb @@ -79,12 +79,15 @@ context "when a configuration file exists" do let(:config) { source_dir("_config.yml") } let(:drafts_dir) { Pathname.new source_dir(File.join("site", "_drafts")) } + let(:config_data) do + %( + source: site + ) + end before(:each) do File.open(config, "w") do |f| - f.write(%( -source: site -)) + f.write(config_data) end end @@ -97,6 +100,29 @@ capture_stdout { described_class.process(args) } expect(path).to exist end + + context "and collections_dir is set" do + let(:collections_dir) { "my_collections" } + let(:drafts_dir) { Pathname.new source_dir("site", collections_dir, "_drafts") } + let(:config_data) do + %( + source: site + collections_dir: #{collections_dir} + ) + end + + it "should create drafts at the correct location" do + expect(path).not_to exist + capture_stdout { described_class.process(args) } + expect(path).to exist + end + + it "should write a helpful message when successful" do + output = capture_stdout { described_class.process(args) } + generated_path = File.join("site", collections_dir, "_drafts", "a-test-post.md").cyan + expect(output).to include("New draft created at #{generated_path}") + end + end end context "when source option is set" do diff --git a/spec/post_spec.rb b/spec/post_spec.rb index 77cbcb3..07d3b70 100644 --- a/spec/post_spec.rb +++ b/spec/post_spec.rb @@ -150,6 +150,29 @@ end end end + + context "and collections_dir is set" do + let(:collections_dir) { "my_collections" } + let(:posts_dir) { Pathname.new source_dir("site", collections_dir, "_posts") } + let(:config_data) do + %( + source: site + collections_dir: #{collections_dir} + ) + end + + it "should create posts at the correct location" do + expect(path).not_to exist + capture_stdout { described_class.process(args) } + expect(path).to exist + end + + it "should write a helpful message when successful" do + output = capture_stdout { described_class.process(args) } + generated_path = File.join("site", collections_dir, "_posts", filename).cyan + expect(output).to include("New post created at #{generated_path}") + end + end end context "when source option is set" do diff --git a/spec/publish_spec.rb b/spec/publish_spec.rb index 1744db6..2b06695 100644 --- a/spec/publish_spec.rb +++ b/spec/publish_spec.rb @@ -102,14 +102,15 @@ let(:config) { source_dir("_config.yml") } let(:drafts_dir) { Pathname.new source_dir("site", "_drafts") } let(:posts_dir) { Pathname.new source_dir("site", "_posts") } - - let(:args) { ["site/_drafts/#{draft_to_publish}"] } + let(:config_data) do + %( + source: site + ) + end before(:each) do File.open(config, "w") do |f| - f.write(%( -source: site -)) + f.write(config_data) end end @@ -129,8 +130,6 @@ let(:drafts_dir) { Pathname.new source_dir("site", "_drafts") } let(:posts_dir) { Pathname.new source_dir("site", "_posts") } - let(:args) { ["site/_drafts/#{draft_to_publish}"] } - it "should use source directory set by command line option" do expect(post_path).not_to exist expect(draft_path).to exist diff --git a/spec/unpublish_spec.rb b/spec/unpublish_spec.rb index e5fd87d..27484b4 100644 --- a/spec/unpublish_spec.rb +++ b/spec/unpublish_spec.rb @@ -87,14 +87,15 @@ let(:config) { source_dir("_config.yml") } let(:drafts_dir) { Pathname.new(source_dir("site", "_drafts")) } let(:posts_dir) { Pathname.new(source_dir("site", "_posts")) } - - let(:args) { ["site/_posts/#{post_filename}"] } + let(:config_data) do + %( + source: site + ) + end before(:each) do File.open(config, "w") do |f| - f.write(%( -source: site -)) + f.write(config_data) end end @@ -109,14 +110,38 @@ expect(post_path).not_to exist expect(draft_path).to exist end + + context "and collections_dir is set" do + let(:collections_dir) { "my_collections" } + let(:drafts_dir) { Pathname.new(source_dir("site", collections_dir, "_drafts")) } + let(:posts_dir) { Pathname.new(source_dir("site", collections_dir, "_posts")) } + let(:config_data) do + %( + source: site + collections_dir: #{collections_dir} + ) + end + + it "should move posts to the correct location" do + expect(post_path).to exist + expect(draft_path).not_to exist + capture_stdout { described_class.process(args) } + expect(draft_path).to exist + end + + it "should write a helpful message when successful" do + output = capture_stdout { described_class.process(args) } + post_filepath = File.join("site", collections_dir, "_posts", post_filename) + draft_filepath = File.join("site", collections_dir, "_drafts", post_name) + expect(output).to include("Post #{post_filepath} was moved to #{draft_filepath}") + end + end end context "when source option is set" do let(:drafts_dir) { Pathname.new(source_dir("site", "_drafts")) } let(:posts_dir) { Pathname.new(source_dir("site", "_posts")) } - let(:args) { ["site/_posts/#{post_filename}"] } - it "should use source directory set by command line option" do expect(post_path).to exist expect(draft_path).not_to exist