Skip to content
Permalink
Browse files

Add an :enable action

  • Loading branch information...
RoboticCheese committed May 4, 2015
1 parent c9710c2 commit 87602138a876344367f1abf212222f5945748008
@@ -33,7 +33,7 @@ Recipes
***default***

Installs Divvy (from the Mac App Store or a Windows direct download by
default) and starts it.
default), sets it to start on login, and starts it up.

Resources
=========
@@ -50,16 +50,17 @@ Syntax:

Actions:

| Action | Description |
|------------|-----------------|
| `:install` | Install the app |
| `:start` | Run the app |
| Action | Description |
|------------|----------------------------------|
| `:install` | Install the app |
| `:enable` | Enable the app to start on login |
| `:start` | Run the app |

Attributes:

| Attribute | Default | Description |
|------------|----------------------|----------------------|
| action | `[:install, :start]` | Action(s) to perform |
| Attribute | Default | Description |
|------------|-------------------------------|----------------------|
| action | `[:install, :enable, :start]` | Action(s) to perform |

Providers
=========
@@ -21,7 +21,7 @@
if defined?(ChefSpec)
ChefSpec.define_matcher(:divvy_app)

[:install, :start].each do |a|
[:install, :enable, :start].each do |a|
define_method("#{a}_divvy_app") do |name|
ChefSpec::Matchers::ResourceMatcher.new(:divvy_app, a, name)
end
@@ -51,6 +51,14 @@ def whyrun_supported?
)
end

#
# Enable the app to start on boot/login.
#
action :enable do
enable!
new_resource.enabled(true)
end

#
# Start the app.
#
@@ -61,6 +69,17 @@ def whyrun_supported?

private

#
# Enable the Divvy app using whatever command is appropriate for the
# current platform.
#
# @raise [NotImplementedError] if not defined for this provider.
#
def enable!
fail(NotImplementedError,
"`enable!` method not implemented for #{self.class} provider")
end

#
# Start the Divvy app running using whatever command is appropriate for
# the current platform.
@@ -69,7 +88,7 @@ def whyrun_supported?
#
def start!
fail(NotImplementedError,
"`start` method not implemented for #{self.class} provider")
"`start!` method not implemented for #{self.class} provider")
end

#
@@ -37,6 +37,35 @@ class MacOsX < DivvyApp

private

#
# (see DivvyApp#enable!)
#
def enable!
# TODO: This should eventually take the form of applescript and
# login_item resources in the mac_os_x cookbook.
cmd = "osascript -e 'tell application \"System Events\" to make " \
'new login item at end with properties ' \
"{name: \"Divvy\", path: \"#{PATH}\", hidden: false}'"
enabled_status = enabled?
execute 'enable divvy' do
command cmd
action :run
only_if { !enabled_status }
end
end

#
# Shell out and use AppleScript to check whether the "Divvy" login
# item already exists.
#
# @return [TrueClass, FalseClass]
#
def enabled?
cmd = "osascript -e 'tell application \"System Events\" to get " \
"the name of the login item \"Divvy\"'"
!Mixlib::ShellOut.new(cmd).run_command.stdout.empty?
end

#
# (see DivvyApp#start!)
#
@@ -33,6 +33,17 @@ class Windows < DivvyApp

private

#
# (see DivvyApp#enable!)
#
def enable!
exe = ::File.join(PATH, 'Divvy.exe').gsub('/', '\\')
windows_auto_run 'Divvy' do
program exe
action :create
end
end

#
# (see DivvyApp#start!)
#
@@ -27,8 +27,8 @@ class Resource
# @author Jonathan Hartman <j@p4nt5.com>
class DivvyApp < Resource::LWRPBase
self.resource_name = :divvy_app
actions :install, :start
default_action [:install, :start]
actions :install, :enable, :start
default_action [:install, :enable, :start]

#
# Attribute for the app's installed status.
@@ -38,6 +38,14 @@ class DivvyApp < Resource::LWRPBase
default: nil
alias_method :installed?, :installed

#
# Attribute for the app's enabled status.
#
attribute :enabled,
kind_of: [NilClass, TrueClass, FalseClass],
default: nil
alias_method :enabled?, :enabled

#
# Attribute for the app's running status.
#
@@ -21,5 +21,5 @@
include_recipe 'mac-app-store' if node['platform'] == 'mac_os_x'

divvy_app 'default' do
action [:install, :start]
action [:install, :enable, :start]
end
@@ -27,6 +27,53 @@
end
end

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

