Class: Typingpool::Test::Script

Inherits:
Typingpool::Test show all
Defined in:
lib/typingpool/test/script.rb

Constant Summary collapse

@@readymade_project_path =
nil

Instance Method Summary collapse

Methods inherited from Typingpool::Test

#add_goodbye_message, #amazon_credentials?, app_dir, #audio_dir, #broken_url_eventually?, #cleared_vcr_fixture_path_for, #config, #delete_vcr_fixture, #dummy_config, #fetch_url, #fixtures_dir, #in_temp_dir, #project_default, #s3_credentials?, #sftp_credentials?, #skip_during_vcr_playback, #skip_if_no_amazon_credentials, #skip_if_no_s3_credentials, #skip_if_no_sftp_credentials, #skip_if_no_upload_credentials, #skip_with_message, #vcr_dir, #with_vcr, #working_url?, #working_url_eventually?, #works_eventually?

Instance Method Details

#assert_all_assets_have_upload_status(assignment_csv, type, status) ⇒ Object



354
355
356
357
358
# File 'lib/typingpool/test/script.rb', line 354

def assert_all_assets_have_upload_status(assignment_csv, type, status)
  recorded_uploads = assignment_csv.map{|assignment| assignment["#{type}_uploaded"] }
  refute_empty(recorded_uploads)
  assert_equal(recorded_uploads.count, recorded_uploads.select{|uploaded| uploaded == status }.count)
end

#assert_assignment_csv_has_transcription_count(count, project, which_csv = 'assignment.csv') ⇒ Object



346
347
348
# File 'lib/typingpool/test/script.rb', line 346

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

#assert_has_partial_transcript(dir) ⇒ Object



342
343
344
# File 'lib/typingpool/test/script.rb', line 342

def assert_has_partial_transcript(dir)
  assert_has_transcript(dir, 'transcript_in_progress.html')
end

#assert_has_transcript(dir, transcript_file = 'transcript.html') ⇒ Object



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

def assert_has_transcript(dir, transcript_file='transcript.html')
  transcript_path = File.join(transcripts_dir_project(dir).local, transcript_file)
  assert(File.exists?(transcript_path))
  assert(not((transcript = IO.read(transcript_path)).empty?))
  transcript
end

#assert_html_has_audio_count(count, html) ⇒ Object



350
351
352
# File 'lib/typingpool/test/script.rb', line 350

def assert_html_has_audio_count(count, html)
  assert_equal(count, Nokogiri::HTML(html).css('audio').size)
end

#assert_script_abort_match(args, regex) ⇒ Object



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

def assert_script_abort_match(args, regex)
  with_temp_transcripts_dir do |dir|
    exception = assert_raises(Typingpool::Error::Shell) do
      yield([*args, '--config', config_path(dir)])
    end
    assert_match(regex, exception.message)
  end #with_temp_transcripts_dir do...
end

#assign_defaultObject



184
185
186
187
188
189
190
191
192
193
# File 'lib/typingpool/test/script.rb', line 184

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



65
66
67
68
# File 'lib/typingpool/test/script.rb', line 65

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
124
# File 'lib/typingpool/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
  #Utility.system_quietly(path_to_script(script_name), *args)
end

#call_tp_assign(*args) ⇒ Object



180
181
182
# File 'lib/typingpool/test/script.rb', line 180

def call_tp_assign(*args)
  call_script('tp-assign', '--sandbox', *args)
end

#call_tp_collect(fixture_path, *args) ⇒ Object



238
239
240
# File 'lib/typingpool/test/script.rb', line 238

def call_tp_collect(fixture_path, *args)
  call_script('tp-collect', '--sandbox', '--fixture', fixture_path, *args)
end

#call_tp_finish(*args) ⇒ Object



162
163
164
# File 'lib/typingpool/test/script.rb', line 162

def call_tp_finish(*args)
  call_script('tp-finish', *args)
end

#call_tp_make(*args) ⇒ Object



141
142
143
# File 'lib/typingpool/test/script.rb', line 141

def call_tp_make(*args)
  call_script('tp-make', *args)
end

#config_path(dir) ⇒ Object



70
71
72
# File 'lib/typingpool/test/script.rb', line 70

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

#copy_fixtures_to_transcripts_dir(dir, fixture_prefix) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/typingpool/test/script.rb', line 312

def copy_fixtures_to_transcripts_dir(dir, fixture_prefix)
  copies = 0
  with_fixtures_in_transcripts_dir(dir, fixture_prefix) do |fixture_path, project_path|
    if File.exists? project_path
      FileUtils.mv(project_path, File.join(File.dirname(project_path), "orig_#{File.basename(project_path)}"))
    end
    FileUtils.cp(fixture_path, project_path)
    copies += 1
  end
  copies > 0 or raise Error, "No fixtures to copy with prefix #{fixture_prefix} from dir #{dir}"
  copies
