Class: TestML::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/testml/setup.rb

Instance Method Summary collapse

Instance Method Details

#rel(path, base = '.') ⇒ Object



61
62
63
64
# File 'lib/testml/setup.rb', line 61

def rel path, base='.'
  base = Pathname.new(File.absolute_path(base))
  Pathname.new(path).relative_path_from(base).to_s
end

#setup(testml_conf) ⇒ Object



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
# File 'lib/testml/setup.rb', line 10

def setup(testml_conf)
  testml_conf ||= DEFAULT_TESTML_CONF
  fail "TestML conf file '#{testml_conf}' not found" \
    unless File.exists? testml_conf
  fail "TestML conf file must be .yaml" \
    unless testml_conf.match /\.ya?ml$/
  # File paths are relative to the yaml file location
  base = File.dirname testml_conf
  conf = YAML.load File.read testml_conf
  source = conf['source_testml_dir'] \
    or fail "`rake testml` requires 'source_testml_dir' key in '#{testml_conf}'"
  target = conf['local_testml_dir'] \
    or fail "`rake testml` requires 'local_testml_dir' key in '#{testml_conf}'"
  tests = conf['test_file_dir'] || '.'
  source = File.expand_path source, base
  target = File.expand_path target, base
  tests = File.expand_path tests, base
  fail "'#{source}' directory does not exist" \
    unless Dir.exists? source
  mkdir target unless Dir.exists? target
  mkdir tests unless Dir.exists? tests
  template = conf['test_file_template']
  skip = conf['exclude_testml_files'] || []
  files = conf['include_testml_files'] ||
    Dir.new(source).grep(/\.tml$/)
  files.sort.each do |file|
    next if skip.include? file
    s = "#{source}/#{file}"
    t = "#{target}/#{file}"
    if ! uptodate?(t, [s])
      puts "Copying #{rel(s)} to #{rel(t)}"
      cp rel(s), rel(t)
    end
    if template
      test = file.sub /\.tml$/, '.rb'
      test = conf['test_file_prefix'] + test \
        if conf['test_file_prefix']
      test = File.expand_path test, tests
      hash = {
        file: rel(t, base),
      }
      code = template % hash
      if ! File.exists?(test) or code != File.read(test)
        action = File.exists?(test) ? 'Updating' : 'Creating'
        puts "#{action} test file '#{rel test}'"
        File.write test, code
      end
    end
  end
end