diff --git a/metadata.rb b/metadata.rb index 22e825a9..005e76b9 100644 --- a/metadata.rb +++ b/metadata.rb @@ -5,7 +5,7 @@ description 'Resources for configuring and provisioning macOS' long_description 'Resources for configuring and provisioning macOS' chef_version '~> 13.0' if respond_to?(:chef_version) -version '1.0.0' +version '1.1.0' source_url 'https://github.com/Microsoft/macos-cookbook' issues_url 'https://github.com/Microsoft/macos-cookbook/issues' diff --git a/resources/macos_user.rb b/resources/macos_user.rb index 75da963d..c9a2aeda 100644 --- a/resources/macos_user.rb +++ b/resources/macos_user.rb @@ -5,6 +5,7 @@ property :password, String, default: 'password' property :autologin, [TrueClass] property :admin, [TrueClass] +property :fullname, String action_class do def user_home @@ -25,19 +26,28 @@ def sysadminctl '/usr/sbin/sysadminctl' end - def admin_user? + def user_fullname + property_is_set?(:fullname) ? ['-fullName', new_resource.fullname] : '' + end + + def admin_user if property_is_set?(:admin) '-admin' else '' end end + + def user_already_exists? + users_output = shell_out!('/usr/bin/dscl', '.', '-list', '/users').stdout + users_output.split("\n").include?(new_resource.username) + end end action :create do execute "add user #{new_resource.username}" do - command "#{sysadminctl} -addUser #{new_resource.username} -password #{new_resource.password} #{admin_user?}" - not_if { ::File.exist? user_home } + command [sysadminctl, '-addUser', new_resource.username, *user_fullname, '-password', new_resource.password, admin_user] + not_if { ::File.exist?(user_home) && user_already_exists? } end if property_is_set?(:autologin) @@ -73,6 +83,6 @@ def admin_user? execute "delete user: #{user}" do command "#{sysadminctl} -deleteUser #{new_resource.username}" - only_if { "/usr/bin/dscl . -list /users | grep ^#{new_resource.username}$" } + only_if { user_already_exists? } end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6e059ed4..b09cc2b1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,6 +4,7 @@ require_relative '../libraries/macos_user' require_relative '../libraries/plist' require_relative '../libraries/xcode' +require_relative '../libraries/xcversion' RSpec.configure do |config| config.platform = 'mac_os_x' diff --git a/spec/unit/libraries/xcode_spec.rb b/spec/unit/libraries/xcode_spec.rb new file mode 100644 index 00000000..85d6581a --- /dev/null +++ b/spec/unit/libraries/xcode_spec.rb @@ -0,0 +1,55 @@ +require 'spec_helper' +include MacOS::Xcode + +describe MacOS::Xcode::Simulator do + context 'when provided an available list of simulators' do + before do + allow_any_instance_of(MacOS::Xcode::Simulator).to receive(:available_versions) + .and_return(<<-XCVERSION_OUTPUT + Xcode 9.2 (/Applications/Xcode-9.2.app) + iOS 8.1 Simulator (not installed) + iOS 8.2 Simulator (not installed) + iOS 8.3 Simulator (not installed) + iOS 8.4 Simulator (not installed) + iOS 9.0 Simulator (not installed) + iOS 9.1 Simulator (not installed) + iOS 9.2 Simulator (not installed) + iOS 9.3 Simulator (not installed) + iOS 10.0 Simulator (not installed) + iOS 10.1 Simulator (not installed) + tvOS 9.0 Simulator (not installed) + tvOS 9.1 Simulator (not installed) + tvOS 9.2 Simulator (not installed) + tvOS 10.0 Simulator (not installed) + watchOS 2.0 Simulator (not installed) + watchOS 2.1 Simulator (not installed) + watchOS 2.2 Simulator (not installed) + tvOS 10.1 Simulator (not installed) + iOS 10.2 Simulator (not installed) + watchOS 3.1 Simulator (not installed) + iOS 10.3.1 Simulator (not installed) + watchOS 3.2 Simulator (not installed) + tvOS 10.2 Simulator (not installed) + iOS 11.0 Simulator (not installed) + watchOS 4.0 Simulator (not installed) + tvOS 11.0 Simulator (not installed) + tvOS 11.1 Simulator (not installed) + watchOS 4.1 Simulator (not installed) + iOS 11.1 Simulator (not installed) + XCVERSION_OUTPUT + ) + end + it 'returns the latest semantic version of iOS 11' do + s = MacOS::Xcode::Simulator.new('11') + expect(s.version).to eq 'iOS 11.1' + end + it 'returns the latest semantic version of iOS 10' do + s = MacOS::Xcode::Simulator.new('10') + expect(s.version).to eq 'iOS 10.3.1' + end + it 'returns the latest semantic version of iOS 9' do + s = MacOS::Xcode::Simulator.new('9') + expect(s.version).to eq 'iOS 9.3' + end + end +end diff --git a/spec/unit/libraries/xcversion_spec.rb b/spec/unit/libraries/xcversion_spec.rb new file mode 100644 index 00000000..18cbf5b6 --- /dev/null +++ b/spec/unit/libraries/xcversion_spec.rb @@ -0,0 +1,56 @@ +require 'spec_helper' +include MacOS::XCVersion + +describe MacOS::XCVersion.install_xcode('9.0') do + context "when passed '9.0'" do + it 'returns the full xcversion install command for Xcode 9.0' do + expect(MacOS::XCVersion.install_xcode('9.0')) + .to eq "/opt/chef/embedded/bin/xcversion install '9'" + end + end +end + +describe MacOS::XCVersion.install_xcode('8.3.3') do + context "when passed '8.3.3'" do + it 'returns the full xcversion install command for Xcode 8.3.3' do + expect(MacOS::XCVersion.install_xcode('8.3.3')) + .to eq "/opt/chef/embedded/bin/xcversion install '8.3.3'" + end + end +end + +describe MacOS::XCVersion.install_xcode('8.2') do + context "when passed '8.2'" do + it 'returns the full xcversion install command for Xcode 8.2' do + expect(MacOS::XCVersion.install_xcode('8.2')) + .to eq "/opt/chef/embedded/bin/xcversion install '8.2'" + end + end +end + +describe MacOS::XCVersion.apple_pseudosemantic_version('9.0') do + context "when passed '9.0'" do + it "returns '9'" do + expect(MacOS::XCVersion.apple_pseudosemantic_version('9.0')) + .to eq '9' + end + end +end + +describe MacOS::XCVersion.apple_pseudosemantic_version('8.3.3') do + context "when passed '8.3.3'" do + it "returns '8.3.3'" do + expect(MacOS::XCVersion.apple_pseudosemantic_version('8.3.3')) + .to eq '8.3.3' + end + end +end + +describe MacOS::XCVersion.apple_pseudosemantic_version('8.2') do + context "when passed '8.2'" do + it "it returns '8.2'" do + expect(MacOS::XCVersion.apple_pseudosemantic_version('8.2')) + .to eq '8.2' + end + end +end diff --git a/test/cookbooks/macos_test/recipes/new_users.rb b/test/cookbooks/macos_test/recipes/new_users.rb index a12c4d6e..24ddb38e 100644 --- a/test/cookbooks/macos_test/recipes/new_users.rb +++ b/test/cookbooks/macos_test/recipes/new_users.rb @@ -7,5 +7,6 @@ macos_user 'create non-admin user johnny' do username 'johnny' + fullname 'Johnny Appleseed' password 'yang-yolked-cordon-karate' end diff --git a/test/cookbooks/macos_test/test/smoke/default/new_users_test.rb b/test/cookbooks/macos_test/test/smoke/default/new_users_test.rb index 4d6a0a53..2f786fa3 100644 --- a/test/cookbooks/macos_test/test/smoke/default/new_users_test.rb +++ b/test/cookbooks/macos_test/test/smoke/default/new_users_test.rb @@ -14,4 +14,10 @@ its('gid') { should eq 20 } its('home') { should eq '/Users/johnny' } end + + realname_cmd = 'dscl . read /Users/johnny RealName | grep -v RealName | cut -c 2-' + + describe command(realname_cmd) do + its('stdout.strip') { should eq 'Johnny Appleseed' } + end end