fix_to_chix 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -1,4 +1,7 @@
1
- == 0.0.1 2009-10-29
1
+ === 0.0.2 2009-10-30
2
2
 
3
- * 1 major enhancement:
4
- * Initial release
3
+ * Parsing fixtures with the default names (:one and :two) to factory names <model_name>_one and <model_name>_two so you don't end up with factories with the same name.
4
+
5
+ === 0.0.1 2009-10-29
6
+
7
+ * initial release
data/Manifest.txt CHANGED
@@ -14,6 +14,7 @@ script/destroy
14
14
  script/generate
15
15
  spec/controller_spec.rb
16
16
  spec/factory_writer_spec.rb
17
+ spec/fix_to_chix_spec.rb
17
18
  spec/fixture_parser_spec.rb
18
19
  spec/fixture_selector_spec.rb
19
20
  spec/fixtures/authors.yml
data/README.rdoc CHANGED
@@ -10,18 +10,6 @@ convert existing yaml fixtures to {factory_girl}[http://github.com/thoughtbot/fa
10
10
 
11
11
  It currently looks for yml files in your test and spec folders and translates them to factory_girl factory definitions written to test/factories.rb or spec/factories.rb
12
12
 
13
- It does not differ fixtures with the same name in different files. For example, a fixture named *one* in authors.yml will have the same factory name as *one* in posts.yml:
14
-
15
- Factory.define :one, :class_name => Author do |a|
16
- ...
17
- end
18
-
19
- Factory.define :one, :class_name => Post do |p|
20
- ...
21
- end
22
-
23
- I may solve that by using a name like :author_one and :post_one, but I'm still thinking about it.
24
-
25
13
  == SYNOPSIS:
26
14
 
27
15
  Two simple steps to run fix_to_chix:
data/Rakefile CHANGED
@@ -26,4 +26,4 @@ require 'newgem/tasks' # load /tasks/*.rake
26
26
  Dir['tasks/**/*.rake'].each { |t| load t }
27
27
 
28
28
  # TODO - want other tests/tasks run by default? Add them to the list
29
- # task :default => [:spec, :features]
29
+ task :default => [:package, :spec]
@@ -5,21 +5,14 @@ require 'fix_to_chix/fixture_parser'
5
5
  module FixToChix
6
6
  class Controller
7
7
  def self.parse_it_all!
8
-
9
8
  selector = FixtureSelector.new
10
-
11
- selector.spec_fixtures.each do |fixture_file_name|
12
- parser = FixtureParser.new(fixture_file_name)
13
- parser.parse_fixture
14
- FactoryWriter.write(parser.output_buffer, SPEC_TARGET_FILE)
9
+ %w[spec test].each do |type|
10
+ selector.send("#{type}_fixtures").each do |fixture_file_name|
11
+ parser = FixtureParser.new(fixture_file_name)
12
+ parser.parse_fixture
13
+ FactoryWriter.write(parser.output_buffer, FixToChix.class_eval("#{type.upcase}_TARGET_FILE"))
14
+ end
15
15
  end
16
-
17
- selector.test_fixtures.each do |fixture_file_name|
18
- parser = FixtureParser.new(fixture_file_name)
19
- parser.parse_fixture
20
- FactoryWriter.write(parser.output_buffer, TEST_TARGET_FILE)
21
- end
22
-
23
16
  end
24
17
  end
25
18
  end
@@ -7,7 +7,7 @@ module FixToChix
7
7
  output_buffer.each { |output| f.puts output }
8
8
  end
9
9
  end
10
-
10
+
11
11
  end
12
12
 
13
13
  end
@@ -1,12 +1,16 @@
1
+ class FileNotFoundError < IOError; end;
2
+
1
3
  module FixToChix
2
4
 
3
5
  class FixtureParser
4
6
 
7
+ DEFAULT_NAMES = ["one", "two"]
8
+
5
9
  attr_reader :output_buffer, :factory_names, :factory_writer
6
10
 
7
11
  def initialize(fixture_file)
8
12
  raise ArgumentError if fixture_file.nil?
9
- raise StandardError unless File.exists?(fixture_file)
13
+ raise FileNotFoundError unless File.exists?(fixture_file)
10
14
  @fixture_file = fixture_file
11
15
  end
12
16
 
@@ -28,7 +32,7 @@ module FixToChix
28
32
  def define_factories
29
33
  factory_definition = []
30
34
  @factory_names.each do |name|
31
- factory_definition << "Factory.define :#{name}, :class => #{model_name.camelize} do |#{model_name[0].chr}|"
35
+ factory_definition << "Factory.define :#{self.factory_name(name)}, :class => #{model_name.camelize} do |#{model_name[0].chr}|"
32
36
  define_attributes_for(name).each do |attrib|
33
37
  factory_definition << attrib
34
38
  end
@@ -48,5 +52,9 @@ module FixToChix
48
52
  attr_value = attr_value.to_i == 0 ? "'#{attr_value}'" : "#{attr_value}"
49
53
  " #{model_name[0].chr}.#{attribute} #{attr_value}"
50
54
  end
55
+
56
+ def factory_name(node_name)
57
+ DEFAULT_NAMES.include?(node_name) ? "#{self.model_name}_#{node_name}" : node_name
58
+ end
51
59
  end
52
60
  end
@@ -1,27 +1,35 @@
1
1
  module FixToChix
2
2
 
3
3
  class FixtureSelector
4
+
5
+ attr_reader :test_fixtures_dir, :spec_fixtures_dir
4
6
 
5
- def initialize
7
+ def initialize(options={})
8
+ @test_fixtures_dir = options[:test_fixtures_dir] || TEST_FIXTURES
9
+ @spec_fixtures_dir = options[:spec_fixtures_dir] || SPEC_FIXTURES
6
10
  end
7
11
 
8
12
  def spec_fixtures(options={})
9
- all_fixtures = fetch_fixtures(options).select {|f| f =~ /spec/ }
13
+ all_fixtures = fetch_fixtures.select {|f| f =~ /spec/ }
10
14
  filter(all_fixtures, options)
11
15
  end
12
16
 
13
17
  def test_fixtures(options={})
14
- all_fixtures = fetch_fixtures(options).select {|f| f =~ /test/ }
18
+ all_fixtures = fetch_fixtures.select {|f| f =~ /test/ }
15
19
  filter(all_fixtures, options)
16
20
  end
17
21
 
18
22
  private
19
- def fetch_fixtures(options)
23
+ def fetch_fixtures
20
24
  project_fixtures = []
21
- project_fixtures.concat(Dir[TEST_FIXTURES]) unless Dir[TEST_FIXTURES].empty?
22
- project_fixtures.concat(Dir[SPEC_FIXTURES]) unless Dir[SPEC_FIXTURES].empty?
25
+ project_fixtures.concat(fetch_dir_files(self.test_fixtures_dir))
26
+ project_fixtures.concat(fetch_dir_files(self.spec_fixtures_dir))
23
27
  project_fixtures
24
28
  end
29
+
30
+ def fetch_dir_files(dir)
31
+ Dir[dir] unless Dir[dir].empty?
32
+ end
25
33
 
26
34
  def filter(all_fixtures, options)
27
35
  return all_fixtures.select {|f| f =~ options[:matching] } unless options[:matching].nil?
data/lib/fix_to_chix.rb CHANGED
@@ -7,16 +7,16 @@ require 'fix_to_chix/controller'
7
7
 
8
8
  module FixToChix
9
9
 
10
- VERSION = '0.0.1'
11
-
12
- APP_ROOT = "." # have to call fixtochix from the app base
10
+ VERSION = '0.0.2'
11
+
12
+ APP_ROOT = "." # need to call fixtochix command from your app base
13
13
 
14
14
  TEST_FIXTURES = "#{APP_ROOT}/test/fixtures/*"
15
15
  SPEC_FIXTURES = "#{APP_ROOT}/spec/fixtures/*"
16
16
 
17
17
  SPEC_TARGET = "#{APP_ROOT}/spec"
18
18
  TEST_TARGET = "#{APP_ROOT}/test"
19
-
19
+
20
20
  SPEC_TARGET_FILE = "#{SPEC_TARGET}/factories.rb"
21
21
  TEST_TARGET_FILE = "#{TEST_TARGET}/factories.rb"
22
22
 
@@ -2,10 +2,20 @@ require File.dirname(__FILE__) + '/spec_helper.rb'
2
2
 
3
3
  describe FixToChix::Controller do
4
4
 
5
- it "should parse it all" do
6
- selector = mock('FixtureSelector', :spec_fixtures => [], :test_fixtures => [])
7
- FixToChix::FixtureSelector.should_receive(:new).and_return(selector)
5
+ it "should parse spec fixtures" do
6
+ selector = mock('FixtureSelector', :spec_fixtures => ["spec_fix"], :test_fixtures => [])
7
+ FixToChix::FixtureSelector.stub!(:new).and_return(selector)
8
+ FixToChix::FixtureParser.should_receive(:new).with("spec_fix").and_return(mock('selector', :parse_fixture => "", :output_buffer => "buff"))
9
+ FixToChix::FactoryWriter.should_receive(:write).with("buff", FixToChix::SPEC_TARGET_FILE)
8
10
  FixToChix::Controller.parse_it_all!
9
11
  end
10
12
 
13
+ it "should parse test fixtures" do
14
+ selector = mock('FixtureSelector', :spec_fixtures => [], :test_fixtures => ["test_fix"])
15
+ FixToChix::FixtureSelector.stub!(:new).and_return(selector)
16
+ FixToChix::FixtureParser.should_receive(:new).with("test_fix").and_return(mock('selector', :parse_fixture => "", :output_buffer => "buff"))
17
+ FixToChix::FactoryWriter.should_receive(:write).with("buff", FixToChix::TEST_TARGET_FILE)
18
+ FixToChix::Controller.parse_it_all!
19
+ end
20
+
11
21
  end
@@ -2,4 +2,10 @@ require File.dirname(__FILE__) + '/spec_helper.rb'
2
2
 
3
3
  describe FixToChix::FactoryWriter, "writing factories" do
4
4
 
5
+ it "should write the output buffer" do
6
+ File.should_receive(:open).with("factories_file", 'a').and_yield(mock("file handler", :puts => ""))
7
+ buffer = mock("output buffer", :each => "bla")
8
+ FixToChix::FactoryWriter.write(buffer, "factories_file")
9
+ end
10
+
5
11
  end
@@ -0,0 +1,10 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe FixToChix do
4
+
5
+ it "should parse it all" do
6
+ FixToChix::Controller.should_receive(:parse_it_all!)
7
+ FixToChix.execute
8
+ end
9
+
10
+ end
@@ -3,11 +3,11 @@ require File.dirname(__FILE__) + '/spec_helper.rb'
3
3
  describe FixToChix::FixtureParser do
4
4
 
5
5
  it "raises error if no fixture file passed as argument" do
6
- lambda { FixToChix::FixtureParser.new }.should raise_error
6
+ lambda { FixToChix::FixtureParser.new }.should raise_error(ArgumentError)
7
7
  end
8
8
 
9
9
  it "raises error if file not found" do
10
- lambda { FixToChix::FixtureParser.new("bla/bla/bla") }.should raise_error
10
+ lambda { FixToChix::FixtureParser.new("bla/bla/bla") }.should raise_error(FileNotFoundError)
11
11
  end
12
12
 
13
13
  describe "reading file" do
@@ -72,7 +72,12 @@ describe FixToChix::FixtureParser do
72
72
  @parser.output_buffer.join.should match(/a.blog 'I write a blog'/)
73
73
  @parser.output_buffer.join.should match(/a.age 900/)
74
74
  end
75
-
75
+
76
+ it "generates compound file if fixture name is :one or :two" do
77
+ @parser.factory_name("one").should == "author_one"
78
+ @parser.factory_name("anakin").should == "anakin"
79
+ end
80
+
76
81
  end
77
82
 
78
83
  end
@@ -1,20 +1,38 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper.rb'
2
2
 
3
- describe FixToChix::FixtureSelector, "selecting fixtures" do
4
-
5
- before do
6
- @ftc_fixture_selector = FixToChix::FixtureSelector.new
7
- @ftc_fixture_selector.stub!(:fetch_fixtures).and_return(["spec/fixtures/users.yml","spec/fixtures/user_items.yml", "test/posts.yml"])
8
- end
3
+ describe FixToChix::FixtureSelector do
9
4
 
10
- it "selects all fixtures by default" do
11
- @ftc_fixture_selector.spec_fixtures.should == ["spec/fixtures/users.yml","spec/fixtures/user_items.yml"]
12
- @ftc_fixture_selector.test_fixtures.should == ["test/posts.yml"]
13
- end
5
+ describe "selecting fixtures" do
6
+
7
+ before do
8
+ @selector = FixToChix::FixtureSelector.new
9
+ @selector.stub!(:test_fixtures_dir).and_return("test")
10
+ @selector.stub!(:spec_fixtures_dir).and_return("spec")
11
+ @selector.stub!(:fetch_dir_files).with("test").and_return(["test/posts.yml"])
12
+ @selector.stub!(:fetch_dir_files).with("spec").and_return(["spec/fixtures/users.yml","spec/fixtures/user_items.yml"])
13
+ end
14
+
15
+ it "selects all fixtures by default" do
16
+ @selector.spec_fixtures.should == ["spec/fixtures/users.yml","spec/fixtures/user_items.yml"]
17
+ @selector.test_fixtures.should == ["test/posts.yml"]
18
+ end
19
+
20
+ it "selects matching with regexp" do
21
+ @selector.spec_fixtures(:matching => /item/).should == ["spec/fixtures/user_items.yml"]
22
+ @selector.test_fixtures(:matching => /user/).should == []
23
+ end
14
24
 
15
- it "selects matching with regexp" do
16
- @ftc_fixture_selector.spec_fixtures(:matching => /item/).should == ["spec/fixtures/user_items.yml"]
17
- @ftc_fixture_selector.test_fixtures(:matching => /user/).should == []
18
25
  end
19
26
 
27
+ describe "overriding default values" do
28
+ it "changes test fixture dir" do
29
+ @selector = FixToChix::FixtureSelector.new(:test_fixtures_dir => "/some/other")
30
+ @selector.test_fixtures_dir.should == "/some/other"
31
+ end
32
+
33
+ it "changes spec fixture dir" do
34
+ @selector = FixToChix::FixtureSelector.new(:spec_fixtures_dir => "/some/other")
35
+ @selector.spec_fixtures_dir.should == "/some/other"
36
+ end
37
+ end
20
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fix_to_chix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caike Souza
@@ -70,6 +70,7 @@ files:
70
70
  - script/generate
71
71
  - spec/controller_spec.rb
72
72
  - spec/factory_writer_spec.rb
73
+ - spec/fix_to_chix_spec.rb
73
74
  - spec/fixture_parser_spec.rb
74
75
  - spec/fixture_selector_spec.rb
75
76
  - spec/fixtures/authors.yml