Module: Typingpool::Utility::Test::Script

Includes:
Typingpool::Utility::Test
Included in:
Test::Script
Defined in:
lib/typingpool/utility/test/script.rb

Constant Summary collapse

@@readymade_project_path =
nil

Instance Method Summary collapse

Methods included from Typingpool::Utility::Test

#add_goodbye_message, #amazon_credentials?, #audio_dir, #broken_url_eventually?, #cleared_vcr_fixture_path_for, #config, #delete_vcr_fixture, #dummy_config, #fixtures_dir, #project_default, #s3_credentials?, #sftp_credentials?, #vcr_core_host_matcher, #vcr_dir, #vcr_load, #vcr_stop, #with_vcr, #working_url_eventually?, #works_eventually?

Instance Method Details

#assign_defaultObject



210
211
212
213
214
215
216
217
218
219
# File 'lib/typingpool/utility/test/script.rb', line 210

def assign_default
  Hash[
       :template => 'interview/phone',
       :deadline => '5h',
       :lifetime => '10h',
       :approval => '10h',
       :qualify => ['approval_rate >= 90', 'hits_approved > 10'],
       :keyword => ['test', 'mp3', 'typingpooltest']
      ]
end

#audio_files(subdir = 'mp3') ⇒ Object



205
206
207
208
# File 'lib/typingpool/utility/test/script.rb', line 205

def audio_files(subdir='mp3')
  dir = File.join(audio_dir, subdir)
  Dir.entries(dir).reject{|entry| entry.match(/^\./) }.map{|entry| File.join(dir, entry)}.select{|path| File.file?(path) }
end

#call_script(script_name, *args) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/typingpool/utility/test/script.rb', line 112

def call_script(script_name, *args)
  out, err, status = Open3.capture3(path_to_script(script_name), *args)
  if status.success?
    return [out.to_s.chomp, err.to_s.chomp]
  else
    if err
      raise Error::Shell, err.chomp
    else
      raise Error::Shell
    end
  end
end

#config_path(dir) ⇒ Object



79
80
81
# File 'lib/typingpool/utility/test/script.rb', line 79

def config_path(dir)
  File.join(dir, project_default[:config_filename])   
end

#copy_fixtures_to_project_dir(fixture_prefix, project_path) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/typingpool/utility/test/script.rb', line 345

def copy_fixtures_to_project_dir(fixture_prefix, project_path)
  copies = 0
  with_fixtures_in_project_dir(fixture_prefix, project_path) do |source_fixture_path, project_fixture_path|
    if File.exist? project_fixture_path
      FileUtils.mv(project_fixture_path, File.join(File.dirname(project_fixture_path), "orig_#{File.basename(project_fixture_path)}"))
    end
    FileUtils.cp(source_fixture_path, project_fixture_path)
    copies += 1
  end
  copies > 0 or raise Error, "No fixtures to copy with prefix #{fixture_prefix}"
  copies
end

#copy_tp_assign_fixtures(dir, fixture_prefix, config_path = config_path(dir), project_title = ) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/typingpool/utility/test/script.rb', line 243

def copy_tp_assign_fixtures(dir, fixture_prefix, config_path=config_path(dir), project_title=project_default[:title])
  project = Project.new(project_title, Config.file(config_path))
  if Typingpool::Test.record
    project_time(project, Time.now)
    with_fixtures_in_transcripts_dir(dir, "#{fixture_prefix}_", project_title) do |fixture_path, project_path|
      FileUtils.cp(project_path, fixture_path)
    end
  elsif not(Typingpool::Test.live)
    copy_fixtures_to_project_dir("#{fixture_prefix}_", File.join(dir, project_title))
    reconfigure_project_csv(project)
  end
end

#do_laterObject

Intended to be overriden by some classes that mixin this module



85
86
87
# File 'lib/typingpool/utility/test/script.rb', line 85

def do_later
  at_exit{ yield }
end

#make_fixture_transcripts_dir(name) ⇒ Object



326
327
328
329
330
331
332
333
# File 'lib/typingpool/utility/test/script.rb', line 326