it 'runs an execute resource' do
p = provider
expect(p).to receive(:execute).with('enable divvy').and_yield
cmd = 'osascript -e \'tell application "System Events" to make new ' \
'login item at end with properties {name: "Divvy", ' \
'path: "/Applications/Divvy.app", hidden: false}\''
expect(p).to receive(:command).with(cmd)
expect(p).to receive(:action).with(:run)
expect(p).to receive(:only_if).and_yield
expect(p).to receive(:enabled?)
p.send(:enable!)
end
end

describe '#enabled?' do
let(:enabled?) { nil }
let(:stdout) { enabled? ? 'Divvy' : '' }

before(:each) do
cmd = 'osascript -e \'tell application "System Events" to get the ' \
'name of the login item "Divvy"\''
allow(Mixlib::ShellOut).to receive(:new).with(cmd)
.and_return(double(run_command: double(stdout: stdout)))
end

context 'Divvy not enabled' do
let(:enabled?) { false }

it 'returns false' do
expect(provider.send(:enabled?)).to eq(false)
end
end

context 'Divvy enabled' do
let(:enabled?) { true }

it 'returns true' do
expect(provider.send(:enabled?)).to eq(true)
end
end
end

describe '#install!' do
before(:each) do
allow_any_instance_of(described_class).to receive(:authorize_app!)
@@ -31,6 +31,23 @@
end
end

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

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

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

describe '#action_start' do
before(:each) do
allow_any_instance_of(described_class).to receive(:start!)
@@ -48,15 +65,11 @@
end
end

describe '#start!' do
it 'raises an error' do
expect { provider.send(:start!) }.to raise_error(NotImplementedError)
end
end

describe '#install!' do
it 'raises an error' do
expect { provider.send(:install!) }.to raise_error(NotImplementedError)
[:start!, :enable!, :install!].each do |m|
describe "##{m}" do
it 'raises an error' do
expect { provider.send(m) }.to raise_error(NotImplementedError)
end
end
end
end
@@ -8,6 +8,22 @@
let(:new_resource) { Chef::Resource::DivvyApp.new(name, nil) }
let(:provider) { described_class.new(new_resource, nil) }

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

it 'enables auto-run for Divvy' do
p = provider
allow(File).to receive(:join).with(described_class::PATH, 'Divvy.exe')
.and_return('c:/divvy/Divvy.exe')
expect(p).to receive(:windows_auto_run).with('Divvy').and_yield
expect(p).to receive(:program).with('c:\divvy\Divvy.exe')
expect(p).to receive(:action).with(:create)
p.send(:enable!)
end
end

describe '#start!' do
before(:each) do
allow_any_instance_of(described_class).to receive(:execute)
@@ -14,19 +14,23 @@
end

it 'sets the correct supported actions' do
expected = [:nothing, :install, :start]
expected = [:nothing, :install, :enable, :start]
expect(resource.instance_variable_get(:@allowed_actions)).to eq(expected)
end

it 'sets the correct default action' do
expected = [:install, :start]
expected = [:install, :enable, :start]
expect(resource.instance_variable_get(:@action)).to eq(expected)
end

it 'sets the installed status to nil' do
expect(resource.instance_variable_get(:@installed)).to eq(nil)
end

it 'sets the enabled status to nil' do
expect(resource.instance_variable_get(:@enabled)).to eq(nil)
end

it 'sets the running status to nil' do
expect(resource.instance_variable_get(:@running)).to eq(nil)
end
@@ -59,7 +63,41 @@
r
end

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

[:enabled, :enabled?].each do |m|
describe "##{m}" do
context 'default unknown enabled status' do
it 'returns nil' do
expect(resource.send(m)).to eq(nil)
end
end

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

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

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

it 'returns false' do
expect(resource.send(m)).to eq(false)
end
end
@@ -93,7 +131,7 @@
r
end

it 'returns true' do
it 'returns false' do
expect(resource.send(m)).to eq(false)
end
end
@@ -12,7 +12,11 @@
expect(chef_run).to install_divvy_app('default')
end

it 'runs the Divvy app' do
it 'enables the Divvy app' do
expect(chef_run).to enable_divvy_app('default')
end

it 'starts the Divvy app' do
expect(chef_run).to start_divvy_app('default')
end
end

0 comments on commit 8760213

Please sign in to comment.
You can’t perform that action at this time.