Permalink
Browse files

wip: start converting specs to One True Framework

  • Loading branch information...
1 parent 17b0972 commit f02f3759f138c9f28c744da0c0da3b26dc7b1631 @vito vito committed Mar 3, 2013
@@ -4,12 +4,8 @@
if ENV['VMC_TEST_USER'] && ENV['VMC_TEST_PASSWORD'] && ENV['VMC_TEST_TARGET']
describe 'A new user tries to use VMC against v1 production', :ruby19 => true do
- include ConsoleAppSpeckerMatchers
include VMC::Interactive
- let(:output) { StringIO.new }
- let(:out) { output.string.strip_progress_dots }
-
let(:target) { ENV['VMC_TEST_TARGET'] }
let(:username) { ENV['VMC_TEST_USER'] }
let(:password) { ENV['VMC_TEST_PASSWORD'] }
View
@@ -26,6 +26,8 @@ def vmc_bin
RSpec.configure do |c|
c.include Fake::FakeMethods
c.include V1Fake::FakeMethods
+ c.include ConsoleAppSpeckerMatchers
+
c.mock_with :rr
if RUBY_VERSION =~ /^1\.8\.\d/
@@ -46,18 +48,6 @@ def vmc_bin
end
end
-class String
- def strip_heredoc
- min = scan(/^[ \t]*(?=\S)/).min
- indent = min ? min.size : 0
- gsub(/^[ \t]{#{indent}}/, '')
- end
-
- def strip_progress_dots
- gsub(/\. \x08([\x08\. ]+)/, "... ")
- end
-end
-
def name_list(xs)
if xs.empty?
"none"
@@ -66,19 +56,6 @@ def name_list(xs)
end
end
-def invoke_cli(cli, *args)
- stub.proxy(cli).invoke.with_any_args
- stub(cli.class).new { cli }
- cli.invoke(*args)
-end
-
-def stub_output(cli)
- stub(cli).print
- stub(cli).puts
- stub(Interact::Progress::Dots).start!
- stub(Interact::Progress::Dots).stop!
-end
-
def run(command)
SpeckerRunner.new(command) do |runner|
yield runner
@@ -1,32 +1,79 @@
+def command(klass, &specs)
+ describe klass do
+ before do
+ any_instance_of klass do |cli|
+ stub(cli).precondition
+ stub(cli).client { client }
+ end
+ end
+
+ before(:all) do
+ klass.class_eval do
+ def wrap_errors
+ yield
+ rescue VMC::UserError => e
+ err e.message
+ end
+ end
+ end
+
+ after(:all) do
+ klass.class_eval do
+ remove_method :wrap_errors
+ end
+ end
+
+ class_eval(&specs)
+ end
+end
+
module CommandHelper
- def vmc(argv)
+ def vmc(argv, script = false)
Mothership.new.exit_status 0
stub(VMC::CLI).exit { |code| code }
- capture_output { VMC::CLI.start(argv + ["--debug"]) }
- end
-
- def expect_status_and_output(status = 0, out = "", err = "")
- expect([
- status,
- stdout.string.strip_progress_dots,
- stderr.string.strip_progress_dots
- ]).to eq([status, out, err])
+ capture_output { VMC::CLI.start(argv + ["--debug", "--no-script"]) }
end
def bool_flag(flag)
"#{'no-' unless send(flag)}#{flag.to_s.gsub('_', '-')}"
end
- attr_reader :stdout, :stderr, :status
+ attr_reader :stdout, :stderr, :stdin, :status
def capture_output
$real_stdout = $stdout
$real_stderr = $stderr
+ $real_stdin = $stdin
$stdout = @stdout = StringIO.new
$stderr = @stderr = StringIO.new
+ $stdin = @stdin = StringIO.new
@status = yield
+ @stdout.rewind
+ @stderr.rewind
+ @status
ensure
$stdout = $real_stdout
$stderr = $real_stderr
+ $stdin = $real_stdin
+ end
+
+ def output
+ @output ||= TrackingExpector.new(stdout)
+ end
+
+ def error_output
+ @error_output ||= TrackingExpector.new(stderr)
+ end
+
+ def mock_invoke(*args)
+ any_instance_of described_class do |cli|
+ mock(cli).invoke *args
+ end
+ end
+
+ def dont_allow_invoke(*args)
+ any_instance_of described_class do |cli|
+ dont_allow(cli).invoke *args
+ end
end
end
@@ -50,16 +50,16 @@
it 'prints out the instances in the correct order' do
subject
- expect(stdout.string).to match /.*instance \#1.*instance \#2.*instance \#12.*/m
+ expect(output).to say("instance #1")
+ expect(output).to say("instance #2")
+ expect(output).to say("instance #12")
end
it 'prints out one of the instances correctly' do
subject
- expect(stdout.string).to include <<-OUT.strip_heredoc
- instance #2: started
- started: #{time.strftime("%F %r")}
- debugger: port bar at foo
- console: port qux at baz
- OUT
+ expect(output).to say("instance #2: started")
+ expect(output).to say(" started: #{time.strftime("%F %r")}")
+ expect(output).to say(" debugger: port bar at foo")
+ expect(output).to say(" console: port qux at baz")
end
end
@@ -1,23 +1,14 @@
require "spec_helper"
require "webmock/rspec"
-describe VMC::App::Scale do
- let(:global) { { :color => false } }
- let(:given) { {} }
- let(:client) { fake_client }
- let!(:cli) { described_class.new }
-
- before do
- stub(cli).client { client }
- stub_output(cli)
- end
-
- subject { invoke_cli(cli, :scale, inputs, given, global) }
+command VMC::App::Scale do
+ let(:client) { fake_client :apps => [app] }
context "when the --disk flag is given" do
let(:before_value) { 512 }
let(:app) { fake :app, :disk_quota => before_value }
- let(:inputs) { { :app => app, :disk => "1G" } }
+
+ subject { vmc %W[scale #{app.name} --disk 1G] }
it "changes the application's disk quota" do
mock(app).update!
@@ -28,18 +19,20 @@
context "when the --memory flag is given" do
let(:before_value) { 512 }
let(:app) { fake :app, :memory => before_value }
- let(:inputs) { { :app => app, :memory => "1G" } }
+
+ subject { vmc %W[scale #{app.name} --memory 1G] }
it "changes the application's memory" do
mock(app).update!
expect { subject }.to change(app, :memory).from(before_value).to(1024)
end
+ # TODO: determine if the command should do this on v2
context "if --restart is true" do
it "restarts the application" do
stub(app).update!
stub(app).started? { true }
- mock(cli).invoke :restart, :app => app
+ mock_invoke :restart, :app => app
subject
end
end
@@ -49,7 +42,7 @@
let(:before_value) { 3 }
let(:app) { fake :app, :total_instances => before_value }
- let(:inputs) { { :app => app, :instances => 5 } }
+ subject { vmc %W[scale #{app.name} --instances 5] }
it "changes the application's number of instances" do
mock(app).update!
@@ -60,7 +53,8 @@
context "when the --plan flag is given" do
context "when the plan name begins with a 'p'" do
let(:app) { fake :app, :production => false }
- let(:inputs) { { :app => app, :plan => "P100" } }
+
+ subject { vmc %W[scale #{app.name} --plan P100] }
it "changes the application's 'production' flag to true" do
mock(app).update!
@@ -70,7 +64,8 @@
context "when the plan name does not begin with a 'p'" do
let(:app) { fake :app, :production => true }
- let(:inputs) { { :app => app, :plan => "D100" } }
+
+ subject { vmc %W[scale #{app.name} --plan D100] }
it "changes the application's 'production' flag to false" do
mock(app).update!
@@ -1,44 +1,11 @@
require "spec_helper"
require "webmock/rspec"
-describe VMC::App::Start do
- include ConsoleAppSpeckerMatchers
-
+command VMC::App::Start do
let(:client) { fake_client :apps => [app] }
let(:app) { fake :app }
- def output
- stdout.rewind
- TrackingExpector.new(stdout)
- end
-
- def error_output
- stderr.rewind
- TrackingExpector.new(stderr)
- end
-
- before do
- any_instance_of described_class do |cli|
- stub(cli).precondition
- stub(cli).client { client }
- end
- end
-
- before(:all) do
- described_class.class_eval do
- def wrap_errors
- yield
- end
- end
- end
-
- after(:all) do
- described_class.class_eval do
- remove_method :wrap_errors
- end
- end
-
- subject { vmc %W[start #{app.name} --no-quiet] }
+ subject { vmc %W[start #{app.name}] }
context "with an app that's already started" do
let(:app) { fake :app, :state => "STARTED" }
@@ -198,4 +165,4 @@ def after_sleep
it_waits_for_application_to_become_healthy
end
end
-end
+end
Oops, something went wrong.

0 comments on commit f02f375

Please sign in to comment.