|
| 1 | +require 'helper' |
| 2 | +require 'jekyll/commands/draft' |
| 3 | + |
| 4 | +class TestDraftCommand < Test::Unit::TestCase |
| 5 | + |
| 6 | + context 'when no flags are given' do |
| 7 | + setup do |
| 8 | + @name = 'A test post' |
| 9 | + @args = [@name] |
| 10 | + @drafts_dir = '_drafts' |
| 11 | + @path = File.join(@drafts_dir, 'a-test-post.markdown') |
| 12 | + stub(Jekyll).configuration { Jekyll::Configuration::DEFAULTS } |
| 13 | + @site = Site.new(Jekyll.configuration) |
| 14 | + FileUtils.mkdir @drafts_dir |
| 15 | + end |
| 16 | + |
| 17 | + teardown do |
| 18 | + FileUtils.rm_r @drafts_dir |
| 19 | + end |
| 20 | + |
| 21 | + should 'create a new draft post' do |
| 22 | + assert !File.exist?(@path) |
| 23 | + capture_stdout { Jekyll::Commands::Draft.process(@args) } |
| 24 | + assert File.exist?(@path) |
| 25 | + end |
| 26 | + |
| 27 | + should 'write a success message' do |
| 28 | + output = capture_stdout { Jekyll::Commands::Draft.process(@args) } |
| 29 | + success_message = "New draft created at ./#{@path}.\n" |
| 30 | + assert_equal success_message, output |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + context 'when the draft already exists' do |
| 35 | + setup do |
| 36 | + @name = 'An existing draft' |
| 37 | + @args = [@name] |
| 38 | + @drafts_dir = '_drafts' |
| 39 | + @path = File.join(@drafts_dir, 'an-existing-draft.markdown') |
| 40 | + stub(Jekyll).configuration { Jekyll::Configuration::DEFAULTS } |
| 41 | + @site = Site.new(Jekyll.configuration) |
| 42 | + FileUtils.mkdir @drafts_dir |
| 43 | + FileUtils.touch @path |
| 44 | + end |
| 45 | + |
| 46 | + teardown do |
| 47 | + FileUtils.rm_r @drafts_dir |
| 48 | + end |
| 49 | + |
| 50 | + should 'raise an ArgumentError' do |
| 51 | + exception = assert_raise ArgumentError do |
| 52 | + capture_stdout { Jekyll::Commands::Draft.process(@args) } |
| 53 | + end |
| 54 | + assert_equal "A draft already exists at ./#{@path}", exception.message |
| 55 | + end |
| 56 | + |
| 57 | + should 'overwrite the existing draft if --force is given' do |
| 58 | + capture_stdout { Jekyll::Commands::Draft.process(@args, { "force" => true }) } |
| 59 | + assert File.readlines(@path).grep(/layout: post/).any? |
| 60 | + end |
| 61 | + |
| 62 | + end |
| 63 | + |
| 64 | + context 'when no args are given' do |
| 65 | + setup do |
| 66 | + @empty_args = [] |
| 67 | + end |
| 68 | + |
| 69 | + should 'raise an ArgumentError' do |
| 70 | + exception = assert_raise ArgumentError do |
| 71 | + Jekyll::Commands::Draft.process(@empty_args) |
| 72 | + end |
| 73 | + assert_equal 'You must specify a name.', exception.message |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | +end |
0 commit comments