/
unpublish_spec.rb
153 lines (130 loc) · 4.76 KB
/
unpublish_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# frozen_string_literal: true
RSpec.describe(Jekyll::Commands::Unpublish) do
let(:drafts_dir) { Pathname.new(source_dir("_drafts")) }
let(:posts_dir) { Pathname.new(source_dir("_posts")) }
let(:post_name) { "a-test-post.md" }
let(:post_filename) { "2012-03-04-#{post_name}" }
let(:post_path) { posts_dir.join post_filename }
let(:draft_path) { drafts_dir.join post_name }
let(:args) { ["_posts/#{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 post_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
end
it "moves a post back to _drafts" do
expect(post_path).to exist
expect(draft_path).not_to exist
capture_stdout { described_class.process(args) }
expect(post_path).not_to exist
expect(draft_path).to exist
end
it "writes a helpful message on success" do
expect(post_path).to exist
output = capture_stdout { described_class.process(args) }
expect(output).to include("Post _posts/#{post_filename} was moved to _drafts/#{post_name}")
end
it "creates the drafts folder if necessary" do
FileUtils.rm_r drafts_dir if File.directory? drafts_dir
capture_stdout { described_class.process(args) }
expect(drafts_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 post path.")
end
it "errors if no file exists at given path" do
weird_path = "_posts/i-forgot-the-date.md"
expect(lambda {
capture_stdout { described_class.process [weird_path] }
}).to raise_error("There was no post found at '#{weird_path}'.")
end
context "when the draft already exists" do
let(:args) { ["_posts/#{post_filename}"] }
before(:each) do
FileUtils.touch draft_path
end
it "raises an error" do
expect(lambda {
capture_stdout { described_class.process(args) }
}).to raise_error("A draft already exists at _drafts/#{post_name}")
expect(draft_path).to exist
expect(post_path).to exist
end
it "overwrites if --force is given" do
expect(lambda {
capture_stdout { described_class.process(args, "force" => true) }
}).not_to raise_error
expect(draft_path).to exist
expect(post_path).not_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).to exist
expect(draft_path).not_to exist
capture_stdout { described_class.process(args) }
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")) }
it "should use source directory set by command line option" do
expect(post_path).to exist
expect(draft_path).not_to exist
capture_stdout { described_class.process(args, "source" => "site") }
expect(post_path).not_to exist
expect(draft_path).to exist
end
end
end