gemirro 0.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of gemirro might be problematic. Click here for more details.

Files changed (48) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +5 -0
  3. data/.rubocop.yml +24 -0
  4. data/.travis.yml +7 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +674 -0
  7. data/MANIFEST +46 -0
  8. data/README.md +60 -0
  9. data/Rakefile +10 -0
  10. data/bin/gemirro +6 -0
  11. data/gemirro.gemspec +31 -0
  12. data/lib/gemirro.rb +37 -0
  13. data/lib/gemirro/cli.rb +63 -0
  14. data/lib/gemirro/cli/index.rb +24 -0
  15. data/lib/gemirro/cli/init.rb +17 -0
  16. data/lib/gemirro/cli/server.rb +19 -0
  17. data/lib/gemirro/cli/update.rb +18 -0
  18. data/lib/gemirro/configuration.rb +137 -0
  19. data/lib/gemirro/gem.rb +70 -0
  20. data/lib/gemirro/gems_fetcher.rb +126 -0
  21. data/lib/gemirro/http.rb +37 -0
  22. data/lib/gemirro/indexer.rb +56 -0
  23. data/lib/gemirro/mirror_directory.rb +59 -0
  24. data/lib/gemirro/mirror_file.rb +48 -0
  25. data/lib/gemirro/server.rb +164 -0
  26. data/lib/gemirro/source.rb +58 -0
  27. data/lib/gemirro/version.rb +5 -0
  28. data/lib/gemirro/versions_fetcher.rb +31 -0
  29. data/lib/gemirro/versions_file.rb +65 -0
  30. data/spec/gemirro/cli_spec.rb +51 -0
  31. data/spec/gemirro/configuration_spec.rb +88 -0
  32. data/spec/gemirro/gem_spec.rb +37 -0
  33. data/spec/gemirro/gems_fetcher_spec.rb +104 -0
  34. data/spec/gemirro/http_spec.rb +36 -0
  35. data/spec/gemirro/indexer_spec.rb +55 -0
  36. data/spec/gemirro/mirror_directory_spec.rb +37 -0
  37. data/spec/gemirro/mirror_file_spec.rb +23 -0
  38. data/spec/gemirro/server_spec.rb +96 -0
  39. data/spec/gemirro/source_spec.rb +44 -0
  40. data/spec/gemirro/versions_fetcher_spec.rb +25 -0
  41. data/spec/gemirro/versions_file_spec.rb +52 -0
  42. data/spec/spec_helper.rb +17 -0
  43. data/task/manifest.rake +9 -0
  44. data/task/rspec.rake +6 -0
  45. data/task/rubocop.rake +5 -0
  46. data/template/config.rb +25 -0
  47. data/template/public/gems/.gitkeep +0 -0
  48. metadata +230 -0
