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_ruby_files(name) ⇒ Object

Find all Ruby all files under the named dir



10
11
12
# File 'lib/atto/run.rb', line 10

def all_ruby_files(name)
  Dir["#{name}/**/*.rb"]
end

#helpObject

Shows help message



63
64
65
66
67
68
69
70
71
# File 'lib/atto/run.rb', line 63

def help
  warn "Usage: #{$0} OPTIONS.\nRuns unit tests in the current directory.\n"
  warn "    --help        -h      Display this help message."
  warn "    --project=dir -pdir   Sets project directory."
  warn "    --lib=sub     -lsub   Sets project library subdirectory."
  warn "    --test=sub    -tsub   Sets project test subdirectory."
  warn "    --skip        -s      Skips tests on startup.\n"
  return 0
end

#mainObject

Main, runs the tests when needed



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/atto/run.rb', line 74

def main
  return help if Atto::Cop.get(:help)
  @projdir  = Atto::Cop.get(:project, Dir.pwd)
  @libdir   = File.join(@projdir, Atto::Cop.get(:lib, 'lib'))
  @testdir  = File.join(@projdir, Atto::Cop.get(:test, 'test'))
  update_all
  @ran_tests = run_tests(@testfiles, Atto::Cop.get(:skip, false))
  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 with testfiles



24
25
26
27
28
29
30
31
32
33
# File 'lib/atto/run.rb', line 24

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



49
50
51
# File 'lib/atto/run.rb', line 49

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

#run_tests(list, skip = false) ⇒ Object

Runs a list of tests



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/atto/run.rb', line 36

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

#update_allObject

Updates all state info



54
55
56
57
58
59
60
# File 'lib/atto/run.rb', line 54

def update_all
  @libfiles  = all_ruby_files(@libdir)
  @testfiles = all_ruby_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



15
16
17
18
19
20
21
# File 'lib/atto/run.rb', line 15

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