Class: Clash::Test

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/clash/test.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#boldit, #colorize, #default_array, #greenit, #pout, #print_fail, #print_pass, #redit, #yellowit

Constructor Details

#initialize(options = {}) ⇒ Test

Returns a new instance of Test.



7
8
9
10
11
12
# File 'lib/clash/test.rb', line 7

def initialize(options={})
  @test_failures = []
  @options  = options
  @options['config'] ||= {}
  @cleanup = []
end

Instance Attribute Details

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/clash/test.rb', line 5

def title
  @title
end

Instance Method Details

#buildObject



72
73
74
75
76
77
78
79
# File 'lib/clash/test.rb', line 72

def build
  if jekyll_config = @options['config']['jekyll']
    configs = default_array(jekyll_config).join(',')
    system("jekyll build --trace --config #{configs}")
  else
    system("jekyll build --trace")
  end
end

#cleanup_configObject



36
37
38
39
40
41
42
43
44
# File 'lib/clash/test.rb', line 36

def cleanup_config
  @cleanup.each do |file|
    if File.extname(file) == '.bak'
      FileUtils.mv(file, file.sub(/\.bak$/,''), force: true)
    else
      FileUtils.rm(file)
    end
  end
end

#compareObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/clash/test.rb', line 86

def compare
  default_array(@options['compare']).each do |files|
    f = files.gsub(',',' ').split

    differ = Diff.new(f.first, f.last, context: @options['context'])
    diff = differ.diff

    @test_failures.concat differ.test_failures

    diff.each do |title, diff|
      @test_failures << "#{title}\n#{diff}\n"
    end
  end
end

#configObject



26
27
28
29
30
31
32
33
34
# File 'lib/clash/test.rb', line 26

def config
  @options['config'].each do |name, file|
    case name
    when 'jekyll' then next
    when 'octopress' then config_octopress(file)
    else config_plugin(name, file)
    end
  end
end

#config_octopress(file) ⇒ Object



46
47
48
# File 'lib/clash/test.rb', line 46

def config_octopress(file)
  copy_config(file, '_octopress.yml')
end

#config_plugin(name, file) ⇒ Object



50
51
52
# File 'lib/clash/test.rb', line 50

def config_plugin(name, file)
  copy_config(file, "_plugins/#{name}/config.yml")
end

#copy_config(file, target) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/clash/test.rb', line 54

def copy_config(file, target)
  if File.exists?(file)
    # Make a backup of existing files first
    #
    if File.exists?(target)
      FileUtils.mv target, "#{target}.bak"
      @cleanup << "#{target}.bak"
    else
      @cleanup << target
    end

    FileUtils.mkdir_p(File.dirname(target))
    FileUtils.cp file, target
  else
    @test_failures << "Config file: #{file} cannot be found.\n"
  end
end

#enforce_missingObject



101
102
103
104
105
106
107
108
# File 'lib/clash/test.rb', line 101

def enforce_missing
  default_array(@options['enforce_missing']).each do |file|
    if File.exists?(file)
      message = yellowit("\nFile #{file} shouldn't exist.") + "\n  But it does!"
      @test_failures << message
    end
  end
end


110
111
112
113
114
115
116
# File 'lib/clash/test.rb', line 110

def print_result
  if @test_failures.empty?
    print_pass
  else
    print_fail
  end
end

#resultsObject



118
119
120
121
122
123
# File 'lib/clash/test.rb', line 118

def results
  if !@test_failures.empty?
    @test_failures.unshift(test_title)
    @test_failures
  end
end

#runObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/clash/test.rb', line 14

def run
  system_cmd(@options['before'])
  config
  build if @options['build']
  compare
  enforce_missing
  system_cmd(@options['after'])
  cleanup_config
  print_result
  results
end

#system_cmd(cmds) ⇒ Object



81
82
83
84
# File 'lib/clash/test.rb', line 81

def system_cmd(cmds)
  cmds = default_array(cmds)
  cmds.each {|cmd| system(cmd) }
end

#test_titleObject



125
126
127
128
129
130
131
132
# File 'lib/clash/test.rb', line 125

def test_title
  title = boldit("#{@options['index']})")
  title << " #{@options['title']}" unless @options['title'].nil?
  "\#{title}\n========================================================\n"
end