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, #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
# File 'lib/clash/test.rb', line 7

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

#acceptObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/clash/test.rb', line 115

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



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

def build
  options = "--trace"

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

  system "jekyll build #{options}"
end

#cleanup_configObject



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

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



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

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

#compareObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/clash/test.rb', line 134

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



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

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

#config_plugin(name, file) ⇒ Object



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

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

#copy_config(file, target) ⇒ Object



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

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



149
150
151
152
153
154
155
156
# File 'lib/clash/test.rb', line 149

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



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

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



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

def plugins_path
  jekyll_site.plugin_manager.plugins_path.first
end


158
159
160
161
162
163
164
# File 'lib/clash/test.rb', line 158

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

#resultsObject



166
167
168
169
170
171
# File 'lib/clash/test.rb', line 166

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

#runObject



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

def run
  Dir.chdir(@options['dir']) do
    clear_cache
    system_cmd(@options['before'])
    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) ⇒ Object



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

def system_cmd(cmds)
  cmds = Array(cmds)
  cmds.each {|cmd| 
    if @options['tasks'].include?(cmd)
      system_cmd(@options['tasks'][cmd])
    else
      system(cmd) 
    end
  }
end

#test_titleObject



173
174
175
176
177
178
179
180
# File 'lib/clash/test.rb', line 173

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