def make_fixture_transcripts_dir(name)
  dir = File.join(fixtures_dir, name)
  if File.exist? dir
    raise Error::Test, "Fixture transcript dir already exists for #{name} at #{dir}"
  end
  Dir.mkdir(dir)
  dir
end

#path_to_script(script_name) ⇒ Object



107
108
109
# File 'lib/typingpool/utility/test/script.rb', line 107

def path_to_script(script_name)
  File.join(Utility.app_dir, 'bin', script_name)
end

#project_time(project, time = nil) ⇒ Object



310
311
312
313
314
315
316
317
318
# File 'lib/typingpool/utility/test/script.rb', line 310

def project_time(project, time=nil)
  file = project.local.file('data', 'time.txt')
  if time
    file.write(time.to_i)
  else
    time = Time.at(file.read.to_i)
  end
  time
end

#project_transcript_count(project, which_csv) ⇒ Object



368
369
370
# File 'lib/typingpool/utility/test/script.rb', line 368

def project_transcript_count(project, which_csv)
  project.local.file('data', which_csv).as(:csv).reject{|assignment| assignment['transcript'].to_s.empty?}.size
end

#readymade_project_pathObject



69
70
71
72
73
74
75
76
77
# File 'lib/typingpool/utility/test/script.rb', line 69

def readymade_project_path
  unless @@readymade_project_path
    transcripts_dir = @@readymade_project_path = Dir.mktmpdir('typingpool_')
    do_later{ FileUtils.remove_entry_secure(transcripts_dir) }
    write_testing_config_for_transcripts_dir(transcripts_dir, reconfigure_for_s3(self.config))
    tp_make(transcripts_dir, config_path(transcripts_dir), 'mp3', true)
  end
  @@readymade_project_path
end

#reconfigure_for_s3(config) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/typingpool/utility/test/script.rb', line 35

def reconfigure_for_s3(config)
  unless s3_credentials?(config)
    raise Error::Test, "No S3 credentials available"
  end
  config.to_hash.delete('sftp')
  config
end

#reconfigure_for_testing(config) ⇒ Object



49
50
51
52
53
# File 'lib/typingpool/utility/test/script.rb', line 49

def reconfigure_for_testing(config)
  config['assign']['reward'] = '0.02'
  config.assign.to_hash.delete('qualify')
  config
end

#reconfigure_for_transcripts_dir(config, transcripts_dir) ⇒ Object



43
44
45
46
47
# File 'lib/typingpool/utility/test/script.rb', line 43

def reconfigure_for_transcripts_dir(config, transcripts_dir)
  config.transcripts = transcripts_dir
  config.cache = File.join(transcripts_dir, '.cache')
  config
end

#reconfigure_project(project) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/typingpool/utility/test/script.rb', line 89

def reconfigure_project(project)
  #rewrite URLs in assignment.csv according to config at config_path
  File.delete(project.local.file('data', 'id.txt'))
  project.local.create_id
  reconfigure_project_csv(project)
  project
end

#reconfigure_project_csv(project) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/typingpool/utility/test/script.rb', line 97

def reconfigure_project_csv(project)
  assignments = project.local.file('data', 'assignment.csv').as(:csv)
  urls = project.create_remote_names(assignments.map{|assignment| Project.local_basename_from_url(assignment['audio_url']) }).map{|file| project.remote.file_to_url(file) }
  assignments.each! do |assignment|
    assignment['audio_url'] = urls.shift
    assignment['project_id'] = project.local.id
  end
  assignments
end

#restore_project_dir_from_fixtures(fixture_prefix, project_path) ⇒ Object



358
359
360
361
362
363
364
365
366
# File 'lib/typingpool/utility/test/script.rb', line 358

def restore_project_dir_from_fixtures(fixture_prefix, project_path)
  with_fixtures_in_project_dir(fixture_prefix, project_path) do |source_fixture_path, project_fixture_path|
    FileUtils.rm(project_fixture_path) if File.exists? project_fixture_path
    path_to_orig = File.join(File.dirname(project_fixture_path), "orig_#{File.basename(project_fixture_path)}")
    if File.exist?(path_to_orig)
      FileUtils.mv(path_to_orig, project_fixture_path)
    end
  end #with_fixtures_in_transctips_dir
