/
publish_spec.rb
139 lines (117 loc) · 4.46 KB
/
publish_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# frozen_string_literal: true
RSpec.describe(Jekyll::Commands::Publish) do
let(:drafts_dir) { Pathname.new source_dir("_drafts") }
let(:posts_dir) { Pathname.new source_dir("_posts") }
let(:draft_to_publish) { "a-test-post.md" }
let(:datestamp) { Time.now.strftime(Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT) }
let(:post_filename) { "#{datestamp}-#{draft_to_publish}" }
let(:args) { ["_drafts/#{draft_to_publish}"] }
let(:draft_path) { drafts_dir.join draft_to_publish }
let(:post_path) { posts_dir.join post_filename }
before(:all) do
FileUtils.mkdir_p source_dir unless File.directory? source_dir
Dir.chdir source_dir
end
before(:each) do
FileUtils.mkdir_p drafts_dir unless File.directory? drafts_dir
FileUtils.mkdir_p posts_dir unless File.directory? posts_dir
FileUtils.touch draft_path
end
after(:each) do
FileUtils.rm_r drafts_dir if File.directory? drafts_dir
FileUtils.rm_r posts_dir if File.directory? posts_dir
FileUtils.rm_r draft_path if File.file? draft_path
FileUtils.rm_r post_path if File.file? post_path
end
it "publishes a draft post" do
expect(post_path).not_to exist
expect(draft_path).to exist
capture_stdout { described_class.process(args) }
expect(post_path).to exist
end
it "publishes with a specified date" do
path = posts_dir.join "2012-03-04-#{draft_to_publish}"
expect(path).not_to exist
capture_stdout { described_class.process(args, "date"=>"2012-3-4") }
expect(path).to exist
end
it "writes a helpful message on success" do
expect(draft_path).to exist
output = capture_stdout { described_class.process(args) }
expect(output).to include("Draft _drafts/#{draft_to_publish} was moved to _posts/#{post_filename}")
end
it "publishes a draft on the specified date" do
path = posts_dir.join "2012-03-04-a-test-post.md"
capture_stdout { described_class.process(args, "date" => "2012-3-4") }
expect(path).to exist
end
it "creates the posts folder if necessary" do
FileUtils.rm_r posts_dir if File.directory? posts_dir
capture_stdout { described_class.process(args) }
expect(posts_dir).to exist
end
it "errors if there is no argument" do
expect(lambda {
capture_stdout { described_class.process }
}).to raise_error("You must specify a draft path.")
end
it "outputs a warning and returns if no file exists at given path" do
weird_path = "_drafts/i-do-not-exist.markdown"
output = capture_stdout { described_class.process [weird_path] }
expect(output).to include("There was no draft found at '_drafts/i-do-not-exist.markdown'.")
expect(draft_path).to exist
expect(post_path).to_not exist
end
context "when the post already exists" do
let(:args) { ["_drafts/#{draft_to_publish}"] }
before(:each) do
FileUtils.touch post_path
end
it "outputs a warning and returns" do
output = capture_stdout { described_class.process(args) }
expect(output).to include("A post already exists at _posts/#{post_filename}")
expect(draft_path).to exist
expect(post_path).to exist
end
it "overwrites if --force is given" do
output = capture_stdout { described_class.process(args, "force" => true) }
expect(output).to_not include("A post already exists at _posts/#{post_filename}")
expect(draft_path).not_to exist
expect(post_path).to exist
end
end
context "when a configuration file exists" do
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(:config_data) do
%(
source: site
)
end
before(:each) do
File.open(config, "w") do |f|
f.write(config_data)
end
end
after(:each) do
FileUtils.rm(config)
end
it "should use source directory set by config" do
expect(post_path).not_to exist
expect(draft_path).to exist
capture_stdout { described_class.process(args) }
expect(post_path).to exist
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") }
it "should use source directory set by command line option" do
expect(post_path).not_to exist
expect(draft_path).to exist
capture_stdout { described_class.process(args, "source" => "site") }
expect(post_path).to exist
end
end
end