branchable_cdn_assets 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,138 @@
1
+ require 'spec_helper'
2
+
3
+ describe BranchableCDNAssets::Manifest do
4
+
5
+ describe "#source_file" do
6
+ it "returns the path initialized with" do
7
+ expect( BranchableCDNAssets::Manifest.new('/this/path').source_file ).to eq '/this/path'
8
+ end
9
+ end
10
+
11
+ describe "#file_set" do
12
+ let(:source_file) { 'manifest.manifest' }
13
+
14
+ context "when source_file exists" do
15
+
16
+ before :each do
17
+ allow( File ).to receive(:exists?).with(source_file).and_return(true)
18
+ allow( IO ).to receive(:read).with(source_file).and_return("foo\nbar")
19
+ end
20
+
21
+ it "returns a Set" do
22
+ expect( BranchableCDNAssets::Manifest.new(source_file).file_set ).to be_a Set
23
+ end
24
+
25
+ it "contains the files sep by newline in the manifest" do
26
+ files = BranchableCDNAssets::Manifest.new(source_file).file_set
27
+ expect( files.include?('foo') ).to be_truthy
28
+ end
29
+ end
30
+
31
+ context "when source_file doesn't exist" do
32
+
33
+ before :each do
34
+ allow( File ).to receive(:exists?).with(source_file).and_return(false)
35
+ end
36
+
37
+ it "returns an empty set" do
38
+ expect( BranchableCDNAssets::Manifest.new(source_file).file_set ).to be_empty
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ describe "#files" do
45
+
46
+ let(:source_file) { 'manifest.manifest' }
47
+ let(:manifest) { BranchableCDNAssets::Manifest.new(source_file) }
48
+
49
+ before :each do
50
+ allow( File ).to receive(:exists?).with(source_file).and_return(true)
51
+ allow( IO ).to receive(:read).with(source_file).and_return("foo\nbar")
52
+ end
53
+
54
+ it "returns an Array" do
55
+ expect( manifest.files ).to be_a Array
56
+ end
57
+
58
+ it "matches the file_set Set" do
59
+ expect( manifest.files ).to match_array manifest.file_set.to_a
60
+ end
61
+
62
+ end
63
+
64
+ describe "#merge_files" do
65
+
66
+ it "adds arrays of new files" do
67
+ manifest = BranchableCDNAssets::Manifest.new('foo')
68
+ expect{ manifest.merge_files(["hello","world"]) }.to change{manifest.files.count}.by(2)
69
+ expect( manifest.files.include?("hello") ).to be_truthy
70
+ end
71
+
72
+ it "adds single new files" do
73
+ manifest = BranchableCDNAssets::Manifest.new('foo')
74
+ expect{ manifest.merge_files("hello") }.to change{manifest.files.count}.by(1)
75
+ expect( manifest.files.include?("hello") ).to be_truthy
76
+ end
77
+
78
+ it "doesn't add duplicate files" do
79
+ manifest = BranchableCDNAssets::Manifest.new('foo')
80
+ manifest.merge_files("hello")
81
+
82
+ expect{ manifest.merge_files("hello") }.not_to change{manifest.files.count}
83
+ end
84
+
85
+ end
86
+
87
+ describe "#remove_files" do
88
+
89
+ before :each do
90
+ @manifest = BranchableCDNAssets::Manifest.new('foo')
91
+ @manifest.merge_files(["hello","world"])
92
+ end
93
+
94
+ it "removes arrays of files" do
95
+ expect{ @manifest.remove_files(["hello","world"]) }.to change{@manifest.files.count}.by(-2)
96
+ end
97
+
98
+ it "removes single files" do
99
+ expect{ @manifest.remove_files("hello") }.to change{@manifest.files.count}.by(-1)
100
+ end
101
+
102
+ end
103
+
104
+ describe "#update_source_file!" do
105
+
106
+ context "when files is empty" do
107
+ it "removes the source file and returns" do
108
+ manifest = BranchableCDNAssets::Manifest.new('foo')
109
+
110
+ expect( File ).not_to receive(:open)
111
+ expect( File ).to receive(:delete).with('foo')
112
+
113
+ manifest.update_source_file!
114
+ end
115
+ end
116
+
117
+ context "when files has contents" do
118
+
119
+ before :each do
120
+ @source_file = Tempfile.new('foo')
121
+ @source_path = @source_file.path
122
+ @manifest = BranchableCDNAssets::Manifest.new(@source_path)
123
+ @manifest.merge_files(["hello","world"])
124
+ end
125
+
126
+ it "writes contents to the source_file" do
127
+ expect( File ).to receive(:open).with(@source_path, 'w').and_yield(@source_file)
128
+ expect( @source_file ).to receive(:write).with("hello\nworld")
129
+
130
+ @manifest.update_source_file!
131
+ end
132
+
133
+ end
134
+
135
+ end
136
+
137
+
138
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+ require 'branchable_cdn_assets/rake_tasks'
3
+
4
+ describe BranchableCDNAssets::RakeTasks do
5
+
6
+ describe "::register" do
7
+
8
+ let(:data) do
9
+ [
10
+ environments: {
11
+ production: {
12
+ host: 'production',
13
+ url: 'http://production.com',
14
+ root: '/var/www/production'
15
+ },
16
+ default: {
17
+ host: 'default',
18
+ url: 'http://default.com',
19
+ root: '/var/www/default'
20
+ }
21
+ }
22
+ ]
23
+ end
24
+
25
+ before :each do
26
+ Given.fixture 'base'
27
+ Rake::Task.clear
28
+ end
29
+
30
+ after :each do
31
+ Given.cleanup!
32
+ end
33
+
34
+ it "adds tasks under provided namespace" do
35
+ described_class.register(:test, *data)
36
+
37
+ expect( Rake::Task.tasks.map(&:to_s) ).to include 'test:list'
38
+ end
39
+
40
+ it "sends method with check to the file_manager" do
41
+ class FileManagerStub
42
+ extend BranchableCDNAssets::CheckBefore
43
+
44
+ [:list, :pull!].each do |m|
45
+ define_method m do |*args|
46
+ nil
47
+ end
48
+ end
49
+ end
50
+
51
+ file_mananger_double = FileManagerStub.new
52
+ allow( BranchableCDNAssets::FileManager ).to receive(:new).with(anything()).and_return( file_mananger_double )
53
+
54
+ expect( file_mananger_double ).to receive(:list)
55
+ expect( file_mananger_double ).to receive(:pull!)
56
+
57
+ described_class.register(:cdn, *data)
58
+ Rake::Task['cdn:list'].invoke
59
+ Rake::Task['cdn:pull'].invoke
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe BranchableCDNAssets do
4
+
5
+ it "has a version" do
6
+ expect( BranchableCDNAssets::VERSION >= "0.0.0" ).to be_truthy
7
+ end
8
+
9
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe BranchableCDNAssets::Cloudfront do
4
+
5
+ let(:keys) do
6
+ {
7
+ distribution_id: 'dist_id',
8
+ access_key: 'access_key',
9
+ secret_key: 'secret_key'
10
+ }
11
+ end
12
+
13
+ describe "#initialize" do
14
+
15
+ before :each do
16
+ @subject = described_class.new(keys)
17
+ end
18
+
19
+ it "sets #distribution_id" do
20
+ expect( @subject.distribution_id ).to eq 'dist_id'
21
+ end
22
+ it "sets #access_key" do
23
+ expect( @subject.access_key ).to eq 'access_key'
24
+ end
25
+ it "sets #secret_key" do
26
+ expect( @subject.secret_key ).to eq 'secret_key'
27
+ end
28
+
29
+ it "sets #cdn with a Fog instance" do
30
+ fog = double("Fog::CDN")
31
+ expect( Fog::CDN ).to receive(:new).with(
32
+ provider: 'AWS',
33
+ aws_access_key_id: 'access_key',
34
+ aws_secret_access_key: 'secret_key'
35
+ ).and_return(fog)
36
+ expect( described_class.new(keys).cdn ).to eq fog
37
+ end
38
+
39
+ end
40
+
41
+ describe "#invalidate_files" do
42
+ before :each do
43
+ @fog = double("Fog::CDN")
44
+ @resp = double( "Response",
45
+ body: { "InvalidationBatch" => {
46
+ "Path" => ['file_one','file_two']
47
+ }
48
+ })
49
+ allow( Fog::CDN ).to receive(:new).with(
50
+ provider: 'AWS',
51
+ aws_access_key_id: 'access_key',
52
+ aws_secret_access_key: 'secret_key'
53
+ ).and_return(@fog)
54
+ @subject = described_class.new(keys)
55
+ end
56
+
57
+ it "calls cloudfront to invalidate given files" do
58
+ expect( @fog ).to receive(:post_invalidation)
59
+ .with( 'dist_id', ['file_one','file_two'])
60
+ .and_return(@resp)
61
+
62
+ @subject.invalidate_files ['file_one','file_two']
63
+ end
64
+ end
65
+
66
+
67
+ end
@@ -0,0 +1,21 @@
1
+ require 'rspec'
2
+ require 'pry'
3
+ require 'tempfile'
4
+ require 'catch_and_release'
5
+ require 'catch_and_release/rspec'
6
+ require 'support/given'
7
+ require 'support/hash'
8
+
9
+ require 'branchable_cdn_assets'
10
+
11
+ # silence stdout
12
+ $stdout = StringIO.open('','w+')
13
+
14
+ RSpec.configure do |config|
15
+ config.mock_with :rspec do |mocks|
16
+ mocks.verify_doubled_constant_names = true
17
+ mocks.verify_partial_doubles = true
18
+ end
19
+
20
+ config.include CatchAndRelease::RSpec
21
+ end
@@ -0,0 +1,28 @@
1
+ module Given
2
+ ROOT = Dir.pwd
3
+ TMP = File.join( Dir.pwd, 'tmp' )
4
+
5
+ class << self
6
+
7
+ def fixture name
8
+ cleanup!
9
+
10
+ `rsync -av ./spec/fixtures/#{name}/ #{TMP}/`
11
+ Dir.chdir TMP
12
+ end
13
+
14
+ def file name, content
15
+ file_path = File.join( TMP, name )
16
+ FileUtils.mkdir_p( File.dirname(file_path) )
17
+ File.open( file_path, 'w' ) do |file|
18
+ file.write content
19
+ end
20
+ end
21
+
22
+ def cleanup!
23
+ Dir.chdir ROOT
24
+ `rm -rf #{TMP}`
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,8 @@
1
+ class Hash
2
+ def without_key key
3
+ new_hash = self.dup
4
+ new_hash.delete(key)
5
+
6
+ return new_hash
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: branchable_cdn_assets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Steven Sloan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: asgit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '0.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: here_or_there
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '0.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fog
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.18'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.18'
83
+ description: ' Helpers for syncing and finding assets accross multiple remotes '
84
+ email: marketing-dev@mailchimp.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - lib/branchable_cdn_assets.rb
90
+ - lib/branchable_cdn_assets/check_before.rb
91
+ - lib/branchable_cdn_assets/cloudfront.rb
92
+ - lib/branchable_cdn_assets/config.rb
93
+ - lib/branchable_cdn_assets/config/environment_attribute_reader.rb
94
+ - lib/branchable_cdn_assets/file_manager.rb
95
+ - lib/branchable_cdn_assets/file_manager/checks.rb
96
+ - lib/branchable_cdn_assets/manifest.rb
97
+ - lib/branchable_cdn_assets/rake_tasks.rb
98
+ - lib/branchable_cdn_assets/shell.rb
99
+ - lib/branchable_cdn_assets/version.rb
100
+ - spec/lib/branchable_cdn_assets/check_before_spec.rb
101
+ - spec/lib/branchable_cdn_assets/config_spec.rb
102
+ - spec/lib/branchable_cdn_assets/file_manager/find_spec.rb
103
+ - spec/lib/branchable_cdn_assets/file_manager_spec.rb
104
+ - spec/lib/branchable_cdn_assets/manifest_spec.rb
105
+ - spec/lib/branchable_cdn_assets/rake_tasks_spec.rb
106
+ - spec/lib/branchable_cdn_assets_spec.rb
107
+ - spec/lib/cloudfront_spec.rb
108
+ - spec/spec_helper.rb
109
+ - spec/support/given.rb
110
+ - spec/support/hash.rb
111
+ homepage: http://github.com/mailchimp/branchable_cdn_assets
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: 1.9.3
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.2.0
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Helpers for syncing and finding assets accross multiple remotes
135
+ test_files:
136
+ - spec/lib/branchable_cdn_assets/check_before_spec.rb
137
+ - spec/lib/branchable_cdn_assets/config_spec.rb
138
+ - spec/lib/branchable_cdn_assets/file_manager/find_spec.rb
139
+ - spec/lib/branchable_cdn_assets/file_manager_spec.rb
140
+ - spec/lib/branchable_cdn_assets/manifest_spec.rb
141
+ - spec/lib/branchable_cdn_assets/rake_tasks_spec.rb
142
+ - spec/lib/branchable_cdn_assets_spec.rb
143
+ - spec/lib/cloudfront_spec.rb
144
+ - spec/spec_helper.rb
145
+ - spec/support/given.rb
146
+ - spec/support/hash.rb