2
# Programmer: Chris Bunch
3
# Test code for lib/parse_args.rb
4
# Right now, just testing argument parsing, not actual content of args
7
require 'flexmock/test_unit'
12
ALL_FLAGS = ["file", "foo", "ips"]
13
TARFILE = "goo.tar.gz"
16
class TestParseArgs < Test::Unit::TestCase
17
context "test bad input" do
19
# blank out STDERR.write so that abort(msg) doesn't write msg
20
# to stderr during test runs and clog up the rake results
21
stderr = flexmock(STDERR)
22
stderr.should_receive(:write).and_return("")
24
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
28
should "throw exceptions on bad input types" do
29
assert_raise(RuntimeError) { parse_args(nil) }
30
assert_raise(RuntimeError) { parse_args(1) }
31
assert_raise(RuntimeError) { parse_args("") }
34
should "return an empty hash on no args" do
35
assert_equal({}, parse_args([]))
38
should "exit if params are present but not flags" do
39
assert_raise(SystemExit) { parse_args(["foo", "boo", "goo"]) }
42
should "exit on invalid flags" do
43
args = ["--file", TARFILE, "--baz"]
44
assert_raise(SystemExit) { parse_args(args) }
47
should "exit if a param is present without a flag" do
48
args = ["baz", "-file", TARFILE]
49
assert_raise(SystemExit) { parse_args(args) }
51
args = ["-file", TARFILE, "baz"]
52
assert_raise(SystemExit) { parse_args(args) }
56
context "test good input" do
57
should "test out one flag with one param" do
58
args = ["-file", TARFILE]
59
assert_equal({"file" => TARFILE}, parse_args(args))
62
should "test out one flag with no param" do
64
assert_equal({"foo" => "NO ARG"}, parse_args(args))
67
should "test out two flags, one with a param" do
68
args = ["-foo", "-file", TARFILE]
69
assert_equal({"foo" => "NO ARG", "file" => TARFILE}, parse_args(args))
72
should "same as before but in a different order" do
73
args = ["-file", TARFILE, "-foo"]
74
assert_equal({"foo" => "NO ARG", "file" => TARFILE}, parse_args(args))
77
should "match common virtualized deployment flags" do
78
args = ["-file", TARFILE, "-ips", YAMLFILE]
79
assert_equal({"file" => TARFILE, "ips" => YAMLFILE}, parse_args(args))
82
should "allow users to type in flags with two dashes as well" do
83
args = ["--file", TARFILE]
84
assert_equal({"file" => TARFILE}, parse_args(args))
87
should "test two dashes, no param" do
89
assert_equal({"foo" => "NO ARG"}, parse_args(args))
92
should "test two flags, one with a param, both with two dashes" do
93
args = ["--foo", "--file", TARFILE]
94
assert_equal({"foo" => "NO ARG", "file" => TARFILE}, parse_args(args))
97
should "test two flags with params, both with dashes" do
98
args = ["--file", TARFILE, "--foo"]
99
assert_equal({"foo" => "NO ARG", "file" => TARFILE}, parse_args(args))
102
should "match common virtualized deployment flags with two dashes" do
103
args = ["--file", TARFILE, "--ips", YAMLFILE]
104
assert_equal({"file" => TARFILE, "ips" => YAMLFILE}, parse_args(args))
b'\\ No newline at end of file'