update more rspec expectations

This commit is contained in:
Josh Holtrop 2014-06-12 15:43:50 -04:00
parent 0778f043ee
commit 5783ed993a
2 changed files with 96 additions and 96 deletions

View File

@ -16,8 +16,8 @@ module Rscons
describe "#initialize" do
context "when corrupt" do
it "prints a warning and defaults to an empty hash" do
JSON.should_receive(:load).and_return("string")
$stderr.should_receive(:puts).with(/Warning:.*was.corrupt/)
expect(JSON).to receive(:load).and_return("string")
expect($stderr).to receive(:puts).with(/Warning:.*was.corrupt/)
c = Cache.instance
c.send(:initialize!)
expect(c.instance_variable_get(:@cache).is_a?(Hash)).to be_truthy
@ -27,18 +27,18 @@ module Rscons
describe "#clear" do
it "removes the cache file" do
FileUtils.should_receive(:rm_f).with(Cache::CACHE_FILE)
expect(FileUtils).to receive(:rm_f).with(Cache::CACHE_FILE)
JSON.stub(:load) {{}}
Cache.instance.clear
end
end
describe "#write" do
it "should fill in 'version' and write to file" do
it "fills in 'version' and write to file" do
cache = {}
fh = $stdout
fh.should_receive(:puts)
File.should_receive(:open).and_yield(fh)
expect(fh).to receive(:puts)
expect(File).to receive(:open).and_yield(fh)
build_from(cache).write
expect(cache["version"]).to eq Rscons::VERSION
end
@ -51,28 +51,28 @@ module Rscons
end
it "returns false when target file does not exist" do
File.should_receive(:exists?).with("target").and_return(false)
expect(File).to receive(:exists?).with("target").and_return(false)
expect(build_from({}).up_to_date?("target", "command", [], empty_env)).to be_falsey
end
it "returns false when target is not registered in the cache" do
File.should_receive(:exists?).with("target").and_return(true)
expect(File).to receive(:exists?).with("target").and_return(true)
expect(build_from({}).up_to_date?("target", "command", [], empty_env)).to be_falsey
end
it "returns false when the target's checksum does not match" do
_cache = {"targets" => {"target" => {"checksum" => "abc"}}}
cache = build_from(_cache)
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("def")
expect(File).to receive(:exists?).with("target").and_return(true)
expect(cache).to receive(:calculate_checksum).with("target").and_return("def")
expect(cache.up_to_date?("target", "command", [], empty_env)).to be_falsey
end
it "returns false when the build command has changed" do
_cache = {"targets" => {"target" => {"checksum" => "abc", "command" => Digest::MD5.hexdigest("old command".inspect)}}}
cache = build_from(_cache)
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("abc")
expect(File).to receive(:exists?).with("target").and_return(true)
expect(cache).to receive(:calculate_checksum).with("target").and_return("abc")
expect(cache.up_to_date?("target", "command", [], empty_env)).to be_falsey
end
@ -81,8 +81,8 @@ module Rscons
"command" => Digest::MD5.hexdigest("command".inspect),
"deps" => [{"fname" => "dep.1"}]}}}
cache = build_from(_cache)
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("abc")
expect(File).to receive(:exists?).with("target").and_return(true)
expect(cache).to receive(:calculate_checksum).with("target").and_return("abc")
expect(cache.up_to_date?("target", "command", ["dep.1", "dep.2"], empty_env)).to be_falsey
end
@ -97,10 +97,10 @@ module Rscons
"checksum" => "extra.dep.chk"}],
"user_deps" => []}}}
cache = build_from(_cache)
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("abc")
cache.should_receive(:calculate_checksum).with("dep.1").and_return("dep.1.chk")
cache.should_receive(:calculate_checksum).with("dep.2").and_return("dep.2.changed")
expect(File).to receive(:exists?).with("target").and_return(true)
expect(cache).to receive(:calculate_checksum).with("target").and_return("abc")
expect(cache).to receive(:calculate_checksum).with("dep.1").and_return("dep.1.chk")
expect(cache).to receive(:calculate_checksum).with("dep.2").and_return("dep.2.changed")
expect(cache.up_to_date?("target", "command", ["dep.1", "dep.2"], empty_env)).to be_falsey
end
@ -115,8 +115,8 @@ module Rscons
"checksum" => "extra.dep.chk"}],
"user_deps" => []}}}
cache = build_from(_cache)
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("abc")
expect(File).to receive(:exists?).with("target").and_return(true)
expect(cache).to receive(:calculate_checksum).with("target").and_return("abc")
expect(cache.up_to_date?("target", "command", ["dep.1", "dep.2"], empty_env, strict_deps: true)).to be_falsey
end
@ -127,9 +127,9 @@ module Rscons
"user_deps" => []}}}
cache = build_from(_cache)
env = "env"
env.should_receive(:get_user_deps).with("target").and_return(["file.ld"])
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("abc")
expect(env).to receive(:get_user_deps).with("target").and_return(["file.ld"])
expect(File).to receive(:exists?).with("target").and_return(true)
expect(cache).to receive(:calculate_checksum).with("target").and_return("abc")
expect(cache.up_to_date?("target", "command", ["dep.1"], env)).to be_falsey
end
@ -146,13 +146,13 @@ module Rscons
"checksum" => "user.dep.chk"}]}}}
cache = build_from(_cache)
env = "env"
env.should_receive(:get_user_deps).with("target").and_return(["user.dep"])
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("abc")
cache.should_receive(:calculate_checksum).with("dep.1").and_return("dep.1.chk")
cache.should_receive(:calculate_checksum).with("dep.2").and_return("dep.2.chk")
cache.should_receive(:calculate_checksum).with("extra.dep").and_return("extra.dep.chk")
cache.should_receive(:calculate_checksum).with("user.dep").and_return("INCORRECT")
expect(env).to receive(:get_user_deps).with("target").and_return(["user.dep"])
expect(File).to receive(:exists?).with("target").and_return(true)
expect(cache).to receive(:calculate_checksum).with("target").and_return("abc")
expect(cache).to receive(:calculate_checksum).with("dep.1").and_return("dep.1.chk")
expect(cache).to receive(:calculate_checksum).with("dep.2").and_return("dep.2.chk")
expect(cache).to receive(:calculate_checksum).with("extra.dep").and_return("extra.dep.chk")
expect(cache).to receive(:calculate_checksum).with("user.dep").and_return("INCORRECT")
expect(cache.up_to_date?("target", "command", ["dep.1", "dep.2"], env)).to be_falsey
end
@ -167,11 +167,11 @@ module Rscons
"checksum" => "extra.dep.chk"}],
"user_deps" => []}}}
cache = build_from(_cache)
File.should_receive(:exists?).with("target").and_return(true)
cache.should_receive(:calculate_checksum).with("target").and_return("abc")
cache.should_receive(:calculate_checksum).with("dep.1").and_return("dep.1.chk")
cache.should_receive(:calculate_checksum).with("dep.2").and_return("dep.2.chk")
cache.should_receive(:calculate_checksum).with("extra.dep").and_return("extra.dep.chk")
expect(File).to receive(:exists?).with("target").and_return(true)
expect(cache).to receive(:calculate_checksum).with("target").and_return("abc")
expect(cache).to receive(:calculate_checksum).with("dep.1").and_return("dep.1.chk")
expect(cache).to receive(:calculate_checksum).with("dep.2").and_return("dep.2.chk")
expect(cache).to receive(:calculate_checksum).with("extra.dep").and_return("extra.dep.chk")
expect(cache.up_to_date?("target", "command", ["dep.1", "dep.2"], empty_env)).to be_truthy
end
end
@ -181,14 +181,14 @@ module Rscons
_cache = {}
cache = build_from(_cache)
env = "env"
env.should_receive(:get_user_deps).with("the target").and_return(["user.dep"])
cache.should_receive(:calculate_checksum).with("the target").and_return("the checksum")
cache.should_receive(:calculate_checksum).with("dep 1").and_return("dep 1 checksum")
cache.should_receive(:calculate_checksum).with("dep 2").and_return("dep 2 checksum")
cache.should_receive(:calculate_checksum).with("user.dep").and_return("user.dep checksum")
expect(env).to receive(:get_user_deps).with("the target").and_return(["user.dep"])
expect(cache).to receive(:calculate_checksum).with("the target").and_return("the checksum")
expect(cache).to receive(:calculate_checksum).with("dep 1").and_return("dep 1 checksum")
expect(cache).to receive(:calculate_checksum).with("dep 2").and_return("dep 2 checksum")
expect(cache).to receive(:calculate_checksum).with("user.dep").and_return("user.dep checksum")
cache.register_build("the target", "the command", ["dep 1", "dep 2"], env)
cached_target = cache.instance_variable_get(:@cache)["targets"]["the target"]
cached_target.should_not be_nil
expect(cached_target).to_not be_nil
expect(cached_target["command"]).to eq Digest::MD5.hexdigest("the command".inspect)
expect(cached_target["checksum"]).to eq "the checksum"
expect(cached_target["deps"]).to eq [
@ -212,15 +212,15 @@ module Rscons
it "makes directories and records any created in the cache" do
_cache = {}
cache = build_from(_cache)
File.should_receive(:exists?).with("one").and_return(true)
File.should_receive(:exists?).with("one/two").and_return(false)
FileUtils.should_receive(:mkdir).with("one/two")
File.should_receive(:exists?).with("one/two/three").and_return(false)
FileUtils.should_receive(:mkdir).with("one/two/three")
File.should_receive(:exists?).with("one").and_return(true)
File.should_receive(:exists?).with("one/two").and_return(true)
File.should_receive(:exists?).with("one/two/four").and_return(false)
FileUtils.should_receive(:mkdir).with("one/two/four")
expect(File).to receive(:exists?).with("one").and_return(true)
expect(File).to receive(:exists?).with("one/two").and_return(false)
expect(FileUtils).to receive(:mkdir).with("one/two")
expect(File).to receive(:exists?).with("one/two/three").and_return(false)
expect(FileUtils).to receive(:mkdir).with("one/two/three")
expect(File).to receive(:exists?).with("one").and_return(true)
expect(File).to receive(:exists?).with("one/two").and_return(true)
expect(File).to receive(:exists?).with("one/two/four").and_return(false)
expect(FileUtils).to receive(:mkdir).with("one/two/four")
cache.mkdir_p("one/two/three")
cache.mkdir_p("one\\two\\four")
expect(cache.directories).to eq ["one/two", "one/two/three", "one/two/four"]
@ -229,9 +229,9 @@ module Rscons
it "handles absolute paths" do
_cache = {}
cache = build_from(_cache)
File.should_receive(:exists?).with("/one").and_return(true)
File.should_receive(:exists?).with("/one/two").and_return(false)
FileUtils.should_receive(:mkdir).with("/one/two")
expect(File).to receive(:exists?).with("/one").and_return(true)
expect(File).to receive(:exists?).with("/one/two").and_return(false)
expect(FileUtils).to receive(:mkdir).with("/one/two")
cache.mkdir_p("/one/two")
expect(cache.directories).to eq ["/one/two"]
end
@ -248,13 +248,13 @@ module Rscons
it "does not re-calculate the checksum when it is already cached" do
cache = build_from({})
cache.instance_variable_set(:@lookup_checksums, {"f1" => "f1.chk"})
cache.should_not_receive(:calculate_checksum)
expect(cache).to_not receive(:calculate_checksum)
expect(cache.send(:lookup_checksum, "f1")).to eq "f1.chk"
end
it "calls calculate_checksum when the checksum is not cached" do
cache = build_from({})
cache.should_receive(:calculate_checksum).with("f1").and_return("ck")
expect(cache).to receive(:calculate_checksum).with("f1").and_return("ck")
expect(cache.send(:lookup_checksum, "f1")).to eq "ck"
end
end
@ -262,8 +262,8 @@ module Rscons
describe "#calculate_checksum" do
it "calculates the MD5 of the file contents" do
contents = "contents"
File.should_receive(:read).with("fname", mode: "rb").and_return(contents)
Digest::MD5.should_receive(:hexdigest).with(contents).and_return("the_checksum")
expect(File).to receive(:read).with("fname", mode: "rb").and_return(contents)
expect(Digest::MD5).to receive(:hexdigest).with(contents).and_return("the_checksum")
expect(build_from({}).send(:calculate_checksum, "fname")).to eq "the_checksum"
end
end

View File

@ -3,11 +3,11 @@ module Rscons
describe "#initialize" do
it "adds the default builders when they are not excluded" do
env = Environment.new
env.builders.size.should be > 0
expect(env.builders.size).to be > 0
expect(env.builders.map {|name, builder| builder.is_a?(Builder)}.all?).to be_truthy
env.builders.find {|name, builder| name == "Object"}.should_not be_nil
env.builders.find {|name, builder| name == "Program"}.should_not be_nil
env.builders.find {|name, builder| name == "Library"}.should_not be_nil
expect(env.builders.find {|name, builder| name == "Object"}).to_not be_nil
expect(env.builders.find {|name, builder| name == "Program"}).to_not be_nil
expect(env.builders.find {|name, builder| name == "Library"}).to_not be_nil
end
it "excludes the default builders with exclude_builders: :all" do
@ -18,14 +18,14 @@ module Rscons
context "when a block is given" do
it "yields self and invokes #process()" do
env = Environment.new do |env|
env.should_receive(:process)
expect(env).to receive(:process)
end
end
end
end
describe "#clone" do
it 'should create unique copies of each construction variable' do
it 'creates unique copies of each construction variable' do
env = Environment.new
env["CPPPATH"] << "path1"
env2 = env.clone
@ -59,7 +59,7 @@ module Rscons
it "yields self and invokes #process()" do
env = Environment.new
env.clone do |env2|
env2.should_receive(:process)
expect(env2).to receive(:process)
end
end
end
@ -161,10 +161,10 @@ module Rscons
env.Program("a.out", "main.c")
cache = "cache"
Cache.should_receive(:instance).and_return(cache)
cache.should_receive(:clear_checksum_cache!)
env.should_receive(:run_builder).with(anything, "a.out", ["main.c"], cache, {}).and_return(true)
cache.should_receive(:write)
expect(Cache).to receive(:instance).and_return(cache)
expect(cache).to receive(:clear_checksum_cache!)
expect(env).to receive(:run_builder).with(anything, "a.out", ["main.c"], cache, {}).and_return(true)
expect(cache).to receive(:write)
env.process
end
@ -175,11 +175,11 @@ module Rscons
env.Object("main.o", "other.cc")
cache = "cache"
Cache.should_receive(:instance).and_return(cache)
cache.should_receive(:clear_checksum_cache!)
env.should_receive(:run_builder).with(anything, "main.o", ["other.cc"], cache, {}).and_return("main.o")
env.should_receive(:run_builder).with(anything, "a.out", ["main.o"], cache, {}).and_return("a.out")
cache.should_receive(:write)
expect(Cache).to receive(:instance).and_return(cache)
expect(cache).to receive(:clear_checksum_cache!)
expect(env).to receive(:run_builder).with(anything, "main.o", ["other.cc"], cache, {}).and_return("main.o")
expect(env).to receive(:run_builder).with(anything, "a.out", ["main.o"], cache, {}).and_return("a.out")
expect(cache).to receive(:write)
env.process
end
@ -190,10 +190,10 @@ module Rscons
env.Object("main.o", "other.cc")
cache = "cache"
Cache.should_receive(:instance).and_return(cache)
cache.should_receive(:clear_checksum_cache!)
env.should_receive(:run_builder).with(anything, "main.o", ["other.cc"], cache, {}).and_return(false)
cache.should_receive(:write)
expect(Cache).to receive(:instance).and_return(cache)
expect(cache).to receive(:clear_checksum_cache!)
expect(env).to receive(:run_builder).with(anything, "main.o", ["other.cc"], cache, {}).and_return(false)
expect(cache).to receive(:write)
expect { env.process }.to raise_error BuildError, /Failed.to.build.main.o/
end
@ -203,12 +203,12 @@ module Rscons
env.Object("module.o", "module.c")
cache = "cache"
Cache.should_receive(:instance).and_return(cache)
cache.should_receive(:clear_checksum_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|
raise "Ruby exception thrown by builder"
end
cache.should_receive(:write)
expect(cache).to receive(:write)
expect { env.process }.to raise_error RuntimeError, /Ruby exception thrown by builder/
end
@ -258,8 +258,8 @@ module Rscons
context "with no errors" do
it "prints the short description and executes the command" do
env = Environment.new(echo: :short)
env.should_receive(:puts).with("short desc")
env.should_receive(:system).with(*Rscons.command_executer, "a", "command").and_return(true)
expect(env).to receive(:puts).with("short desc")
expect(env).to receive(:system).with(*Rscons.command_executer, "a", "command").and_return(true)
env.execute("short desc", ["a", "command"])
end
end
@ -267,10 +267,10 @@ module Rscons
context "with errors" do
it "prints the short description, executes the command, and prints the failed command line" do
env = Environment.new(echo: :short)
env.should_receive(:puts).with("short desc")
env.should_receive(:system).with(*Rscons.command_executer, "a", "command").and_return(false)
$stdout.should_receive(:write).with("Failed command was: ")
env.should_receive(:puts).with("a command")
expect(env).to receive(:puts).with("short desc")
expect(env).to receive(:system).with(*Rscons.command_executer, "a", "command").and_return(false)
expect($stdout).to receive(:write).with("Failed command was: ")
expect(env).to receive(:puts).with("a command")
env.execute("short desc", ["a", "command"])
end
end
@ -279,8 +279,8 @@ module Rscons
context "with echo: :command" do
it "prints the command executed and executes the command" do
env = Environment.new(echo: :command)
env.should_receive(:puts).with("a command '--arg=val with spaces'")
env.should_receive(:system).with({modified: :environment}, *Rscons.command_executer, "a", "command", "--arg=val with spaces", {opt: :val}).and_return(false)
expect(env).to receive(:puts).with("a command '--arg=val with spaces'")
expect(env).to receive(:system).with({modified: :environment}, *Rscons.command_executer, "a", "command", "--arg=val with spaces", {opt: :val}).and_return(false)
env.execute("short desc", ["a", "command", "--arg=val with spaces"], env: {modified: :environment}, options: {opt: :val})
end
end
@ -297,7 +297,7 @@ module Rscons
expect(env.instance_variable_get(:@targets)).to eq({})
env.Object("target.o", ["src1.c", "src2.c"], var: "val")
target = env.instance_variable_get(:@targets)["target.o"]
target.should_not be_nil
expect(target).to_not be_nil
expect(target[:builder].is_a?(Builder)).to be_truthy
expect(target[:sources]).to eq ["src1.c", "src2.c"]
expect(target[:vars]).to eq({var: "val"})
@ -335,8 +335,8 @@ module Rscons
cache = "cache"
env = Environment.new
env.add_builder(ABuilder.new)
env.builders["Object"].should_receive(:run).with("mod.o", ["mod.c"], cache, env, anything).and_return("mod.o")
env.builders["ABuilder"].should_receive(:run).with("mod2.ab_out", ["mod2.ab_in"], cache, env, anything).and_return("mod2.ab_out")
expect(env.builders["Object"]).to receive(:run).with("mod.o", ["mod.c"], cache, env, anything).and_return("mod.o")
expect(env.builders["ABuilder"]).to receive(:run).with("mod2.ab_out", ["mod2.ab_in"], cache, env, anything).and_return("mod2.ab_out")
expect(env.build_sources(["precompiled.o", "mod.c", "mod2.ab_in"], [".o", ".ab_out"], cache, {})).to eq ["precompiled.o", "mod.o", "mod2.ab_out"]
end
end
@ -368,13 +368,13 @@ module Rscons
it "determines shell flag to be /c when SHELL is specified as 'cmd'" do
env = Environment.new
env["SHELL"] = "cmd"
IO.should_receive(:popen).with(["cmd", "/c", "my_cmd"])
expect(IO).to receive(:popen).with(["cmd", "/c", "my_cmd"])
env.shell("my_cmd")
end
it "determines shell flag to be -c when SHELL is specified as something else" do
env = Environment.new
env["SHELL"] = "my_shell"
IO.should_receive(:popen).with(["my_shell", "-c", "my_cmd"])
expect(IO).to receive(:popen).with(["my_shell", "-c", "my_cmd"])
env.shell("my_cmd")
end
end
@ -383,7 +383,7 @@ module Rscons
it "executes the shell command and parses the returned flags when the input argument begins with !" do
env = Environment.new
env["CFLAGS"] = ["-g"]
env.should_receive(:shell).with("my_command").and_return(%[-arch my_arch -Done=two -include ii -isysroot sr -Iincdir -Llibdir -lmy_lib -mno-cygwin -mwindows -pthread -std=c99 -Wa,'asm,args 1 2' -Wl,linker,"args 1 2" -Wp,cpp,args,1,2 -arbitrary +other_arbitrary some_lib /a/b/c/lib])
expect(env).to receive(:shell).with("my_command").and_return(%[-arch my_arch -Done=two -include ii -isysroot sr -Iincdir -Llibdir -lmy_lib -mno-cygwin -mwindows -pthread -std=c99 -Wa,'asm,args 1 2' -Wl,linker,"args 1 2" -Wp,cpp,args,1,2 -arbitrary +other_arbitrary some_lib /a/b/c/lib])
rv = env.parse_flags("!my_command")
expect(rv).to eq({
"CCFLAGS" => %w[-arch my_arch -include ii -isysroot sr -mno-cygwin -pthread -arbitrary +other_arbitrary],
@ -435,14 +435,14 @@ module Rscons
describe ".parse_makefile_deps" do
it 'handles dependencies on one line' do
File.should_receive(:read).with('makefile').and_return(<<EOS)
expect(File).to receive(:read).with('makefile').and_return(<<EOS)
module.o: source.cc
EOS
expect(Environment.parse_makefile_deps('makefile', 'module.o')).to eq ['source.cc']
end
it 'handles dependencies split across many lines' do
File.should_receive(:read).with('makefile').and_return(<<EOS)
expect(File).to receive(:read).with('makefile').and_return(<<EOS)
module.o: module.c \\
module.h \\
other.h