Class: Autotestforphp

Inherits:
Object
  • Object
show all
Defined in:
lib/autotestforphp.rb,
lib/autotestforphp/cli.rb,
lib/autotestforphp/result.rb,
lib/autotestforphp/notification.rb,
lib/autotestforphp/notification/mac.rb,
lib/autotestforphp/notification/linux.rb,
lib/autotestforphp/notification/windows.rb

Defined Under Namespace

Modules: Notification Classes: CLI, Result

Constant Summary collapse

VERSION =
'0.0.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAutotestforphp

Returns a new instance of Autotestforphp.



31
32
33
34
# File 'lib/autotestforphp.rb', line 31

def initialize
  #    reset
  self.files_to_watch = []
end

Instance Attribute Details

#files_to_watchObject

Returns the value of attribute files_to_watch.



24
25
26
# File 'lib/autotestforphp.rb', line 24

def files_to_watch
  @files_to_watch
end

#filtersObject

Returns the value of attribute filters.



24
25
26
# File 'lib/autotestforphp.rb', line 24

def filters
  @filters
end

#interruptedObject

Returns the value of attribute interrupted.



24
25
26
# File 'lib/autotestforphp.rb', line 24

def interrupted
  @interrupted
end

#last_mtimeObject

Returns the value of attribute last_mtime.



24
25
26
# File 'lib/autotestforphp.rb', line 24

def last_mtime
  @last_mtime
end

#taintedObject

Returns the value of attribute tainted.



24
25
26
# File 'lib/autotestforphp.rb', line 24

def tainted
  @tainted
end

#wants_to_quitObject

Returns the value of attribute wants_to_quit.



24
25
26
# File 'lib/autotestforphp.rb', line 24

def wants_to_quit
  @wants_to_quit
end

Class Method Details

.installObject



13
14
15
# File 'lib/autotestforphp.rb', line 13

def install
  new.install
end

.run(folders_to_watch) ⇒ Object



16
17
18
19
20
21
# File 'lib/autotestforphp.rb', line 16

def run(folders_to_watch)
  folders_to_watch.map!{ |folder| File.expand_path(folder) }
  atp = new
  atp.watch(folders_to_watch)
  atp.run
end

Instance Method Details

#add_sigint_handlerObject



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

def add_sigint_handler
  trap 'INT' do
    if self.interrupted then
      self.wants_to_quit = true
    else
      puts "Interrupt a second time to quit"
      self.interrupted = true
      sleep 1.5
      raise Interrupt, nil # let the run loop catch it
    end
  end
end

#base_pathObject



39
40
41
42
43
44
# File 'lib/autotestforphp.rb', line 39

def base_path
  if @base_path.nil?
    self.base_path = File.expand_path('./') + "/"
  end
  @base_path
end

#base_path=(path) ⇒ Object



36
37
38
# File 'lib/autotestforphp.rb', line 36

def base_path=(path)
  @base_path = path
end

#bootstrap_file_exists?Boolean

Returns:

  • (Boolean)


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

def bootstrap_file_exists?
  File.exist?(bootstrap_file_path)
end

#bootstrap_file_pathObject



54
55
56
# File 'lib/autotestforphp.rb', line 54

def bootstrap_file_path
  "#{base_path}autotestforphp/bootstrap-autotest.php"
end

#config_file_exists?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/autotestforphp.rb', line 67

def config_file_exists?
  File.exist?(config_file_path)
end

#config_file_pathObject



45
46
47
# File 'lib/autotestforphp.rb', line 45

def config_file_path
  "#{base_path}autotestforphp/config.autotest"
end

#files_have_changedObject



135
136
137
138
139
# File 'lib/autotestforphp.rb', line 135

def files_have_changed
  not self.files_to_watch.find { |f|
    File.exist?(f) and File.mtime(f) > self.last_mtime
  }.nil?
end

#installObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/autotestforphp.rb', line 83

def install
  Dir.mkdir("#{base_path}autotestforphp/") unless File.exist?("#{base_path}autotestforphp/")
  File.open(phpunit_config_file_path, 'w') do |f|
    f.puts <<-CONFIG
<phpunit bootstrap="./autotestforphp/bootstrap-autotest.php">
  <testsuite name="Application">
      <directory>./test/</directory>
  </testsuite>
  <logging>
      <log type="test-xml" target="./autotestforphp/logfile.xml" />
  </logging>
</phpunit>
CONFIG
  end
  File.open(bootstrap_file_path, 'w').close
  File.open(config_file_path, 'w').close
end

#make_test_cmdObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/autotestforphp.rb', line 71

def make_test_cmd
  case RUBY_PLATFORM
  when /linux/
    cmd = "phpunit"
  when /mswin/
    cmd = "phpunit.bat"
  else
    raise('Your OS not supported yet!')
  end
  "#{cmd} --configuration \"#{phpunit_config_file_path}\""
end

#phpunit_config_file_exists?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/autotestforphp.rb', line 58

def phpunit_config_file_exists?
  File.exist?(phpunit_config_file_path)
end

#phpunit_config_file_pathObject



51
52
53
# File 'lib/autotestforphp.rb', line 51

def phpunit_config_file_path
  "#{base_path}autotestforphp/phpunit-autotest.xml"
end

#phpunit_log_file_exists?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/autotestforphp.rb', line 64

def phpunit_log_file_exists?
  File.exist?(phpunit_log_file_path)
end

#phpunit_log_file_pathObject



48
49
50
# File 'lib/autotestforphp.rb', line 48

def phpunit_log_file_path
  "#{base_path}autotestforphp/logfile.xml"
end

#resetObject



155
156
157
158
159
160
# File 'lib/autotestforphp.rb', line 155

def reset
  self.interrupted = false
  self.tainted = false
  self.wants_to_quit = false
  self.watch(self.filters)
end

#runObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/autotestforphp.rb', line 101

def run
  add_sigint_handler

  loop do # ^c handler
    begin        
      run_all_tests
      wait_for_changes
    rescue Interrupt
      break if self.wants_to_quit
      reset
    end
  end
end

#run_all_testsObject



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/autotestforphp.rb', line 115

def run_all_tests
  cmd = make_test_cmd
  self.last_mtime = Time.now

  clear_log
  clear_screen
  puts cmd
  system(cmd)

  notify
end

#wait_for_changesObject



152
153
154
# File 'lib/autotestforphp.rb', line 152

def wait_for_changes
  sleep 1.5 until files_have_changed
end

#watch(folders) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/autotestforphp.rb', line 126

def watch(folders)
  self.last_mtime = Time.now
  self.filters = folders
  folders.each do |arg|
    Dir[arg].each do |file|
      self.files_to_watch << file unless File.directory?(file)
    end
  end
end