summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/commands/mspec_spec.rb
blob: 5d2134a0543595a33262fb0cccdd7713f1568edf (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
require 'spec_helper'
require 'yaml'
require 'mspec/commands/mspec'

describe MSpecMain, "#options" do
  before :each do
    @options, @config = new_option
    MSpecOptions.stub(:new).and_return(@options)

    @script = MSpecMain.new
    @script.stub(:config).and_return(@config)
    @script.stub(:load)
  end

  it "enables the configure option" do
    @options.should_receive(:configure)
    @script.options
  end

  it "provides a custom action (block) to the config option" do
    @script.options ["-B", "config"]
    @config[:options].should include("-B", "config")
  end

  it "loads the file specified by the config option" do
    @script.should_receive(:load).with("config")
    @script.options ["-B", "config"]
  end

  it "enables the target options" do
    @options.should_receive(:targets)
    @script.options
  end

  it "sets config[:options] to all argv entries that are not registered options" do
    @options.on "-X", "--exclude", "ARG", "description"
    @script.options [".", "-G", "fail", "-X", "ARG", "--list", "unstable", "some/file.rb"]
    @config[:options].should == [".", "-G", "fail", "--list", "unstable", "some/file.rb"]
  end

  it "calls #custom_options" do
    @script.should_receive(:custom_options).with(@options)
    @script.options
  end
end

describe MSpecMain, "#run" do
  before :each do
    @options, @config = new_option
    MSpecOptions.stub(:new).and_return(@options)
    @script = MSpecMain.new
    @script.stub(:config).and_return(@config)
    @script.stub(:exec)
    @err = $stderr
    $stderr = IOStub.new
  end

  after :each do
    $stderr = @err
  end

  it "uses exec to invoke the runner script" do
    @script.should_receive(:exec).with("ruby", "#{MSPEC_HOME}/bin/mspec-run")
    @script.options []
    @script.run
  end

  it "shows the command line on stderr" do
    @script.should_receive(:exec).with("ruby", "#{MSPEC_HOME}/bin/mspec-run")
    @script.options []
    @script.run
    $stderr.to_s.should == "$ ruby #{Dir.pwd}/bin/mspec-run\n"
  end

  it "adds config[:launch] to the exec options" do
    @script.should_receive(:exec).with("ruby",
        "-Xlaunch.option", "#{MSPEC_HOME}/bin/mspec-run")
    @config[:launch] << "-Xlaunch.option"
    @script.options []
    @script.run
    $stderr.to_s.should == "$ ruby -Xlaunch.option #{Dir.pwd}/bin/mspec-run\n"
  end

  it "calls #multi_exec if the command is 'ci' and the multi option is passed" do
    @script.should_receive(:multi_exec).and_return do |argv|
      argv.should == ["ruby", "#{MSPEC_HOME}/bin/mspec-ci"]
    end
    @script.options ["ci", "-j"]
    lambda do
      @script.run
    end.should raise_error(SystemExit)
  end
end

describe "The --warnings option" do
  before :each do
    @options, @config = new_option
    MSpecOptions.stub(:new).and_return(@options)
    @script = MSpecMain.new
    @script.stub(:config).and_return(@config)
  end

  it "is enabled by #options" do
    @options.stub(:on)
    @options.should_receive(:on).with("--warnings", an_instance_of(String))
    @script.options
  end

  it "sets flags to -w" do
    @config[:flags] = []
    @script.options ["--warnings"]
    @config[:flags].should include("-w")
  end

  it "set OUTPUT_WARNINGS = '1' in the environment" do
    ENV['OUTPUT_WARNINGS'] = '0'
    @script.options ["--warnings"]
    ENV['OUTPUT_WARNINGS'].should == '1'
  end
end

describe "The -j, --multi option" do
  before :each do
    @options, @config = new_option
    MSpecOptions.stub(:new).and_return(@options)
    @script = MSpecMain.new
    @script.stub(:config).and_return(@config)
  end

  it "is enabled by #options" do
    @options.stub(:on)
    @options.should_receive(:on).with("-j", "--multi", an_instance_of(String))
    @script.options
  end

  it "sets the multiple process option" do
    ["-j", "--multi"].each do |opt|
      @config[:multi] = nil
      @script.options [opt]
      @config[:multi].should == true
    end
  end
end

describe "The -h, --help option" do
  before :each do
    @options, @config = new_option
    MSpecOptions.stub(:new).and_return(@options)
    @script = MSpecMain.new
    @script.stub(:config).and_return(@config)
  end

  it "is enabled by #options" do
    @options.stub(:on)
    @options.should_receive(:on).with("-h", "--help", an_instance_of(String))
    @script.options
  end

  it "passes the option to the subscript" do
    ["-h", "--help"].each do |opt|
      @config[:options] = []
      @script.options ["ci", opt]
      @config[:options].sort.should == ["-h"]
    end
  end

  it "prints help and exits" do
    @script.should_receive(:puts).twice
    @script.should_receive(:exit).twice
    ["-h", "--help"].each do |opt|
      @script.options [opt]
    end
  end
end

describe "The -v, --version option" do
  before :each do
    @options, @config = new_option
    MSpecOptions.stub(:new).and_return(@options)
    @script = MSpecMain.new
    @script.stub(:config).and_return(@config)
  end

  it "is enabled by #options" do
    @options.stub(:on)
    @options.should_receive(:on).with("-v", "--version", an_instance_of(String))
    @script.options
  end

  it "passes the option to the subscripts" do
    ["-v", "--version"].each do |opt|
      @config[:options] = []
      @script.options ["ci", opt]
      @config[:options].sort.should == ["-v"]
    end
  end

  it "prints the version and exits if no subscript is invoked" do
    @config[:command] = nil
    File.stub(:basename).and_return("mspec")
    @script.should_receive(:puts).twice.with("mspec #{MSpec::VERSION}")
    @script.should_receive(:exit).twice
    ["-v", "--version"].each do |opt|
      @script.options [opt]
    end
  end
end