Top Level Namespace

Defined Under Namespace

Modules: Rho Classes: JakeRCC

Instance Method Summary collapse

Instance Method Details

#cleanup_appsObject



132
133
134
135
# File 'lib/build/run_rhoconnect_spec.rb', line 132

def cleanup_apps
  puts "cleanup"
  FileUtils.rm_r File.expand_path($tmp_path) if File.directory?($tmp_path)
end

#run_rhoconnect_spec(platform, appname, flags) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/build/run_rhoconnect_spec.rb', line 5

def run_rhoconnect_spec(platform,appname,flags)
  test_appname = "testapp"
  puts "run_spec_app(#{platform},#{appname})"

  rhobuildyml = File.join($rho_root,'rhobuild.yml')
  $app_path = File.expand_path(File.join(File.dirname(__FILE__),'..','..','spec',appname))
  puts "app path: #{$app_path}"

  $app_config = Jake.config(File.open(File.join($app_path, "build.yml")))
  config = Jake.config(File.open(rhobuildyml,'r'))

  source_path = File.expand_path(File.join($app_path,'..','server'))
  $tmp_path = File.join(File.dirname(__FILE__),'..','..','tmp')

  cleanup_apps

  FileUtils.mkdir_p File.expand_path($tmp_path)
  server_path = File.expand_path(File.join($tmp_path,'testapp'))

  $rhoconnect_bin = "#{$rhoconnect_root}/bin/rhoconnect"
  puts "$rhoconnect_bin: #{$rhoconnect_bin}"

  RhoconnectHelper.set_rhoconnect_bin $rhoconnect_root
  puts "rhoconnect_bin: #{RhoconnectHelper.rhoconnect_bin}"

  RhoconnectHelper.set_rc_out File.open( File.join($app_path, "rhoconnect.log" ), "w")
  RhoconnectHelper.set_redis_out File.open( File.join($app_path, "redis.log" ), "w")
  RhoconnectHelper.set_resque_out File.open( File.join($app_path, "resque.log" ), "w")
  RhoconnectHelper.set_rc_push_out File.open( File.join($app_path, "rc_push.log" ), "w")
  RhoconnectHelper.set_enable_push(false)
  RhoconnectHelper.set_enable_rails(false)
  pong = `redis-cli ping`
  RhoconnectHelper.set_enable_redis(!(pong =~ /PONG/)) # Do not touch redis if it's running

  RhoconnectHelper.stop_rhoconnect_stack

  @mutex  = Mutex.new
  # @signal = ConditionVariable.new
  @server, @addr, @port = Jake.run_local_server(8081)
  @server.mount_proc('/', nil) do |req, res|
    res.status = 200
    @mutex.synchronize do
      # puts " query_string: #{req.query_string}"
      # puts " Body: #{req.body}"
      @file_name =  req.query_string.split("=")[1]
      xml_file = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', @file_name))
      File.open(xml_file, "w") { |f| f << req.body }
      puts "Test results are saved in #{@file_name}"
      # @signal.signal
    end
  end
  trap(:INT) { @server.shutdown }

  puts "Generating app ..."
  # Generate app, set rhoconnect gem path to its sources, but do not run bundler
  res = RhoconnectHelper.generate_app($tmp_path, test_appname, false)

  target_gemfile = File.join(server_path, 'Gemfile')
  puts "Patching Gemfile with aws-s3 ..."
  File.open(target_gemfile, 'a') {|f| f.puts "gem 'aws-s3', '>= 0.6.3'" }
  puts "Patching Gemfile with sqlite3 ..."
  File.open(target_gemfile, 'a') {|f| f.puts "gem 'sqlite3', '>= 1.3.3'" }
  puts "Bundle install ..."
  Kernel.system("bundle","install",:chdir => server_path)

  puts "Adding source files ..."
  FileUtils.cp_r ["#{source_path}/sources","#{source_path}/settings"], server_path

  puts "Cleanup rhoconnect data ..."
  FileUtils.rm_r(File.join(server_path,"data")) if File.directory?(File.join(server_path,"data"))

  RhoconnectHelper.start_rhoconnect_stack(server_path,true)

  generateRuby = flags && flags[:ruby]==true
  generateJS = flags && flags[:js]==true

  if generateRuby then
    File.open(File.join($app_path, 'app', 'sync_server.rb'), 'w') do |f|
      f.puts "SYNC_SERVER_HOST = '#{RhoconnectHelper.host}'"
      f.puts "SYNC_SERVER_PORT = #{RhoconnectHelper.port}"
    end
    File.open(File.join($app_path, 'app', 'local_server.rb'), 'w') do |f|
      f.puts "SPEC_LOCAL_SERVER_HOST = '#{@addr}'"
      f.puts "SPEC_LOCAL_SERVER_PORT = #{@port}"
    end
  end

  if generateJS then
    File.open(File.join($app_path, 'public', 'app', 'sync_server.js'), 'w') do |f|
      f.puts "var SYNC_SERVER_HOST = '#{RhoconnectHelper.host}';"
      f.puts "var SYNC_SERVER_PORT = #{RhoconnectHelper.port};"
    end
    File.open(File.join($app_path, 'public', 'app', 'local_server.js'), 'w') do |f|
      f.puts "var SPEC_LOCAL_SERVER_HOST = '#{@addr}';"
      f.puts "var SPEC_LOCAL_SERVER_PORT = #{@port};"
    end
  end

  puts "Running specs ..."
  chdir $rho_root
  Rake::Task.tasks.each { |t| t.reenable }
  run_specs = $device && $device != "emulator" ? "run:#{platform}:#{$device}:spec" : "run:#{platform}:spec"

  uninstall_app = false

    Rake::Task[run_specs].invoke(uninstall_app)


rescue SystemExit => e
  # FIXME: iphone rake task throws SystemExit exception. Swallow it!
  # puts "Got exception: #{e}"
rescue Exception => e
  puts e.message
  puts e.backtrace.join("\n")
ensure
  # @mutex.synchronize do
  #   @signal.wait(@mutex, 30) # wait timeout
  #   puts "Processed #{@file_name}"
  # end
  RhoconnectHelper.stop_rhoconnect_stack
  cleanup_apps
  Rake::Task["stop:#{platform}:#{$device}"].invoke
  # TODO:
  # `killall iphonesim_43 2> /dev/null` if platform == 'iphone'
  puts "run_spec_app(#{platform},#{appname}) done"
end