spec/kitchen/util_spec.rb in test-kitchen-1.2.1 vs spec/kitchen/util_spec.rb in test-kitchen-1.3.0

- old
+ new

@@ -14,25 +14,25 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -require_relative '../spec_helper' +require_relative "../spec_helper" -require 'logger' +require "logger" -require 'kitchen/util' +require "kitchen/util" describe Kitchen::Util do - describe '.to_logger_level' do + describe ".to_logger_level" do it "returns nil for invalid symbols" do Kitchen::Util.to_logger_level(:nope).must_be_nil end - %w{debug info warn error fatal}.each do |level| + %w[debug info warn error fatal].each do |level| it "returns Logger::#{level.upcase} for :#{level} input" do Kitchen::Util.to_logger_level(level.to_sym). must_equal Logger.const_get(level.upcase) end end @@ -42,11 +42,11 @@ it "returns :fatal for invalid symbols" do Kitchen::Util.from_logger_level("nope").must_equal :fatal end - %w{debug info warn error fatal}.each do |level| + %w[debug info warn error fatal].each do |level| it "returns :#{level} for Logger::#{level.upcase} input" do Kitchen::Util.from_logger_level(Logger.const_get(level.upcase)). must_equal(level.to_sym) end end @@ -64,12 +64,12 @@ Kitchen::Util.symbolized_hash(hash).must_equal hash end it "converts string keys into symbols" do Kitchen::Util. - symbolized_hash({ "one" => [{ "two" => :three, :four => "five" }] }). - must_equal({ :one => [{ :two => :three, :four => "five" }] }) + symbolized_hash("one" => [{ "two" => :three, :four => "five" }]). + must_equal(:one => [{ :two => :three, :four => "five" }]) end end describe ".stringified_hash" do @@ -83,12 +83,12 @@ Kitchen::Util.stringified_hash(hash).must_equal hash end it "converts symbol keys into strings" do Kitchen::Util. - stringified_hash({ :one => [{ :two => :three, "four" => "five" }] }). - must_equal({ "one" => [{ "two" => :three, "four" => "five" }] }) + stringified_hash(:one => [{ :two => :three, "four" => "five" }]). + must_equal("one" => [{ "two" => :three, "four" => "five" }]) end end describe ".duration" do @@ -100,8 +100,66 @@ Kitchen::Util.duration(60).must_equal "(1m0.00s)" end it "formats large values into minutes and seconds" do Kitchen::Util.duration(48033).must_equal "(800m33.00s)" + end + end + + describe ".wrap_unix_command" do + + it "returns the wrapped command" do + end + + it "returns a false if command is nil" do + Kitchen::Util.wrap_command(nil).must_equal("sh -c '\nfalse\n'") + end + + it "returns a true if command string is empty" do + Kitchen::Util.wrap_command("yoyo").must_equal("sh -c '\nyoyo\n'") + end + + it "handles a command string with a trailing newline" do + Kitchen::Util.wrap_command("yep\n").must_equal("sh -c '\nyep\n'") + end + end + + describe ".outdent!" do + + it "modifies the argument string in place, destructively" do + string = "yep" + + Kitchen::Util.outdent!(string).object_id.must_equal string.object_id + end + + it "returns the same string if no leading whitespace exists" do + string = "one\ntwo\nthree" + + Kitchen::Util.outdent!(string).must_equal "one\ntwo\nthree" + end + + it "strips same amount of leading whitespace as found on first line" do + string = " one\n two\n three\nfour" + + Kitchen::Util.outdent!(string).must_equal "one\n two\n three\nfour" + end + end + + describe ".shell_helpers" do + + %w[ + exists do_wget do_curl do_fetch do_perl do_python do_download + ].each do |func| + it "contains a #{func} shell function" do + Kitchen::Util.shell_helpers.must_match "#{func}() {" + end + end + + it "does not contain bare single quotes" do + Kitchen::Util.shell_helpers.wont_match "'" + end + + def regexify(str) + Regexp.new("^\s+" + Regexp.escape(str) + "$") end end end