From 7ce5a8f9d9d2112abc19e663065922bb020168de Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 5 Nov 2013 12:26:42 -0500 Subject: [PATCH] working on Environment specs --- lib/rscons/environment.rb | 2 - spec/rscons/environment_spec.rb | 116 ++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 2 deletions(-) diff --git a/lib/rscons/environment.rb b/lib/rscons/environment.rb index 3944ac3..f4fd2de 100644 --- a/lib/rscons/environment.rb +++ b/lib/rscons/environment.rb @@ -89,8 +89,6 @@ module Rscons # Return the file name to be built from source_fname with suffix suffix. # This method takes into account the Environment's build directories. - # It also creates any parent directories needed to be able to open and - # write to the output file. def get_build_fname(source_fname, suffix) build_fname = source_fname.set_suffix(suffix).gsub('\\', '/') @build_dirs.each do |src_dir, obj_dir| diff --git a/spec/rscons/environment_spec.rb b/spec/rscons/environment_spec.rb index 8ef3661..fec1ee5 100644 --- a/spec/rscons/environment_spec.rb +++ b/spec/rscons/environment_spec.rb @@ -1,5 +1,35 @@ module Rscons describe Environment do + describe "#initialize" do + it "stores the construction variables passed in" do + env = Environment.new("CFLAGS" => ["-g"], "CPPPATH" => ["dir"]) + env["CFLAGS"].should == ["-g"] + env["CPPPATH"].should == ["dir"] + end + + it "adds the default builders when they are not excluded" do + env = Environment.new + env.builders.size.should be > 0 + env.builders.map {|name, builder| builder.is_a?(Builder)}.all?.should be_true + 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 + end + + it "excludes the default builders with exclude_builders: :all" do + env = Environment.new(exclude_builders: :all) + env.builders.size.should == 0 + end + + it "excludes the named builders" do + env = Environment.new(exclude_builders: ["Library"]) + env.builders.size.should be > 0 + 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 be_nil + end + end + describe "#clone" do it 'should create unique copies of each construction variable' do env = Environment.new @@ -11,6 +41,92 @@ module Rscons end end + describe "#add_builder" do + it "adds the builder to the list of builders" do + env = Environment.new(exclude_builders: :all) + env.builders.keys.should == [] + env.add_builder(Rscons::Object.new) + env.builders.keys.should == ["Object"] + end + end + + describe "#get_build_fname" do + context "with no build directories" do + it "returns the name of the source file with suffix changed" do + env = Environment.new + env.get_build_fname("src/dir/file.c", ".o").should == "src/dir/file.o" + env.get_build_fname("src\\dir\\other.d", ".a").should == "src/dir/other.a" + env.get_build_fname("source.cc", ".o").should == "source.o" + end + end + + context "with build directories" do + it "uses the build directories to create the output file name" do + env = Environment.new + env.build_dir("src", "bld") + env.build_dir(%r{^libs/([^/]+)}, 'build/libs/\1') + env.get_build_fname("src/input.cc", ".o").should == "bld/input.o" + env.get_build_fname("libs/lib1/some/file.c", ".o").should == "build/libs/lib1/some/file.o" + env.get_build_fname("libs/otherlib/otherlib.cc", ".o").should == "build/libs/otherlib/otherlib.o" + env.get_build_fname("other_directory/o.d", ".a").should == "other_directory/o.a" + end + end + end + + describe "#[]" do + it "allows reading construction variables" do + env = Environment.new("CFLAGS" => ["-g", "-Wall"]) + env["CFLAGS"].should == ["-g", "-Wall"] + end + end + + describe "#[]=" do + it "allows writing construction variables" do + env = Environment.new("CFLAGS" => ["-g", "-Wall"]) + env["CFLAGS"] -= ["-g"] + env["CFLAGS"] += ["-O3"] + env["CFLAGS"].should == ["-Wall", "-O3"] + env["other_var"] = "val33" + env["other_var"].should == "val33" + end + end + + describe "#append" do + it "allows adding many construction variables at once" do + env = Environment.new("CFLAGS" => ["-g"], "CPPPATH" => ["inc"]) + env.append("CFLAGS" => ["-Wall"], "CPPPATH" => ["include"]) + env["CFLAGS"].should == ["-Wall"] + env["CPPPATH"].should == ["include"] + end + end + + describe "#process" do + it "runs builders for all of the targets specified" do + env = Environment.new + env.Program("a.out", "main.c") + + cache = "cache" + Cache.should_receive(:new).and_return(cache) + env.should_receive(:run_builder).with(anything, "a.out", ["main.c"], cache, {}).and_return(true) + cache.should_receive(:write) + + env.process + end + + it "raises a BuildError when building fails" do + env = Environment.new + env.Program("a.out", "main.o") + env.Object("main.o", "other.cc") + + cache = "cache" + Cache.should_receive(:new).and_return(cache) + env.should_receive(:run_builder).with(anything, "main.o", ["other.cc"], cache, {}).and_return(false) + cache.should_receive(:write) + + expect { env.process }.to raise_error BuildError, /Failed.to.build.a\.out/ + end + end + describe ".parse_makefile_deps" do it 'handles dependencies on one line' do File.should_receive(:read).with('makefile').and_return(<