tcr 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9eb63ae66e63fd83d042f819294f1490e4194e52
4
- data.tar.gz: b63f2673ed97cb1e59a245c9fa6931abf8abd06a
3
+ metadata.gz: 565fc7b37f7758c3b70c3edba26a8dace04d4ced
4
+ data.tar.gz: 6ddd25facb69f62f6c4756b6d18d0d133dea3dd6
5
5
  SHA512:
6
- metadata.gz: 6a672d5b5d03361c7cbbd491814b313905fee72905ccee3ee7a811210ada82d7ae92bdb3f66951c5417db4fca9553b0bc237f69401b76e4272c22462cfdc22e9
7
- data.tar.gz: 8bb20e98750081a6faa8fb275181dff36c073dcf4253c42a41cc8a9a4f61c0f6f10330e32a507ba81301fc85285c037258b512a92a2c28d1b99bdd13f6479ee0
6
+ metadata.gz: cd8fec962f5fc58784118afd0801c60789b47aef67953f34bf7a9c42c5ffc1614de7633f11a59353ced13ca18347315a1cf7eac081e11cc862117fe27d8a2581
7
+ data.tar.gz: fa8cd68d5104df3fa8aa7490c6f3f52ebae1440efd39390597989c5ead538bf6203d9da1d605696d8d3530b50c77d07017d7912e9e6ebf44712cab4492639ac2
data/lib/tcr/cassette.rb CHANGED
@@ -8,7 +8,7 @@ module TCR
8
8
  if File.exists?(filename)
9
9
  @recording = false
10
10
  @contents = File.open(filename) { |f| f.read }
11
- @sessions = JSON.parse(@contents)
11
+ @sessions = unmarshal(@contents)
12
12
  else
13
13
  @recording = true
14
14
  @sessions = []
@@ -31,14 +31,36 @@ module TCR
31
31
 
32
32
  def save
33
33
  if recording?
34
- File.open(filename, "w") { |f| f.write(JSON.pretty_generate(@sessions)) }
34
+ File.open(filename, "w") { |f| f.write(marshal(@sessions)) }
35
35
  end
36
36
  end
37
37
 
38
38
  protected
39
39
 
40
+ def unmarshal(content)
41
+ case TCR.configuration.format
42
+ when "json"
43
+ JSON.parse(content)
44
+ when "yaml"
45
+ YAML.load(content)
46
+ else
47
+ raise RuntimeError.new "unrecognized format #{TCR.configuration.format}, please use `json` or `yaml`"
48
+ end
49
+ end
50
+
51
+ def marshal(content)
52
+ case TCR.configuration.format
53
+ when "json"
54
+ JSON.pretty_generate(content)
55
+ when "yaml"
56
+ YAML.dump(content)
57
+ else
58
+ raise RuntimeError.new "unrecognized format #{TCR.configuration.format}, please use `json` or `yaml`"
59
+ end
60
+ end
61
+
40
62
  def filename
41
- "#{TCR.configuration.cassette_library_dir}/#{name}.json"
63
+ "#{TCR.configuration.cassette_library_dir}/#{name}.#{TCR.configuration.format}"
42
64
  end
43
65
  end
44
66
  end
@@ -1,6 +1,6 @@
1
1
  module TCR
2
2
  class Configuration
3
- attr_accessor :cassette_library_dir, :hook_tcp_ports, :block_for_reads
3
+ attr_accessor :cassette_library_dir, :hook_tcp_ports, :block_for_reads, :format
4
4
 
5
5
  def initialize
6
6
  reset_defaults!
@@ -10,6 +10,7 @@ module TCR
10
10
  @cassette_library_dir = "fixtures/tcr_cassettes"
11
11
  @hook_tcp_ports = []
12
12
  @block_for_reads = false
13
+ @format = "json"
13
14
  end
14
15
  end
15
16
  end
data/lib/tcr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module TCR
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/spec/tcr_spec.rb CHANGED
@@ -15,8 +15,10 @@ describe TCR do
15
15
 
16
16
  around(:each) do |example|
17
17
  File.unlink("test.json") if File.exists?("test.json")
18
+ File.unlink("test.yaml") if File.exists?("test.yaml")
18
19
  example.run
19
20
  File.unlink("test.json") if File.exists?("test.json")
21
+ File.unlink("test.yaml") if File.exists?("test.yaml")
20
22
  end
21
23
 
22
24
  describe ".configuration" do
@@ -157,6 +159,16 @@ describe TCR do
157
159
  }.to change{ File.exists?("./test.json") }.from(false).to(true)
158
160
  end
159
161
 
162
+ it "creates a cassette file on use with yaml" do
163
+ TCR.configure { |c| c.format = "yaml" }
164
+
165
+ expect {
166
+ TCR.use_cassette("test") do
167
+ tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
168
+ end
169
+ }.to change{ File.exists?("./test.yaml") }.from(false).to(true)
170
+ end
171
+
160
172
  it "records the tcp session data into the file" do
161
173
  TCR.use_cassette("test") do
162
174
  tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
@@ -168,6 +180,20 @@ describe TCR do
168
180
  cassette_contents.include?("220 smtp.mandrillapp.com ESMTP").should == true
169
181
  end
170
182
 
183
+ it "records the tcp session data into the yaml file" do
184
+ TCR.configure { |c| c.format = "yaml" }
185
+
186
+ TCR.use_cassette("test") do
187
+ tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
188
+ io = Net::InternetMessageIO.new(tcp_socket)
189
+ line = io.readline
190
+ tcp_socket.close
191
+ end
192
+ cassette_contents = File.open("test.yaml") { |f| f.read }
193
+ cassette_contents.include?("---").should == true
194
+ cassette_contents.include?("220 smtp.mandrillapp.com ESMTP").should == true
195
+ end
196
+
171
197
  it "plays back tcp sessions without opening a real connection" do
172
198
  expect(TCPSocket).to_not receive(:real_open)
173
199
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tcr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Forman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-02 00:00:00.000000000 Z
11
+ date: 2017-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec