File: test_new_command.rb

package info (click to toggle)
jekyll 2.2.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,908 kB
  • ctags: 687
  • sloc: ruby: 6,811; sh: 121; xml: 106; makefile: 35
file content (104 lines) | stat: -rw-r--r-- 2,784 bytes parent folder | download
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
require 'helper'
require 'jekyll/commands/new'

class TestNewCommand < Test::Unit::TestCase
  def dir_contents(path)
    Dir["#{path}/**/*"].each do |file|
      file.gsub! path, ''
    end
  end

  def site_template
    File.expand_path("../lib/site_template", File.dirname(__FILE__))
  end

  context 'when args contains a path' do
    setup do
      @path = 'new-site'
      @args = [@path]
      @full_path = File.expand_path(@path, Dir.pwd)
    end

    teardown do
      FileUtils.rm_r @full_path
    end

    should 'create a new directory' do
      assert !File.exist?(@full_path)
      capture_stdout { Jekyll::Commands::New.process(@args) }
      assert File.exist?(@full_path)
    end

    should 'display a success message' do
      output = capture_stdout { Jekyll::Commands::New.process(@args) }
      success_message = "New jekyll site installed in #{@full_path}. \n"
      assert_equal success_message, output
    end

    should 'copy the static files in site template to the new directory' do
      static_template_files = dir_contents(site_template).reject do |f|
        File.extname(f) == '.erb'
      end

      capture_stdout { Jekyll::Commands::New.process(@args) }

      new_site_files = dir_contents(@full_path).reject do |f|
        File.extname(f) == '.markdown'
      end

      assert_same_elements static_template_files, new_site_files
    end

    should 'process any ERB files' do
      erb_template_files = dir_contents(site_template).select do |f|
        File.extname(f) == '.erb'
      end

      stubbed_date = '2013-01-01'
      stub.instance_of(Time).strftime { stubbed_date }

      erb_template_files.each do |f|
        f.chomp! '.erb'
        f.gsub! '0000-00-00', stubbed_date
      end

      capture_stdout { Jekyll::Commands::New.process(@args) }

      new_site_files = dir_contents(@full_path).select do |f|
        erb_template_files.include? f
      end

      assert_same_elements erb_template_files, new_site_files
    end
  end

  context 'when multiple args are given' do
    setup do
      @site_name_with_spaces = 'new site name'
      @multiple_args = @site_name_with_spaces.split
    end

    teardown do
      FileUtils.rm_r File.expand_path(@site_name_with_spaces, Dir.pwd)
    end

    should 'create a new directory' do
      assert !File.exist?(@site_name_with_spaces)
      capture_stdout { Jekyll::Commands::New.process(@multiple_args) }
      assert File.exist?(@site_name_with_spaces)
    end
  end

  context 'when no args are given' do
    setup do
      @empty_args = []
    end

    should 'raise an ArgumentError' do
      exception = assert_raise ArgumentError do
        Jekyll::Commands::New.process(@empty_args)
      end
      assert_equal 'You must specify a path.', exception.message
    end
  end
end