Class: Typingpool::Test::Script

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

Instance Method Summary collapse

Methods inherited from Typingpool::Test

#add_goodbye_message, #amazon_credentials?, app_dir, #audio_dir, #config, #dummy_config, #fetch_url, #fixtures_dir, #in_temp_dir, #project_default, #s3_credentials?, #sftp_credentials?, #skip_if_no_amazon_credentials, #skip_if_no_s3_credentials, #skip_if_no_sftp_credentials, #skip_if_no_upload_credentials, #skip_with_message, #working_url?

Instance Method Details

#assert_all_assets_have_upload_status(assignment_csv, types, status) ⇒ Object



278
279
280
281
282
283
284
# File 'lib/typingpool/test/script.rb', line 278

def assert_all_assets_have_upload_status(assignment_csv, types, status)
  types.each do |type|
    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
end

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



270
271
272
# File 'lib/typingpool/test/script.rb', line 270

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



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

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

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



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

def assert_has_transcript(dir, transcript_file='transcript.html')
  transcript_path = File.join(temp_tp_dir_project_dir(dir), 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



274
275
276
# File 'lib/typingpool/test/script.rb', line 274

def assert_html_has_audio_count(count, html)
  assert_equal(count, noko(html).css('audio').size)
end

#assert_script_abort_match(args, regex) ⇒ Object



293
294
295
296
297
298
299
# File 'lib/typingpool/test/script.rb', line 293

def assert_script_abort_match(args, regex)
  in_temp_tp_dir do |dir|
    assert_shell_error_match(regex) do 
      yield([*args, '--config', config_path(dir)])
    end
  end #in_temp_tp_dir do...
end

#assert_shell_error_match(regex) ⇒ Object



286
287
288
289
290
291
# File 'lib/typingpool/test/script.rb', line 286

def assert_shell_error_match(regex)
  exception = assert_raises(Typingpool::Error::Shell) do
    yield
  end
  assert_match(regex, exception.message)
end

#assign_defaultObject



133
134
135
136
137
138
139
140
141
142
# File 'lib/typingpool/test/script.rb', line 133

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



9
10
11
12
# File 'lib/typingpool/test/script.rb', line 9

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



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

def call_script(*args)
  Utility.system_quietly(*args)
end

#call_tp_assign(*args) ⇒ Object



129
130
131
# File 'lib/typingpool/test/script.rb', line 129

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

#call_tp_collect(fixture_path, *args) ⇒ Object



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

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

#call_tp_finish(*args) ⇒ Object



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

def call_tp_finish(*args)
  call_script(path_to_tp_finish, *args)
end

#call_tp_make(*args) ⇒ Object



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

def call_tp_make(*args)
  call_script(path_to_tp_make, *args, '--devtest')
end

#config_from_dir(dir) ⇒ Object



18
19
20
# File 'lib/typingpool/test/script.rb', line 18

def config_from_dir(dir)
  Config.file(config_path(dir))
end

#config_path(dir) ⇒ Object



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

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

#copy_fixtures_to_temp_tp_dir(dir, fixture_prefix) ⇒ Object



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

def copy_fixtures_to_temp_tp_dir(dir, fixture_prefix)
  with_fixtures_in_temp_tp_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)
  end
end

#fixture_project_dir(name) ⇒ Object



213
214
215
# File 'lib/typingpool/test/script.rb', line 213

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

#in_temp_tp_dirObject



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

def in_temp_tp_dir
  ::Dir.mktmpdir('typingpool_') do |dir|
    setup_temp_tp_dir(dir)
    yield(dir)
  end
end

#make_fixture_project_dir(name) ⇒ Object



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

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_temp_tp_dir_config(dir, config = self.config) ⇒ Object



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

def make_temp_tp_dir_config(dir, config=self.config)
  config.transcripts = File.join(dir, 'projects')
  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

#noko(html) ⇒ Object



301
302
303
# File 'lib/typingpool/test/script.rb', line 301

def noko(html)
  Nokogiri::HTML(html) 
end

#path_to_tp_assignObject



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

def path_to_tp_assign
  File.join(self.class.app_dir, 'bin', 'tp-assign')
end

#path_to_tp_collectObject



154
155
156
# File 'lib/typingpool/test/script.rb', line 154

def path_to_tp_collect
  File.join(self.class.app_dir, 'bin', 'tp-collect')
end

#path_to_tp_configObject



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

def path_to_tp_config
  File.join(self.class.app_dir, 'bin', 'tp-config')