end

#copy_readymade_project_into(config_path) ⇒ Object



34
35
36
37
# File 'lib/typingpool/test/script.rb', line 34

def copy_readymade_project_into(config_path)
  config = Typingpool::Config.file(config_path)
  FileUtils.cp_r(File.join(@@readymade_project_path, '.'), File.dirname(config_path))
end

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



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/typingpool/test/script.rb', line 215

def copy_tp_assign_fixtures(dir, fixture_prefix, config_path=config_path(dir))
  project = transcripts_dir_project(dir, Typingpool::Config.file(config_path))
  if Typingpool::Test.record
    project_time(project, Time.now)
    with_fixtures_in_transcripts_dir(dir, "#{fixture_prefix}_") do |fixture_path, project_path|
      FileUtils.cp(project_path, fixture_path)
    end
  elsif not(Typingpool::Test.live)
    copy_fixtures_to_transcripts_dir(dir, "#{fixture_prefix}_")
    reconfigure_project_csv_in(config_path)
  end
end

#fixture_project_dir(name) ⇒ Object



285
286
287
# File 'lib/typingpool/test/script.rb', line 285

def fixture_project_dir(name)
  File.join(fixtures_dir, name)
end

#init_readymade_projectObject



25
26
27
28
29
30
31
32
# File 'lib/typingpool/test/script.rb', line 25

def init_readymade_project
  unless @@readymade_project_path
    dir = @@readymade_project_path = Dir.mktmpdir('typingpool_')
    Minitest.after_run{ FileUtils.remove_entry_secure(dir) }
    make_transcripts_dir_config(dir, Config.file(setup_s3_config(dir, self.config)))
    tp_make(dir, config_path(dir), 'mp3', true)
  end 
end

#make_fixture_project_dir(name) ⇒ Object



289
290
291
292
293
294
295
296
# File 'lib/typingpool/test/script.rb', line 289

def make_fixture_project_dir(name)
  dir = fixture_project_dir(name)
  if File.exists? dir
    raise Error::Test, "Fixture project already exists for #{name} at #{dir}"
  end
  Dir.mkdir(dir)
  dir
end

#make_transcripts_dir_config(dir, config = self.config) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/typingpool/test/script.rb', line 94

def make_transcripts_dir_config(dir, config=self.config)
  config.transcripts = dir
  config.cache = File.join(dir, '.cache')
  config['assign']['reward'] = '0.02'
  config.assign.to_hash.delete('qualify')
  write_config(config, dir, project_default[:config_filename])   
end

#path_to_script(script_name) ⇒ Object



126
127
128
# File 'lib/typingpool/test/script.rb', line 126

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

#project_time(project, time = nil) ⇒ Object



228
229
230
231
232
233
234
235
236
# File 'lib/typingpool/test/script.rb', line 228

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

#reconfigure_project_csv_in(config_path) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/typingpool/test/script.rb', line 49

def reconfigure_project_csv_in(config_path)
  project = Project.new(project_default[:title], Config.file(config_path))
  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
end

#reconfigure_readymade_project_in(config_path) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/typingpool/test/script.rb', line 39

def reconfigure_readymade_project_in(config_path)
  #rewrite URLs in assignment.csv according to config at config_path
  make_transcripts_dir_config(File.dirname(config_path), Config.file(config_path))
  project = Project.new(project_default[:title], Config.file(config_path))
  File.delete(project.local.file('data', 'id.txt'))
  project.local.create_id
  id = project.local.id
  reconfigure_project_csv_in(config_path)
end

#remove_fixture_project_dir(name) ⇒ Object



298
299
300
# File 'lib/typingpool/test/script.rb', line 298

def remove_fixture_project_dir(name)
  FileUtils.remove_entry_secure(fixture_project_dir(name), :secure => true)
end

#rm_fixtures_from_transcripts_dir(dir, fixture_prefix) ⇒ Object



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

def rm_fixtures_from_transcripts_dir(dir, fixture_prefix)
  with_fixtures_in_transcripts_dir(dir, fixture_prefix) do |fixture_path, project_path|
    FileUtils.rm(project_path)
    path_to_orig = File.join(File.dirname(project_path), "orig_#{File.basename(project_path)}")
    if File.exists?(path_to_orig)
      FileUtils.mv(path_to_orig, project_path)
    end
  end
end

#setup_readymade_project_into(config_path) ⇒ Object



19
20
21
22
23
# File 'lib/typingpool/test/script.rb', line 19

def setup_readymade_project_into(config_path)
  init_readymade_project
  copy_readymade_project_into(config_path)
  reconfigure_readymade_project_in(config_path)
