alloy-microgem 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/LICENSE +21 -0
  2. data/README.rdoc +55 -0
  3. data/Rakefile +24 -0
  4. data/TODO +13 -0
  5. data/bin/zinflate +17 -0
  6. data/bin//302/265gem +14 -0
  7. data/lib/microgem.rb +56 -0
  8. data/lib/microgem/bare_specification.rb +40 -0
  9. data/lib/microgem/bin_wrapper_emitter.rb +58 -0
  10. data/lib/microgem/config.rb +96 -0
  11. data/lib/microgem/dependency.rb +37 -0
  12. data/lib/microgem/downloader.rb +48 -0
  13. data/lib/microgem/installer.rb +147 -0
  14. data/lib/microgem/options_parser.rb +67 -0
  15. data/lib/microgem/requirement.rb +36 -0
  16. data/lib/microgem/source.rb +108 -0
  17. data/lib/microgem/specification.rb +65 -0
  18. data/lib/microgem/specification_emitter.rb +75 -0
  19. data/lib/microgem/unpacker.rb +55 -0
  20. data/lib/microgem/utils.rb +51 -0
  21. data/lib/microgem/version.rb +35 -0
  22. data/lib/microgem/yamlable.rb +32 -0
  23. data/test/bare_specification_test.rb +43 -0
  24. data/test/bin_wrapper_emitter_test.rb +57 -0
  25. data/test/config_test.rb +131 -0
  26. data/test/dependency_test.rb +41 -0
  27. data/test/downloader_test.rb +100 -0
  28. data/test/fixtures/gems.github.com +0 -0
  29. data/test/fixtures/gems.rubyforge.org +0 -0
  30. data/test/fixtures/gems/rake-0.8.0/rake.rb +1 -0
  31. data/test/fixtures/gems/rake-0.8.1/rake.rb +1 -0
  32. data/test/fixtures/gems/test-spec-0.3.0/test-spec.rb +1 -0
  33. data/test/fixtures/gems/test-spec-mock-0.3.0/test-spec-mock.rb +1 -0
  34. data/test/fixtures/rails-2.1.1.gemspec +26 -0
  35. data/test/fixtures/rails-2.1.1.gemspec.marshal +0 -0
  36. data/test/fixtures/rake-0.8.1.gem +0 -0
  37. data/test/fixtures/rake-0.8.1.gemspec +20 -0
  38. data/test/fixtures/rake-0.8.1.gemspec.marshal +0 -0
  39. data/test/fixtures/rake-0.8.1.gemspec.rz +2 -0
  40. data/test/fixtures/specs.4.8.gz +0 -0
  41. data/test/installer_test.rb +198 -0
  42. data/test/microgem_test.rb +34 -0
  43. data/test/options_parser_test.rb +36 -0
  44. data/test/requirement_test.rb +40 -0
  45. data/test/source_test.rb +153 -0
  46. data/test/specification_emitter_test.rb +139 -0
  47. data/test/specification_test.rb +45 -0
  48. data/test/test_helper.rb +71 -0
  49. data/test/unpacker_test.rb +91 -0
  50. data/test/utils_test.rb +42 -0
  51. data/test/version_test.rb +27 -0
  52. data/test/yamlable_test.rb +15 -0
  53. metadata +114 -0