end

#script_with_vcr(dir, fixture_name, config_path = config_path(dir), can_ever_run_live = true, are_recording = Typingpool::Test.record, choices = nil) {|[dir, config_path, project_default[:title], choices, *vcr_args(fixture_name, can_ever_run_live, are_recording)]| ... } ⇒ Object

Yields:



188
189
190
# File 'lib/typingpool/utility/test/script.rb', line 188

def script_with_vcr(dir, fixture_name, config_path=config_path(dir), can_ever_run_live=true, are_recording=Typingpool::Test.record, choices=nil)
  yield([dir, config_path, project_default[:title], choices, *vcr_args(fixture_name, can_ever_run_live, are_recording)])
end

#simulate_failed_audio_upload_in(dir, config_path = config_path(dir)) ⇒ Object



320
321
322
323
324
# File 'lib/typingpool/utility/test/script.rb', line 320

def simulate_failed_audio_upload_in(dir, config_path=config_path(dir))
  project = Typingpool::Project.new(project_default[:title], Config.file(config_path))
  csv = project.local.file('data', 'assignment.csv').as(:csv)
  csv.each!{|a| a['audio_uploaded'] = 'maybe'}
end

#split_reviews(output) ⇒ Object



372
373
374
# File 'lib/typingpool/utility/test/script.rb', line 372

def split_reviews(output)
  output.split(/Transcript for\b/)
end

#tp_assign(dir, config_path = config_path(dir), project_title = , *args) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/typingpool/utility/test/script.rb', line 221

def tp_assign(dir, config_path=config_path(dir), project_title=project_default[:title], *args)
  call_script(
              'tp-assign',
              '--sandbox',
              project_title,
              assign_default[:template],
              '--config', config_path,
              *[:deadline, :lifetime, :approval].map{|param| ["--#{param}", assign_default[param]] }.flatten,
              *[:qualify, :keyword].map{|param| assign_default[param].map{|value| ["--#{param}", value] } }.flatten,
              *args)
  
end

#tp_assign_with_vcr(dir, fixture_name, config_path = config_path(dir), project_title = ) ⇒ Object



234
235
236
237
238
239
240
241
# File 'lib/typingpool/utility/test/script.rb', line 234

def tp_assign_with_vcr(dir, fixture_name, config_path=config_path(dir), project_title=project_default[:title])
  project = Project.new(project_default[:title], Typingpool::Config.file(config_path))
  args = [dir, config_path, project_title, *vcr_args(fixture_name)]
  unless (Typingpool::Test.live || Typingpool::Test.record)
    args.push('--testtime', project_time(project).to_i.to_s)
  end
  tp_assign(*args)
end

#tp_collect_with_fixture(dir, fixture_name, are_recording = false) ⇒ Object



256
257
258
259
260
261
262
263
264
# File 'lib/typingpool/utility/test/script.rb', line 256

def tp_collect_with_fixture(dir, fixture_name, are_recording=false)
  fixture_handle = File.join(vcr_dir, fixture_name)
  args = ['tp-collect', '--sandbox', '--testfixture', fixture_handle, '--config', config_path(dir)]
  if are_recording
    delete_vcr_fixture(fixture_name)
    args.push('--testfixturerecord')
  end
  call_script(*args)
end

#tp_config_with_input(args, input) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/typingpool/utility/test/script.rb', line 296

def tp_config_with_input(args, input)
  output = {}
  Open3.popen3(path_to_script('tp-config'), *args) do |stdin, stdout, stderr, wait_thr|
    input.each do |sending|
      stdin.puts(sending)
    end
    output[:out] = stdout.gets(nil)
    output[:err] = stderr.gets(nil)
    [stdin, stdout, stderr].each{|stream| stream.close }
    output[:status] = wait_thr.value
  end #Open3.popen3...
  output
end

#tp_finish(dir, config_path = config_path(dir), project_title = , choices = nil, *args) ⇒ Object



