action_mailer_cache_delivery 0.3.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b32f90df212b794281209543071116b5033563133727fed9136111757edee925
4
+ data.tar.gz: 4dd8d0a28b2d22fee4904d98826472fdaff49338e1e7522d9b8fb95431d7796a
5
+ SHA512:
6
+ metadata.gz: c27d4453849af80f682435fddae8c738f844627da25bc46f38a55a397e42f163c40728fa138b32ca5f5e5408f7e467266483d070fdd4c1b8e38936acc6020c5e
7
+ data.tar.gz: 16c2c5ef19d1157290c3b314eded5f5ac4e99b34a4d69fcc2bfda1822e1ae611de8c293dfdd964f5e20c1a0d66007d2de321bcc828073dc2d479c4b9c0584609
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Enhances ActionMailer to support the `:cache` delivery method, which behaves like `:test`, except that the deliveries are marshalled to a temporary cache file, thus making them available to other processes.
4
4
 
5
- You'll want to use this gem if you're testing with Selenium (or any other tool which distinct processes).
5
+ You'll want to use this gem if you're testing with Selenium (or any other tool which distinct processes). Another approach would be to [share deliveries between threads](https://gist.github.com/p0deje/ffc833630ee70f2af5ff) (in this case, this gem is not needed at all).
6
6
 
7
7
  ## Installation
8
8
 
@@ -8,12 +8,14 @@ module ActionMailer
8
8
  # @return [Array] array of mails (each mail is an instance of Mail.)
9
9
  #
10
10
  def cached_deliveries
11
- if File.exists?(cache_settings[:location])
12
- File.open(cache_settings[:location], 'r') do |file|
13
- Marshal.load(file)
11
+ with_cache_lock("#{cache_settings[:location]}.lock") do
12
+ if File.exist?(cache_settings[:location])
13
+ File.open(cache_settings[:location], 'r') do |file|
14
+ Marshal.load(file)
15
+ end
16
+ else
17
+ []
14
18
  end
15
- else
16
- []
17
19
  end
18
20
  end
19
21
 
@@ -23,15 +25,30 @@ module ActionMailer
23
25
  # It also cleans ActionMailer::Base.deliveries
24
26
  #
25
27
  def clear_cache
26
- deliveries.clear
28
+ with_cache_lock("#{cache_settings[:location]}.lock") do
29
+ deliveries.clear
27
30
 
28
- if File.exists?(cache_settings[:location])
29
- File.open(cache_settings[:location], 'w') do |file|
30
- Marshal.dump(deliveries, file)
31
+ if File.exist?(cache_settings[:location])
32
+ File.open(cache_settings[:location], 'w') do |file|
33
+ Marshal.dump(deliveries, file)
34
+ end
31
35
  end
32
36
  end
33
37
  end
34
38
 
39
+ #
40
+ # Locks file to prevent concurrent mail jobs from race condition.
41
+ # @api private
42
+ #
43
+ def with_cache_lock(file)
44
+ lockfile = File.open(file, 'w')
45
+ lockfile.flock(File::LOCK_EX)
46
+ yield
47
+ ensure
48
+ lockfile.flock(File::LOCK_UN)
49
+ lockfile.close
50
+ end
51
+
35
52
  end # << self
36
53
  end # Base
37
54
  end # ActionMailer
@@ -27,23 +27,27 @@ module Mail
27
27
 
28
28
  # @api private
29
29
  def deliver!(mail)
30
- # write empty array to cache file if doesn't exist
31
- unless File.exists?(@settings[:location])
30
+ ActionMailer::Base.with_cache_lock("#{@settings[:location]}.lock") do
31
+ # write empty array to cache file if doesn't exist
32
+ unless File.exist?(@settings[:location])
33
+ File.open(@settings[:location], 'w') do |file|
34
+ Marshal.dump([], file)
35
+ end
36
+ end
37
+
38
+ # get delivered mails
39
+ mails = File.open(@settings[:location], 'r') do |file|
40
+ Marshal.load(file)
41
+ end
42
+ # append new one
43
+ mails << mail
44
+ # write all emails to cache file
32
45
  File.open(@settings[:location], 'w') do |file|
33
- Marshal.dump([], file)
46
+ Marshal.dump(mails, file)
34
47
  end
35
- end
36
48
 
37
- # get delivered mails
38
- mails = ActionMailer::Base.cached_deliveries
39
- # append new one
40
- mails << mail
41
- # write all emails to cache file
42
- File.open(@settings[:location], 'w') do |file|
43
- Marshal.dump(mails, file)
49
+ Mail::TestMailer.deliveries << mail
44
50
  end
45
-
46
- Mail::TestMailer.deliveries << mail
47
51
  end
48
52
 
49
53
  end # CacheDelivery
@@ -1,3 +1,3 @@
1
1
  module ActionMailerCacheDelivery
2
- VERSION = '0.3.7'
2
+ VERSION = '0.4.0'
3
3
  end # ActionMailerCacheDelivery
@@ -10,26 +10,28 @@ describe ActionMailerCacheDelivery do
10
10
  before(:each) do
11
11
  ActionMailerCacheDelivery.install
12
12
  ActionMailer::Base.delivery_method = :cache
13
+ ActionMailer::Base.cache_settings[:location] = "#{Dir.tmpdir}/test.cache"
13
14
  end
14
15
 
15
16
  describe 'clear_cache' do
16
17
  it 'allows clearing cache before cache file is created' do
17
- ActionMailer::Base.cache_settings[:location] = 'test_dir/test_dir/test.cache'
18
- lambda { ActionMailer::Base.clear_cache }.should_not raise_error
18
+ ActionMailer::Base.cache_settings[:location] = ''
19
+ expect { ActionMailer::Base.clear_cache }.not_to raise_error
19
20
  end
20
21
 
21
22
  it 'should clear cache file' do
22
23
  mail.deliver
23
24
  ActionMailer::Base.clear_cache
24
- File.open(ActionMailer::Base.cache_settings[:location], 'r') do |file|
25
+ mails = File.open(ActionMailer::Base.cache_settings[:location], 'r') do |file|
25
26
  Marshal.load(file)
26
- end.should == []
27
+ end
28
+ expect(mails).to eq([])
27
29
  end
28
30
 
29
31
  it 'should clear ActionMailer::Base.deliveries' do
30
32
  mail.deliver
31
33
  ActionMailer::Base.clear_cache
32
- ActionMailer::Base.deliveries.should == []
34
+ expect(ActionMailer::Base.deliveries).to eq([])
33
35
  end
34
36
  end
35
37
 
@@ -39,20 +41,19 @@ describe ActionMailerCacheDelivery do
39
41
  end
40
42
 
41
43
  it 'returns empty array if cache has not been created yet' do
42
- ActionMailer::Base.cache_settings[:location] = 'test_dir/test_dir/test.cache'
43
- ActionMailer::Base.cached_deliveries.should == []
44
+ expect(ActionMailer::Base.cached_deliveries).to eq([])
44
45
  end
45
46
 
46
47
  it 'should return array' do
47
48
  mail.deliver
48
- ActionMailer::Base.cached_deliveries.should be_an(Array)
49
+ expect(ActionMailer::Base.cached_deliveries).to be_an(Array)
49
50
  end
50
51
 
51
52
  it 'should return all the sent emails' do
52
53
  5.times do
53
54
  mail.deliver
54
55
  end
55
- ActionMailer::Base.cached_deliveries.length.should == 5
56
+ expect(ActionMailer::Base.cached_deliveries.length).to eq(5)
56
57
  end
57
58
  end
58
59
  end
@@ -61,25 +62,27 @@ describe ActionMailerCacheDelivery do
61
62
  describe 'extend ActionMailer' do
62
63
  it 'should add cached_deliveries method to ActionMailer' do
63
64
  ActionMailerCacheDelivery.install
64
- ActionMailer::Base.should respond_to(:cached_deliveries)
65
+ expect(ActionMailer::Base).to respond_to(:cached_deliveries)
65
66
  end
66
67
 
67
68
  it 'should add clear_cache method to ActionMailer' do
68
69
  ActionMailerCacheDelivery.install
69
- ActionMailer::Base.should respond_to(:clear_cache)
70
+ expect(ActionMailer::Base).to respond_to(:clear_cache)
70
71
  end
71
72
  end
72
73
 
73
74
  describe 'install' do
74
75
  it 'should add :cache delivery method to ActionMailer' do
75
76
  ActionMailerCacheDelivery.install
76
- ActionMailer::Base.delivery_methods.should include(:cache)
77
+ expect(ActionMailer::Base.delivery_methods).to include(:cache)
77
78
  end
78
79
 
79
80
  it 'should set cache path to /tmp when Rails is not defined' do
81
+ hide_const('Rails')
80
82
  ActionMailerCacheDelivery.install
81
83
  ActionMailer::Base.delivery_method = :cache
82
- ActionMailer::Base.cache_settings[:location].should == "#{Dir.tmpdir}/cache/action_mailer_cache_deliveries.cache"
84
+ expect(ActionMailer::Base.cache_settings[:location])
85
+ .to eq("#{Dir.tmpdir}/cache/action_mailer_cache_deliveries.cache")
83
86
  end
84
87
  end
85
88
  end
@@ -15,30 +15,32 @@ describe Mail::CacheDelivery do
15
15
  it 'should allow setting custom location' do
16
16
  ActionMailer::Base.cache_settings = { location: '/tmp/mail.cache' }
17
17
  mail.deliver
18
- File.exists?('/tmp/mail.cache').should be_true
18
+ expect(File.exist?('/tmp/mail.cache')).to eq(true)
19
19
  end
20
20
  end
21
21
 
22
22
  describe 'deliver!' do
23
23
  it 'should write mail to cache file' do
24
24
  mail.deliver
25
- File.open(ActionMailer::Base.cache_settings[:location], 'r') do |file|
25
+ mails = File.open(ActionMailer::Base.cache_settings[:location], 'r') do |file|
26
26
  Marshal.load(file)
27
- end.should == [mail]
27
+ end
28
+ expect(mails).to eq([mail])
28
29
  end
29
30
 
30
31
  it 'should append mail to cache file' do
31
32
  5.times do
32
33
  mail.deliver
33
34
  end
34
- File.open(ActionMailer::Base.cache_settings[:location], 'r') do |file|
35
+ mails = File.open(ActionMailer::Base.cache_settings[:location], 'r') do |file|
35
36
  Marshal.load(file)
36
- end.length.should == 5
37
+ end
38
+ expect(mails.length).to eq(5)
37
39
  end
38
40
 
39
41
  it 'should append mail to TestMailer deliveries' do
40
42
  mail.deliver
41
- Mail::TestMailer.deliveries.should == [mail]
43
+ expect(Mail::TestMailer.deliveries).to eq([mail])
42
44
  end
43
45
  end
44
46
  end # Mail::CachedDelivery
data/spec/spec_helper.rb CHANGED
@@ -9,3 +9,10 @@ class Mailer < ActionMailer::Base
9
9
  mail(from: 'from@test.com', to: 'to@test.com', subject: 'Test email', body: 'Hello.')
10
10
  end
11
11
  end # Mailer
12
+
13
+ RSpec.configure do |config|
14
+ config.before(:each) do
15
+ stub_const('Rails', Module.new)
16
+ allow(Rails).to receive(:root).and_return(Dir.pwd)
17
+ end
18
+ end
metadata CHANGED
@@ -1,62 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_mailer_cache_delivery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
5
- prerelease:
4
+ version: 0.4.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Alex Rodionov
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-30 00:00:00.000000000 Z
11
+ date: 2023-05-15 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: actionmailer
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '3.0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '3.0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
33
  version: '2.8'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
40
  version: '2.8'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rake
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ~>
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0.9'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ~>
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0.9'
62
55
  description: Cache delivery method for ActionMailer for testing emails with Selenium
@@ -65,7 +58,7 @@ executables: []
65
58
  extensions: []
66
59
  extra_rdoc_files: []
67
60
  files:
68
- - .travis.yml
61
+ - ".travis.yml"
69
62
  - Gemfile
70
63
  - LICENSE
71
64
  - README.md
@@ -82,33 +75,25 @@ files:
82
75
  homepage: https://github.com/p0deje/action_mailer_cache_delivery
83
76
  licenses:
84
77
  - MIT
85
- post_install_message:
78
+ metadata: {}
79
+ post_install_message:
86
80
  rdoc_options: []
87
81
  require_paths:
88
82
  - lib
89
83
  required_ruby_version: !ruby/object:Gem::Requirement
90
- none: false
91
84
  requirements:
92
- - - ! '>='
85
+ - - ">="
93
86
  - !ruby/object:Gem::Version
94
87
  version: '0'
95
- segments:
96
- - 0
97
- hash: 583340984348986686
98
88
  required_rubygems_version: !ruby/object:Gem::Requirement
99
- none: false
100
89
  requirements:
101
- - - ! '>='
90
+ - - ">="
102
91
  - !ruby/object:Gem::Version
103
92
  version: '0'
104
- segments:
105
- - 0
106
- hash: 583340984348986686
107
93
  requirements: []
108
- rubyforge_project:
109
- rubygems_version: 1.8.23
110
- signing_key:
111
- specification_version: 3
94
+ rubygems_version: 3.3.25
95
+ signing_key:
96
+ specification_version: 4
112
97
  summary: Cache delivery method for ActionMailer
113
98
  test_files:
114
99
  - spec/action_mailer_cache_delivery_spec.rb