This repository has been archived by the owner on Nov 19, 2019. It is now read-only.
/
post_generator_spec.rb
111 lines (85 loc) · 3.25 KB
/
post_generator_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
require 'spec_helper'
module Postmarkdown
describe PostGenerator do
include GeneratorSpec::TestCase
destination File.expand_path('../../../../../tmp', __FILE__)
before do
prepare_destination
Post.stub(:directory) { File.expand_path('tmp/app/posts') }
Post.class_variable_set('@@posts', nil)
end
context 'with the slug parameter' do
it 'creates a file for the slug and the current date' do
Time.zone.stub(:now).and_return Time.utc(2012, 1, 1, 10, 20, 30)
run_generator %w(test-post)
Dir.glob('tmp/app/posts/*').should == ['tmp/app/posts/2012-01-01-102030-test-post.markdown']
Post.all.count.should == 1
post = Post.first
post.slug.should == 'test-post'
post.date.should == Date.parse('2012-01-01')
post.title.should == 'Test post'
end
end
context 'with the slug parameter including an underscore' do
it 'creates the correct file and sets the right values' do
Time.zone.stub(:now).and_return Time.utc(2012, 1, 1, 10, 20, 30)
run_generator %w(test-post_with_underscores)
Dir.glob('tmp/app/posts/*').should == ['tmp/app/posts/2012-01-01-102030-test-post_with_underscores.markdown']
Post.all.count.should == 1
post = Post.first
post.slug.should == 'test-post_with_underscores'
post.date.should == Date.parse('2012-01-01')
post.title.should == 'Test post_with_underscores'
end
end
context 'with the slug and date parameters' do
it 'creates a file for the slug and the given date' do
run_generator %w(other-post --date=2012-01-02)
Dir.glob('tmp/app/posts/*').should == ['tmp/app/posts/2012-01-02-000000-other-post.markdown']
Post.all.count.should == 1
post = Post.first
post.slug.should == 'other-post'
post.date.should == Date.parse('2012-01-02')
post.title.should == 'Other post'
end
end
context 'with the slug, date and time parameters' do
it 'creates a file for the slug and the given date' do
run_generator %w(other-post --date=2012-01-02-102030)
Dir.glob('tmp/app/posts/*').should == ['tmp/app/posts/2012-01-02-102030-other-post.markdown']
Post.all.count.should == 1
post = Post.first
post.slug.should == 'other-post'
post.date.should == Time.utc(2012, 01, 02, 10, 20, 30).to_date
post.title.should == 'Other post'
end
end
context 'with invalid slug' do
it 'raises a system exit exception' do
lambda { run_generator %w(!test-post) }.should raise_error(SystemExit)
end
it 'does not create the file' do
Dir['app/posts/*'].should be_empty
end
end
context 'with invalid date' do
it 'raises a system exit exception' do
lambda { run_generator %w(test-post --date=2012-02) }.should raise_error(SystemExit)
end
it 'does not create the file' do
Dir['app/posts/*'].should be_empty
end
end
it 'does not error if git is unavailable' do
begin
old_path = ENV['PATH']
ENV['PATH'] = ''
expect {
run_generator %w(test)
}.to_not raise_error
ensure
ENV['PATH'] = old_path
end
end
end
end