@@ -0,0 +1,36 @@
1
+ ##!/usr/bin/env macruby
2
+
3
+ require File.expand_path('../test_helper', __FILE__)
4
+
5
+ describe "Gem::Micro::Options, with options" do
6
+ def setup
7
+ @parser = Gem::Micro::OptionsParser.new
8
+ end
9
+
10
+ it "should set the log_level" do
11
+ @parser.parse(%w{ install rake --debug }).options.should == { :log_level => :debug }
12
+ end
13
+
14
+ it "should force commands" do
15
+ @parser.parse(%w{ install rake --force }).options.should == { :force => true }
16
+ end
17
+
18
+ it "should enable the simple downloader" do
19
+ @parser.parse(%w{ install rake --simple-downloader }).options.should == { :simple_downloader => true }
20
+ end
21
+
22
+ it "should enable the simple unpacker" do
23
+ @parser.parse(%w{ install rake --simple-unpacker }).options.should == { :simple_unpacker => true }
24
+ end
25
+
26
+ it "should enable the simple downloader and unpacker" do
27
+ @parser.parse(%w{ install rake --simple }).options.should ==
28
+ { :simple_downloader => true, :simple_unpacker => true }
29
+ end
30
+
31
+ it "should return the command and its arguments" do
32
+ @parser.parse(%w{ install rake --debug })
33
+ @parser.command.should == 'install'
34
+ @parser.arguments.should == ['rake']
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ ##!/usr/bin/env macruby
2
+
3
+ require File.expand_path('../test_helper', __FILE__)
4
+
5
+ describe "Gem::Requirement" do
6
+ def setup
7
+ @requirement = Gem::Requirement[:requirements => [['>=', Gem::Version[:version => '0.8.1']]]]
8
+ end
9
+
10
+ it "should be equal to one in the gem specs dependencies" do
11
+ gem_spec = gem_spec_fixture('rails', '2.1.1')
12
+ version_requirement = gem_spec.dependencies.first.version_requirements
13
+
14
+ version_requirement.should == @requirement
15
+ end
16
+
17
+ it "should be equal if both the operator and version are equal" do
18
+ Gem::Requirement[:requirements => [['>=', Gem::Version[:version => '0.8.1']]]].should == @requirement
19
+ end
20
+
21
+ it "should not be equal if the operators aren't equal" do
22
+ Gem::Requirement[:requirements => [['=', Gem::Version[:version => '0.8.1']]]].should.not == @requirement
23
+ end
24
+
25
+ it "should not be equal if the versions aren't equal" do
26
+ Gem::Requirement[:requirements => [['>=', Gem::Version[:version => '9.8.7']]]].should.not == @requirement
27
+ end
28
+
29
+ it "should return its highest required version" do
30
+ @requirement.version.should == Gem::Version[:version => '0.8.1']
31
+ end
32
+
33
+ it "should return the operator of the highest required version" do
34
+ @requirement.operator.should == '>='
35
+ end
36
+
37
+ it "should return a string representation" do
38
+ @requirement.to_s.should == ">= 0.8.1"
39
+ end
40
+ end
@@ -0,0 +1,153 @@
1
+ ##!/usr/bin/env macruby
2
+
3
+ require File.expand_path('../test_helper', __FILE__)
4
+
5
+ describe "Gem::Micro::Source, class methods" do
6
+ include Gem::Micro::Utils
7
+
8
+ def setup
9
+ @sources = Gem::Micro::Source.sources
10
+ end
11
+
12
+ def teardown
13
+ Gem::Micro::Source.instance_variable_set(:@sources, nil)
14
+ end
15
+
16
+ it "should load the sources specified on config.sources in config.gem_home" do
17
+ Gem::Micro::Source.instance_variable_set(:@sources, nil)
18
+
19
+ hosts = %w{ gems.superalloy.nl gems.fngtps.com }
20
+ config.stubs(:sources).returns(hosts)
21
+ sources = Gem::Micro::Source.sources
22
+
23
+ sources.map { |s| s.host }.should == hosts
24
+ sources.map { |s| s.index_file }.should ==
25
+ [File.join(config.gem_home, hosts.first), File.join(config.gem_home, hosts.last)]
26
+ end
27
+
28
+ it "should return matching gem specs from any source and mark them to know from which source they came" do
29
+ rake = ['rake', Gem::Version[:version => '0.8.1']]
30
+
31
+ rubyforge_bare_spec = mock('Rubyforge')
32
+ Gem::Micro::BareSpecification.expects(:new).with(@sources.first, *rake).returns(rubyforge_bare_spec)
33
+ rubyforge_bare_spec.expects(:gem_spec).returns('rake from rubyforge')
34
+
35
+ Gem::Micro::Source.gem_spec(*rake).should == 'rake from rubyforge'
36
+ end
37
+
38
+ it "should tell the sources to update" do
39
+ @sources.first.expects(:update!)
40
+ @sources.last.expects(:update!)
41
+ Gem::Micro::Source.update!
42
+ end
43
+ end
44
+
45
+ describe "Gem::Micro::Source, in general" do
46
+ def setup
47
+ @dir = Gem::Micro::Utils.tmpdir
48
+ @source = Gem::Micro::Source.new('gems.rubyforge.org', @dir)
49
+ end
50
+
51
+ it "should return the path to the index file" do
52
+ @source.index_file.should == File.join(@dir, 'gems.rubyforge.org')
53
+ end
54
+
55
+ it "should return the url from where to get the marshalled gem list" do
56
+ @source.specs_url.should == 'http://gems.rubyforge.org/specs.4.8.gz'
57
+ end
58
+
59
+ it "should return the path to the work archive file" do
60
+ @source.work_archive_file.should == File.join(@dir, 'specs.4.8.gz')
61
+ end
62
+
63
+ it "should return the path to the work index file" do
64
+ @source.work_index_file.should == File.join(@dir, 'specs.4.8')
65
+ end
66
+ end
67
+
68
+ describe "Gem::Micro::Source, for a non existing index" do
69
+ include Gem::Micro::Utils
70
+
71
+ def setup
72
+ @dir = File.join(tmpdir, 'specs')
73
+ ensure_dir(@dir)
74
+ @source = Gem::Micro::Source.new('gems.rubyforge.org', @dir)
75
+ end
76
+
77
+ def teardown
78
+ remove_microgem_tmpdir!
79
+ end
80
+
81
+ it "should return that it doesn't exist" do
82
+ @source.should.not.exist
83
+ end
84
+
85
+ it "should download and unpack the index to index_file" do
86
+ Gem::Micro::Downloader.expects(:get).with(@source.specs_url, @source.work_archive_file)
87
+ FileUtils.cp(fixture('specs.4.8.gz'), tmpdir) # fake the download to the tmpdir
88
+
89
+ @source.update!
90
+
91
+ File.should.exist @source.index_file
92
+ Marshal.load(File.read(@source.index_file)).should.be.instance_of Array
93
+ end
94
+
95
+ it "should update! if the #specs are accessed but the index_file doesn't exist yet" do
96
+ # make sure the fixture is in place
97
+ Gem::Micro::Downloader.stubs(:get)
98
+ FileUtils.cp(fixture('specs.4.8.gz'), tmpdir)
99
+ @source.update!
100
+
101
+ @source.stubs(:exist?).returns(false)
102
+ @source.expects(:update!)
103
+ @source.specs
104
+ end
105
+ end
106
+
107
+ describe "Gem::Micro::Source, for an existing index" do
108
+ include Gem::Micro::Utils
109
+
110
+ def setup
111
+ @dir = File.join(tmpdir, 'specs')
112
+ ensure_dir(@dir)
113
+ @source = Gem::Micro::Source.new('gems.rubyforge.org', @dir)
114
+
115
+ # create the index
116
+ Gem::Micro::Downloader.stubs(:get)
117
+ FileUtils.cp(fixture('specs.4.8.gz'), tmpdir)
118
+ @source.update!
119
+
120
+ @rake = ['rake', Gem::Version[:version => '0.8.3']]
121
+ end
122
+
123
+ def teardown
124
+ remove_microgem_tmpdir!
125
+ end
126
+
127
+ it "should return that it exists" do
128
+ @source.should.exist
129
+ end
130
+
131
+ it "should return a spec matching the given name and version" do
132
+ rake_0_8_1 = @source.spec('rake', Gem::Version[:version => '0.8.1'])
133
+ rake_0_8_1[1].to_s.should == '0.8.1'
134
+ end
135
+
136
+ it "should return the last spec matching the given name and a version of any?" do
137
+ rake_0_8_3 = @source.spec('rake', Gem::Version[:version => '0'])
138
+ rake_0_8_3[1].to_s.should == '0.8.3'
139
+ end
140
+
141
+ it "should return a gem spec matching the given name" do
142
+ spec = mock('BareSpecification')
143
+ Gem::Micro::BareSpecification.expects(:new).with(@source, *@rake).returns(spec)
144
+ spec.expects(:gem_spec).returns("The Gem::Specification")
145
+
146
+ @source.gem_spec(*@rake).should == "The Gem::Specification"
147
+ end
148
+
149
+ it "should use the latest version if the specified version is any?" do
150
+ Gem::Micro::BareSpecification.expects(:new).with(@source, *@rake).returns(stub_everything)
151
+ @source.gem_spec('rake', Gem::Version[:version => '0'])
152
+ end
153
+ end
@@ -0,0 +1,139 @@
1
+ ##!/usr/bin/env macruby
2
+
3
+ require File.expand_path('../test_helper', __FILE__)
4
+
5
+ describe "Gem::Micro::SpecificationEmitter" do
6
+ def setup
7
+ @gem_spec = Gem::Specification.new
8
+ set_ivar('dependencies', [])
9
+
10
+ @emitter = Gem::Micro::SpecificationEmitter.new(@gem_spec)
11
+ end
12
+
13
+ it "should return a properly formatted Time" do
14
+ time = Time.now
15
+ @emitter.format(time).should == time.strftime("%Y-%m-%d")
16
+ end
17
+
18
+ it "should return a properly formatted Date" do
19
+ date = Date.today
20
+ @emitter.format(date).should == date.to_s
21
+ end
22
+
23
+ it "should return a properly formatted Gem::Version" do
24
+ version = Gem::Version[:version => '0.8.1']
25
+ @emitter.format(version).should == version.to_s
26
+ end
27
+
28
+ it "should return a properly formatted Gem::Requirement" do
29
+ requirement = Gem::Requirement[:requirements => [['>=', Gem::Version[:version => '0.8.1']]]]
30
+ @emitter.format(requirement).should == requirement.to_s
31
+ end
32
+
33
+ it "should return an array of key-value pairs sorted by key" do
34
+ vars = [
35
+ ['authors', ['Eloy Duran', 'Manfred Stienstra']],
36
+ ['email', 'eloy@fngtps.com']
37
+ ]
38
+
39
+ # assign variables
40
+ vars.each { |k,v| set_ivar(k, v) }
41
+
42
+ # reverse to test sorting
43
+ reversed_ivars = vars.reverse.map { |k,_| "@#{k}" }
44
+ @gem_spec.stubs(:instance_variables).returns(reversed_ivars)
45
+
46
+ @emitter.gem_spec_variables.should == vars
47
+ end
48
+
49
+ it "should not include special values in the gem_spec_variables" do
50
+ set_ivar('name', 'rake')
51
+ set_ivar('mocha', 'dev var')
52
+ set_ivar('source', 'gems.rubyforge.org')
53
+ set_ivar('version', '0.8.1')
54
+ set_ivar('dependencies', [])
55
+ set_ivar('new_platform', 'ruby')
56
+ set_ivar('specification_version', '2')
57
+ set_ivar('required_rubygems_version', '0')
58
+ keys = @emitter.gem_spec_variables.map { |k,_| k }
59
+
60
+ keys.should.not.include 'name'
61
+ keys.should.not.include 'mocha'
62
+ keys.should.not.include 'source'
63
+ keys.should.not.include 'version'
64
+ keys.should.not.include 'dependencies'
65
+ keys.should.not.include 'new_platform'
66
+ keys.should.not.include 'specification_version'
67
+ keys.should.not.include 'required_rubygems_version'
68
+ end
69
+
70
+ it "should generate the proper Ruby code for `specification_version'" do
71
+ set_ivar('specification_version', 2)
72
+ expected = 's.specification_version = 2 if s.respond_to? :specification_version='
73
+ @emitter.to_ruby.should.include expected
74
+ end
75
+
76
+ it "should generate the proper Ruby code for `required_rubygems_version'" do
77
+ set_ivar('required_rubygems_version', Gem::Requirement[:requirements => [['>=', Gem::Version[:version => '0.8.1']]]])
78
+ expected = 's.required_rubygems_version = Gem::Requirement.new(">= 0.8.1") if s.respond_to? :required_rubygems_version='
79
+ @emitter.to_ruby.should.include expected
80
+ end
81
+
82
+ it "should generate the proper Ruby code for `required_rubygems_version' if it's nil" do
83
+ set_ivar('required_rubygems_version', nil)
84
+ expected = 's.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version='
85
+ @emitter.to_ruby.should.include expected
86
+ end
87
+
88
+ it "should generate the proper Ruby code for a Gem::Dependency" do
89
+ @gem_spec = gem_spec_fixture('rails', '2.1.1')
90
+ @emitter = Gem::Micro::SpecificationEmitter.new(@gem_spec)
91
+
92
+ rake_dependency = 's.add_dependency("rake", [">= 0.8.1"])'
93
+ @emitter.to_ruby.should.include rake_dependency
94
+ end
95
+
96
+ # For these we need to download the specific gems their metadata yaml files
97
+ [['rake', '0.8.1'], ['rails', '2.1.1']].each do |gem_name, gem_version|
98
+ xit "should return a representation of the Ruby #{gem_name} gemspec, which is accepted by RubyGems" do
99
+ begin
100
+ gemspec_file = "#{gem_name}-#{gem_version}.gemspec"
101
+
102
+ @gem_spec = YAML.load(File.read(fixture("#{gemspec_file}.full")))
103
+ @emitter = Gem::Micro::SpecificationEmitter.new(@gem_spec)
104
+
105
+ @gem_spec.instance_variable_set(:@rubygems_version, Gem::Version[:version => '1.3.1'])
106
+ spec_file = File.join(TMP_PATH, "#{gemspec_file}.rb")
107
+
108
+ File.open(spec_file, 'w') do |f|
109
+ f << %{
110
+ require 'rubygems'
111
+
112
+ spec = #{@emitter.to_ruby}
113
+
114
+ # If the dynamically created one equals the fixture we quit without error
115
+ exit 1 unless spec == eval(File.read("#{fixture(gemspec_file)}"))
116
+ }
117
+ end
118
+
119
+ # debugging
120
+ unless system("ruby '#{spec_file}'")
121
+ output_file = File.join(TMP_PATH, gemspec_file)
122
+ File.open(output_file, 'w') { |f| f << @emitter.to_ruby }
123
+ puts `diff -y '#{fixture(gemspec_file)}' '#{output_file}'`
124
+ end
125
+
126
+ assert system("ruby '#{spec_file}'")
127
+
128
+ ensure
129
+ File.unlink(spec_file) rescue nil
130
+ end
131
+ end
132
+ end
133
+
134
+ private
135
+
136
+ def set_ivar(name, value)
137
+ @gem_spec.instance_variable_set("@#{name}", value)
138
+ end
139
+ end
@@ -0,0 +1,45 @@
1
+ ##!/usr/bin/env macruby
2
+
3
+ require File.expand_path('../test_helper', __FILE__)
4
+
5
+ describe "Gem::Specification" do
6
+ def setup
7
+ @gem_spec = Marshal.load(File.read(fixture('rake-0.8.1.gemspec.marshal')))
8
+ end
9
+
10
+ it "should return its dependencies" do
11
+ gem_spec = Marshal.load(File.read(fixture('rails-2.1.1.gemspec.marshal')))
12
+
13
+ expected_version_requirements = [[]]
14
+ expected_dep_names = %w{
15
+ rake
16
+ activesupport
17
+ activerecord
18
+ actionpack
19
+ actionmailer
20
+ activeresource
21
+ }
22
+
23
+ gem_spec.dependencies.map { |dep| dep.name }.should == expected_dep_names
24
+ end
25
+
26
+ it "should return its gem dirname" do
27
+ @gem_spec.gem_dirname.should == 'rake-0.8.1'
28
+ end
29
+
30
+ it "should return its gem filename" do
31
+ @gem_spec.gem_filename.should == 'rake-0.8.1.gem'
32
+ end
33
+
34
+ it "should install itself" do
35
+ installer = Gem::Micro::Installer.new(@gem_spec)
36
+ Gem::Micro::Installer.expects(:new).with(@gem_spec).returns(installer)
37
+ installer.expects(:install!)
38
+
39
+ @gem_spec.install!
40
+ end
41
+
42
+ it "should emit itself as Ruby code" do
43
+ @gem_spec.to_ruby.should == Gem::Micro::SpecificationEmitter.new(@gem_spec).to_ruby
44
+ end
45
+ end
@@ -0,0 +1,71 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'microgem'
3
+
4
+ FIXTURE_PATH = File.expand_path('../fixtures', __FILE__)
5
+ TMP_PATH = File.expand_path('../../tmp', __FILE__)
6
+
7
+ # Copy the fixtures to Gem::Micro::Config.gem_home
8
+ c = Gem::Micro::Config
9
+ c.sources.each do |hostname|
10
+ unless File.exist?(File.join(c.gem_home, hostname))
11
+ FileUtils.cp File.join(FIXTURE_PATH, hostname), c.gem_home
12
+ end
13
+ end
14
+
15
+ # silence the logger
16
+ module Gem::Micro::Utils
17
+ def log(level, message)
18
+ # nada
19
+ end
20
+ end
21
+
22
+ require 'test/unit'
23
+ class Test::Unit::TestCase
24
+ private
25
+
26
+ def fixture(name)
27
+ File.join(FIXTURE_PATH, name)
28
+ end
29
+
30
+ def gem_spec_fixture(name, version)
31
+ Marshal.load(File.read(fixture("#{name}-#{version}.gemspec.marshal")))
32
+ end
33
+ end
34
+
35
+ module Kernel
36
+ def remove_microgem_tmpdir!
37
+ FileUtils.rm_rf Gem::Micro::Utils.tmpdir
38
+ end
39
+ remove_microgem_tmpdir! # ensure it doesn't exist before test run
40
+
41
+ # we don't want to load rubygems, so use these work arounds
42
+ alias_method :__require_before_microgems, :require
43
+ def require(lib)
44
+ __require_before_microgems(lib) unless lib == 'rubygems'
45
+ end
46
+
47
+ def gem(*args)
48
+ # do nothing
49
+ end
50
+ end
51
+
52
+ require 'net/http'
53
+ module Net
54
+ class HTTP
55
+ class TryingToMakeHTTPConnectionException < StandardError; end
56
+ def connect
57
+ raise TryingToMakeHTTPConnectionException, "Please mock your HTTP calls so you don't do any HTTP requests."
58
+ end
59
+ end
60
+ end
61
+
62
+ # for test/spec gem
63
+ $: << File.expand_path('../../', `gem which test/spec`.split("\n").last)
64
+ require 'test/spec'
65
+
66
+ # for mocha gem
67
+ module Gem
68
+ class LoadError < StandardError; end
69
+ end
70
+ $: << File.expand_path('../', `gem which mocha`.split("\n").last)
71
+ require 'mocha'