repo-fixture 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4ab1242b730e36cda5acc34c41384546058ce03f
4
+ data.tar.gz: 4d1bb0ce70eb1a541023a56848317f7d00bc6162
5
+ SHA512:
6
+ metadata.gz: ab1cbad9f3af721fcf77ddf93d6238492298bed6e0d1e6220b32eefe1cd9c93a6b1de91405a071587764fd77681a4c8947883c42fabb1539e1fae79e2c5340ac
7
+ data.tar.gz: 77509df96d653ffba83baec0491fa609ad03b0bda97d6687ae9a822c9cc188fa8bc4d3fb89b88a5b319840ecc96f33f76daaaee15efc4d23ec26f0e3d88d6df8
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'pry-nav'
7
+ gem 'rake'
8
+ end
9
+
10
+ group :test do
11
+ gem 'rspec'
12
+ gem 'rr'
13
+ end
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ == 1.0.0
2
+
3
+ * Birthday!
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ repo-fixture
2
+ ============
3
+
4
+ Build and package up git repositories as test fixtures.
5
+
6
+ ## Installation
7
+
8
+ `gem install repo-fixture`
9
+
10
+ ## Usage
11
+
12
+ ```ruby
13
+ require 'repo-fixture'
14
+ ```
15
+
16
+ ### Philosophy
17
+
18
+ Test fixtures are static files that contain data used in tests. For example, you might be writing and testing a YAML parser that needs to recognize various types of YAML data. You could test these various types by creating a number of .yml fixture files that you then feed to your parser.
19
+
20
+ RepoFixture brings this concept to git repositories. It facilitates creating and packaging the repositories into individual fixture files (currently only zip is supported). RepoFixture can also load fixture files and expand the repo's contents into your temp folder, creating a pristine test environment every time.
21
+
22
+ ### Generating Fixtures
23
+
24
+ To create a new repo-based fixture:
25
+
26
+ ```ruby
27
+ my_fixture = RepoFixture.create do |fixture|
28
+ fixture.copy_files(Dir.glob('path/to/files/**/**')) do |file|
29
+ file.sub('path/to/files', '') # the path in the zip file itself
30
+ end
31
+
32
+ fixture.add_all
33
+ fixture.commit('Committing all files')
34
+ end
35
+ ```
36
+
37
+ Note that the `fixture` object in the example above responds to all the methods in [`TmpRepo`](https://github.com/camertron/tmp-repo).
38
+
39
+ Once you've put your fixture repo into the state you want, use the `export` method to generate a fixture file:
40
+
41
+ ```ruby
42
+ my_fixture.export('path/to/my_fixture.zip')
43
+ ```
44
+
45
+ It's always a good idea to clean up after yourself as well:
46
+
47
+ ```ruby
48
+ my_fixture.unlink
49
+ ```
50
+
51
+ ### Loading Fixtures
52
+
53
+ Once you've created a fixture file, you might want to load it again:
54
+
55
+ ```ruby
56
+ my_fixture = RepoFixture.load('./path/to/my_fixture.zip')
57
+ my_fixture.working_dir # => somewhere in your tmp directory
58
+ ```
59
+
60
+ ## Requirements
61
+
62
+ No external requirements.
63
+
64
+ ## Running Tests
65
+
66
+ `bundle exec rake` should do the trick.
67
+
68
+ ## Authors
69
+
70
+ * Cameron C. Dutro: http://github.com/camertron
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
4
+
5
+ require 'bundler'
6
+ require 'rspec/core/rake_task'
7
+ require 'rubygems/package_task'
8
+
9
+ require './lib/repo-fixture'
10
+
11
+ Bundler::GemHelper.install_tasks
12
+
13
+ task :default => :spec
14
+
15
+ desc 'Run specs'
16
+ RSpec::Core::RakeTask.new do |t|
17
+ t.pattern = './spec/**/*_spec.rb'
18
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'tmp-repo'
4
+
5
+ module RepoFixture
6
+ autoload :Fixture, 'repo-fixture/fixture'
7
+ autoload :ZipStrategy, 'repo-fixture/zip_strategy'
8
+
9
+ def self.create
10
+ fixture = Fixture.new(TmpRepo.new)
11
+ yield fixture
12
+ fixture
13
+ end
14
+
15
+ def self.load(file, strategy = Fixture::DEFAULT_STRATEGY)
16
+ strategy_class_for(strategy).load(file)
17
+ end
18
+
19
+ def self.strategy_class_for(strategy)
20
+ const_str = "#{camelize(strategy.to_s)}Strategy"
21
+ if RepoFixture.const_defined?(const_str)
22
+ RepoFixture.const_get(const_str)
23
+ else
24
+ raise ArgumentError, "'#{strategy}' isn't a valid export strategy."
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def self.camelize(str)
31
+ str.gsub(/(^\w|[-_]\w)/) { $1[-1].upcase }
32
+ end
33
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'fileutils'
4
+
5
+ module RepoFixture
6
+
7
+ class Fixture
8
+ DEFAULT_STRATEGY = :zip
9
+
10
+ attr_reader :repo
11
+
12
+ def initialize(repo)
13
+ @repo = repo
14
+ end
15
+
16
+ # Copies files into the repo.
17
+ # Optional block receives each file, return value is the desired path inside the archive.
18
+ def copy_files(files)
19
+ Array(files).each do |file|
20
+ output_file = if block_given?
21
+ yield file
22
+ else
23
+ file
24
+ end
25
+
26
+ output_file = File.join(repo.working_dir, output_file)
27
+ FileUtils.mkdir_p(File.dirname(output_file))
28
+ FileUtils.cp(file, output_file)
29
+ end
30
+ end
31
+
32
+ def respond_to?(method)
33
+ repo.respond_to?(method)
34
+ end
35
+
36
+ def method_missing(method, *args, &block)
37
+ repo.send(method, *args, &block)
38
+ end
39
+
40
+ def export(export_file, strategy = DEFAULT_STRATEGY)
41
+ strategy_class_for(strategy).export(export_file, self)
42
+ end
43
+
44
+ def sh(command)
45
+ repo.in_repo { `#{command}` }
46
+ end
47
+
48
+ private
49
+
50
+ def strategy_class_for(strategy)
51
+ RepoFixture.strategy_class_for(strategy)
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: UTF-8
2
+
3
+ module RepoFixture
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'zip'
4
+ require 'fileutils'
5
+
6
+ module RepoFixture
7
+ class ZipStrategy
8
+
9
+ class << self
10
+ def export(output_file, fixture)
11
+ repo = fixture.repo
12
+ working_dir_path_length = repo.working_dir.to_s.length
13
+ Zip::File.open(output_file, Zip::File::CREATE) do |zipfile|
14
+ Dir.glob("#{repo.working_dir}/**/**", File::FNM_DOTMATCH).each do |file|
15
+ if File.file?(file)
16
+ # plus 1 to remove the leading slash
17
+ relative_file = file[(working_dir_path_length + 1)..-1]
18
+ zipfile.add(relative_file, file)
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ def load(file)
25
+ output_dir = TmpRepo.random_dir
26
+
27
+ Zip::File.open(file) do |zipfile|
28
+ zipfile.each do |entry|
29
+ output_file = File.join(output_dir, entry.name)
30
+ FileUtils.mkdir_p(File.dirname(output_file))
31
+ entry.extract(output_file)
32
+ end
33
+ end
34
+
35
+ Fixture.new(
36
+ TmpRepo.new(output_dir)
37
+ )
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,21 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), 'lib')
2
+ require 'repo-fixture/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "repo-fixture"
6
+ s.version = ::RepoFixture::VERSION
7
+ s.authors = ["Cameron Dutro"]
8
+ s.email = ["camertron@gmail.com"]
9
+ s.homepage = "http://github.com/camertron"
10
+
11
+ s.description = s.summary = "Build and package up git repositories as test fixtures."
12
+
13
+ s.platform = Gem::Platform::RUBY
14
+ s.has_rdoc = true
15
+
16
+ s.add_dependency 'tmp-repo', '~> 1.0.1'
17
+ s.add_dependency 'rubyzip', '~> 1.1.0'
18
+
19
+ s.require_path = 'lib'
20
+ s.files = Dir["{lib,spec}/**/*", "Gemfile", "History.txt", "README.md", "Rakefile", "repo-fixture.gemspec"]
21
+ end
@@ -0,0 +1,6 @@
1
+ class HelloWorld
2
+ foo: ->
3
+ console.log('foo')
4
+
5
+ bar: ->
6
+ console.log('bar')
@@ -0,0 +1,9 @@
1
+ class MyTester
2
+ def foo
3
+ puts 'foo'
4
+ end
5
+
6
+ def bar
7
+ puts 'bar'
8
+ end
9
+ end
@@ -0,0 +1,74 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ include RepoFixture
6
+
7
+ describe Fixture do
8
+ let(:fixture_class) { Fixture }
9
+ let(:fixture) { Fixture.new(TmpRepo.new) }
10
+
11
+ after(:each) do
12
+ fixture.unlink
13
+ end
14
+
15
+ describe '#copy_files' do
16
+ it 'copies files into the repo' do
17
+ path = File.dirname(__FILE__)
18
+
19
+ files_to_copy = Dir.glob(
20
+ File.join(File.expand_path('./fixture_files', path), '**')
21
+ )
22
+
23
+ expect(files_to_copy.size).to be > 0
24
+
25
+ fixture.copy_files(files_to_copy) do |file|
26
+ file[(path.length + 1)..-1]
27
+ end
28
+
29
+ copied_files = Dir.glob("#{fixture.working_dir}/**/**").select do |file|
30
+ File.file?(file)
31
+ end
32
+
33
+ expect(copied_files.size).to eq(files_to_copy.size)
34
+
35
+ files_to_copy.each do |file_to_copy|
36
+ expect(copied_files).to include(
37
+ File.join(fixture.working_dir, file_to_copy[(path.length + 1)..-1])
38
+ )
39
+ end
40
+ end
41
+ end
42
+
43
+ describe '#working_dir' do
44
+ it 'forwards messages to the underlying TmpRepo instance' do
45
+ expect(fixture).to respond_to(:working_dir)
46
+ expect(fixture.working_dir.to_s).to start_with(Dir.tmpdir)
47
+ end
48
+
49
+ it "doesn't forward methods that TmpRepo doesn't understand" do
50
+ expect(fixture).to_not respond_to(:foobar)
51
+ expect(lambda { fixture.foobar }).to raise_error(NoMethodError)
52
+ end
53
+ end
54
+
55
+ describe '#sh' do
56
+ it 'executes arbitrary commands in the context of the working directory' do
57
+ # Use include here instead of equality to avoid a test failure when
58
+ # running on systems that add a prefix to their tmp directory (i.e. MacOS).
59
+ expect(fixture.sh('pwd').strip).to include(fixture.working_dir.to_s)
60
+ end
61
+ end
62
+
63
+ describe '#export' do
64
+ it 'calls export on the given strategy class' do
65
+ output_file = './test.zip'
66
+ strategy = :zip
67
+
68
+ mock.proxy(ZipStrategy).export(output_file, fixture)
69
+ fixture.export(output_file, strategy)
70
+
71
+ File.unlink(output_file)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,36 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+ require 'pathname'
5
+
6
+ include RepoFixture
7
+
8
+ describe RepoFixture do
9
+ let(:fixture_class) { RepoFixture }
10
+
11
+ describe '#create' do
12
+ it 'yields a fixture object' do
13
+ fixture_class.create do |fixture|
14
+ expect(fixture).to be_a(Fixture)
15
+ end
16
+ end
17
+ end
18
+
19
+ describe '#load' do
20
+ it 'calls load on the appropriate strategy class' do
21
+ file = './test.zip'
22
+ mock(ZipStrategy).load(file)
23
+ fixture_class.load(file)
24
+ end
25
+ end
26
+
27
+ describe '#strategy_class_for' do
28
+ it 'returns the strategy class for the strategy name' do
29
+ expect(fixture_class.strategy_class_for(:zip)).to eq(ZipStrategy)
30
+ end
31
+
32
+ it "raises an error if the strategy doesn't exist" do
33
+ expect(lambda { fixture_class.strategy_class_for(:foo) }).to raise_error(ArgumentError)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rspec'
4
+ require 'repo-fixture'
5
+ require 'pry-nav'
6
+
7
+ RSpec.configure do |config|
8
+ config.mock_with :rr
9
+ end
@@ -0,0 +1,67 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ include RepoFixture
6
+
7
+ describe ZipStrategy do
8
+ let(:fixture_class) { RepoFixture }
9
+
10
+ describe '#export' do
11
+ it 'creates a zip file containing all the files in the repo' do
12
+ fixture_file = './test.zip'
13
+
14
+ fixture = fixture_class.create do |fixture|
15
+ fixture.create_file('myfile.txt') { |f| f.write('foobarbaz') }
16
+ fixture.add_all
17
+ fixture.commit('Committing a foo file')
18
+ end
19
+
20
+ fixture.export(fixture_file, :zip)
21
+
22
+ Zip::File.open(fixture_file) do |zipfile|
23
+ zipfile.glob('myfile.txt').first.tap do |entry|
24
+ expect(entry.get_input_stream.read).to eq('foobarbaz')
25
+ end
26
+
27
+ expect(zipfile.glob('.git/COMMIT_EDITMSG').first).to_not be_nil
28
+ end
29
+
30
+ fixture.unlink
31
+ File.unlink(fixture_file)
32
+ end
33
+ end
34
+
35
+ context 'with a created fixture' do
36
+ before(:each) do
37
+ fixture = fixture_class.create do |fixture|
38
+ fixture.create_file('myfile.txt') { |f| f.write('foobarbaz') }
39
+ fixture.add_all
40
+ fixture.commit('Committing a foo file')
41
+ end
42
+
43
+ @fixture_file = Pathname('./test.zip')
44
+ fixture.export(@fixture_file, :zip)
45
+ expect(@fixture_file).to exist
46
+ fixture.unlink
47
+ end
48
+
49
+ after(:each) do
50
+ File.unlink(@fixture_file)
51
+ end
52
+
53
+ describe '#load' do
54
+ it 'should load a fixture using the given strategy' do
55
+ fixture = fixture_class.load(@fixture_file)
56
+
57
+ Dir.glob("#{fixture.working_dir}/**/**", File::FNM_DOTMATCH).tap do |files|
58
+ ['.git/COMMIT_EDITMSG', 'myfile.txt'].each do |expected_file|
59
+ expect(files).to include(File.join(fixture.working_dir, expected_file))
60
+ end
61
+ end
62
+
63
+ fixture.unlink
64
+ end
65
+ end
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: repo-fixture
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Cameron Dutro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tmp-repo
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubyzip
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.1.0
41
+ description: Build and package up git repositories as test fixtures.
42
+ email:
43
+ - camertron@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - History.txt
50
+ - README.md
51
+ - Rakefile
52
+ - lib/repo-fixture.rb
53
+ - lib/repo-fixture/fixture.rb
54
+ - lib/repo-fixture/version.rb
55
+ - lib/repo-fixture/zip_strategy.rb
56
+ - repo-fixture.gemspec
57
+ - spec/fixture_files/moar_test.coffee
58
+ - spec/fixture_files/test.rb
59
+ - spec/fixture_spec.rb
60
+ - spec/repo-fixture_spec.rb
61
+ - spec/spec_helper.rb
62
+ - spec/zip_strategy_spec.rb
63
+ homepage: http://github.com/camertron
64
+ licenses: []
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.2.2
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Build and package up git repositories as test fixtures.
86
+ test_files: []