require 'test/unit' require 'package' class TestFileSpecificationSemantics < Test::Unit::TestCase class PackageSpecificationTest < Package::PackageSpecification attr_reader :add_file_calls def add_file(*a) (@add_file_calls ||= []) << a[0..2] end end def setup @ps = PackageSpecificationTest.new end def test_simple_semantics @ps.instance_eval do lib "a.rb" lib "b.rb", "c.rb" bin ["d.rb", "e.rb"] doc "a.rb", ["b.rb", "c.rb"] data ["a.rb", "b.rb"], "c.rb" end assert_equal([[:lib, "a.rb", nil], [:lib, "b.rb", nil], [:lib, "c.rb", nil], [:bin, "d.rb", nil], [:bin, "e.rb", nil], [:doc, "a.rb", nil], [:doc, "b.rb", nil], [:doc, "c.rb", nil], [:data, "a.rb", nil], [:data, "b.rb", nil], [:data, "c.rb", nil]], @ps.add_file_calls) end def test_ignore_semantics @ps.instance_eval do lib "a.rb" lib "x.orig" lib "b.rb", "c.rb", "c.rb.orig" bin ["d.rb", "e.rb", "core"] doc "a.rb", "core", ["b.rb", "c.rb", "core"] data ["a.rb", "b.rb", "core"], "c.rb", "d.orig" end assert_equal([[:lib, "a.rb", nil], [:lib, "x.orig", nil], [:lib, "b.rb", nil], [:lib, "c.rb", nil], [:lib, "c.rb.orig", nil], [:bin, "d.rb", nil], [:bin, "e.rb", nil], [:doc, "a.rb", nil], [:doc, "core", nil], [:doc, "b.rb", nil], [:doc, "c.rb", nil], [:data, "a.rb", nil], [:data, "b.rb", nil], [:data, "c.rb", nil], [:data, "d.orig", nil]], @ps.add_file_calls) end def test_rename_semantics r1 = lambda{|x| "proc" } r2 = "foo" @ps.instance_eval do lib "a.rb" => r1 lib "b.rb" => r2, "c.rb" => r1 bin ["d.rb", "e.rb"] => r1 doc "a.rb" => r1, ["b.rb", "c.rb"] => r2 data ["a.rb", "b.rb"] => r1, "c.rb" => r2 end calls = @ps.add_file_calls.map{|(s,*x)| [s.to_s, *x]}.sort.map{|s,*x| [s.to_sym, *x]} assert_equal([[:bin, "d.rb", r1], [:bin, "e.rb", r1], [:data, "a.rb", r1], [:data, "b.rb", r1], [:data, "c.rb", r2], [:doc, "a.rb", r1], [:doc, "b.rb", r2], [:doc, "c.rb", r2], [:lib, "a.rb", r1], [:lib, "b.rb", r2], [:lib, "c.rb", r1]], calls) end def test_rename_and_ignore_semantics r1 = lambda{|x| "proc" } r2 = "foo" @ps.instance_eval do lib "a.rb" => r1, "b.rb.orig" => r2 lib "b.rb" => r2, "c.rb" => r1 bin ["d.rb", "e.rb", "b.rb.orig"] => r1 doc "a.rb" => r1, ["b.rb", "c.rb", "b.rb.orig"] => r2, "x.orig" => r1 data ["a.rb", "b.rb", "b.rb.orig"] => r1, "c.rb" => r2, "x.orig" => r2 end calls = @ps.add_file_calls.map{|(s,*x)| [s.to_s, *x]}.sort.map{|s,*x| [s.to_sym, *x]} assert_equal([[:bin, "d.rb", r1], [:bin, "e.rb", r1], [:data, "a.rb", r1], [:data, "b.rb", r1], [:data, "c.rb", r2], [:data, "x.orig", r2], [:doc, "a.rb", r1], [:doc, "b.rb", r2], [:doc, "c.rb", r2], [:doc, "x.orig", r1], [:lib, "a.rb", r1], [:lib, "b.rb", r2], [:lib, "b.rb.orig", r2], [:lib, "c.rb", r1]], calls) end def test_argument_type_checking ex = Package::SpecificationError assert_raises(ex){ @ps.instance_eval{ lib "foo", "bar" => "baz" } } assert_raises(ex){ @ps.instance_eval{ lib :foo } } assert_raises(ex){ @ps.instance_eval{ lib ["foo", :foo] } } assert_raises(ex){ @ps.instance_eval{ lib "foo" => :b } } end end class TestSemanticsVersioning < Test::Unit::TestCase def test_detect_nonexistent_versions assert_raises(Package::SpecificationError){ Package.setup("bogus"){ } } assert_nothing_raised{Package.package_specification_with_semantics("1.0") } end def test_detect_highlevel_interface assert_nothing_raised do Package.setup("1.0", :run_tasks => false, :parse_cmdline => false, :load_conf => false) { } end end end class TestFileSpecificationDirTranslation < Test::Unit::TestCase def setup @ps = Package::PackageSpecification.new end def test_simple_translation @ps.translate(:bin, "" => "foo") assert_equal("foo", @ps.translate_dir(:bin, "")) assert_equal("foo/bar", @ps.translate_dir(:bin, "bar")) assert_equal("foo/bar/baz", @ps.translate_dir(:bin, "bar/baz")) end def test_translation_with_inheritance_simple @ps.translate(:bin, "" => "foo", :inherit => true) assert_equal("foo", @ps.translate_dir(:bin, "")) assert_equal("foo/bar", @ps.translate_dir(:bin, "bar")) assert_equal("foo/bar/baz", @ps.translate_dir(:bin, "bar/baz")) @ps.translate(:lib, "" => "foo", :inherit => false) assert_equal("foo", @ps.translate_dir(:lib, "")) assert_equal("bar", @ps.translate_dir(:lib, "bar")) assert_equal("bar/baz", @ps.translate_dir(:lib, "bar/baz")) end def test_translation_inheritance_and_overrides @ps.translate(:lib, "" => "foo", :inherit => true) assert_equal("foo", @ps.translate_dir(:lib, "")) assert_equal("foo/bar", @ps.translate_dir(:lib, "bar")) assert_equal("foo/bar/baz", @ps.translate_dir(:lib, "bar/baz")) @ps.translate(:lib, "bar" => "babar") assert_equal("babar", @ps.translate_dir(:lib, "bar")) @ps.translate(:doc, "" => "a", :inherit => true) @ps.translate(:doc, "b" => "c", :inherit => true) @ps.translate(:doc, "b/d" => "e", :inherit => false) @ps.translate(:doc, "b/d/f" => "g", :inherit => true) assert_equal("a", @ps.translate_dir(:doc, "")) matches = Hash[*%w[x a/x b c b/x c/x b/d e b/d/x c/d/x b/d/f g b/d/f/x g/x]] matches.each_pair{|src, dst| assert_equal(dst, @ps.translate_dir(:doc, src))} end def test_translation_with_expansion class << @ps; def expand_dir_glob(x); %w[ext/a ext/b ext/c ext/d] end end @ps.translate(:ext, "ext/*" => "") matches = Hash[*%w[ext/a/x x ext/b/x x ext/c/x x ext/d/x x ext/d/f/x f/x ext/e/x ext/e/x ] ] matches.each_pair{|src, dst| assert_equal(dst, @ps.translate_dir(:ext, src))} end def test_is_glob? as = lambda{|a| assert(a)} globs = %w_foo**/a foo*/a foo?/a foo[a-b]/a foo{a,b}/a {a,b}/a [a-b]/a *a/a ?a/a _ not_globs = %w_foo\{a/a foo\[a/a foo\*a/a foo\?a/a_ @ps.instance_eval{ globs.each{|x| as[is_glob?(x)] } } @ps.instance_eval{ not_globs.each{|x| as[!is_glob?(x)] } } end end # vim: sw=2 et sts=2 ts=8