Skip to content

Commit 84125c3

Browse files
ashmarolijekyllbot
authored andcommitted
Warn on error and exit gracefully (#83)
Merge pull request 83
1 parent c5acb5c commit 84125c3

File tree

7 files changed

+64
-45
lines changed

7 files changed

+64
-45
lines changed

lib/jekyll-compose/file_creator.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ def initialize(file_info, force = false, root = nil)
1111
end
1212

1313
def create!
14-
validate_should_write!
14+
return unless create?
15+
1516
ensure_directory_exists
1617
write_file
1718
end
@@ -24,8 +25,12 @@ def file_path
2425

2526
private
2627

27-
def validate_should_write!
28-
return Jekyll.logger.warn "A #{file.resource_type} already exists at #{file_path}".yellow if File.exist?(file_path) && !force
28+
def create?
29+
return true if force
30+
return true unless File.exist?(file_path)
31+
32+
Jekyll.logger.warn "A #{file.resource_type} already exists at #{file_path}"
33+
false
2934
end
3035

3136
def ensure_directory_exists

lib/jekyll-compose/file_mover.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def resource_type_to
1919
end
2020

2121
def move
22-
validate_source
23-
validate_should_write!
22+
return unless valid_source? && valid_destination?
23+
2424
ensure_directory_exists
2525
move_file
2626
end
@@ -45,6 +45,24 @@ def move_file
4545

4646
private
4747

48+
def valid_source?
49+
return true if File.exist?(from)
50+
51+
invalidate_with "There was no #{resource_type_from} found at '#{from}'."
52+
end
53+
54+
def valid_destination?
55+
return true if force
56+
return true unless File.exist?(to)
57+
58+
invalidate_with "A #{resource_type_to} already exists at #{to}"
59+
end
60+
61+
def invalidate_with(msg)
62+
Jekyll.logger.warn msg
63+
false
64+
end
65+
4866
def from
4967
movement.from
5068
end

spec/draft_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@
6363
FileUtils.touch path
6464
end
6565

66-
it "displays a warning and abort" do
66+
it "displays a warning and returns" do
6767
output = capture_stdout { described_class.process(args) }
68-
expect(output).to include("A draft already exists at _drafts/an-existing-draft.md".yellow)
68+
expect(output).to include("A draft already exists at _drafts/an-existing-draft.md")
69+
expect(File.read(path)).to_not match("layout: post")
6970
end
7071

7172
it "overwrites if --force is given" do
72-
expect(lambda {
73-
capture_stdout { described_class.process(args, "force" => true) }
74-
}).not_to raise_error
75-
expect(File.read(path)).to match(%r!layout: post!)
73+
output = capture_stdout { described_class.process(args, "force" => true) }
74+
expect(output).to_not include("A draft already exists at _drafts/an-existing-draft.md")
75+
expect(File.read(path)).to match("layout: post")
7676
end
7777
end
7878

spec/page_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@
5252
FileUtils.touch path
5353
end
5454

55-
it "displays a warning" do
55+
it "displays a warning and returns" do
5656
output = capture_stdout { described_class.process(args) }
57-
expect(output).to include("A page already exists at #{filename}".yellow)
57+
expect(output).to include("A page already exists at #{filename}")
58+
expect(File.read(path)).to_not match("layout: page")
5859
end
5960

6061
it "overwrites if --force is given" do
61-
expect(lambda {
62-
capture_stdout { described_class.process(args, "force" => true) }
63-
}).not_to raise_error
64-
expect(File.read(path)).to match(%r!layout: page!)
62+
output = capture_stdout { described_class.process(args, "force" => true) }
63+
expect(output).to_not include("A page already exists at #{filename}")
64+
expect(File.read(path)).to match("layout: page")
6565
end
6666
end
6767

spec/post_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@
7474
FileUtils.touch path
7575
end
7676

77-
it "displays a warning" do
77+
it "displays a warning and returns" do
7878
output = capture_stdout { described_class.process(args) }
79-
expect(output).to include("A post already exists at _posts/#{filename}".yellow)
79+
expect(output).to include("A post already exists at _posts/#{filename}")
80+
expect(File.read(path)).to_not match("layout: post")
8081
end
8182

8283
it "overwrites if --force is given" do
83-
expect(lambda {
84-
capture_stdout { described_class.process(args, "force" => true) }
85-
}).not_to raise_error
86-
expect(File.read(path)).to match(%r!layout: post!)
84+
output = capture_stdout { described_class.process(args, "force" => true) }
85+
expect(output).to_not include("A post already exists at _posts/#{filename}")
86+
expect(File.read(path)).to match("layout: post")
8787
end
8888
end
8989

spec/publish_spec.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@
6767
}).to raise_error("You must specify a draft path.")
6868
end
6969

