dir = File.dirname(__FILE__) require "#{dir}/spec_helper" context "A MkmfRakeBuilder" do setup do @builder = MkmfRakeBuilder.new dir = File.dirname(__FILE__) @project_dir = @builder.project_dir = "/project/dir" @rake_application = @builder.rake_application = mock("rake_application") @rake_tasks = @builder.rake_tasks = mock("rake_tasks") @rake_file_tasks = @builder.rake_file_tasks = mock("rake_file_tasks") @dir_class = @builder.dir_class = mock("dir_class") @file_class = @builder.file_class = File end specify "should build the default task" do @rake_tasks.should_receive(:define_task).with({:extension => [ :compile, :install ]}) @rake_tasks.should_receive(:define_task).with({:default => [ :extension ]}) @builder.build_default_task end specify "should build the compile task" do extensions_to_build = @builder.extensions_to_build = [[:foo, "foo"], [:bar, "bar"]] @rake_tasks.should_receive(:define_task).with({:compile => [:foo, :bar]}) @builder.build_compile_task end specify "should build spec task to require spec suite" do spec_task = nil @rake_tasks.should_receive(:define_task).with({:spec => [:compile, :install]}). and_return{|definition, spec_task| spec_task = spec_task} @builder.build_spec_task require_args = nil @builder.define_instance_method :require do |*args| require_args = args end spec_task.call require_args.should == ["/project/dir/spec/spec_suite"] end specify "should build lib task to create the lib directory" do spec_task = nil @rake_tasks.should_receive(:define_task).with(:lib). and_return{|definition, spec_task| spec_task = spec_task} @builder.build_lib_task directory_args = nil @builder.define_instance_method :directory do |*args| directory_args = args end spec_task.call directory_args.should == ["lib"] end specify "should build all extension compile tasks" do extensions_to_build = @builder.extensions_to_build = [[:foo, "foo"], [:bar, "bar"]] specify_build_extension_compile_task("foo", "foo") specify_build_extension_compile_task("bar", "bar") @builder.build_extension_compile_tasks end specify "should build extension compile task that creates a makefile and executable" do extension_path = "extension/path" class << @builder public :build_extension_compile_task end makefile_task, executable_task = specify_build_extension_compile_task(extension_path, "path") do @builder.build_extension_compile_task(extension_path) end specify_makefile_task(makefile_task) specify_executable_task(executable_task) end def specify_makefile_task(makefile_task) dir_class = @builder.dir_class = mock("dir_class_for_makefile") makefile_chdir_task = nil dir_class.should_receive(:chdir).with("ext/extension/path"). and_return do |name, makefile_chdir_task| makefile_chdir_task = makefile_chdir_task end makefile_task.call sh_args = [] @builder.define_instance_method :sh do |*args| sh_args << args end makefile_chdir_task.call sh_args.should == [["ruby extconf.mkmf.rb"]] end def specify_executable_task(executable_task) dir_class = @builder.dir_class = mock("dir_class_for_executable") executable_chdir_task = nil dir_class.should_receive(:chdir).with("ext/extension/path"). and_return do |name, executable_chdir_task| executable_chdir_task = executable_chdir_task end executable_task.call sh_args = [] @builder.define_instance_method :sh do |*args| sh_args << args end executable_chdir_task.call sh_args.should == [["make"]] end specify "should build install task to install all of the compiled extensions" do extensions_to_build = @builder.extensions_to_build = [[:foo, "extensions/foo"], [:bar, "extensions/bar"]] file_extension = @builder.file_extension = "file_extension" rake_task = nil @rake_tasks.should_receive(:define_task).with(:install). and_return {|name, rake_task| rake_task = rake_task} @builder.build_install_task.should == ["extensions/foo", "extensions/bar"] file_class = @builder.file_class = mock("file_class") file_class.should_receive(:dirname).at_least(1).with("extensions/foo").and_return("extensions") file_class.should_receive(:dirname).at_least(1).with("extensions/bar").and_return("extensions") file_class.should_receive(:basename).at_least(1).with("extensions/foo").and_return("foo") file_class.should_receive(:basename).at_least(1).with("extensions/bar").and_return("bar") file_class.should_receive(:exists?).at_least(1). with("/project/dir/ext/extensions/foo/foo.file_extension").and_return(true) file_class.should_receive(:exists?).at_least(1). with("/project/dir/ext/extensions/bar/bar.file_extension").and_return(true) cp_args = [] @builder.define_instance_method :cp do |*args| cp_args << args end rake_task.call cp_args.should == [ ["/project/dir/ext/extensions/foo/foo.file_extension", "/project/dir/lib/extensions/foo.file_extension"], ["/project/dir/ext/extensions/bar/bar.file_extension", "/project/dir/lib/extensions/bar.file_extension"] ] end specify "should build cleanup task that removes all of the compiled files" do extensions_to_build = @builder.extensions_to_build = [[:foo, "extensions/foo"], [:bar, "extensions/bar"]] file_extension = @builder.file_extension = "file_extension" cleanup_task = nil @rake_tasks.should_receive(:define_task).with(:cleanup). and_return do |name, cleanup_task| cleanup_task = cleanup_task end @builder.build_cleanup_task.should == ["extensions/foo", "extensions/bar"] file_class = @builder.file_class = mock("file_class") file_class.should_receive(:dirname).at_least(1).with("extensions/foo").and_return("extensions") file_class.should_receive(:dirname).at_least(1).with("extensions/bar").and_return("extensions") file_class.should_receive(:basename).at_least(1).with("extensions/foo").and_return("foo") file_class.should_receive(:basename).at_least(1).with("extensions/bar").and_return("bar") file_class.should_receive(:exists?).at_least(1). with("/project/dir/ext/extensions/foo/foo.file_extension").and_return(true) file_class.should_receive(:exists?).at_least(1). with("/project/dir/ext/extensions/bar/bar.file_extension").and_return(true) file_class.should_receive(:exists?).at_least(1). with("/project/dir/lib/extensions/foo.file_extension").and_return(true) file_class.should_receive(:exists?).at_least(1). with("/project/dir/lib/extensions/bar.file_extension").and_return(true) file_class.should_receive(:exists?).at_least(1). with("/project/dir/ext/extensions/foo/Makefile").and_return(true) file_class.should_receive(:exists?).at_least(1). with("/project/dir/ext/extensions/bar/Makefile").and_return(true) rm_args = [] @builder.define_instance_method :rm do |*args| rm_args << args end cleanup_task.call rm_args.should == [ ["/project/dir/lib/extensions/foo.file_extension"], ["/project/dir/ext/extensions/foo/foo.file_extension"], ["/project/dir/ext/extensions/foo/Makefile"], ["/project/dir/lib/extensions/bar.file_extension"], ["/project/dir/ext/extensions/bar/bar.file_extension"], ["/project/dir/ext/extensions/bar/Makefile"], ] end def specify_build_extension_compile_task(extension_path, extension) file_extension = @builder.file_extension = "file_extension" @rake_application.should_receive(:last_comment=).with("Builds the #{extension} extension") expected_executable = "ext/#{extension_path}/#{extension}.file_extension" @rake_tasks.should_receive(:define_task).with({extension.to_sym => ["ext/#{extension_path}/Makefile", expected_executable ]}) makefile_task = nil @rake_file_tasks.should_receive(:define_task). with({"ext/#{extension_path}/Makefile" => ["ext/#{extension_path}/extconf.mkmf.rb"]}). and_return do |task_name, makefile_task| makefile_task = makefile_task end file_list = FileList[ "ext/#{extension_path}/*.c", "ext/#{extension_path}/*.h", "ext/#{extension_path}/extconf.mkmf.rb", "ext/#{extension_path}/Makefile", "lib" ] executable_task = nil @rake_file_tasks.should_receive(:define_task).with({expected_executable => file_list}). and_return do |task_name, executable_task| executable_task = executable_task end yield if block_given? [makefile_task, executable_task] end def specify_make_executable_task(expected_executable, extension_path) end end