end

#path_to_tp_finishObject



99
100
101
# File 'lib/typingpool/test/script.rb', line 99

def path_to_tp_finish
  ::File.join(self.class.app_dir, 'bin', 'tp-finish')
end

#path_to_tp_makeObject



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

def path_to_tp_make
  ::File.join(self.class.app_dir, 'bin', 'tp-make')
end

#path_to_tp_reviewObject



170
171
172
# File 'lib/typingpool/test/script.rb', line 170

def path_to_tp_review
  File.join(self.class.app_dir, 'bin', 'tp-review')
end

#remove_fixture_project_dir(name) ⇒ Object



226
227
228
# File 'lib/typingpool/test/script.rb', line 226

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

#rm_fixtures_from_temp_tp_dir(dir, fixture_prefix) ⇒ Object



249
250
251
252
253
254
255
256
257
# File 'lib/typingpool/test/script.rb', line 249

def rm_fixtures_from_temp_tp_dir(dir, fixture_prefix)
  with_fixtures_in_temp_tp_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_amazon(dir) ⇒ Object



23
24
25
# File 'lib/typingpool/test/script.rb', line 23

def setup_amazon(dir)
  Amazon.setup(:sandbox => true, :config => config_from_dir(dir))
end

#setup_s3_config(dir, config = config_from_dir(dir), filename = '.config_s3') ⇒ Object



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

def setup_s3_config(dir, config=config_from_dir(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_from_dir(dir)) ⇒ Object



46
47
48
49
50
51
# File 'lib/typingpool/test/script.rb', line 46

def setup_s3_config_with_bad_password(dir, config=config_from_dir(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

#setup_temp_tp_dir(dir) ⇒ Object



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

def setup_temp_tp_dir(dir)
  make_temp_tp_dir_config(dir)
  Dir.mkdir(File.join(dir, 'projects'))
end

#temp_tp_dir_project(dir, config = config_from_dir(dir)) ⇒ Object



73
74
75
# File 'lib/typingpool/test/script.rb', line 73

def temp_tp_dir_project(dir, config=config_from_dir(dir))
  Project.new(project_default[:title], config)
end

#temp_tp_dir_project_dir(temp_tp_dir) ⇒ Object



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

def temp_tp_dir_project_dir(temp_tp_dir)
  ::File.join(temp_tp_dir, 'projects', project_default[:title])
end

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



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

def tp_assign(dir, config_path=config_path(dir))
  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
                 )
end

#tp_collect_with_fixture(dir, fixture_path) ⇒ Object



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

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

#tp_config(*args) ⇒ Object



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

def tp_config(*args)
  call_script(path_to_tp_config, *args)
end

#tp_config_with_input(args, input) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/typingpool/test/script.rb', line 199

def tp_config_with_input(args, input)
  output = {}
  Open3.popen3(path_to_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 = self.config_path(dir)) ⇒ Object



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

def tp_finish(dir, config_path=self.config_path(dir))
  tp_finish_inside_sandbox(dir, config_path)
  tp_finish_outside_sandbox(dir, config_path)
end

#tp_finish_inside_sandbox(dir, config_path = self.config_path(dir)) ⇒ Object



113
114
115
# File 'lib/typingpool/test/script.rb', line 113

def tp_finish_inside_sandbox(dir, config_path=self.config_path(dir))
  tp_finish_outside_sandbox(dir, config_path, '--sandbox')
end

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



117
118
119
120
121
122
123
# File 'lib/typingpool/test/script.rb', line 117

def tp_finish_outside_sandbox(dir, config_path=self.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') ⇒ Object



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

def tp_make(in_dir, config=config_path(in_dir), audio_subdir='mp3')
  call_tp_make(
               '--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
               )
end

#tp_review_with_fixture(dir, fixture_path, choices) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/typingpool/test/script.rb', line 174

def tp_review_with_fixture(dir, fixture_path, choices)
  output = {}
  Open3.popen3(path_to_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

#vcr_dirObject



305
306
307
# File 'lib/typingpool/test/script.rb', line 305

def vcr_dir
  File.join(fixtures_dir, 'vcr')
end

#with_fixtures_in_temp_tp_dir(dir, fixture_prefix) ⇒ Object



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

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

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



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

def write_config(config, dir, filename=project_default[:config_filename])
  path = ::File.join(dir, filename)
  ::File.open(path, 'w') do |out|
    out << YAML.dump(config.to_hash)
  end
  path
end