70-
it "errors if no file exists at given path" do
70+
it "outputs a warning and returns if no file exists at given path" do
7171
weird_path = "_drafts/i-do-not-exist.markdown"
72-
expect(lambda {
73-
capture_stdout { described_class.process [weird_path] }
74-
}).to raise_error("There was no draft found at '_drafts/i-do-not-exist.markdown'.")
72+
output = capture_stdout { described_class.process [weird_path] }
73+
expect(output).to include("There was no draft found at '_drafts/i-do-not-exist.markdown'.")
74+
expect(draft_path).to exist
75+
expect(post_path).to_not exist
7576
end
7677

7778
context "when the post already exists" do
@@ -81,18 +82,16 @@
8182
FileUtils.touch post_path
8283
end
8384

84-
it "raises an error" do
85-
expect(lambda {
86-
capture_stdout { described_class.process(args) }
87-
}).to raise_error("A post already exists at _posts/#{post_filename}")
85+
it "outputs a warning and returns" do
86+
output = capture_stdout { described_class.process(args) }
87+
expect(output).to include("A post already exists at _posts/#{post_filename}")
8888
expect(draft_path).to exist
8989
expect(post_path).to exist
9090
end
9191

9292
it "overwrites if --force is given" do
93-
expect(lambda {
94-
capture_stdout { described_class.process(args, "force" => true) }
95-
}).not_to raise_error
93+
output = capture_stdout { described_class.process(args, "force" => true) }
94+
expect(output).to_not include("A post already exists at _posts/#{post_filename}")
9695
expect(draft_path).not_to exist
9796
expect(post_path).to exist
9897
end

spec/unpublish_spec.rb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@
5252
}).to raise_error("You must specify a post path.")
5353
end
5454

55-
it "errors if no file exists at given path" do
55+
it "outputs a warning and returns if no file exists at given path" do
5656
weird_path = "_posts/i-forgot-the-date.md"
57-
expect(lambda {
58-
capture_stdout { described_class.process [weird_path] }
59-
}).to raise_error("There was no post found at '#{weird_path}'.")
57+
output = capture_stdout { described_class.process [weird_path] }
58+
expect(output).to include("There was no post found at '#{weird_path}'.")
6059
end
6160

6261
context "when the draft already exists" do
@@ -66,18 +65,16 @@
6665
FileUtils.touch draft_path
6766
end
6867

69-
it "raises an error" do
70-
expect(lambda {
71-
capture_stdout { described_class.process(args) }
72-
}).to raise_error("A draft already exists at _drafts/#{post_name}")
68+
it "displays a warning and returns" do
69+
output = capture_stdout { described_class.process(args) }
70+
expect(output).to include("A draft already exists at _drafts/#{post_name}")
7371
expect(draft_path).to exist
7472
expect(post_path).to exist
7573
end
7674

7775
it "overwrites if --force is given" do
78-
expect(lambda {
79-
capture_stdout { described_class.process(args, "force" => true) }
80-
}).not_to raise_error
76+
output = capture_stdout { described_class.process(args, "force" => true) }
77+
expect(output).to_not include("A draft already exists at _drafts/#{post_name}")
8178
expect(draft_path).to exist
8279
expect(post_path).not_to exist
8380
end

0 commit comments

Comments
 (0)