summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/helpers/ruby_exe_spec.rb
blob: 79ce55ca75644c89e17902985151f5954c44eb85 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
require 'spec_helper'
require 'mspec/guards'
require 'mspec/helpers'
require 'rbconfig'

class RubyExeSpecs
  public :ruby_exe_options
  public :resolve_ruby_exe
  public :ruby_cmd
  public :ruby_exe
end

RSpec.describe "#ruby_exe_options" do
  before :each do
    @ruby_exe_env = ENV['RUBY_EXE']
    @script = RubyExeSpecs.new
  end

  after :each do
    ENV['RUBY_EXE'] = @ruby_exe_env
  end

  it "returns ENV['RUBY_EXE'] when passed :env" do
    ENV['RUBY_EXE'] = "kowabunga"
    expect(@script.ruby_exe_options(:env)).to eq("kowabunga")
  end

  it "returns 'bin/jruby' when passed :engine and RUBY_ENGINE is 'jruby'" do
    stub_const "RUBY_ENGINE", 'jruby'
    expect(@script.ruby_exe_options(:engine)).to eq('bin/jruby')
  end

  it "returns 'bin/rbx' when passed :engine, RUBY_ENGINE is 'rbx'" do
    stub_const "RUBY_ENGINE", 'rbx'
    expect(@script.ruby_exe_options(:engine)).to eq('bin/rbx')
  end

  it "returns 'ir' when passed :engine and RUBY_ENGINE is 'ironruby'" do
    stub_const "RUBY_ENGINE", 'ironruby'
    expect(@script.ruby_exe_options(:engine)).to eq('ir')
  end

  it "returns 'maglev-ruby' when passed :engine and RUBY_ENGINE is 'maglev'" do
    stub_const "RUBY_ENGINE", 'maglev'
    expect(@script.ruby_exe_options(:engine)).to eq('maglev-ruby')
  end

  it "returns 'topaz' when passed :engine and RUBY_ENGINE is 'topaz'" do
    stub_const "RUBY_ENGINE", 'topaz'
    expect(@script.ruby_exe_options(:engine)).to eq('topaz')
  end

  it "returns RUBY_ENGINE + $(EXEEXT) when passed :name" do
    bin = RUBY_ENGINE + (RbConfig::CONFIG['EXEEXT'] || RbConfig::CONFIG['exeext'] || '')
    name = File.join ".", bin
    expect(@script.ruby_exe_options(:name)).to eq(name)
  end

  it "returns $(bindir)/$(RUBY_INSTALL_NAME) + $(EXEEXT) when passed :install_name" do
    bin = RbConfig::CONFIG['RUBY_INSTALL_NAME'] + (RbConfig::CONFIG['EXEEXT'] || RbConfig::CONFIG['exeext'] || '')
    name = File.join RbConfig::CONFIG['bindir'], bin
    expect(@script.ruby_exe_options(:install_name)).to eq(name)
  end
end

RSpec.describe "#resolve_ruby_exe" do
  before :each do
    @name = "ruby_spec_exe"
    @script = RubyExeSpecs.new
  end

  it "returns the value returned by #ruby_exe_options if it exists and is executable" do
    expect(@script).to receive(:ruby_exe_options).and_return(@name)
    expect(File).to receive(:file?).with(@name).and_return(true)
    expect(File).to receive(:executable?).with(@name).and_return(true)
    expect(File).to receive(:expand_path).with(@name).and_return(@name)
    expect(@script.resolve_ruby_exe).to eq(@name)
  end

  it "expands the path portion of the result of #ruby_exe_options" do
    expect(@script).to receive(:ruby_exe_options).and_return("#{@name}")
    expect(File).to receive(:file?).with(@name).and_return(true)
    expect(File).to receive(:executable?).with(@name).and_return(true)
    expect(File).to receive(:expand_path).with(@name).and_return("/usr/bin/#{@name}")
    expect(@script.resolve_ruby_exe).to eq("/usr/bin/#{@name}")
  end

  it "adds the flags after the executable" do
    @name = 'bin/rbx'
    expect(@script).to receive(:ruby_exe_options).and_return(@name)
    expect(File).to receive(:file?).with(@name).and_return(true)
    expect(File).to receive(:executable?).with(@name).and_return(true)
    expect(File).to receive(:expand_path).with(@name).and_return(@name)

    expect(ENV).to receive(:[]).with("RUBY_FLAGS").and_return('-X19')
    expect(@script.resolve_ruby_exe).to eq('bin/rbx -X19')
  end

  it "raises an exception if no exe is found" do
    expect(File).to receive(:file?).at_least(:once).and_return(false)
    expect {
      @script.resolve_ruby_exe
    }.to raise_error(Exception)
  end
end

