Chefspec Windows 'require 'win32ole' stubbing

Hi All,

Currently Using the Windows Cookbook to get the ‘windows_shortcut’ Custom Resource. Using a variation os the example as follows:

require 'win32ole’
all_users_desktop = WIN32OLE.new(“WScript.Shell”).SpecialFolders(“AllUsersDesktop”)

windows_shortcut “#{all_users_desktop}/Notepad.lnk” do
target "C:\WINDOWS\notepad.exe"
description "Launch Notepad"
iconlocation "C:\windows\notepad.exe, 0"
end

This converges successfully, its a nice resource.

However I need to write tests for this using chefspec, which currently doesnt like:

require 'win32ole

How do you get chefspec to stub or pull in this library so I can write unit tests?

Thanks Luke.

If Chef can load the win32ole livrary then you should also be able to require ‘win32ole’.

We have a situation whereby we write cookbooks on Windows but our build/ci is Linux based.

For CI the win32ole library is not present, we have never found a way to stub Kernel.require but for good reason, if we did the tests will likely break wholesale as Ruby will not be able to load the libraries in memory.

So instead we have a few solutions, the easiest is to

  1. stub contants like WIN32 similar to https://github.com/chef-cookbooks/chef-client/issues/212
  2. put the require ‘library’ code into a method and stub the method call
1 Like

Hi Chris,

Thanks very much ! This helps me a lot.

Didn’t realise there was an official limitation, we are a Linux shop that is squeezing a new windows project into our existing pipeline.

Appreciate you taking the time to share your expertise.

Kind Regards,

Luke.