firebrew 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +3 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +127 -0
  8. data/Rakefile +6 -0
  9. data/bin/firebrew +5 -0
  10. data/firebrew.gemspec +29 -0
  11. data/lib/firebrew.rb +20 -0
  12. data/lib/firebrew/amo_api/search.rb +50 -0
  13. data/lib/firebrew/command_line.rb +140 -0
  14. data/lib/firebrew/entity.rb +39 -0
  15. data/lib/firebrew/firefox/basic_extension.rb +10 -0
  16. data/lib/firebrew/firefox/command.rb +27 -0
  17. data/lib/firebrew/firefox/extension.rb +104 -0
  18. data/lib/firebrew/firefox/profile.rb +56 -0
  19. data/lib/firebrew/runner.rb +83 -0
  20. data/lib/firebrew/version.rb +3 -0
  21. data/spec/double/firefox.rb +2 -0
  22. data/spec/firebrew/amo_api/search_spec.rb +69 -0
  23. data/spec/firebrew/command_line_spec.rb +150 -0
  24. data/spec/firebrew/entity_spec.rb +79 -0
  25. data/spec/firebrew/firefox/command_spec.rb +39 -0
  26. data/spec/firebrew/firefox/extension_spec.rb +159 -0
  27. data/spec/firebrew/firefox/profile_spec.rb +69 -0
  28. data/spec/firebrew/runner_spec.rb +142 -0
  29. data/spec/firebrew_spec.rb +7 -0
  30. data/spec/fixtures/amo_api/search/base.xml +28 -0
  31. data/spec/fixtures/amo_api/search/empty.xml +4 -0
  32. data/spec/fixtures/amo_api/search/single.xml +12 -0
  33. data/spec/fixtures/firefox/extension/extensions.v16.json +227 -0
  34. data/spec/fixtures/firefox/extension/extensions_added_hoge.v16.json +235 -0
  35. data/spec/fixtures/firefox/profile/base.ini +18 -0
  36. data/spec/fixtures/firefox/profile/empty.ini +2 -0
  37. data/spec/spec_helper.rb +13 -0
  38. metadata +196 -0
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ module Firebrew
4
+ class EntityTest1
5
+ include Entity
6
+ entity_attr :value1, :value2
7
+ def initialize(params={})
8
+ params.each{|(k,v)| self.send("#{k}=", v) }
9
+ end
10
+ end
11
+
12
+ class EntityTest2
13
+ include Entity
14
+ entity_attr :value3, :value4
15
+ end
16
+
17
+ class EntityTest1Ex < EntityTest1
18
+ entity_attr :value5, :value6
19
+ end
20
+
21
+ describe Firebrew::Entity do
22
+ describe '::attributes()' do
23
+ describe 'EntityTest1(value1, value2)' do
24
+ it { expect(EntityTest1.attributes).to eq([:value1, :value2]) }
25
+ end
26
+
27
+ describe 'EntityTest2(value3, value4)' do
28
+ it { expect(EntityTest2.attributes).to eq([:value3, :value4]) }
29
+ end
30
+
31
+ describe 'EntityTest1Ex(value1, value2, value5, value6)' do
32
+ it { expect(EntityTest1Ex.attributes).to eq([:value1, :value2, :value5, :value6]) }
33
+ end
34
+ end
35
+
36
+ describe '::entity_attr(*attrs)' do
37
+ subject do
38
+ Class.new do
39
+ include Entity
40
+ end
41
+ end
42
+
43
+ it 'should add the `attrs`' do
44
+ expect(subject.entity_attr(:attr1, :attr2)).to eq([:attr1, :attr2])
45
+ end
46
+
47
+ context 'when the `attrs` was already existed' do
48
+ it 'should add attributes which not exist in the `::attributes`' do
49
+ subject.entity_attr(:attr1)
50
+ expect(subject.entity_attr(:attr1, :attr2)).to eq([:attr2])
51
+ end
52
+ end
53
+
54
+ context 'when the `attrs` contain duplicate values' do
55
+ it 'should add attributes which not contain duplicate values' do
56
+ expect(subject.entity_attr(:attr1, :attr2, :attr2)).to eq([:attr1, :attr2])
57
+ end
58
+ end
59
+ end
60
+
61
+ describe '#==(rop)' do
62
+ context 'when `self` equaled `rop`' do
63
+ it do
64
+ a = EntityTest1.new(value1: 1, value2: 2)
65
+ b = EntityTest1.new(value1: 1, value2: 2)
66
+ expect(a == b).to be_truthy
67
+ end
68
+ end
69
+
70
+ context 'when `self` not equaled `rop`' do
71
+ it do
72
+ a = EntityTest1.new(value1: 1, value2: 2)
73
+ b = EntityTest1.new(value1: 3, value2: 2)
74
+ expect(a == b).to be_falsy
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ module Firebrew::Firefox
4
+ describe Command do
5
+ subject do
6
+ Command.new(self.config, self.executer)
7
+ end
8
+
9
+ let(:config){Firebrew::Runner.default_config}
10
+ let(:executer){Command::Executer.new}
11
+
12
+ context 'when the indicated firefox command by the `config[:firefox]` not existed' do
13
+ let(:config){super().merge firefox: 'firefox/not/existed/path'}
14
+ it { expect{subject}.to raise_error(Firebrew::FirefoxCommandError) }
15
+ end
16
+
17
+ context 'when the indicated command by the `config[:firefox]` was not firefox' do
18
+ let(:executer) do
19
+ double('executer', exec: ['Other program', 0])
20
+ end
21
+ it { expect{subject}.to raise_error(Firebrew::FirefoxCommandError) }
22
+
23
+ describe 'command status' do
24
+ let(:executer) do
25
+ double('executer', exec: ['Fake Mozilla Firefox', 1])
26
+ end
27
+ it { expect{subject}.to raise_error(Firebrew::FirefoxCommandError) }
28
+ end
29
+ end
30
+
31
+ describe '#version()' do
32
+ subject { super().version }
33
+ let(:executer) do
34
+ double('executer', exec: ['Mozilla Firefox 30.0', 0])
35
+ end
36
+ it { is_expected.to eq('30.0') }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,159 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+
4
+ module Firebrew::Firefox
5
+ describe Firebrew::Firefox::Extension do
6
+ let(:manager) do
7
+ Extension::Manager.new(
8
+ profile: Profile.new(
9
+ path: './tmp'
10
+ )
11
+ )
12
+ end
13
+
14
+ let(:extensions){[
15
+ Extension.new({
16
+ name: 'Japanese Language Pack',
17
+ guid: 'langpack-ja@firefox.mozilla.org',
18
+ version: '30.0',
19
+ uri: './tmp/extensions/langpack-ja@firefox.mozilla.org.xpi'
20
+ }, self.manager),
21
+ Extension.new({
22
+ name: 'Vimperator',
23
+ guid: 'vimperator@mozdev.org',
24
+ version: '3.8.2',
25
+ uri: './tmp/extensions/vimperator@mozdev.org.xpi'
26
+ }, self.manager)
27
+ ]}
28
+
29
+ before do
30
+ FileUtils.cp('./spec/fixtures/firefox/extension/extensions.v16.json', './tmp/extensions.json')
31
+ end
32
+
33
+ describe :Manager do
34
+ subject { self.instance }
35
+ let(:instance){self.manager}
36
+
37
+ describe '#all()' do
38
+ subject { self.instance.all }
39
+
40
+ it { is_expected.to be_instance_of(Array) }
41
+ it { expect(subject.size).to eq(2) }
42
+
43
+ it 'should construct' do
44
+ expect(subject[0]).to eq(self.extensions[0])
45
+ expect(subject[1]).to eq(self.extensions[1])
46
+ end
47
+
48
+ context 'when not existed `extension.json`' do
49
+ before {FileUtils.rm_f './tmp/extensions.json'}
50
+ it { expect{subject}.to raise_error(Firebrew::ExtensionsFileNotFoundError) }
51
+ end
52
+ end
53
+
54
+ describe '#find(name)' do
55
+ subject {super().find self.name}
56
+ let(:name){self.extensions[1].name}
57
+
58
+ it 'should find' do
59
+ is_expected.to eq(self.extensions[1])
60
+ end
61
+
62
+ context 'when the extension corresponding to the `name` not existed' do
63
+ let(:name){'hoge'}
64
+ it { is_expected.to be_nil }
65
+ end
66
+ end
67
+
68
+ describe '#find!(name)' do
69
+ subject {super().find! self.name}
70
+ let(:name){self.extensions[1].name}
71
+
72
+ it { expect{subject}.to_not raise_error }
73
+ it 'should find' do
74
+ is_expected.to eq(self.extensions[1])
75
+ end
76
+
77
+ context 'when the extension corresponding to the `name` not existed' do
78
+ let(:name){'hoge'}
79
+ it { expect{subject}.to raise_error(Firebrew::ExtensionNotFoundError) }
80
+ end
81
+ end
82
+
83
+ describe '#install(extension)' do
84
+ before { self.instance.install(self.extension) }
85
+
86
+ let(:extension) do
87
+ Extension.new({
88
+ guid: 'hoge@example.org',
89
+ uri: './spec/fixtures/amo_api/search/base.xml'
90
+ }, self.manager)
91
+ end
92
+
93
+ it 'should copy the `path/to/profile/extensions/guid.xpi`' do
94
+ path = File.join(self.instance.profile.path, 'extensions/%s.xpi' % self.extension.guid)
95
+ expect(File.exists? path).to be_truthy
96
+ end
97
+
98
+ it 'should add the `extension` extension' do
99
+ all = self.instance.all
100
+ extension = self.extension
101
+ extension.uri = './tmp/extensions/hoge@example.org.xpi'
102
+ expect(all.size).to eq(3)
103
+ expect(all[0]).to eq(self.extensions[0])
104
+ expect(all[1]).to eq(self.extensions[1])
105
+ expect(all[2]).to eq(self.extension)
106
+ end
107
+ end
108
+
109
+ describe '#uninstall(extension)' do
110
+ let(:extension) do
111
+ Extension.new({
112
+ guid: 'hoge@example.org',
113
+ uri: './tmp/extensions/hoge@example.org.xpi'
114
+ }, self.manager)
115
+ end
116
+
117
+ before do
118
+ FileUtils.cp('./spec/fixtures/firefox/extension/extensions_added_hoge.v16.json', './tmp/extensions.json')
119
+ File.write self.extension.uri, ''
120
+ self.manager.uninstall(self.extension)
121
+ end
122
+
123
+ it 'should remove the `extension` file' do
124
+ expect(File.exists? self.extension.uri).to be_falsey
125
+ end
126
+
127
+ it 'should remove the `extension` extension' do
128
+ all = self.instance.all
129
+ expect(all.size).to eq(2)
130
+ expect(all[0]).to eq(self.extensions[0])
131
+ expect(all[1]).to eq(self.extensions[1])
132
+ end
133
+ end
134
+ end
135
+
136
+ describe 'instance' do
137
+ subject {self.instance}
138
+ let(:instance){self.extensions[1]}
139
+
140
+ before do
141
+ open(self.instance.uri,'w'){|o| o.write self.instance.name}
142
+ end
143
+
144
+ after do
145
+ FileUtils.rm_f self.instance.uri
146
+ end
147
+
148
+ describe '#delete()' do
149
+ before do
150
+ self.instance().delete
151
+ end
152
+
153
+ it 'should not existed this file' do
154
+ expect(File.exists? self.instance.uri).to be_falsey
155
+ end
156
+ end
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ module Firebrew::Firefox
4
+ describe Firebrew::Firefox::Profile do
5
+ describe :Manager do
6
+ subject do
7
+ Profile::Manager.new(
8
+ base_dir: self.base_dir,
9
+ data_file: self.data_file
10
+ )
11
+ end
12
+
13
+ let(:base_dir){'./spec/fixtures/firefox/profile'}
14
+ let(:data_file){'base.ini'}
15
+
16
+ context 'when "profiles.ini" not existed' do
17
+ let(:base_dir){'not/existed/directory'}
18
+ let(:data_file){'not_found.ini'}
19
+ it { expect{subject}.to raise_error(Firebrew::ProfilesFileNotFoundError) }
20
+ end
21
+
22
+ describe '#all()' do
23
+ subject { super().all }
24
+ it { expect(subject.size).to eq(3) }
25
+
26
+ it 'should construct records' do
27
+ expect(subject[0].name).to eq('default')
28
+ expect(subject[0].path).to eq(File.expand_path File.join(self.base_dir, 'Profiles/abcd.default'))
29
+ expect(subject[0].is_default).to be_truthy
30
+
31
+ expect(subject[1].name).to eq('other_profile')
32
+ expect(subject[1].path).to eq(File.expand_path File.join(self.base_dir, 'Profiles/efgh.other_profile'))
33
+ expect(subject[1].is_default).to be_falsey
34
+
35
+ expect(subject[2].name).to eq('abs_profile')
36
+ expect(subject[2].path).to eq('/path/to/abs_profile')
37
+ expect(subject[2].is_default).to be_falsey
38
+ end
39
+
40
+ context 'when profiles were empty' do
41
+ let(:data_file){'empty.ini'}
42
+ it { is_expected.to be_empty }
43
+ end
44
+ end
45
+
46
+ describe '#find(name)' do
47
+ subject { super().find(self.name) }
48
+ let(:name){'other_profile'}
49
+ it { expect(subject.name).to eq('other_profile') }
50
+
51
+ context 'when not existed the `name` in the profiles.' do
52
+ let(:name){'not_existed_profile_name'}
53
+ it { is_expected.to be_nil }
54
+ end
55
+ end
56
+
57
+ describe '#find!(name)' do
58
+ subject {super().find! self.name}
59
+ let(:name){'other_profile'}
60
+ it { expect{subject}.to_not raise_error}
61
+
62
+ context 'when not existed the `name` in the profiles.' do
63
+ let(:name){'not_existed_profile_name'}
64
+ it { expect{subject}.to raise_error(Firebrew::ProfileNotFoundError) }
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+
3
+ module Firebrew
4
+ describe Firebrew::Runner do
5
+ describe '::default_config(platform)' do
6
+ subject do
7
+ Runner.default_config(self.platform)
8
+ end
9
+
10
+ let(:platform){RUBY_PLATFORM}
11
+
12
+ before do
13
+ ENV['FIREBREW_FIREFOX_PROFILE_BASE_DIR'] = nil
14
+ ENV['FIREBREW_FIREFOX_PROFILE'] = nil
15
+ ENV['FIREBREW_FIREFOX'] = nil
16
+ end
17
+
18
+ context 'when the `platform` was "MacOS"' do
19
+ let(:platform){'x86_64-darwin13.0'}
20
+ it do
21
+ is_expected.to eq(
22
+ base_dir: '~/Library/Application Support/Firefox',
23
+ firefox: '/Applications/Firefox.app/Contents/MacOS/firefox-bin',
24
+ profile: 'default'
25
+ )
26
+ end
27
+ end
28
+
29
+ context 'when the `platform` was "Linux"' do
30
+ let(:platform){'x86_64-linux'}
31
+ it do
32
+ is_expected.to eq(
33
+ base_dir: '~/.mozilla/firefox',
34
+ firefox: '/usr/bin/firefox',
35
+ profile: 'default'
36
+ )
37
+ end
38
+ end
39
+
40
+ context 'when the `platform` was "Windows 7 x86_64"' do
41
+ let(:platform){'x64-mingw32'}
42
+ it do
43
+ is_expected.to eq(
44
+ base_dir: '~/AppData/Roming/Mozilla/Firefox',
45
+ firefox: 'C:/Program Files (x86)/Mozilla Firefox/firefox.exe',
46
+ profile: 'default'
47
+ )
48
+ end
49
+ end
50
+
51
+ context 'when set environment variables' do
52
+ before do
53
+ ENV['FIREBREW_FIREFOX_PROFILE_BASE_DIR'] = 'path/to/profile_base_directory'
54
+ ENV['FIREBREW_FIREFOX_PROFILE'] = 'profile-name'
55
+ ENV['FIREBREW_FIREFOX'] = 'path/to/firefox'
56
+ end
57
+
58
+ after do
59
+ ENV['FIREBREW_FIREFOX_PROFILE_BASE_DIR'] = nil
60
+ ENV['FIREBREW_FIREFOX_PROFILE'] = nil
61
+ ENV['FIREBREW_FIREFOX'] = nil
62
+ end
63
+
64
+ it do
65
+ is_expected.to eq(
66
+ base_dir: 'path/to/profile_base_directory',
67
+ firefox: 'path/to/firefox',
68
+ profile: 'profile-name'
69
+ )
70
+ end
71
+ end
72
+ end
73
+
74
+ describe :Instance do
75
+ subject {self.instance}
76
+
77
+ let(:instance) do
78
+ Runner.new(
79
+ base_dir: './tmp',
80
+ data_file: 'profiles.ini',
81
+ firefox: './spec/double/firefox.rb'
82
+ )
83
+ end
84
+
85
+ let(:search_params){{term: 'hoge', version: '30.0', max: 1}}
86
+
87
+ before do
88
+ FileUtils.cp './spec/fixtures/firefox/profile/base.ini', './tmp/profiles.ini'
89
+ response = File.read("./spec/fixtures/amo_api/search/base.xml")
90
+ ActiveResource::HttpMock.respond_to do |mock|
91
+ mock.get AmoApi::Search.path(self.search_params), {}, response
92
+ end
93
+ end
94
+
95
+ after do
96
+ FileUtils.rm_f './tmp/*'
97
+ ActiveResource::HttpMock.reset!
98
+ end
99
+
100
+ describe '#install(params)' do
101
+ subject do
102
+ extensions_double = double('extensions', install: nil, find: nil)
103
+ self.instance.profile = double('profile', extensions: extensions_double)
104
+ self.instance.install(self.search_params)
105
+ end
106
+
107
+ it { expect{subject}.to_not raise_error }
108
+
109
+ context 'when the `params[:term]` existed' do
110
+ subject do
111
+ extensions_double = double('extensions', install: nil, find: Firefox::BasicExtension.new)
112
+ self.instance.profile = double('profile', extensions: extensions_double)
113
+ self.instance.install(self.search_params)
114
+ end
115
+
116
+ it { expect{subject}.to raise_error(Firebrew::OperationAlreadyCompletedError) }
117
+ end
118
+ end
119
+
120
+ describe '#uninstall(params)' do
121
+ subject do
122
+ extensions_double = double('extensions', find!: double('ext', delete: nil))
123
+ self.instance.profile = double('profile', extensions: extensions_double)
124
+ self.instance.uninstall(term: 'not-existed-extension')
125
+ end
126
+
127
+ it { expect{subject}.to_not raise_error }
128
+
129
+ context 'when the `params[:term]` not existed' do
130
+ subject do
131
+ extensions_double = double('extensions')
132
+ allow(extensions_double).to receive(:find!).and_raise(Firebrew::ExtensionNotFoundError)
133
+ self.instance.profile = double('profile', extensions: extensions_double)
134
+ self.instance.uninstall(term: 'not-existed-extension')
135
+ end
136
+
137
+ it { expect{subject}.to raise_error(Firebrew::OperationAlreadyCompletedError) }
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end