summaryrefslogtreecommitdiff
path: root/spec/mixlib/shellout/windows_spec.rb
blob: a5586f71b3d25017e3728536180d18aaccfc93cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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 '#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

            context 'with arguments' do
              let(:cmd) { "#{executable_path} arguments" }

              it 'should use specified executable' do
                should eql([cmd_exe, cmd_invocation])
              end
            end

            context 'with quoted arguments' do
              let(:cmd) { "#{executable_path} /i \"C:\Program Files (x86)\NUnit 2.6\bin\framework\nunit.framework.dll\"" }

              it 'should use specified executable' do
                should eql([cmd_exe, cmd_invocation])
              end
            end
          end
        end

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

          # 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

            context 'with arguments' do
              let(:cmd) { "#{executable_path} arguments" }

              it 'should use specified executable' do
                should eql([cmd_exe, cmd_invocation])
              end
            end

            context 'with quoted arguments' do
              let(:cmd) { "#{executable_path} /i \"C:\Program Files (x86)\NUnit 2.6\bin\framework\nunit.framework.dll\"" }

              it 'should use specified executable' do
                should eql([cmd_exe, cmd_invocation])
              end
            end
          end

        end
      end

      context 'with valid executable at location' do
        let(:stubbed_shell_out) { shell_out.tap(&with_valid_exe_at_location) }

        # Examples taken from: https://github.com/opscode/mixlib-shellout/pull/2#issuecomment-4825574
        context 'with executable path enclosed in double quotes' do
          let(:cmd) { "#{executable_path}" }
          let(:executable_path) { '"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe"' }

          it 'should use specified executable' do
            should eql([executable_path, cmd])
          end

          context 'with arguments' do
            let(:cmd) { "#{executable_path} arguments" }

            it 'should use specified executable' do
              should eql([executable_path, cmd])
            end
          end

          context 'with quoted arguments' do
            let(:cmd) { "#{executable_path} /i \"C:\Program Files (x86)\NUnit 2.6\bin\framework\nunit.framework.dll\"" }

            it 'should use specified executable' do
              should eql([executable_path, cmd])
            end
          end
        end

      end
    end
  end
end