151
152
153
154
# File 'lib/typingpool/utility/test/script.rb', line 151

def tp_finish(dir, config_path=config_path(dir), project_title=project_default[:title], choices=nil, *args)
  tp_finish_inside_sandbox(dir, config_path, project_title, choices, *args)
  tp_finish_outside_sandbox(dir, config_path, project_title, choices, *args)
end

#tp_finish_inside_sandbox(dir, config_path = config_path(dir), project_title = , choices = nil, *args) ⇒ Object



157
158
159
# File 'lib/typingpool/utility/test/script.rb', line 157

def tp_finish_inside_sandbox(dir, config_path=config_path(dir), project_title=project_default[:title], choices=nil, *args)
  tp_finish_outside_sandbox(dir, config_path, project_title, choices, '--sandbox', *args)
end

#tp_finish_inside_sandbox_with_vcr(*args) ⇒ Object



200
201
202
# File 'lib/typingpool/utility/test/script.rb', line 200

def tp_finish_inside_sandbox_with_vcr(*args)
  script_with_vcr(*args){|new_args| tp_finish_inside_sandbox(*new_args) }
end

#tp_finish_outside_sandbox(dir, config_path = config_path(dir), project_title = , choices = nil, *args) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/typingpool/utility/test/script.rb', line 161

def tp_finish_outside_sandbox(dir, config_path=config_path(dir), project_title=project_default[:title], choices=nil, *args)
  output = {}
  args = [
          File.join(Utility.app_dir, 'bin', 'tp-finish'),
          project_title,
          '--config',
          config_path,
          *args
         ]
  Open3.popen3(*args) do |stdin, stdout, stderr, wait_thr|
    choices.each{|choice| stdin.puts(choice) } if choices
    output[:out] = stdout.gets(nil)
    output[:err] = stderr.gets(nil)
    [stdin, stdout, stderr].each{|stream| stream.close }
    output[:status] = wait_thr.value
  end
  if output[:status] == 0
    return [output[:out].to_s.chomp, output[:err].to_s.chomp]
  else
    if output[:err]
      raise Error::Shell, output[:err].chomp
    else
      raise Error::Shell
    end
  end #if output[:status] != 0  
end

#tp_finish_outside_sandbox_with_vcr(*args) ⇒ Object



196
197
198
# File 'lib/typingpool/utility/test/script.rb', line 196

def tp_finish_outside_sandbox_with_vcr(*args)
  script_with_vcr(*args){|new_args| tp_finish_outside_sandbox(*new_args) }
end

#tp_finish_with_vcr(*args) ⇒ Object



192
193
194
# File 'lib/typingpool/utility/test/script.rb', line 192

def tp_finish_with_vcr(*args)
  script_with_vcr(*args){|new_args| tp_finish(*new_args) }
end

#tp_make(in_dir, config = config_path(in_dir), audio_subdir = 'mp3', devtest_mode_skipping_upload = false, *args) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/typingpool/utility/test/script.rb', line 125

def tp_make(in_dir, config=config_path(in_dir), audio_subdir='mp3', devtest_mode_skipping_upload=false, *args)
  commands = [
              '--config', config, 
              '--chunks', project_default[:chunks],
              *[:title, :subtitle].map{|param| ["--#{param}", project_default[param]] }.flatten,
              *[:voice, :unusual].map{|param| project_default[param].map{|value| ["--#{param}", value] } }.flatten,
              *audio_files(audio_subdir).map{|path| ['--file', path]}.flatten, 
              *args
             ]
  commands.push('--testnoupload', '--testkeepmergefile') if devtest_mode_skipping_upload
  call_script('tp-make', *commands)
end

#tp_make_with_vcr(dir, fixture_name, config_path = config_path(dir)) ⇒ Object



138
139
140
# File 'lib/typingpool/utility/test/script.rb', line 138

def tp_make_with_vcr(dir, fixture_name, config_path=config_path(dir))
  tp_make(dir, config_path, 'mp3', false, *vcr_args(fixture_name))
end

#tp_review_with_fixture(transcripts_dir, fixture_name, choices, are_recording = false, project_name = nil) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/typingpool/utility/test/script.rb', line 266

