Module: Typingpool::Utility::Test

Included in:
Test, Script
Defined in:
lib/typingpool/utility/test.rb,
lib/typingpool/utility/test/script.rb

Defined Under Namespace

Modules: Script

Instance Method Summary collapse

Instance Method Details

#add_goodbye_message(msg) ⇒ Object



125
126
127
128
129
# File 'lib/typingpool/utility/test.rb', line 125

def add_goodbye_message(msg)
  at_exit do
    STDERR.puts msg
  end
end

#amazon_credentials?(config = self.config) ⇒ Boolean

Returns:

  • (Boolean)


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

def amazon_credentials?(config=self.config)
  config.amazon && config.amazon.key && config.amazon.secret
end

#audio_dirObject



11
12
13
# File 'lib/typingpool/utility/test.rb', line 11

def audio_dir
  File.join(fixtures_dir, 'audio')
end

#broken_url_eventually?(url, max_seconds = 10, min_tries = 2, max_redirects = 6) ⇒ Boolean

Returns:

  • (Boolean)


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

def broken_url_eventually?(url, max_seconds=10, min_tries=2, max_redirects=6)
  works_eventually?(max_seconds, min_tries) do
    not(Utility.working_url?(url, max_redirects))
  end
end

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



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

def cleared_vcr_fixture_path_for(fixture_name, can_ever_run_live=true, are_recording=Typingpool::Test.record)
  if are_recording
    delete_vcr_fixture(fixture_name)
  end
  if (are_recording || not(Typingpool::Test.live) || not(can_ever_run_live))
    File.join(vcr_dir, fixture_name)
  end
end

#configObject



105
106
107
108
109
110
111
# File 'lib/typingpool/utility/test.rb', line 105

def config
  if File.exist?(File.expand_path(Config.default_file))
    Config.file
  else
    Config.from_bundled_template
  end
end

#delete_vcr_fixture(fixture_name) ⇒ Object



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

def delete_vcr_fixture(fixture_name)
  fixture_path = File.join(vcr_dir, [fixture_name, '.yml'].join)
  File.delete(fixture_path) if File.exist? fixture_path
end

#dummy_config(number = 1) ⇒ Object



131
132
133
# File 'lib/typingpool/utility/test.rb', line 131

def dummy_config(number=1)
  Typingpool::Config.file(File.join(fixtures_dir, "config-#{number}"))
end

#fixtures_dirObject



7
8
9
# File 'lib/typingpool/utility/test.rb', line 7

def fixtures_dir
  File.join(Utility.lib_dir, 'test', 'fixtures')
end

#project_defaultObject



136
137
138
139
140
141
142
143
144
145
# File 'lib/typingpool/utility/test.rb', line 136

def project_default
  Hash[
       :config_filename => '.config',
       :subtitle => "Typingpool's test interview transcription",
       :title => "Typingpool's Test & Interview",
       :chunks => '0:22',
       :unusual => ['Hack Day', 'Sunnyvale', 'Chad D'],
       :voice => ['Ryan', 'Havi, hacker'],
      ]
end

#s3_credentials?(config) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/typingpool/utility/test.rb', line 117

def s3_credentials?(config)
  amazon_credentials?(config) && config.amazon.bucket
end

#sftp_credentials?(config) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/typingpool/utility/test.rb', line 121

def sftp_credentials?(config)
  config.sftp && config.sftp.user && config.sftp.host && config.sftp.url
end

#vcr_core_host_matcherObject

great for s3 because we don’t have to worry about changing bucket names, only matches as far as s3.amazonaws.com



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

def vcr_core_host_matcher
  lambda do |request1, request2|
    core_host = lambda{|host| host.split(/\./).reverse.slice(0, 3).reverse.join('.')}
    core_host.call(URI(request1.uri).host) == core_host.call(URI(request2.uri).host)
  end #lambda do...
end

#vcr_dirObject



15
16
17
# File 'lib/typingpool/utility/test.rb', line 15

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

#vcr_load(fixture_path, config, read_only = true, vcr_params = nil) ⇒ Object

recording. Used in automated tests. Uses the great VCR gem.

Automatically filters your Config#amazon#key and Config#amazon#secret from the recorded fixture, and automatically determines the “cassette” name and “cassette library” dir from the supplied path.

Params

fixture_path

Path to where you want the HTTP fixture recorded, including filename.

config

A Config instance, used to extract the Config#amazon#secret and Config#amazon#key that will be filtered from the fixture.

read_only

Default is true. Set to false to enable recording.

vcr_params

Default is nil. A hash of params to pass to VCR.insert_cassette (same set of params that can be passed to VCR.use_cassette), like :preserve_exact_body_bytes or :match_requests_on => [:url, :matcher]. If nil, no extra params will be passed.

Returns

Result of calling VCR.insert_cassette.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/typingpool/utility/test.rb', line 67

def vcr_load(fixture_path, config, read_only=true, vcr_params=nil)
  VCR.configure do |c|
    c.cassette_library_dir = File.dirname(fixture_path)
    c.hook_into :webmock 
    c.filter_sensitive_data('<AWS_KEY>'){ config.amazon.key }
    c.filter_sensitive_data('<AWS_SECRET>'){ config.amazon.secret }
    c.before_record do |interaction|
      if interaction.request.body.size > 10000
        interaction.request.body = '<BIG_UPLOAD>'
      end
    end #c.before_record do...
  end
  WebMock.allow_net_connect! 
  opts = {:record => (read_only ? :none : :once)}
  opts.merge!(vcr_params) if vcr_params
  VCR.turn_on!
  VCR.insert_cassette(File.basename(fixture_path, '.*'), 
                      opts
                     )

end

#vcr_stopObject

Stops playing/recording from the last call to vcr_load. Returns the result of VCR.eject_cassette.



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

def vcr_stop
  VCR.eject_cassette
  VCR.turn_off!
end

#with_vcr(fixture_name, config, opts = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/typingpool/utility/test.rb', line 33

def with_vcr(fixture_name, config, opts={})
  if fixture = cleared_vcr_fixture_path_for(fixture_name)
    read_only = not(Typingpool::Test.record)
    vcr_load(fixture, config, read_only, opts)
  end
  begin
    yield
  ensure
    vcr_stop
  end
end

#working_url_eventually?(url, max_seconds = 10, min_tries = 2, max_redirects = 6) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
164
# File 'lib/typingpool/utility/test.rb', line 160

def working_url_eventually?(url, max_seconds=10, min_tries=2, max_redirects=6)
  works_eventually?(max_seconds, min_tries) do
    Utility.working_url?(url, max_redirects)
  end
end

#works_eventually?(max_seconds = 10, min_tries = 2) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/typingpool/utility/test.rb', line 147

def works_eventually?(max_seconds=10, min_tries=2)
  start = Time.now.to_i
  tries = 0
  wait = 0
  until ((tries >= min_tries) && ((Time.now.to_i + wait - start) >= max_seconds)) do
    sleep wait
    return true if yield
    wait = wait > 0 ? wait * 2 : 1
    tries += 1
  end
  false
end