<commit_before>#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/type'
require 'puppet/provider/reboot/windows'

describe Puppet::Type.type(:reboot).provider(:windows), :if => Puppet.features.microsoft_windows? do
  let(:resource) { Puppet::Type.type(:reboot).new(:provider => :windows, :name => "windows_reboot") }
  let(:provider) { resource.provider}
  let(:native_path)     { "#{ENV['SYSTEMROOT']}\\sysnative\\shutdown.exe" }
  let(:redirected_path) { "#{ENV['SYSTEMROOT']}\\system32\\shutdown.exe" }

  it "should be an instance of Puppet::Type::Reboot::ProviderWindows" do
    provider.must be_an_instance_of Puppet::Type::Reboot::ProviderWindows
  end

  context "self.instances" do
    it "should return an empty array" do
      provider.class.instances.should == []
    end
  end

  context "when resolving the shutdown command" do
    it "should try to disable file system redirection" do
      File.expects(:exists?).with(native_path).returns(true)
      expect(provider.class.shutdown_command).to eq(native_path)
    end

    it "should fall back to system32" do
      File.expects(:exists?).with(native_path).returns(false)
      File.expects(:exists?).with(redirected_path).returns(true)
      expect(provider.class.shutdown_command).to eq(redirected_path)
    end

    it "should use 'shutdown.exe'" do
      File.expects(:exists?).with(native_path).returns(false)
      File.expects(:exists?).with(redirected_path).returns(false)
      expect(provider.class.shutdown_command).to eq('shutdown.exe')
    end

    it "should raise an error if shutdown.exe cannot be found" do
      provider.expects(:command).with(:shutdown).returns(nil)
      provider.expects(:async_shutdown).never

      expect {
        provider.reboot
      }.to raise_error(ArgumentError, 'The shutdown.exe command was not found. On Windows 2003 x64 hotfix 942589 must be installed to access the 64-bit version of shutdown.exe from 32-bit version of ruby.exe.')
    end
  end

  context "when checking if the `when` property is insync" do
    it "issues a shutdown command when a reboot is pending" do
      resource[:when] = :pending

      provider.expects(:command).with(:shutdown).returns(native_path)
      provider.expects(:async_shutdown).with(includes('shutdown.exe'))

      provider.when = :pending
    end
  end

  context "when a reboot is triggered" do
    it "does not include the interactive flag by default" do
      provider.expects(:async_shutdown).with(Not(includes('/i')))
      provider.reboot
    end

    it "includes the interactive flag if specified" do
      resource[:prompt] = true
      provider.expects(:async_shutdown).with(includes('/i'))<commit_msg>(#19162) Restrict example group to systems with the shutdown command<commit_after>#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/type'
require 'puppet/provider/reboot/windows'

describe Puppet::Type.type(:reboot).provider(:windows), :if => Puppet.features.microsoft_windows? do
  let(:resource) { Puppet::Type.type(:reboot).new(:provider => :windows, :name => "windows_reboot") }
  let(:provider) { resource.provider}
  let(:native_path)     { "#{ENV['SYSTEMROOT']}\\sysnative\\shutdown.exe" }
  let(:redirected_path) { "#{ENV['SYSTEMROOT']}\\system32\\shutdown.exe" }

  it "should be an instance of Puppet::Type::Reboot::ProviderWindows" do
    provider.must be_an_instance_of Puppet::Type::Reboot::ProviderWindows
  end

  context "self.instances" do
    it "should return an empty array" do
      provider.class.instances.should == []
    end
  end

  context "when resolving the shutdown command" do
    it "should try to disable file system redirection" do
      File.expects(:exists?).with(native_path).returns(true)
      expect(provider.class.shutdown_command).to eq(native_path)
    end

    it "should fall back to system32" do
      File.expects(:exists?).with(native_path).returns(false)
      File.expects(:exists?).with(redirected_path).returns(true)
      expect(provider.class.shutdown_command).to eq(redirected_path)
    end

    it "should use 'shutdown.exe'" do
      File.expects(:exists?).with(native_path).returns(false)
      File.expects(:exists?).with(redirected_path).returns(false)
      expect(provider.class.shutdown_command).to eq('shutdown.exe')
    end

    it "should raise an error if shutdown.exe cannot be found" do
      provider.expects(:command).with(:shutdown).returns(nil)
      provider.expects(:async_shutdown).never

      expect {
        provider.reboot
      }.to raise_error(ArgumentError, 'The shutdown.exe command was not found. On Windows 2003 x64 hotfix 942589 must be installed to access the 64-bit version of shutdown.exe from 32-bit version of ruby.exe.')
    end
  end

  context "when checking if the `when` property is insync" do
    it "issues a shutdown command when a reboot is pending" do
      resource[:when] = :pending

      provider.expects(:command).with(:shutdown).returns(native_path)
      provider.expects(:async_shutdown).with(includes('shutdown.exe'))

      provider.when = :pending
    end
  end

  context "when a reboot is triggered", :if => Puppet::Util.which('shutdown.exe') do
    it "does not include the interactive flag by default" do
      provider.expects(:async_shutdown).with(Not(includes('/i')))
      provider.reboot
    end

    it "includes the interactive flag if specified" do
      resource[:prompt] = true
      provider.expects(:async_shutdown).with(includes('/i'))
{'fraction_non_alphanumeric': 0.12344093910491563, 'fraction_numerical': 0.008253851797505502, 'mean_word_length': 3.998166819431714, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6803511', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1615, 'n_words': 408}