Skip to content

Commit

Permalink
Implementing some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oleander committed Feb 9, 2011
1 parent 92db03d commit e1cd49d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion bin/ucallback
Expand Up @@ -4,6 +4,6 @@ $:.push File.expand_path("../../lib", __FILE__)
require 'rubygems'
require 'ucallback'

Ucallback.listen do |item|
Ucallback.listen(ARGV[1]) do |item|
system "echo '#{item}' >> #{ARGV[0]}"
end
28 changes: 14 additions & 14 deletions lib/ucallback.rb
Expand Up @@ -2,17 +2,17 @@
require 'fsevent'

class Ucallback < FSEvent
def self.listen(&block)
this = self.new(block)
def self.listen(application ='uTorrent', &block)
this = self.new(application, block)
this.latency = 0.0
this.watch_directories self.log_dir
this.watch_directories this.log_dir
this.start
end


def initialize(block)
@block = block
@last_line = get_last_line
def initialize(application, block)
@block = block
@application = application
@last_line = get_last_line
end

def on_change(directories)
Expand All @@ -24,20 +24,20 @@ def on_change(directories)
@block.call($1) if last_line =~ /#{self.matcher}/i
end

def self.log_dir
# "/private/var/log/"
"/tmp/temp_system"
def log_dir
#"/private/var/log/"
"/tmp/temp_system" # Only for tests
end

def log_file
"#{Ucallback.log_dir}/system.log"
"#{self.log_dir}/system.log"
end

def get_last_line
%x{tail -n 1 #{log_file}}
end

def matcher
"uTorrent: Download complete \((.*?)\) - Priority"
def matcher
"#{@application}: Download complete \((.*?)\) - Priority"
end
end
end
26 changes: 20 additions & 6 deletions spec/ucallback_spec.rb
@@ -1,11 +1,12 @@
require 'spec_helper'
def write
system "echo '2011-02-09 01.56.09 GrowlHelperApp[533] uTorrent: Download complete (House.S07E11.HDTV.XviD-LOL.avi) - Priority 0' >> /tmp/temp_system/system.log"
def write(application = 'uTorrent')
system "echo '2011-02-09 01.56.09 GrowlHelperApp[533] #{application}: Download complete (House.S07E11.HDTV.XviD-LOL.avi) - Priority 0' >> /tmp/temp_system/system.log"
end

describe Ucallback do
before(:all) do
@log_file = File.expand_path(File.dirname(__FILE__) + "/data/system.log")
@bin = File.expand_path(File.dirname(__FILE__) + "../../bin/ucallback")
end

before(:each) do
Expand All @@ -15,13 +16,12 @@ def write
@tmp_log_file = "#{@folder}/system.log"

%x{mkdir -p /tmp/temp_system && rm -r /tmp/temp_system && mkdir -p /tmp/temp_system && mkdir -p #{@folder} && cp #{@log_file} /tmp/temp_system}

@bin = File.expand_path(File.dirname(__FILE__) + "../../bin/ucallback")
system "sleep 3 && #{@bin} #{@file} &"

@loop = 0
end

it "should not see a message if the same already exists" do
system "sleep 3 && #{@bin} #{@file} uTorrent &"
write; sleep 6; write
loop do
sleep 0.2
Expand All @@ -33,6 +33,7 @@ def write
end

it "should know when a message is being passed" do
system "sleep 3 && #{@bin} #{@file} uTorrent &"
sleep 6; write

loop do
Expand All @@ -43,4 +44,17 @@ def write

File.read(@file).should match(/House\.S07E11\.HDTV\.XviD-LOL\.avi/)
end
end

it "should know when a message is being passed, using Transmission" do
system "sleep 3 && #{@bin} #{@file} Transmission &"
sleep 6; write("Transmission")

loop do
sleep 0.2
@loop += 1
break if system "test -e #{@file}" or @loop == 10 # Breaks if the file exists
end

File.read(@file).should match(/House\.S07E11\.HDTV\.XviD-LOL\.avi/)
end
end

0 comments on commit e1cd49d

Please sign in to comment.