diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index c4b176a..bfcab5f 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -37,16 +37,15 @@ describe Rscons do end before(:each) do - @output = "" - $stdout.stub(:write) do |content| - @output += content - end - $stderr.stub(:write) do |content| - @output += content - end + @saved_stdout = $stdout + $stdout = StringIO.new + @saved_stderr = $stderr + $stderr = StringIO.new end after(:each) do + $stdout = @saved_stdout + $stderr = @saved_stderr Dir.chdir(@owd) rm_rf(BUILD_TEST_RUN_DIR) end @@ -68,9 +67,10 @@ describe Rscons do end def lines - @output.lines.map(&:rstrip).tap do |v| - @output = "" - end + rv = ($stdout.string + $stderr.string).lines.map(&:rstrip) + $stdout.string = "" + $stderr.string = "" + rv end ########################################################################### diff --git a/spec/rscons/builders/disassemble_spec.rb b/spec/rscons/builders/disassemble_spec.rb index a4f759e..d807741 100644 --- a/spec/rscons/builders/disassemble_spec.rb +++ b/spec/rscons/builders/disassemble_spec.rb @@ -6,9 +6,9 @@ module Rscons it "supports overriding DISASM_CMD construction variable" do cache = "cache" - cache.stub(:up_to_date?) { false } - cache.stub(:mkdir_p) { } - cache.stub(:register_build) { } + allow(cache).to receive(:up_to_date?) { false } + allow(cache).to receive(:mkdir_p) { } + allow(cache).to receive(:register_build) { } expect(env).to receive(:execute).with("Disassemble a_file.txt", ["my_disasm", "a_file.exe"], anything).and_return(true) subject.run("a_file.txt", ["a_file.exe"], cache, env, "DISASM_CMD" => ["my_disasm", "${_SOURCES}"]) end diff --git a/spec/rscons/builders/object_spec.rb b/spec/rscons/builders/object_spec.rb index e5eaded..7c1861c 100644 --- a/spec/rscons/builders/object_spec.rb +++ b/spec/rscons/builders/object_spec.rb @@ -6,11 +6,11 @@ module Rscons it "supports overriding CCCMD construction variable" do cache = "cache" - cache.stub(:up_to_date?) { false } - cache.stub(:mkdir_p) { } - cache.stub(:register_build) { } - FileUtils.stub(:rm_f) { } - File.stub(:exists?) { false } + allow(cache).to receive(:up_to_date?) { false } + allow(cache).to receive(:mkdir_p) { } + allow(cache).to receive(:register_build) { } + allow(FileUtils).to receive(:rm_f) { } + allow(File).to receive(:exists?) { false } expect(env).to receive(:execute).with("CC mod.o", ["llc", "mod.c"]).and_return(true) subject.run("mod.o", ["mod.c"], cache, env, "CCCMD" => ["llc", "${_SOURCES}"]) end diff --git a/spec/rscons/cache_spec.rb b/spec/rscons/cache_spec.rb index 802425e..4f5d915 100644 --- a/spec/rscons/cache_spec.rb +++ b/spec/rscons/cache_spec.rb @@ -1,11 +1,11 @@ module Rscons describe Cache do before do - File.stub(:read) { nil } + allow(File).to receive(:read) { nil } end def build_from(cache) - JSON.stub(:load) do + allow(JSON).to receive(:load) do cache end Cache.instance.tap do |cache| @@ -28,7 +28,7 @@ module Rscons describe "#clear" do it "removes the cache file" do expect(FileUtils).to receive(:rm_f).with(Cache::CACHE_FILE) - JSON.stub(:load) {{}} + allow(JSON).to receive(:load) {{}} Cache.instance.clear end end @@ -47,7 +47,7 @@ module Rscons describe "#up_to_date?" do empty_env = "env" before do - empty_env.stub(:get_user_deps) { nil } + allow(empty_env).to receive(:get_user_deps) { nil } end it "returns false when target file does not exist" do diff --git a/spec/rscons/environment_spec.rb b/spec/rscons/environment_spec.rb index 185c795..d85b4be 100644 --- a/spec/rscons/environment_spec.rb +++ b/spec/rscons/environment_spec.rb @@ -205,7 +205,7 @@ module Rscons cache = "cache" expect(Cache).to receive(:instance).and_return(cache) expect(cache).to receive(:clear_checksum_cache!) - env.stub(:run_builder) do |builder, target, sources, cache, vars| + allow(env).to receive(:run_builder) do |builder, target, sources, cache, vars| raise "Ruby exception thrown by builder" end expect(cache).to receive(:write) @@ -349,11 +349,11 @@ module Rscons build_op[:vars]["CFLAGS"] += ["-O3", "-DSPECIAL"] end end - env.builders["Object"].stub(:run) do |target, sources, cache, env, vars| + allow(env.builders["Object"]).to receive(:run) do |target, sources, cache, env, vars| expect(vars["CFLAGS"]).to eq [] end env.run_builder(env.builders["Object"], "build/normal/module.o", ["src/normal/module.c"], "cache", {}) - env.builders["Object"].stub(:run) do |target, sources, cache, env, vars| + allow(env.builders["Object"]).to receive(:run) do |target, sources, cache, env, vars| expect(vars["CFLAGS"]).to eq ["-O3", "-DSPECIAL"] end env.run_builder(env.builders["Object"], "build/special/module.o", ["src/special/module.c"], "cache", {}) diff --git a/spec/rscons_spec.rb b/spec/rscons_spec.rb index 37e288a..5800fe4 100644 --- a/spec/rscons_spec.rb +++ b/spec/rscons_spec.rb @@ -2,23 +2,23 @@ describe Rscons do describe ".clean" do it "removes all build targets and created directories" do cache = "cache" - Rscons::Cache.should_receive(:instance).and_return(cache) - cache.should_receive(:targets).and_return(["build/a.out", "build/main.o"]) - FileUtils.should_receive(:rm_f).with("build/a.out") - FileUtils.should_receive(:rm_f).with("build/main.o") - cache.should_receive(:directories).and_return(["build/one", "build/one/two", "build", "other"]) - File.should_receive(:directory?).with("build/one/two").and_return(true) - Dir.should_receive(:entries).with("build/one/two").and_return([".", ".."]) - Dir.should_receive(:rmdir).with("build/one/two") - File.should_receive(:directory?).with("build/one").and_return(true) - Dir.should_receive(:entries).with("build/one").and_return([".", ".."]) - Dir.should_receive(:rmdir).with("build/one") - File.should_receive(:directory?).with("build").and_return(true) - Dir.should_receive(:entries).with("build").and_return([".", ".."]) - Dir.should_receive(:rmdir).with("build") - File.should_receive(:directory?).with("other").and_return(true) - Dir.should_receive(:entries).with("other").and_return([".", "..", "other.file"]) - cache.should_receive(:clear) + expect(Rscons::Cache).to receive(:instance).and_return(cache) + expect(cache).to receive(:targets).and_return(["build/a.out", "build/main.o"]) + expect(FileUtils).to receive(:rm_f).with("build/a.out") + expect(FileUtils).to receive(:rm_f).with("build/main.o") + expect(cache).to receive(:directories).and_return(["build/one", "build/one/two", "build", "other"]) + expect(File).to receive(:directory?).with("build/one/two").and_return(true) + expect(Dir).to receive(:entries).with("build/one/two").and_return([".", ".."]) + expect(Dir).to receive(:rmdir).with("build/one/two") + expect(File).to receive(:directory?).with("build/one").and_return(true) + expect(Dir).to receive(:entries).with("build/one").and_return([".", ".."]) + expect(Dir).to receive(:rmdir).with("build/one") + expect(File).to receive(:directory?).with("build").and_return(true) + expect(Dir).to receive(:entries).with("build").and_return([".", ".."]) + expect(Dir).to receive(:rmdir).with("build") + expect(File).to receive(:directory?).with("other").and_return(true) + expect(Dir).to receive(:entries).with("other").and_return([".", "..", "other.file"]) + expect(cache).to receive(:clear) Rscons.clean end @@ -35,34 +35,34 @@ describe Rscons do it "uses the SHELL environment variable if it tests successfully" do my_ENV = {"SHELL" => "my_shell"} - ENV.stub(:[]) {|*args| my_ENV[*args]} + allow(ENV).to receive(:[]) {|*args| my_ENV[*args]} io = StringIO.new("success\n") - IO.should_receive(:popen).with(["my_shell", "-c", "echo success"]).and_yield(io) + expect(IO).to receive(:popen).with(["my_shell", "-c", "echo success"]).and_yield(io) expect(Rscons.get_system_shell).to eq(["my_shell", "-c"]) end it "uses sh -c on a mingw platform if it tests successfully" do my_ENV = {"SHELL" => nil} - ENV.stub(:[]) {|*args| my_ENV[*args]} + allow(ENV).to receive(:[]) {|*args| my_ENV[*args]} io = StringIO.new("success\n") - IO.should_receive(:popen).with(["sh", "-c", "echo success"]).and_yield(io) - Object.should_receive(:const_get).with("RUBY_PLATFORM").and_return("x86-mingw") + expect(IO).to receive(:popen).with(["sh", "-c", "echo success"]).and_yield(io) + expect(Object).to receive(:const_get).with("RUBY_PLATFORM").and_return("x86-mingw") expect(Rscons.get_system_shell).to eq(["sh", "-c"]) end it "uses cmd /c on a mingw platform if sh -c does not test successfully" do my_ENV = {"SHELL" => nil} - ENV.stub(:[]) {|*args| my_ENV[*args]} + allow(ENV).to receive(:[]) {|*args| my_ENV[*args]} io = StringIO.new("success\n") - IO.should_receive(:popen).with(["sh", "-c", "echo success"]).and_raise "ENOENT" - Object.should_receive(:const_get).with("RUBY_PLATFORM").and_return("x86-mingw") + expect(IO).to receive(:popen).with(["sh", "-c", "echo success"]).and_raise "ENOENT" + expect(Object).to receive(:const_get).with("RUBY_PLATFORM").and_return("x86-mingw") expect(Rscons.get_system_shell).to eq(["cmd", "/c"]) end it "uses sh -c on a non-mingw platform if SHELL is not specified" do my_ENV = {"SHELL" => nil} - ENV.stub(:[]) {|*args| my_ENV[*args]} - Object.should_receive(:const_get).with("RUBY_PLATFORM").and_return("x86-linux") + allow(ENV).to receive(:[]) {|*args| my_ENV[*args]} + expect(Object).to receive(:const_get).with("RUBY_PLATFORM").and_return("x86-linux") expect(Rscons.get_system_shell).to eq(["sh", "-c"]) end end @@ -78,28 +78,28 @@ describe Rscons do end it "returns ['env'] if mingw platform in MSYS and 'env' works" do - Object.should_receive(:const_get).and_return("x86-mingw") - ENV.should_receive(:keys).and_return(["MSYSCON"]) + expect(Object).to receive(:const_get).and_return("x86-mingw") + expect(ENV).to receive(:keys).and_return(["MSYSCON"]) io = StringIO.new("success\n") - IO.should_receive(:popen).with(["env", "echo", "success"]).and_yield(io) + expect(IO).to receive(:popen).with(["env", "echo", "success"]).and_yield(io) expect(Rscons.command_executer).to eq(["env"]) end it "returns [] if mingw platform in MSYS and 'env' does not work" do - Object.should_receive(:const_get).and_return("x86-mingw") - ENV.should_receive(:keys).and_return(["MSYSCON"]) - IO.should_receive(:popen).with(["env", "echo", "success"]).and_raise "ENOENT" + expect(Object).to receive(:const_get).and_return("x86-mingw") + expect(ENV).to receive(:keys).and_return(["MSYSCON"]) + expect(IO).to receive(:popen).with(["env", "echo", "success"]).and_raise "ENOENT" expect(Rscons.command_executer).to eq([]) end it "returns [] if mingw platform not in MSYS" do - Object.should_receive(:const_get).and_return("x86-mingw") - ENV.should_receive(:keys).and_return(["COMSPEC"]) + expect(Object).to receive(:const_get).and_return("x86-mingw") + expect(ENV).to receive(:keys).and_return(["COMSPEC"]) expect(Rscons.command_executer).to eq([]) end it "returns [] if not mingw platform" do - Object.should_receive(:const_get).and_return("x86-linux") + expect(Object).to receive(:const_get).and_return("x86-linux") expect(Rscons.command_executer).to eq([]) end end