RSpec.describe Object, "#ruby_cmd" do
  before :each do
    stub_const 'RUBY_EXE', 'ruby_spec_exe -w -Q'

    @file = "some/ruby/file.rb"
    @code = %(some "real" 'ruby' code)

    @script = RubyExeSpecs.new
  end

  it "returns a command that runs the given file if it is a file that exists" do
    expect(File).to receive(:exist?).with(@file).and_return(true)
    expect(@script.ruby_cmd(@file)).to eq("ruby_spec_exe -w -Q some/ruby/file.rb")
  end

  it "includes the given options and arguments with a file" do
    expect(File).to receive(:exist?).with(@file).and_return(true)
    expect(@script.ruby_cmd(@file, :options => "-w -Cdir", :args => "< file.txt")).to eq(
      "ruby_spec_exe -w -Q -w -Cdir some/ruby/file.rb < file.txt"
    )
  end

  it "includes the given options and arguments with -e" do
    expect(File).to receive(:exist?).with(@code).and_return(false)
    expect(@script.ruby_cmd(@code, :options => "-W0 -Cdir", :args => "< file.txt")).to eq(
      %(ruby_spec_exe -w -Q -W0 -Cdir -e "some \\"real\\" 'ruby' code" < file.txt)
    )
  end

  it "returns a command with options and arguments but without code or file" do
    expect(@script.ruby_cmd(nil, :options => "-c", :args => "> file.txt")).to eq(
      "ruby_spec_exe -w -Q -c > file.txt"
    )
  end
end

RSpec.describe Object, "#ruby_exe" do
  before :each do
    stub_const 'RUBY_EXE', 'ruby_spec_exe -w -Q'

    @script = RubyExeSpecs.new
    allow(@script).to receive(:`).and_return('OUTPUT')

    status_successful = double(Process::Status,  exitstatus: 0)
    allow(Process).to receive(:last_status).and_return(status_successful)
  end

  it "returns command STDOUT when given command" do
    code = "code"
    options = {}
    output = "output"
    allow(@script).to receive(:`).and_return(output)

    expect(@script.ruby_exe(code, options)).to eq output
  end

  it "returns an Array containing the interpreter executable and flags when given no arguments" do
    expect(@script.ruby_exe).to eq(['ruby_spec_exe', '-w', '-Q'])
  end

  it "executes (using `) the result of calling #ruby_cmd with the given arguments" do
    code = "code"
    options = {}
    expect(@script).to receive(:ruby_cmd).and_return("ruby_cmd")
    expect(@script).to receive(:`).with("ruby_cmd")
    @script.ruby_exe(code, options)
  end

  it "raises exception when command exit status is not successful" do
    code = "code"
    options = {}

    status_failed = double(Process::Status, exitstatus: 4)
    allow(Process).to receive(:last_status).and_return(status_failed)

    expect {
      @script.ruby_exe(code, options)
    }.to raise_error(%r{Expected exit status is 0 but actual is 4 for command ruby_exe\(.+\)})
  end

  it "shows in the exception message if exitstatus is nil (e.g., signal)" do
    code = "code"
    options = {}

    status_failed = double(Process::Status, exitstatus: nil)
    allow(Process).to receive(:last_status).and_return(status_failed)

    expect {
      @script.ruby_exe(code, options)
    }.to raise_error(%r{Expected exit status is 0 but actual is nil for command ruby_exe\(.+\)})
  end

  describe "with :dir option" do
    it "is deprecated" do
      expect {
        @script.ruby_exe nil, :dir => "tmp"
      }.to raise_error(/no longer supported, use Dir\.chdir/)
    end
  end

  describe "with :env option" do
    it "preserves the values of existing ENV keys" do
      ENV["ABC"] = "123"
      allow(ENV).to receive(:[])
      expect(ENV).to receive(:[]).with("ABC")
      @script.ruby_exe nil, :env => { :ABC => "xyz" }
    end

    it "adds the :env entries to ENV" do
      expect(ENV).to receive(:[]=).with("ABC", "xyz")
      @script.ruby_exe nil, :env => { :ABC => "xyz" }
    end

    it "deletes the :env entries in ENV when an exception is raised" do
      expect(ENV).to receive(:delete).with("XYZ")
      @script.ruby_exe nil, :env => { :XYZ => "xyz" }
    end

    it "resets the values of existing ENV keys when an exception is raised" do
      ENV["ABC"] = "123"
      expect(ENV).to receive(:[]=).with("ABC", "xyz")
      expect(ENV).to receive(:[]=).with("ABC", "123")

      expect(@script).to receive(:`).and_raise(Exception)
      expect do
        @script.ruby_exe nil, :env => { :ABC => "xyz" }
      end.to raise_error(Exception)
    end
  end

  describe "with :exit_status option" do
    before do
      status_failed = double(Process::Status, exitstatus: 4)
      allow(Process).to receive(:last_status).and_return(status_failed)
    end

    it "raises exception when command ends with not expected status" do
      expect {
        @script.ruby_exe("path", exit_status: 1)
      }.to raise_error(%r{Expected exit status is 1 but actual is 4 for command ruby_exe\(.+\)})
    end

    it "does not raise exception when command ends with expected status" do
      output = "output"
      allow(@script).to receive(:`).and_return(output)

      expect(@script.ruby_exe("path", exit_status: 4)).to eq output
    end
  end
end