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 257 258 259 260 261 262 263 264 265 266 267
|
require 'spec_helper'
describe Fission::CommandLineParser do
before do
@string_io = StringIO.new
Fission::CommandLineParser.any_instance.
stub(:ui).
and_return(Fission::UI.new(@string_io))
end
describe 'initialize' do
it 'should set command to nil' do
Fission::CommandLineParser.new.command.should be_nil
end
end
describe 'parse' do
context 'with no initial arguments' do
it 'should output the usage info' do
lambda {
Fission::CommandLineParser.new([]).parse
}.should raise_error SystemExit
@string_io.string.should match /Usage/
end
end
context 'with -v or --version initial arguments' do
['-v', '--version'].each do |arg|
it "should output the version with #{arg}" do
lambda {
Fission::CommandLineParser.new([arg]).parse
}.should raise_error SystemExit
@string_io.string.should match /#{Fission::VERSION}/
end
end
end
context 'with -h or --help initial arguments' do
['-h', '--help'].each do |arg|
it "should output the usage info with #{arg}" do
lambda {
Fission::CommandLineParser.new([arg]).parse
}.should raise_error SystemExit
@string_io.string.should match /Usage/
end
end
end
context 'with an invalid sub command' do
it 'should display the help' do
lambda {
Fission::CommandLineParser.new(['foo', 'bar']).parse
}.should raise_error SystemExit
@string_io.string.should match /Usage/
end
end
context 'with a valid sub command' do
before do
@cmd_mock = double('command', :summary => '')
end
[ ['clone'],
['delete'],
['snapshot', 'create'],
['snapshot', 'delete'],
['snapshot', 'list'],
['snapshot', 'revert'],
['start'],
['status'],
['stop'],
['suspend']
].each do |command|
it "should accept #{command}" do
Fission::CommandLineParser.new(command).parse
end
it "should populate @command with an instance of the '#{command.join(' ')}' class" do
klass = command.map { |c| c.capitalize }.join('')
parser = Fission::CommandLineParser.new(command).parse
parser.command.should be_an_instance_of Fission::Command.const_get klass
end
end
context 'clone' do
before do
@cmd_mock.stub(:command_name).and_return('clone')
Fission::Command::Clone.should_receive(:new).and_return(@cmd_mock)
end
it 'should create the command' do
Fission::Command::Clone.should_receive(:new).
with(['foo', 'bar'])
Fission::CommandLineParser.new(['clone', 'foo', 'bar']).parse
end
context 'with --start' do
it 'should create the command with the start option' do
Fission::Command::Clone.should_receive(:new).
with(['foo', 'bar', '--start'])
Fission::CommandLineParser.new(['clone', 'foo', 'bar', '--start']).parse
end
end
end
context 'delete' do
before do
@cmd_mock.stub(:command_name).and_return('delete')
Fission::Command::Delete.should_receive(:new).and_return(@cmd_mock)
end
it 'should create the command' do
Fission::Command::Delete.should_receive(:new).
with(['foo'])
Fission::CommandLineParser.new(['delete', 'foo']).parse
end
context 'with --force' do
it 'should create the command with the force option' do
Fission::Command::Delete.should_receive(:new).
with(['foo', '--force'])
Fission::CommandLineParser.new(['delete', 'foo', '--force']).parse
end
end
end
context 'snapshot create' do
before do
@cmd_mock.stub(:command_name).and_return('snapshot create')
Fission::Command::SnapshotCreate.should_receive(:new).and_return(@cmd_mock)
end
it 'should create the command' do
Fission::Command::SnapshotCreate.should_receive(:new).
with(['foo', 'bar'])
Fission::CommandLineParser.new(['snapshot', 'create', 'foo', 'bar']).parse
end
end
context 'snapshot delete' do
before do
@cmd_mock.stub(:command_name).and_return('snapshot delete')
Fission::Command::SnapshotDelete.should_receive(:new).and_return(@cmd_mock)
end
it 'should create the command' do
Fission::Command::SnapshotDelete.should_receive(:new).
with(['foo', 'bar'])
Fission::CommandLineParser.new(['snapshot', 'delete', 'foo', 'bar']).parse
end
end
context 'snapshot list' do
before do
@cmd_mock.stub(:command_name).and_return('snapshot list')
Fission::Command::SnapshotList.should_receive(:new).and_return(@cmd_mock)
end
it 'should create the command' do
Fission::Command::SnapshotList.should_receive(:new).
with(['foo'])
Fission::CommandLineParser.new(['snapshot', 'list', 'foo']).parse
end
end
context 'snapshot revert' do
before do
@cmd_mock.stub(:command_name).and_return('snapshot revert')
Fission::Command::SnapshotRevert.should_receive(:new).and_return(@cmd_mock)
end
it 'should create the command' do
Fission::Command::SnapshotRevert.should_receive(:new).
with(['foo', 'bar'])
Fission::CommandLineParser.new(['snapshot', 'revert', 'foo', 'bar']).parse
end
end
context 'start' do
before do
@cmd_mock.stub(:command_name).and_return('start')
Fission::Command::Start.should_receive(:new).and_return(@cmd_mock)
end
it 'should create the command' do
Fission::Command::Start.should_receive(:new).
with(['foo'])
Fission::CommandLineParser.new(['start', 'foo']).parse
end
context 'with --headless' do
it 'should create the command with the force option' do
Fission::Command::Start.should_receive(:new).
with(['foo', '--headless'])
Fission::CommandLineParser.new(['start', 'foo', '--headless']).parse
end
end
end
context 'status' do
before do
@cmd_mock.stub(:command_name).and_return('status')
Fission::Command::Status.should_receive(:new).and_return(@cmd_mock)
end
it 'should create the command' do
Fission::Command::Status.should_receive(:new).
with([])
Fission::CommandLineParser.new(['status']).parse
end
end
context 'stop' do
before do
@cmd_mock.stub(:command_name).and_return('stop')
Fission::Command::Stop.should_receive(:new).and_return(@cmd_mock)
end
it 'should create the command' do
Fission::Command::Stop.should_receive(:new).
with(['foo'])
Fission::CommandLineParser.new(['stop', 'foo']).parse
end
context 'with --force' do
it 'should create the command with the force option' do
Fission::Command::Stop.should_receive(:new).
with(['foo', '--force'])
Fission::CommandLineParser.new(['stop', 'foo', '--force']).parse
end
end
end
context 'suspend' do
before do
@cmd_mock.stub(:command_name).and_return('suspend')
Fission::Command::Suspend.should_receive(:new).and_return(@cmd_mock)
end
it 'should create the command' do
Fission::Command::Suspend.should_receive(:new).
with(['foo'])
Fission::CommandLineParser.new(['suspend', 'foo']).parse
end
context 'with --all' do
it 'should create the command with the all option' do
Fission::Command::Suspend.should_receive(:new).
with(['--all'])
Fission::CommandLineParser.new(['suspend', '--all']).parse
end
end
end
end
end
end
|