<commit_before>require 'spec_helper'

describe Mixlib::ShellOut::Windows, :windows_only => true do

  # Caveat: Private API methods are subject to change without notice.
  # Monkeypatch at your own risk.
  context 'for private API methods' do

    describe '::IS_BATCH_FILE' do
      subject { candidate =~ Mixlib::ShellOut::Windows::IS_BATCH_FILE }

      def self.with_candidate(_context, _options = {}, &example)
        context "with #{_context}" do
          let(:candidate) { _options[:candidate] }
          it(&example)
        end
      end

      with_candidate('valid .bat file', :candidate => 'autoexec.bat') { should be_true }
      with_candidate('valid .cmd file', :candidate => 'autoexec.cmd') { should be_true }
      with_candidate('valid quoted .bat file', :candidate => '"C:\Program Files\autoexec.bat"') { should be_true }
      with_candidate('valid quoted .cmd file', :candidate => '"C:\Program Files\autoexec.cmd"') { should be_true }

      with_candidate('invalid .bat file', :candidate => 'autoexecbat') { should_not be_true }
      with_candidate('invalid .cmd file', :candidate => 'autoexeccmd') { should_not be_true }
      with_candidate('bat in filename', :candidate => 'abattoir.exe') { should_not be_true }
      with_candidate('cmd in filename', :candidate => 'parse_cmd.exe') { should_not be_true }

      with_candidate('invalid quoted .bat file', :candidate => '"C:\Program Files\autoexecbat"') { should_not be_true }
      with_candidate('invalid quoted .cmd file', :candidate => '"C:\Program Files\autoexeccmd"') { should_not be_true }
      with_candidate('quoted bat in filename', :candidate => '"C:\Program Files\abattoir.exe"') { should_not be_true }
      with_candidate('quoted cmd in filename', :candidate => '"C:\Program Files\parse_cmd.exe"') { should_not be_true }
    end

    describe '#command_to_run' do
      subject { stubbed_shell_out.send(:command_to_run, cmd) }

      let(:stubbed_shell_out) { fail NotImplemented, 'Must declare let(:stubbed_shell_out)' }
      let(:shell_out) { Mixlib::ShellOut.new(cmd) }

      let(:with_valid_exe_at_location) { lambda { |s| s.stub!(:find_exe_at_location).and_return(executable_path) } }
      let(:with_invalid_exe_at_location) { lambda { |s| s.stub!(:find_exe_at_location).and_return(nil) } }

      context 'with batch files' do
        let(:stubbed_shell_out) { shell_out.tap(&with_valid_exe_at_location) }
        let(:cmd_invocation) { "cmd /c \"#{cmd}\"" }
        let(:cmd_exe) { "C:\\Windows\\system32\\cmd.exe" }
        let(:cmd) { "#{executable_path}" }

        context 'with .bat file' do
          let(:executable_path) { '"C:\Program Files\Application\Start.bat"' }

          # Examples taken from: https://github.com/opscode/mixlib-shellout/pull/2#issuecomment-4825574
          context 'with executable path enclosed in double quotes' do

            it 'should use specified batch file' do
              should eql([cmd_exe, cmd_invocation])
            end<commit_msg>[CHEF-2994][WINDOWS] Added specs for empty command<commit_after>require 'spec_helper'

describe Mixlib::ShellOut::Windows, :windows_only => true do

  # Caveat: Private API methods are subject to change without notice.
  # Monkeypatch at your own risk.
  context 'for private API methods' do

    describe '::IS_BATCH_FILE' do
      subject { candidate =~ Mixlib::ShellOut::Windows::IS_BATCH_FILE }

      def self.with_candidate(_context, _options = {}, &example)
        context "with #{_context}" do
          let(:candidate) { _options[:candidate] }
          it(&example)
        end
      end

      with_candidate('valid .bat file', :candidate => 'autoexec.bat') { should be_true }
      with_candidate('valid .cmd file', :candidate => 'autoexec.cmd') { should be_true }
      with_candidate('valid quoted .bat file', :candidate => '"C:\Program Files\autoexec.bat"') { should be_true }
      with_candidate('valid quoted .cmd file', :candidate => '"C:\Program Files\autoexec.cmd"') { should be_true }

      with_candidate('invalid .bat file', :candidate => 'autoexecbat') { should_not be_true }
      with_candidate('invalid .cmd file', :candidate => 'autoexeccmd') { should_not be_true }
      with_candidate('bat in filename', :candidate => 'abattoir.exe') { should_not be_true }
      with_candidate('cmd in filename', :candidate => 'parse_cmd.exe') { should_not be_true }

      with_candidate('invalid quoted .bat file', :candidate => '"C:\Program Files\autoexecbat"') { should_not be_true }
      with_candidate('invalid quoted .cmd file', :candidate => '"C:\Program Files\autoexeccmd"') { should_not be_true }
      with_candidate('quoted bat in filename', :candidate => '"C:\Program Files\abattoir.exe"') { should_not be_true }
      with_candidate('quoted cmd in filename', :candidate => '"C:\Program Files\parse_cmd.exe"') { should_not be_true }
    end

    describe '#command_to_run' do
      subject { stubbed_shell_out.send(:command_to_run, cmd) }

      let(:stubbed_shell_out) { fail NotImplemented, 'Must declare let(:stubbed_shell_out)' }
      let(:shell_out) { Mixlib::ShellOut.new(cmd) }

      let(:with_valid_exe_at_location) { lambda { |s| s.stub!(:find_exe_at_location).and_return(executable_path) } }
      let(:with_invalid_exe_at_location) { lambda { |s| s.stub!(:find_exe_at_location).and_return(nil) } }

      context 'with empty command' do
        let(:stubbed_shell_out) { shell_out }
        let(:cmd) { ' ' }

        it 'should return with a nil executable' do
          should eql([nil, cmd])
        end
      end

      context 'with batch files' do
        let(:stubbed_shell_out) { shell_out.tap(&with_valid_exe_at_location) }
        let(:cmd_invocation) { "cmd /c \"#{cmd}\"" }
        let(:cmd_exe) { "C:\\Windows\\system32\\cmd.exe" }
        let(:cmd) { "#{executable_path}" }

        context 'with .bat file' do
          let(:executable_path) { '"C:\Program Files\Application\Start.bat"' }

          # Examples taken from: https://github.com/opscode/mixlib-shellout/pull/2#issuecomment-4825574
          context 'with executable path enclosed in double quotes' do

            it 'should use specified batch file' do
              should eql([cmd_exe, cmd_invocation])
            end
{'fraction_non_alphanumeric': 0.13386837881219904, 'fraction_numerical': 0.0038523274478330658, 'mean_word_length': 3.5021676300578033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 2, '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': '3206959', 'n_tokens_mistral': 2022, 'n_tokens_neox': 1947, 'n_words': 451}