test/unit/settings_tests.rb in dumpdb-2.0.0 vs test/unit/settings_tests.rb in dumpdb-2.1.0

- old
+ new

@@ -1,206 +1,216 @@ require 'assert' -require 'test/support/test_scripts' require 'dumpdb/settings' -module Dumpdb +require 'dumpdb/db' +require 'test/support/test_scripts' - class SettingsTests < Assert::Context - desc "the script settings" +module Dumpdb::Settings + + class UnitTests < Assert::Context + desc "Dumpdb::Settings" setup do - @setting = Settings::Base.new @script = LocalScript.new end - subject { @setting } - should have_imeth :value - should have_reader :proc + end - should "know its value proc" do + class BaseTests < UnitTests + desc "Base" + setup do + @setting = Base.new + end + subject{ @setting } + + should have_readers :proc + should have_imeths :value + + should "know its proc" do assert_kind_of ::Proc, subject.proc assert_nil subject.proc.call end should "instance eval its proc in the scope of a script to return a value" do - setting = Settings::Base.new(Proc.new { "something: #{type}"}) + setting = Base.new(Proc.new{ "something: #{type}" }) - assert_equal "local", @script.type + assert_equal "local", @script.type assert_equal "something: local", setting.value(@script) end end - class SshSettingTests < SettingsTests - desc "`ssh` setting" + class SshTests < UnitTests + desc "Ssh" - should "be available" do - assert Settings::Ssh + should "be a Base setting" do + assert_true Ssh < Base end end - class DumpFileSettingTests < SettingsTests - desc "`dump_file` setting" + class DumpFileTests < UnitTests + desc "DumpFile" - should "be available" do - assert Settings::DumpFile + should "be a Base setting" do + assert_true DumpFile < Base end end - class SourceTargetSettingTests < SettingsTests - desc "`source` or `target` setting" - setup do - @from_hash = {'host' => 'from_hash'} - end + class SourceTargetTests < UnitTests + desc "SourceTarget" - should "be available" do - assert Settings::SourceTarget + should "be a Base setting" do + assert_true SourceTarget < Base end - should "come from a hash" do - db = Settings::SourceTarget.new(@from_hash).value(@script) + should "have a Db value built from a hash" do + from_hash = { 'host' => 'from_hash' } + db = SourceTarget.new(from_hash).value(@script) - assert_kind_of Db, db + assert_kind_of Dumpdb::Db, db assert_equal 'from_hash', db.host end end - class CmdTests < SettingsTests - desc "command helper class" - setup do - @cmd_str = Proc.new { "this is the #{type} db: :db" } - end + class CmdTests < UnitTests + desc "Cmd" - should "be available" do - assert Settings::Cmd + should "be a Base setting" do + assert_true Cmd < Base end should "eval and apply any placeholders to the cmd string" do - cmd_val = Settings::Cmd.new(@cmd_str).value(@script, @script.source.to_hash) + cmd_str = Proc.new{ "this is the #{type} db: :db" } + cmd_val = Cmd.new(cmd_str).value(@script, @script.source.to_hash) assert_equal "this is the local db: devdb", cmd_val end end - class DumpCmdTests < CmdTests - desc "for dump commands" + class DumpCmdTests < UnitTests + desc "DumpCmd" + should "be a Cmd setting" do + assert_true DumpCmd < Cmd + end + should "eval and apply any source placeholders to the cmd string" do - cmd_val = Settings::DumpCmd.new(@cmd_str).value(@script) + cmd_str = Proc.new{ "this is the #{type} db: :db" } + cmd_val = DumpCmd.new(cmd_str).value(@script) assert_equal "this is the local db: devdb", cmd_val end should "not escape any double-quotes in the cmds" do - orig_cmd = "do_something --value=\"a_val\"" - cmd_val = Settings::DumpCmd.new(Proc.new { orig_cmd }).value(@script) - + orig_cmd = "do_something --value=\"a_val\"" + cmd_val = DumpCmd.new(Proc.new{ orig_cmd }).value(@script) assert_equal orig_cmd, cmd_val end should "not escape any backslashes in the cmds" do - orig_cmd = "do \\something" - cmd_val = Settings::DumpCmd.new(Proc.new { orig_cmd }).value(@script) - + orig_cmd = "do \\something" + cmd_val = DumpCmd.new(Proc.new{ orig_cmd }).value(@script) assert_equal orig_cmd, cmd_val end end - class RemoteDumpCmdTests < DumpCmdTests - desc "using ssh" - setup do - @script = RemoteScript.new - @cmd_str = Proc.new { "echo hello" } - end + class RestoreCmdTests < UnitTests + desc "RestoreCmd" - should "build the cmds to run remtoely using ssh" do - exp_cmd_str = "ssh -A #{@script.ssh_opts} #{@script.ssh} \"echo hello\"" - cmd_val = Settings::DumpCmd.new(@cmd_str).value(@script) - - assert_equal exp_cmd_str, cmd_val + should "be a Cmd setting" do + assert_true RestoreCmd < Cmd end - should "escape any double-quotes in the cmds" do - orig_cmd = "do_something --value=\"a_val\"" - exp_esc_cmd = "do_something --value=\\\"a_val\\\"" - exp_cmd_str = "ssh -A #{@script.ssh_opts} #{@script.ssh} \"#{exp_esc_cmd}\"" - cmd_val = Settings::DumpCmd.new(Proc.new { orig_cmd }).value(@script) - - assert_equal exp_cmd_str, cmd_val - end - - should "escape any backslashes in the cmds" do - orig_cmd = "do \\something" - exp_esc_cmd = "do \\\\something" - exp_cmd_str = "ssh -A #{@script.ssh_opts} #{@script.ssh} \"#{exp_esc_cmd}\"" - cmd_val = Settings::DumpCmd.new(Proc.new { orig_cmd }).value(@script) - - assert_equal exp_cmd_str, cmd_val - end - - should "escape any backslashes before double-quotes in the cmds" do - orig_cmd = "do \\something --value=\"a_val\"" - exp_esc_cmd = "do \\\\something --value=\\\"a_val\\\"" - exp_cmd_str = "ssh -A #{@script.ssh_opts} #{@script.ssh} \"#{exp_esc_cmd}\"" - cmd_val = Settings::DumpCmd.new(Proc.new { orig_cmd }).value(@script) - - assert_equal exp_cmd_str, cmd_val - end - - end - - class RestoreCmdTests < CmdTests - desc "for restore commands" - should "eval and apply any target placeholders to the cmd string" do - cmd_val = Settings::RestoreCmd.new(@cmd_str).value(@script) + cmd_str = Proc.new{ "this is the #{type} db: :db" } + cmd_val = RestoreCmd.new(cmd_str).value(@script) assert_equal "this is the local db: testdb", cmd_val end end - class CopyDumpCmdTests < CmdTests + class CopyDumpCmdTests < UnitTests + desc "CopyDumpCmd" - should "be a copy cmd for non-ssh scripts" do - script = @script - exp_cmd = "cp #{script.source.dump_file} #{script.target.dump_file}" + should "be a Cmd setting" do + assert_true CopyDumpCmd < Cmd + end - assert_equal exp_cmd, script.copy_dump_cmd + should "be a copy cmd for non-ssh scripts" do + exp = "cp #{@script.source.dump_file} #{@script.target.dump_file}" + assert_equal exp, @script.copy_dump_cmd end should "be an sftp cmd for ssh scripts" do script = RemoteScript.new - exp_cmd = "sftp #{script.ssh_opts} #{script.ssh}:#{script.source.dump_file} #{script.target.dump_file}" - - assert_equal exp_cmd, script.copy_dump_cmd + exp = "sftp #{script.ssh_opts} #{script.ssh}:#{script.source.dump_file} " \ + "#{script.target.dump_file}" + assert_equal exp, script.copy_dump_cmd end end - class CmdListTests < SettingsTests - desc "command list helper class" + class CmdListTests < UnitTests + desc "CmdList" setup do @cmds = [ - Settings::Cmd.new(Proc.new { "this is the #{type} target db: :db" }), - Settings::Cmd.new(Proc.new { "this is the #{type} target host: :host" }) + Cmd.new(Proc.new{ "this is the #{type} target db: :db" }), + Cmd.new(Proc.new{ "this is the #{type} target host: :host" }) ] @exp_val_cmds = [ "this is the local target db: testdb", "this is the local target host: testhost" ] end should "be an Array" do - assert_kind_of ::Array, Settings::CmdList.new + assert_kind_of ::Array, CmdList.new end should "return the commands, eval'd and placeholders applied" do - val_cmds = Settings::CmdList.new(@cmds).value(@script, @script.target.to_hash) + val_cmds = CmdList.new(@cmds).value(@script, @script.target.to_hash) assert_equal @exp_val_cmds, val_cmds end end + class RemoteDumpCmdTests < UnitTests + desc "using ssh" + setup do + @script = RemoteScript.new + @cmd_str = Proc.new{ "echo hello" } + end + + should "build the cmds to run remotely using ssh" do + exp = "ssh -A #{@script.ssh_opts} #{@script.ssh} \"echo hello\"" + assert_equal exp, DumpCmd.new(@cmd_str).value(@script) + end + + should "escape any double-quotes in the cmds" do + orig_cmd = "do_something --value=\"a_val\"" + exp_esc_cmd = "do_something --value=\\\"a_val\\\"" + + exp = "ssh -A #{@script.ssh_opts} #{@script.ssh} \"#{exp_esc_cmd}\"" + assert_equal exp, DumpCmd.new(Proc.new{ orig_cmd }).value(@script) + end + + should "escape any backslashes in the cmds" do + orig_cmd = "do \\something" + exp_esc_cmd = "do \\\\something" + + exp = "ssh -A #{@script.ssh_opts} #{@script.ssh} \"#{exp_esc_cmd}\"" + assert_equal exp, DumpCmd.new(Proc.new{ orig_cmd }).value(@script) + end + + should "escape any backslashes before double-quotes in the cmds" do + orig_cmd = "do \\something --value=\"a_val\"" + exp_esc_cmd = "do \\\\something --value=\\\"a_val\\\"" + + exp = "ssh -A #{@script.ssh_opts} #{@script.ssh} \"#{exp_esc_cmd}\"" + assert_equal exp, DumpCmd.new(Proc.new{ orig_cmd }).value(@script) + end + + end end