@@ -0,0 +1,36 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'httpclient'
4
+ require 'gemirro/http'
5
+
6
+ # Http tests
7
+ module Gemirro
8
+ describe 'Http' do
9
+ it 'should return http client' do
10
+ expect(Http.client).to be_a(HTTPClient)
11
+ end
12
+
13
+ it 'should raise error when get request failed' do
14
+ uri = 'http://github.com/PierreRambaud'
15
+ Struct.new('HTTPError', :status, :reason)
16
+ result = Struct::HTTPError.new(401, 'Unauthorized')
17
+ Http.client.should_receive(:get)
18
+ .once.with(uri, follow_redirect: true).and_return(result)
19
+ expect { Http.get(uri) }
20
+ .to raise_error HTTPClient::BadResponseError, 'Unauthorized'
21
+ end
22
+
23
+ it 'should execute get request' do
24
+ uri = 'http://github.com/PierreRambaud'
25
+ Struct.new('HTTPResponse', :status, :body)
26
+ result = Struct::HTTPResponse.new(200, 'body content')
27
+ Http.client.should_receive(:get)
28
+ .once.with(uri, follow_redirect: true).and_return(result)
29
+
30
+ response = Http.get(uri)
31
+ expect(response).to be_a(Struct::HTTPResponse)
32
+ expect(response.body).to eq('body content')
33
+ expect(response.status).to eq(200)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,55 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'rubygems/indexer'
4
+ require 'gemirro/source'
5
+ require 'gemirro/indexer'
6
+ require 'gemirro/mirror_file'
7
+ require 'gemirro/mirror_directory'
8
+
9
+ # Indexer tests
10
+ module Gemirro
11
+ describe 'Indexer' do
12
+ include FakeFS::SpecHelpers
13
+
14
+ before(:each) do
15
+ skip if ::Gem::Version.new(RUBY_VERSION) >= ::Gem::Version.new('2.0.0')
16
+ end
17
+
18
+ it 'should install indicies' do
19
+ dir = MirrorDirectory.new('./')
20
+ dir.add_directory('gem_generate_index/quick/Marshal.4.8')
21
+ ::Gem.configuration.should_receive(:really_verbose).once.and_return(true)
22
+
23
+ @indexer = Indexer.new('./')
24
+ @indexer.should_receive(:say)
25
+ .once.with('Downloading index into production dir ./')
26
+ @indexer.quick_marshal_dir = 'gem_generate_index/quick/Marshal.4.8'
27
+ @indexer.dest_directory = './'
28
+ @indexer.directory = 'gem_generate_index'
29
+ @indexer.files = [
30
+ 'gem_generate_index/quick/Marshal.4.8',
31
+ 'gem_generate_index/specs.4.8.gz'
32
+ ]
33
+
34
+ FileUtils.should_receive(:mkdir_p).once.with('./quick', verbose: true)
35
+ FileUtils.should_receive(:rm_rf)
36
+ .once.with('./quick/Marshal.4.8', verbose: true)
37
+ FileUtils.should_receive(:mv)
38
+ .once
39
+ .with('gem_generate_index/quick/Marshal.4.8', './quick/Marshal.4.8',
40
+ verbose: true, force: true)
41
+ source = Source.new('Rubygems', 'https://rubygems.org')
42
+ Gemirro.configuration.should_receive(:source).and_return(source)
43
+ Struct.new('HttpGet', :code, :body)
44
+ http_get = Struct::HttpGet.new(200, 'content')
45
+ Http.should_receive(:get)
46
+ .with('https://rubygems.org/specs.4.8.gz').and_return(http_get)
47
+
48
+ files = @indexer.install_indicies
49
+ expect(files).to eq(['specs.4.8.gz'])
50
+ files.each do |f|
51
+ expect(MirrorFile.new(f).read).to eq('content')
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,37 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'gemirro/mirror_directory'
4
+ require 'gemirro/mirror_file'
5
+
6
+ # MirrorDirectory tests
7
+ module Gemirro
8
+ describe 'MirrorDirectory' do
9
+ include FakeFS::SpecHelpers
10
+
11
+ before(:each) do
12
+ @mirror_directory = MirrorDirectory.new('./')
13
+ end
14
+
15
+ it 'should be initialized' do
16
+ expect(@mirror_directory.path).to eq('./')
17
+ end
18
+
19
+ it 'should add directory' do
20
+ expect(@mirror_directory.add_directory('test/test2'))
21
+ .to be_a(MirrorDirectory)
22
+ expect(File.directory?('./test/test2')).to be_truthy
23
+ end
24
+
25
+ it 'should add file' do
26
+ result = @mirror_directory.add_file('file', 'content')
27
+ expect(result).to be_a(MirrorFile)
28
+ expect(result.read).to eq('content')
29
+ end
30
+
31
+ it 'should test if file exists' do
32
+ expect(@mirror_directory.file_exists?('test')).to be_falsy
33
+ @mirror_directory.add_file('test', 'content')
34
+ expect(@mirror_directory.file_exists?('test')).to be_truthy
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'gemirro/mirror_file'
4
+
5
+ # Mirror file tests
6
+ module Gemirro
7
+ describe 'MirrorFile' do
8
+ include FakeFS::SpecHelpers
9
+
10
+ before(:each) do
11
+ @mirror_file = MirrorFile.new('./test')
12
+ end
13
+
14
+ it 'should be initialized' do
15
+ expect(@mirror_file.path).to eq('./test')
16
+ end
17
+
18
+ it 'should write and read content' do
19
+ expect(@mirror_file.write('content')).to be_nil
20
+ expect(@mirror_file.read).to eq('content')
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,96 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'gemirro/source'
4
+ require 'gemirro/server'
5
+ require 'gemirro/mirror_file'
6
+ require 'socket'
7
+ require 'mime/types'
8
+
9
+ # tests
10
+ module Gemirro
11
+ describe 'Server' do
12
+ include FakeFS::SpecHelpers
13
+
14
+ it 'should be initialized' do
15
+ Gemirro.configuration.logger.should_receive(:info)
16
+ .once.with('Running server on localhost:2000')
17
+ TCPServer.should_receive(:new).with('localhost', '2000')
18
+ server = Server.new
19
+ expect(server).to be_a(Server)
20
+ expect(server.destination).to eq(Gemirro.configuration.destination)
21
+ end
22
+
23
+ it 'should be initialized with host and port' do
24
+ Gemirro.configuration.logger.should_receive(:info)
25
+ .once.with('Running server on gemirro:1337')
26
+ Gemirro.configuration.should_receive(:server_host)
27
+ .at_least(:once).and_return('gemirro')
28
+ Gemirro.configuration.should_receive(:server_port)
29
+ .at_least(:once).and_return('1337')
30
+ TCPServer.should_receive(:new).with('gemirro', '1337')
31
+ server = Server.new
32
+ expect(server).to be_a(Server)
33
+ expect(server.destination).to eq(Gemirro.configuration.destination)
34
+ end
35
+
36
+ it 'should return logger' do
37
+ TCPServer.should_receive(:new).once
38
+ Gemirro.configuration.logger.should_receive(:info)
39
+ .once.with('Running server on localhost:2000')
40
+ server = Server.new
41
+ expect(server.logger).to be(Gemirro.configuration.logger)
42
+ end
43
+
44
+ it 'should return configuration' do
45
+ TCPServer.should_receive(:new).once
46
+ Gemirro.configuration.logger.should_receive(:info)
47
+ .once.with('Running server on localhost:2000')
48
+ server = Server.new
49
+ expect(server.configuration).to be(Gemirro.configuration)
50
+ end
51
+
52
+ it 'should return versions fetcher' do
53
+ TCPServer.should_receive(:new).once
54
+ Gemirro.configuration.logger.should_receive(:info)
55
+ .once.with('Running server on localhost:2000')
56
+ server = Server.new
57
+ server.configuration.source = Source.new(
58
+ 'rubygems', 'https://rubygems.org')
59
+ Struct.new('ServerVersionsFetcher', :fetch)
60
+
61
+ VersionsFetcher.should_receive(:new)
62
+ .once
63
+ .with(server.configuration.source).and_return(Struct::ServerVersionsFetcher.new(true))
64
+ expect(server.gems_fetcher).to be_a(GemsFetcher)
65
+ end
66
+
67
+ it 'should display directory informations' do
68
+ TCPServer.should_receive(:new).once
69
+ Gemirro.configuration.logger.should_receive(:info)
70
+ .once.with('Running server on localhost:2000')
71
+ server = Server.new
72
+ session = MockTCPSocket.new
73
+ FileUtils.mkdir_p('public/directory')
74
+ MirrorFile.new('public/directory/file').write('content')
75
+ expect(server.display_directory(session, 'public/directory'))
76
+ .to eq(['.', '..', 'file'])
77
+ expect(session.output)
78
+ .to eq("HTTP/1.1 200/OK\r\nContent-type:text/html"\
79
+ "\r\n\r\n<a href=\"directory/file\">file</a><br>")
80
+ end
81
+ end
82
+
83
+ ##
84
+ # Mock TCP Socket
85
+ class MockTCPSocket
86
+ attr_accessor :response, :output
87
+ def initialize
88
+ @responses = []
89
+ @output = ''
90
+ end
91
+
92
+ def print(line)
93
+ @output << line
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,44 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'gemirro/http'
4
+ require 'gemirro/source'
5
+
6
+ # Source tests
7
+ module Gemirro
8
+ describe 'Source' do
9
+ before(:each) do
10
+ @source = Source.new('RubyGems', 'https://rubygems.org')
11
+ end
12
+
13
+ it 'should be initialized' do
14
+ expect(@source.name).to eq('rubygems')
15
+ expect(@source.host).to eq('https://rubygems.org')
16
+ expect(@source.gems).to eq([])
17
+ end
18
+
19
+ it 'should fetch versions' do
20
+ Struct.new('FetchVersions', :body)
21
+ result = Struct::FetchVersions.new(true)
22
+ Http.should_receive(:get).once.with(
23
+ "https://rubygems.org/#{Configuration.versions_file}"
24
+ ).and_return(result)
25
+ expect(@source.fetch_versions).to be_truthy
26
+ end
27
+
28
+ it 'should fetch gem' do
29
+ Struct.new('FetchGem', :body)
30
+ result = Struct::FetchGem.new(true)
31
+ Http.should_receive(:get).once.with(
32
+ 'https://rubygems.org/gems/gemirro-0.0.1.gem').and_return(result)
33
+ expect(@source.fetch_gem('gemirro', '0.0.1')).to be_truthy
34
+ end
35
+
36
+ it 'should add gems' do
37
+ expect(@source.gems).to eq([])
38
+ @source.gem('gemirro')
39
+ result = @source.gems
40
+ expect(result[0].name).to eq('gemirro')
41
+ expect(result[0].requirement).to be_a(::Gem::Requirement)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,25 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'gemirro/versions_fetcher'
4
+
5
+ # VersionsFetcher tests
6
+ module Gemirro
7
+ describe 'VersionsFetcher' do
8
+ before(:each) do
9
+ @source = Source.new('RubyGems', 'https://rubygems.org')
10
+ @fetcher = VersionsFetcher.new(@source)
11
+ end
12
+
13
+ it 'should be initialized' do
14
+ expect(@fetcher.source).to be(@source)
15
+ end
16
+
17
+ it 'should fetch versions' do
18
+ Gemirro.configuration.logger.should_receive(:info)
19
+ .once.with("Updating #{@source.name} (#{@source.host})")
20
+ @source.should_receive(:fetch_versions).once.and_return([])
21
+ VersionsFile.should_receive(:load).with([])
22
+ expect(@fetcher.fetch).to be_nil
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,52 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'zlib'
4
+ require 'gemirro/versions_file'
5
+
6
+ # VersionsFile tests
7
+ module Gemirro
8
+ describe 'VersionsFile' do
9
+ include FakeFS::SpecHelpers
10
+
11
+ it 'should be initialized' do
12
+ @versions_file = VersionsFile.new([
13
+ ['gemirro', '0.0.1'],
14
+ ['gemirro', '0.0.2']
15
+ ])
16
+ expect(@versions_file.versions).to eq([
17
+ ['gemirro', '0.0.1'],
18
+ ['gemirro', '0.0.2']
19
+ ])
20
+ expect(@versions_file.versions_hash).to eq(
21
+ 'gemirro' => [
22
+ ['gemirro', '0.0.1'],
23
+ ['gemirro', '0.0.2']
24
+ ]
25
+ )
26
+ end
27
+
28
+ it 'should load versions file' do
29
+ wio = StringIO.new('w')
30
+ w_gz = Zlib::GzipWriter.new(wio)
31
+ w_gz.write(Marshal.dump([
32
+ ['gemirro', '0.0.1'],
33
+ ['gemirro', '0.0.2']
34
+ ]))
35
+ w_gz.close
36
+
37
+ result = VersionsFile.load(wio.string)
38
+ expect(result).to be_a(VersionsFile)
39
+
40
+ expect(result.versions).to eq([
41
+ ['gemirro', '0.0.1'],
42
+ ['gemirro', '0.0.2']
43
+ ])
44
+ expect(result.versions_hash).to eq(
45
+ 'gemirro' => [
46
+ ['gemirro', '0.0.1'],
47
+ ['gemirro', '0.0.2']
48
+ ]
49
+ )
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,17 @@
1
+ # -*- coding: utf-8 -*-
2
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'simplecov'
5
+ require 'confstruct'
6
+ require 'logger'
7
+ require 'fakefs/spec_helpers'
8
+
9
+ RSpec.configure do |config|
10
+ config.mock_with :rspec do |mocks|
11
+ mocks.syntax = :should
12
+ end
13
+ end
14
+
15
+ SimpleCov.start do
16
+ add_filter '/spec/'
17
+ end
@@ -0,0 +1,9 @@
1
+ # -*- coding: utf-8 -*-
2
+ desc 'Generates the MANIFEST file'
3
+ task :manifest do
4
+ files = `git ls-files`.split("\n").sort
5
+ handle = File.open(File.expand_path('../../MANIFEST', __FILE__), 'w')
6
+
7
+ handle.write(files.join("\n"))
8
+ handle.close
9
+ end
data/task/rspec.rake ADDED
@@ -0,0 +1,6 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ desc 'Run Rspec tests'
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.rspec_opts = '--color --format documentation'
6
+ end
data/task/rubocop.rake ADDED
@@ -0,0 +1,5 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'rubocop/rake_task'
3
+
4
+ desc 'Run RuboCop'
5
+ RuboCop::RakeTask.new(:rubocop)
@@ -0,0 +1,25 @@
1
+ # -*- coding: utf-8 -*-
2
+ # This is the main configuration file for your RubyGems mirror. Here you can
3
+ # change settings such as the location to store Gem files in and what source
4
+ # and Gems you'd like to mirror at start.
5
+ Gemirro.configuration.configure do
6
+ # The directory to store indexing information as well as the Gem files in.
7
+ destination File.expand_path('../public', __FILE__)
8
+
9
+ # If you're in development mode your probably want to switch to a debug
10
+ # logging level.
11
+ logger.level = Logger::INFO
12
+
13
+ # If you want to run your server on a specific host and port, you must
14
+ # change the following parameter (server_host and server_port).
15
+ #
16
+ # server_host = 'localhost'
17
+ # server_port = '2000'
18
+
19
+ # You must define a source wich where gems will be downloaded.
20
+ # All gem in the block will be downloaded with the update command.
21
+ # Other gems will be downloaded with the server.
22
+ define_source 'rubygems', 'http://rubygems.org' do
23
+ gem 'rack', '>= 1.0.0'
24
+ end
25
+ end