def tp_review_with_fixture(transcripts_dir, fixture_name, choices, are_recording=false, project_name=nil)
  fixture_handle = File.join(vcr_dir, fixture_name)
  output = {}
  args = [
          File.join(Utility.app_dir, 'bin', 'tp-review'),
          '--sandbox',
          '--config', config_path(transcripts_dir),
          '--testfixture', fixture_handle
         ]
  args.push(project_name) if project_name
  if are_recording
    delete_vcr_fixture(fixture_name)
    args.push('--testfixturerecord')
  end
  
  Open3.popen3(*args) do |stdin, stdout, stderr, wait_thr|
    choices.each do |choice|
      stdin.puts(choice)
      if choice.strip.match(/^r/i)
        stdin.puts("No reason - this is a test")
      end
    end
    output[:out] = stdout.gets(nil)
    output[:err] = stderr.gets(nil)
    [stdin, stdout, stderr].each{|stream| stream.close }
    output[:status] = wait_thr.value
  end
  output
end

#vcr_args(fixture_name, can_ever_run_live = true, are_recording = Typingpool::Test.record) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/typingpool/utility/test/script.rb', line 142

def vcr_args(fixture_name, can_ever_run_live=true, are_recording=Typingpool::Test.record)
  args = []
  if fixture = cleared_vcr_fixture_path_for(fixture_name, can_ever_run_live, are_recording)
    args.push('--testfixture', fixture)
    args.push('--testfixturerecord') if are_recording
  end #if fixture = ...
  args
end

#with_fixtures_in_project_dir(fixture_prefix, project_path) ⇒ Object



335
336
337
338
339
340
341
342
343
# File 'lib/typingpool/utility/test/script.rb', line 335

def with_fixtures_in_project_dir(fixture_prefix, project_path)
  fixtures = Dir.entries(fixtures_dir).select{|entry| entry.include?(fixture_prefix) && entry.index(fixture_prefix) == 0 }.select{|entry| File.file?(File.join(fixtures_dir, entry)) }
  fixtures.map!{|fixture| fixture[fixture_prefix.size .. -1] }
  fixtures.each do |fixture|
    project_fixture_path = File.join(project_path, 'data', fixture)
    source_fixture_path = File.join(fixtures_dir, [fixture_prefix, fixture].join )
    yield(source_fixture_path, project_fixture_path)
  end
end

#with_temp_readymade_projectObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/typingpool/utility/test/script.rb', line 16

def with_temp_readymade_project
  Dir.mktmpdir('typingpool_') do |transcripts_dir|
    FileUtils.cp_r(File.join(readymade_project_path, '.'), transcripts_dir)
    config = reconfigure_for_transcripts_dir(
                                             Config.file(config_path(transcripts_dir)),
                                             transcripts_dir)
    write_config(transcripts_dir, config)
    reconfigure_project(Project.new(project_default[:title], config))
    yield(transcripts_dir)
  end
end

#with_temp_transcripts_dirObject



28
29
30
31
32
33
# File 'lib/typingpool/utility/test/script.rb', line 28

def with_temp_transcripts_dir
  Dir.mktmpdir('typingpool_') do |transcripts_dir|
    write_testing_config_for_transcripts_dir(transcripts_dir, self.config)
    yield(transcripts_dir)
  end
end

#write_config(dir, config, filename = ) ⇒ Object



55
56
57
58
59
# File 'lib/typingpool/utility/test/script.rb', line 55

def write_config(dir, config, filename=project_default[:config_filename])
  path = File.join(dir, filename)
  File.write(path, YAML.dump(config.to_hash))
  path
end

#write_testing_config_for_transcripts_dir(transcripts_dir, config = self.config) ⇒ Object



61
62
63
64
65
66
# File 'lib/typingpool/utility/test/script.rb', line 61

def write_testing_config_for_transcripts_dir(transcripts_dir, config=self.config)
  write_config(
               transcripts_dir,
               reconfigure_for_transcripts_dir(reconfigure_for_testing(config), transcripts_dir),
               project_default[:config_filename])
end