Class: Atto::Run

Inherits:
Object
  • Object
show all
Defined in:
lib/atto/run.rb

Overview

Test autorunner

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.mainObject

Shortcut to run main



5
6
7
# File 'lib/atto/run.rb', line 5

def self.main
  return self.new.main
end

Instance Method Details

#all_files(name) ⇒ Object

Find all Ruby all files under the named dir



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/atto/run.rb', line 10

def all_files(name)
  return Dir.new(name).inject([]) do |res, e| 
    full   = File.join(name, e)
    if Dir.exist?(full) &&  e !='.' && e != '..'
      res += all_files(full)
    else
      res << full if full =~ /\.rb\Z/
    end
    res
  end
end

#mainObject

Main, runs the tests when needed



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/atto/run.rb', line 69

def main
  @projdir  = ARGV[0] || Dir.pwd
  @libdir   = File.join(@projdir, ARGV[1] || 'lib')
  @testdir  = File.join(@projdir, ARGV[2] || 'test')
  update_all
  @ran_tests = run_tests(@testfiles)
  loop do
    update_all
    torun     = @testinfo.select do |k,v| 
      must_run(@ran_tests, k, v)
    end  
    extra     = @libinfo.select  do |k,v| 
      linked  = @matchfiles[k]
      linked ? must_run(@ran_tests, linked, v) : false
    end.map { |k,v| @matchfiles[k] }
    run_tests(torun.keys + extra).each { |k,v| @ran_tests[k] = v }
    sleep(1)
  end
end

#match_files(libfiles, testfiles) ⇒ Object

matches libfiles wih testfiles



32
33
34
35
36
37
38
39
40
41
# File 'lib/atto/run.rb', line 32

def match_files(libfiles, testfiles)
  return libfiles.inject({}) do |res, libname| 
    testname = libname.dup
    where    = libname.rindex(File::Separator)
    testname[where]= File::Separator + 'test_'
    testname[File.join('','lib','')] = File.join('','test','')
    res[libname] = testname if testfiles.member?(testname)
    res
  end
end

#must_run(ran_tests, k, v) ⇒ Object

Checks if a test file must run



55
56
57
# File 'lib/atto/run.rb', line 55

def must_run(ran_tests, k, v)
  ran_tests[k] ? ran_tests[k] <= v.mtime : true
end

#run_tests(list) ⇒ Object

Runs a list of tests



44
45
46
47
48
49
50
51
52
# File 'lib/atto/run.rb', line 44

def run_tests(list)
  return list.inject({}) do |results, file|
    puts("Running tests for #{file}:")
    res = system("ruby -I #@libdir -I #@testdir #{file}")
    puts res ? "OK!" : "Failed!"
    results[file] = Time.now
    results
  end
end

#update_allObject

updates all state info



60
61
62
63
64
65
66
# File 'lib/atto/run.rb', line 60

def update_all
  @libfiles  = all_files(@libdir)
  @testfiles = all_files(@testdir)
  @matchfiles= match_files(@libfiles, @testfiles)
  @libinfo   = update_info(@libfiles)
  @testinfo  = update_info(@testfiles)
end

#update_info(files) ⇒ Object

updates the timestamp info for files



23
24
25
26
27
28
29
# File 'lib/atto/run.rb', line 23

def update_info(files)
  return files.inject({}) do |res, name| 
    stat      = File.stat(name)
    res[name] = Struct.new(:mtime).new(stat.mtime)
    res
  end
end