end

#setup_s3_config(dir, config = Config.file(config_path(dir)), filename = '.config_s3') ⇒ Object



81
82
83
84
85
# File 'lib/typingpool/test/script.rb', line 81

def setup_s3_config(dir, config=Config.file(config_path(dir)), filename='.config_s3')
  return unless s3_credentials?(config)
  config.to_hash.delete('sftp')
  write_config(config, dir, filename)
end

#setup_s3_config_with_bad_password(dir, config = Config.file(config_path(dir))) ⇒ Object



87
88
89
90
91
92
# File 'lib/typingpool/test/script.rb', line 87

def setup_s3_config_with_bad_password(dir, config=Config.file(config_path(dir)))
  bad_password = 'f'
  refute_equal(config.to_hash['amazon']['secret'], bad_password)
  config.to_hash['amazon']['secret'] = bad_password
  setup_s3_config(dir, config, '.config_s3_bad')
end

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



59
60
61
62
63
# File 'lib/typingpool/test/script.rb', line 59

def simulate_failed_audio_upload_in(dir, config_path=config_path(dir))
  project = 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

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



195
196
197
198
199
200
201
202
203
204
# File 'lib/typingpool/test/script.rb', line 195

def tp_assign(dir, config_path=config_path(dir), *args)
  call_tp_assign(
                 project_default[: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)) ⇒ Object



206
207
208
209
210
211
212
213
# File 'lib/typingpool/test/script.rb', line 206

def tp_assign_with_vcr(dir, fixture_name, config_path=config_path(dir))
  project = transcripts_dir_project(dir, Typingpool::Config.file(config_path))
  args = [dir, config_path, *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_path) ⇒ Object



242
243
244
245
246
247
# File 'lib/typingpool/test/script.rb', line 242

def tp_collect_with_fixture(dir, fixture_path)
  call_tp_collect(
                  fixture_path,
                  '--config', config_path(dir)
                  )
end

#tp_config(*args) ⇒ Object



267
268
269
# File 'lib/typingpool/test/script.rb', line 267

def tp_config(*args)
  call_script('tp-config', *args)
end

#tp_config_with_input(args, input) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/typingpool/test/script.rb', line 271

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), *args) ⇒ Object



166
167
168
169
# File 'lib/typingpool/test/script.rb', line 166

def tp_finish(dir, config_path=config_path(dir), *args)
  tp_finish_inside_sandbox(dir, config_path, *args)
  tp_finish_outside_sandbox(dir, config_path, *args)
end

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



172
173
174
# File 'lib/typingpool/test/script.rb', line 172

def tp_finish_inside_sandbox(dir, config_path=config_path(dir), *args)
  tp_finish_outside_sandbox(dir, config_path, '--sandbox', *args)
end

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



176
177
178
# File 'lib/typingpool/test/script.rb', line 176

def tp_finish_outside_sandbox(dir, config_path=config_path(dir), *args)
  call_tp_finish(project_default[:title], '--config', config_path, *args)
end

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



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/typingpool/test/script.rb', line 145

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_tp_make(*commands)
end

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



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

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(dir, fixture_path, choices) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/typingpool/test/script.rb', line 250

def tp_review_with_fixture(dir, fixture_path, choices)
  output = {}
  Open3.popen3(File.join(self.class.app_dir, 'bin', 'tp-review'), '--sandbox', '--fixture', fixture_path, '--config', config_path(dir), project_default[:title]) 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

#transcripts_dir_project(dir, config = Config.file(config_path(dir))) ⇒ Object



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

def transcripts_dir_project(dir, config=Config.file(config_path(dir)))
  Project.new(project_default[:title], config)
end

#vcr_args(fixture_name) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/typingpool/test/script.rb', line 130

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

#with_fixtures_in_transcripts_dir(dir, fixture_prefix) ⇒ Object



302
303
304
305
306
307
308
309
310
# File 'lib/typingpool/test/script.rb', line 302

def with_fixtures_in_transcripts_dir(dir, fixture_prefix)
  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_path = File.join(transcripts_dir_project(dir).local, 'data', fixture)
    fixture_path = File.join(fixtures_dir, [fixture_prefix, fixture].join )
    yield(fixture_path, project_path)
  end
end

#with_temp_readymade_projectObject



12
13
14
15
16
17
# File 'lib/typingpool/test/script.rb', line 12

def with_temp_readymade_project
  with_temp_transcripts_dir do |dir|
    setup_readymade_project_into(config_path(dir))
    yield(dir)
  end
end

#with_temp_transcripts_dirObject



74
75
76
77
78
79
# File 'lib/typingpool/test/script.rb', line 74

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

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



102
103
104
105
106
# File 'lib/typingpool/test/script.rb', line 102

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