Skip to content

Commit

Permalink
Set up providers to prep for addition of Windows + direct DL support
Browse files Browse the repository at this point in the history
  • Loading branch information
hartmantis committed Apr 29, 2015
1 parent 5254e19 commit 4993d2a
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 18 deletions.
63 changes: 63 additions & 0 deletions libraries/provider_divvy_app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Encoding: UTF-8
#
# Cookbook Name:: divvy
# Library:: provider_divvy_app
#
# Copyright 2015 Jonathan Hartman
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/provider/lwrp_base'
require_relative 'resource_divvy_app'
require_relative 'provider_divvy_app_mac_os_x'

class Chef
class Provider
# A Chef provider for the Divvy app.
#
# @author Jonathan Hartman <j@p4nt5.com>
class DivvyApp < Provider::LWRPBase
use_inline_resources

#
# WhyRun is supported by this provider.
#
# @return [TrueClass, FalseClass]
#
def whyrun_supported?
true
end

#
# Install the app if it's not already.
#
action :install do
install!
new_resource.installed(true)
end

private

#
# Do the actual app installation.
#
# @raise [NotImplementedError] if not defined for this provider.
#
def install!
fail(NotImplementedError,
"`install!` method not implemented for #{self.class} provider")
end
end
end
end
35 changes: 35 additions & 0 deletions libraries/provider_divvy_app_mac_os_x.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Encoding: UTF-8
#
# Cookbook Name:: divvy
# Library:: provider_divvy_app_mac_os_x
#
# Copyright 2015 Jonathan Hartman
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/provider/lwrp_base'
require_relative 'provider_divvy_app'
require_relative 'provider_divvy_app_mac_os_x_app_store'

class Chef
class Provider
class DivvyApp < Provider::LWRPBase
# An empty parent class for the Divvy for OS X providers.
#
# @author Jonathan Hartman <j@p4nt5.com>
class MacOsX < DivvyApp
end
end
end
end
48 changes: 48 additions & 0 deletions libraries/provider_divvy_app_mac_os_x_app_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Encoding: UTF-8
#
# Cookbook Name:: divvy
# Library:: provider_divvy_app_mac_os_x_app_store
#
# Copyright 2015 Jonathan Hartman
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/provider/lwrp_base'
require_relative 'provider_divvy_app'
require_relative 'provider_divvy_app_mac_os_x'

class Chef
class Provider
class DivvyApp < Provider::LWRPBase
class MacOsX < DivvyApp
# A Chef provider for OS X App Store Divvy installs.
#
# @author Jonathan Hartman <j@p4nt5.com>
class AppStore < MacOsX
private

#
# (see DivvyApp#install!)
#
def install!
mac_app_store_app 'Divvy - Window Manager' do
bundle_id 'com.mizage.divvy'
action :install
end
end
end
end
end
end
end
3 changes: 2 additions & 1 deletion libraries/provider_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

require 'chef/dsl'
require 'chef/platform/provider_mapping'
require_relative 'provider_divvy_app'

Chef::Platform.set(platform: :mac_os_x,
resource: :divvy_app,
provider: Chef::Provider::MacAppStoreApp)
provider: Chef::Provider::DivvyApp::MacOsX::AppStore)
16 changes: 8 additions & 8 deletions libraries/resource_divvy_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ class Resource
# A Chef resource for the Divvy app.
#
# @author Jonathan Hartman <j@p4nt5.com>
class DivvyApp < MacAppStoreApp
class DivvyApp < Resource::LWRPBase
self.resource_name = :divvy_app
actions :install
default_action :install

#
# Overload the app name with the one for this app.
# Attribute for the app's installed status.
#
attribute :app_name, kind_of: String, default: 'Divvy - Window Manager'

#
# Overload the bundle ID with the one for this app.
#
attribute :bundle_id, kind_of: String, default: 'com.mizage.Divvy'
attribute :installed,
kind_of: [NilClass, TrueClass, FalseClass],
default: nil
alias_method :installed?, :installed
end
end
end
21 changes: 21 additions & 0 deletions spec/libraries/provider_divvy_app_mac_os_x_app_store_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Encoding: UTF-8

