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, #expand_list_of_numbers, #expand_range, #get_number, #greenit, #pout, #print_fail, #print_pass, #read_test_line_numbers, #redit, #require_gems, #strip_tasks, #system, #test_at_line_number, #yellowit

Constructor Details

#initialize(options = {}) ⇒ Test

Returns a new instance of Test.



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

def initialize(options={})
  @test_failures = []
  @options  = options
  @options['config'] ||= {}
  @options['dir'] ||= '.'
  @cleanup = []
  require_gems
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

#acceptObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/clash/test.rb', line 119

def accept
  Dir.chdir(@options['dir']) do
    Array(@options['compare']).each do |files|
      f = files.gsub(',',' ').split

      if File.directory?(f.first)
        FileUtils.rm_r(f.first)
        FileUtils.mkdir_p(f.first)
        FileUtils.cp_r(File.join(f.last, '.'), f.first)
      else
        FileUtils.mkdir_p File.dirname(f.first)
        FileUtils.cp f.last, f.first
      end

      puts "Copied #{f.last} to #{f.first}"
    end
  end
end

#buildObject



95
96
97
98
99
100
101
102
103
# File 'lib/clash/test.rb', line 95

def build
  options = "--trace"

  if config = @options['config']['jekyll']
    options << " --config #{Array(config).join(',')}"
  end

  system "jekyll build #{options}"
end

#cleanup_configObject



47
48
49
50
51
52
53
54
55
# File 'lib/clash/test.rb', line 47

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

#clear_cacheObject



33
34
35
36
37
# File 'lib/clash/test.rb', line 33

def clear_cache
  if File.exist? '.jekyll-metadata'
    FileUtils.rm '.jekyll-metadata'
  end
end

#compareObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/clash/test.rb', line 138

def compare
  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



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

def config
  @options['config'].each do |name, file|
    if name != 'jekyll'
      config_plugin(name, file)
    end
  end
end

#config_plugin(name, file) ⇒ Object



57
58
59
# File 'lib/clash/test.rb', line 57

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

#copy_config(file, target) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/clash/test.rb', line 77

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



153
154
155
156
157
158
159
160
# File 'lib/clash/test.rb', line 153

def enforce_missing
  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

#jekyll_siteObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/clash/test.rb', line 65

def jekyll_site
  require 'jekyll'
  config = {}
  Array(@options['config']['jekyll'] || '_config.yml').each do |c| 
    config.merge!SafeYAML.load_file(c)
  end
  Jekyll.logger.log_level = :error
  site = Jekyll::Site.new(Jekyll.configuration(config))
  Jekyll.logger.log_level = :info
  site
end

#plugins_pathObject



61
62
63
# File 'lib/clash/test.rb', line 61

def plugins_path
  jekyll_site.plugin_manager.plugins_path.first
end


162
163
164
165
166
167
168
# File 'lib/clash/test.rb', line 162

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

#resultsObject



170
171
172
173
174
175
# File 'lib/clash/test.rb', line 170

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

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/clash/test.rb', line 16

def run
  Dir.chdir(@options['dir']) do
    clear_cache
    system_cmd(@options['before'], @options['trace'])
    config
    build if @options['build']
    unless @options['build_only']
      compare
      enforce_missing
      system_cmd(@options['after'])
    end
    cleanup_config
  end
  print_result
  results
end

#system_cmd(cmds, trace = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/clash/test.rb', line 105

def system_cmd(cmds, trace=nil)
  t = ENV['TRACE']
  ENV['TRACE'] = 'true' if trace
  cmds = Array(cmds)
  cmds.each {|cmd| 
    if @options['tasks'].include?(cmd)
      system_cmd(@options['tasks'][cmd], trace)
    else
      system(cmd) 
    end
  }
  ENV['TRACE'] = t
end

#test_titleObject



177
178
179
180
181
182
183
184
# File 'lib/clash/test.rb', line 177

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