Class: TestSwarm::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/testswarm/job.rb

Defined Under Namespace

Classes: AlreadyPrepared, BuildFailed, FailedCheckout, MissingConfig, UnknownRevision

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Job

Returns a new instance of Job

Raises:


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/testswarm/job.rb', line 16

def initialize(settings)
  @suites    = {}
  @rcs       = settings[:rcs]
  @directory = settings[:directory]
  @diff      = settings[:diff]
  @build     = settings[:build]
  @inject    = settings[:inject]
  @keep      = settings[:keep]
  @new       = true

  raise MissingConfig, "Required setting :rcs is missing" unless @rcs
  raise MissingConfig, "Required setting :rcs->:type is missing" unless @rcs[:type]
  raise MissingConfig, "Required setting :rcs->:url is missing" unless @rcs[:url]
  raise MissingConfig, "Required setting :directory is missing" unless @directory
  raise MissingConfig, "Required setting :inject is missing" unless @inject

  @directory = File.expand_path(@directory)

  Kernel.at_exit { close_logfile }
end

Class Method Details

.create(*args) ⇒ Object


10
11
12
13
14
# File 'lib/testswarm/job.rb', line 10

def self.create(*args)
  job = new(*args)
  job.prepare!
  job
end

Instance Method Details

#add_suite(name, url) ⇒ Object


37
38
39
# File 'lib/testswarm/job.rb', line 37

def add_suite(name, url)
  @suites[name] = url
end

#each_suiteObject


41
42
43
44
45
# File 'lib/testswarm/job.rb', line 41

def each_suite
  @suites.keys.sort.each do |name|
    yield(name, @suites[name])
  end
end

#inject_script(url) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/testswarm/job.rb', line 81

def inject_script(url)
  return unless @inject

  log "chdir #{File.join @directory, revision}"
  Dir.chdir(File.join(@directory, revision))

  Dir.glob(@inject).each do |path|
    log "Injecting #{url} into #{path}"
    html = File.read(path)
    html.gsub! /<\/head>/, %Q{<script>document.write('<scr' + 'ipt src="#{url}?' + (new Date).getTime() + '"><\/scr' + 'ipt>');<\/script><\/head>}
    File.open(path, 'w') { |f| f.write(html) }
  end
end

#log(message) ⇒ Object


95
96
97
98
99
100
# File 'lib/testswarm/job.rb', line 95

def log(message)
  FileUtils.mkdir_p(@directory)
  @logfile ||= File.open(File.join(@directory, 'testswarm.log'), 'a')
  @logfile.sync = true
  @logfile.puts("[#{Time.now.strftime '%Y-%m-%d %H:%M:%S'}] #{message}")
end

#new?Boolean


47
48
49
# File 'lib/testswarm/job.rb', line 47

def new?
  @new
end

#prepare!Object

Raises:


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/testswarm/job.rb', line 51

def prepare!
  raise AlreadyPrepared if @prepared
  @prepared = true

  @pwd = Dir.pwd

  enter_base_directory

  return @new = false unless acquire_lock

  checkout_codebase
  determine_revision
  discard_old_releases

  if existing_job? or not javascript_changed?
    @new = false
    return reset
  end

  build_project
  reset
end

#revisionObject


74
75
76
77
78
79
# File 'lib/testswarm/job.rb', line 74

def revision
  unless @revision
    raise UnknownRevision, "Job may not have been prepared"
  end
  @revision
end