require_relative '../spec_helper'
require_relative '../../libraries/provider_divvy_app_mac_os_x_app_store'

describe Chef::Provider::DivvyApp::MacOsX::AppStore do
let(:name) { 'Some App' }
let(:new_resource) { Chef::Resource::DivvyApp.new(name, nil) }
let(:provider) { described_class.new(new_resource, nil) }

describe '#install!' do
it 'installs the app from the App Store' do
p = provider
expect(p).to receive(:mac_app_store_app).with('Divvy - Window Manager')
.and_yield
expect(p).to receive(:bundle_id).with('com.mizage.divvy')
expect(p).to receive(:action).with(:install)
p.send(:install!)
end
end
end
16 changes: 16 additions & 0 deletions spec/libraries/provider_divvy_app_mac_os_x_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Encoding: UTF-8

require_relative '../spec_helper'
require_relative '../../libraries/provider_divvy_app_mac_os_x'

describe Chef::Provider::DivvyApp::MacOsX do
let(:name) { 'Some App' }
let(:new_resource) { Chef::Resource::DivvyApp.new(name, nil) }
let(:provider) { described_class.new(new_resource, nil) }

describe '#install!' do
it 'raises an error' do
expect { provider.send(:install!) }.to raise_error(NotImplementedError)
end
end
end
39 changes: 39 additions & 0 deletions spec/libraries/provider_divvy_app_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Encoding: UTF-8

require_relative '../spec_helper'
require_relative '../../libraries/provider_divvy_app'

describe Chef::Provider::DivvyApp do
let(:name) { 'Some App' }
let(:new_resource) { Chef::Resource::DivvyApp.new(name, nil) }
let(:provider) { described_class.new(new_resource, nil) }

describe '#whyrun_supported?' do
it 'returns true' do
expect(provider.whyrun_supported?).to eq(true)
end
end

describe '#action_install' do
before(:each) do
allow_any_instance_of(described_class).to receive(:install!)
end

it 'calls the child `install!` method ' do
expect_any_instance_of(described_class).to receive(:install!)
provider.action_install
end

it 'sets the resource installed status' do
p = provider
p.action_install
expect(p.new_resource.installed?).to eq(true)
end
end

describe '#install!' do
it 'raises an error' do
expect { provider.send(:install!) }.to raise_error(NotImplementedError)
end
end
end
4 changes: 2 additions & 2 deletions spec/libraries/provider_mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
context 'Mac OS X' do
let(:platform) { :mac_os_x }

it 'uses the MacAppStoreApp app provider' do
expect(app_provider).to eq(Chef::Provider::MacAppStoreApp)
it 'uses the MacAppStore Divvy app provider' do
expect(app_provider).to eq(Chef::Provider::DivvyApp::MacOsX::AppStore)
end
end

Expand Down
40 changes: 33 additions & 7 deletions spec/libraries/resource_divvy_app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,43 @@
exp = :divvy_app
expect(resource.instance_variable_get(:@resource_name)).to eq(exp)
end
end

describe '#app_name' do
it 'uses the correct app name' do
expect(resource.app_name).to eq('Divvy - Window Manager')
it 'sets the installed status to nil' do
expect(resource.instance_variable_get(:@installed)).to eq(nil)
end
end

describe '#bundle_id' do
it 'uses the correct bundle ID' do
expect(resource.bundle_id).to eq('com.mizage.Divvy')
[:installed, :installed?].each do |m|
describe "##{m}" do
context 'default unknown installed status' do
it 'returns nil' do
expect(resource.send(m)).to eq(nil)
end
end

context 'app installed' do
let(:resource) do
r = super()
r.instance_variable_set(:@installed, true)
r
end

it 'returns true' do
expect(resource.send(m)).to eq(true)
end
end

context 'app not installed' do
let(:resource) do
r = super()
r.instance_variable_set(:@installed, false)
r
end

it 'returns true' do
expect(resource.send(m)).to eq(false)
end
end
end
end
end

0 comments on commit 4993d2a

Please sign in to comment.