expansions 0.0.6

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.
Files changed (43) hide show
  1. data/.gitignore +3 -0
  2. data/.rvmrc +1 -0
  3. data/Gemfile +4 -0
  4. data/Guardfile +5 -0
  5. data/README.md +61 -0
  6. data/Rakefile +1 -0
  7. data/bin/expansions +4 -0
  8. data/expansions.gemspec +29 -0
  9. data/lib/core/array.rb +8 -0
  10. data/lib/core/cli_interface.rb +39 -0
  11. data/lib/core/copy.rb +19 -0
  12. data/lib/core/copy_to_target.rb +11 -0
  13. data/lib/core/enumerable_extensions.rb +11 -0
  14. data/lib/core/erb_template_file.rb +31 -0
  15. data/lib/core/expansion.rb +79 -0
  16. data/lib/core/file.rb +32 -0
  17. data/lib/core/file_merge.rb +45 -0
  18. data/lib/core/kernel.rb +35 -0
  19. data/lib/core/log.rb +15 -0
  20. data/lib/core/mustache_template_file.rb +9 -0
  21. data/lib/core/shell.rb +7 -0
  22. data/lib/core/shell_action_against_file.rb +10 -0
  23. data/lib/core/string.rb +5 -0
  24. data/lib/core/template_processors.rb +22 -0
  25. data/lib/core/template_visitor.rb +18 -0
  26. data/lib/core/version.rb +3 -0
  27. data/lib/expansions.rb +24 -0
  28. data/spec/spec_helper.rb +50 -0
  29. data/spec/specs/array_spec.rb +20 -0
  30. data/spec/specs/copy_spec.rb +87 -0
  31. data/spec/specs/copy_to_target_spec.rb +22 -0
  32. data/spec/specs/enumerable_extensions_spec.rb +27 -0
  33. data/spec/specs/erb_template_file_spec.rb +63 -0
  34. data/spec/specs/expansion_spec.rb +107 -0
  35. data/spec/specs/file_merge_spec.rb +131 -0
  36. data/spec/specs/file_spec.rb +79 -0
  37. data/spec/specs/kernel_spec.rb +56 -0
  38. data/spec/specs/mustache_template_file_spec.rb +48 -0
  39. data/spec/specs/shell_action_against_file_spec.rb +22 -0
  40. data/spec/specs/string_spec.rb +11 -0
  41. data/spec/specs/template_processors_spec.rb +62 -0
  42. data/spec/specs/template_visitor_spec.rb +84 -0
  43. metadata +215 -0
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ module Expansions
4
+ class AFakeVisitor
5
+ def initialize
6
+ array :sources do|a|
7
+ a.readable
8
+ a.writable
9
+ a.mutator :run_using
10
+ end
11
+ end
12
+ end
13
+ describe Copy do
14
+ let(:sources){[]}
15
+ let(:copy_visitor){AFakeVisitor.new}
16
+
17
+ before (:each) do
18
+ @sut = Copy.new(copy_visitor)
19
+ @sut.sources = sources
20
+ end
21
+
22
+ context "when a folder is registered" do
23
+ let(:folder){"item"}
24
+
25
+ before (:each) do
26
+ @sut.folder(folder)
27
+ end
28
+
29
+ it "should be placed in the list of sources" do
30
+ sources[0].should == folder
31
+ end
32
+ end
33
+
34
+ context "when the contents of a folder is registered" do
35
+ let(:folder){"item"}
36
+
37
+ before (:each) do
38
+ @sut.contents(folder)
39
+ end
40
+
41
+ it "should be placed in the list of sources" do
42
+ sources[0].should == "#{folder}/."
43
+ end
44
+ end
45
+
46
+ context "when copying contents in a set of folders" do
47
+ let(:folder){"item"}
48
+
49
+ before (:each) do
50
+ @sut.all_contents_in(%w[1 2])
51
+ end
52
+
53
+ it "should place all of the folders in the sources set" do
54
+ sources[0].should == "1/."
55
+ sources[1].should == "2/."
56
+ end
57
+ end
58
+
59
+ context "when copying folders in a set of folders" do
60
+ let(:folder){"item"}
61
+
62
+ before (:each) do
63
+ @sut.all_folders_in(%w[1 2])
64
+ end
65
+
66
+ it "should place all of the folders in the sources set" do
67
+ sources[0].should == "1"
68
+ sources[1].should == "2"
69
+ end
70
+ end
71
+ context "when expanding all of the items" do
72
+ let(:folder){"item"}
73
+
74
+ before (:each) do
75
+ %w[1 2 3].each{|item| sources << item}
76
+ end
77
+
78
+ before (:each) do
79
+ @sut.run
80
+ end
81
+
82
+ it "should copy each of the sources tot he target" do
83
+ copy_visitor.sources.count.should == 3
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ module Expansions
4
+ describe CopyToTarget do
5
+ context "when performing a copy" do
6
+ let(:target){"the_target"}
7
+ let(:shell){fake}
8
+ let(:source){"the_source"}
9
+
10
+ before (:each) do
11
+ Shell.stub(:instance).and_return(shell)
12
+ @sut = CopyToTarget.new(target)
13
+ end
14
+ before (:each) do
15
+ @sut.run_using(source)
16
+ end
17
+ it "should copy the source to the target" do
18
+ shell.should have_received(:run,"cp -rf #{source} #{target}")
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Enumerable do
4
+ class Visitor
5
+ attr_accessor :items_processed
6
+
7
+ def initialize
8
+ @items_processed = 0
9
+ end
10
+ def run_using(item)
11
+ @items_processed += 1
12
+ end
13
+ end
14
+ context "when processing each item with a visitor" do
15
+ let(:visitor){Visitor.new}
16
+ let(:items){(1..10).to_a}
17
+
18
+ before (:each) do
19
+ items.process_all_items_using(visitor)
20
+ end
21
+
22
+ it "should run each item against the visitor" do
23
+ visitor.items_processed.should == 10
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ module Expansions
4
+ describe ERBTemplateFile do
5
+ before (:each) do
6
+ @original_template = <<-original
7
+ This is the first line @item@
8
+ This is the second line @item@
9
+ original
10
+ end
11
+ context "when preparing a template" do
12
+ let(:sut){ERBTemplateFile.new}
13
+
14
+ before (:each) do
15
+ @result = sut.prepare_template(@original_template)
16
+ end
17
+
18
+ it "should replace all occurences of @item@ with <%=item %>" do
19
+ @expected = <<-template
20
+ This is the first line <%= item %>
21
+ This is the second line <%= item %>
22
+ template
23
+
24
+ @result.should == @expected
25
+ end
26
+ end
27
+ context "when processing" do
28
+ let(:item){"yo"}
29
+ let(:file){fake}
30
+ let(:file_name){"blah.rb"}
31
+ let(:sut){ERBTemplateFile.new}
32
+
33
+ before (:each) do
34
+ @filesystem = RelativeFileSystem.new
35
+ @output = RelativeFileSystem.file_name("out.rb")
36
+ @original_template = <<-original
37
+ This is the first line @item@
38
+ This is the second line @item@
39
+ original
40
+
41
+ File.stub(:read).with(file_name).and_return(@original_template)
42
+ end
43
+
44
+ after(:each) do
45
+ @filesystem.teardown
46
+ end
47
+
48
+ before (:each) do
49
+ sut.process(:input => file_name,:output => @output,:binding => binding)
50
+ end
51
+
52
+ it "should expand everything" do
53
+ @expected = <<-template
54
+ This is the first line #{item}
55
+ This is the second line #{item}
56
+ template
57
+
58
+ File.read_all_text(@output).should == @expected
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+
3
+ module Expansions
4
+ describe Expansion do
5
+ let(:sources){[]}
6
+ let(:mark_executable_visitor){fake}
7
+ let(:dos_2_unix_visitor){fake}
8
+ let(:rm_rf_visitor){fake}
9
+ let(:module_factory){fake}
10
+
11
+ context "when specifying a copy" do
12
+ before (:each) do
13
+ @sut = Expansion.new
14
+ end
15
+
16
+ before (:each) do
17
+ @sut.copy_to "blah" do
18
+ end
19
+ end
20
+
21
+ it "should not affect the merges" do
22
+ @sut.files_to_merge.count.should == 0
23
+ end
24
+
25
+ it "should add a new copy element to the list of copies to be performed" do
26
+ @sut.copies.values.count.should == 1
27
+ @sut.copies[:blah].should be_an(Copy)
28
+ end
29
+ end
30
+
31
+ context "when specifying a merge" do
32
+ before (:each) do
33
+ @sut = Expansion.new
34
+ end
35
+ before (:each) do
36
+ @sut.merge_to "blah" do
37
+ end
38
+ end
39
+
40
+ it "should not alter the copy files" do
41
+ @sut.copies.count.should == 0
42
+ end
43
+
44
+ it "should add a new copy element to the list of copies to be performed" do
45
+ @sut.files_to_merge.values.count.should == 1
46
+ @sut.files_to_merge[:blah].should be_an(FileMerge)
47
+ end
48
+ end
49
+
50
+ context "when a cleanup is specified" do
51
+ before (:each) do
52
+ @sut = Expansion.new
53
+ end
54
+ before (:each) do
55
+ @sut.cleanup do
56
+ end
57
+ end
58
+ it "should be added to the set of cleanup blocks" do
59
+ @sut.cleanup_items.count.should == 1
60
+ end
61
+ end
62
+
63
+ class FakeVisitor
64
+ attr_accessor :items
65
+ def initialize
66
+ @items = 0
67
+ end
68
+ def run_using(file)
69
+ @items+=1
70
+ end
71
+ end
72
+ context "when a set of templates is specified" do
73
+ let(:path){"**/*"}
74
+ let(:template_visitor){FakeVisitor.new}
75
+ before (:each) do
76
+ TemplateVisitor.stub(:instance).and_return(template_visitor)
77
+ @files = %w[1 2 3 4]
78
+ Kernel.stub(:glob).with(path).and_return(@files)
79
+ @sut = Expansion.new
80
+ @sut.globber = lambda{|the_path|
81
+ the_path.should == path
82
+ return @files
83
+ }
84
+ end
85
+ before (:each) do
86
+ @sut.look_for_templates_in(path)
87
+ end
88
+
89
+ it "should process each of the loaded templates using the template visitor" do
90
+ template_visitor.items.should == 4
91
+ end
92
+ end
93
+
94
+ context "when a before is specified" do
95
+ before (:each) do
96
+ @sut = Expansion.new
97
+ end
98
+ before (:each) do
99
+ @sut.before do
100
+ end
101
+ end
102
+ it "should be added to the set of setup blocks" do
103
+ @sut.setup_items.count.should == 1
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,131 @@
1
+ require 'spec_helper'
2
+
3
+ module Expansions
4
+ describe FileMerge do
5
+ context "when a file is added to the merge" do
6
+ let(:file){"file.rb"}
7
+ let(:output_file){"out_file.rb"}
8
+ let(:sut){FileMerge.new(output_file)}
9
+ before (:each) do
10
+ File.stub(:exists?).with(file).and_return(true)
11
+ end
12
+ context "and it is not already in the list" do
13
+ before (:each) do
14
+ sut.add(file)
15
+ end
16
+ it "should be added to the list of before files" do
17
+ sut.before_files[0].should == file
18
+ end
19
+ end
20
+ context "and it is already in the list" do
21
+ before (:each) do
22
+ sut.before_files << file
23
+ end
24
+ before (:each) do
25
+ sut.add(file)
26
+ end
27
+ it "should not be added to the list of before files" do
28
+ sut.before_files.count.should == 1
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ context "when a file is added after the original contents" do
35
+ let(:file){"file.rb"}
36
+ let(:output_file){"out_file.rb"}
37
+ let(:sut){FileMerge.new(output_file)}
38
+ before (:each) do
39
+ File.stub(:exists?).with(file).and_return(true)
40
+ end
41
+
42
+ before (:each) do
43
+ sut.add_after_original_contents(file)
44
+ end
45
+
46
+ it "should be added to the set of after files" do
47
+ sut.after_files[0].should == file
48
+ end
49
+ end
50
+
51
+ context "when a set of files are merged to a target file" do
52
+ let(:target){RelativeFileSystem.file_name("merged.rb")}
53
+ let(:sut){FileMerge.new(target)}
54
+ before (:each) do
55
+ @file_system = RelativeFileSystem.new
56
+ @file_system.write_file("1.rb","first\n")
57
+ @file_system.write_file("2.rb","second\n")
58
+ @file_system.write_file("3.rb","third\n")
59
+ end
60
+ after (:each) do
61
+ @file_system.teardown
62
+ end
63
+ before (:each) do
64
+ files = [1,2,3].map{|item| RelativeFileSystem.file_name("#{item}.rb")}.to_a
65
+ File.open_for_write(target) do|file|
66
+ sut.merge_files(files,file)
67
+ end
68
+ end
69
+ it "the target file should contain all of the file contents" do
70
+ IO.read(target).should == "first\nsecond\nthird\n"
71
+ end
72
+ end
73
+
74
+ context "when a merge occurs" do
75
+ let(:target){RelativeFileSystem.file_name("original.rb")}
76
+ let(:sut){FileMerge.new(target)}
77
+ let(:first){"1.rb"}
78
+ let(:second){"2.rb"}
79
+ let(:third){"3.rb"}
80
+ let(:original){"original.rb"}
81
+
82
+ before (:each) do
83
+ @file_system = RelativeFileSystem.new
84
+ @file_system.write_file("1.rb","first\n")
85
+ @file_system.write_file("2.rb","second\n")
86
+ @file_system.write_file("3.rb","third\n")
87
+ @file_system.write_file("original.rb","original\n")
88
+ end
89
+
90
+ after (:each) do
91
+ @file_system.teardown
92
+ end
93
+
94
+ context "and original contents are flagged to be read" do
95
+ before (:each) do
96
+ sut.add_before_original_contents(RelativeFileSystem.file_name(first))
97
+ sut.add_before_original_contents(RelativeFileSystem.file_name(second))
98
+ sut.add_after_original_contents(RelativeFileSystem.file_name(third))
99
+ end
100
+
101
+ before (:each) do
102
+ sut.run
103
+ end
104
+
105
+ it "should output the correct contents" do
106
+ contents = IO.read(target)
107
+ contents.should == "first\nsecond\noriginal\nthird\n"
108
+ end
109
+ end
110
+
111
+ context "and original contents are not flagged to be read" do
112
+ before (:each) do
113
+ sut.add_before_original_contents(RelativeFileSystem.file_name(first))
114
+ sut.add_before_original_contents(RelativeFileSystem.file_name(second))
115
+ sut.add_after_original_contents(RelativeFileSystem.file_name(third))
116
+ sut.dont_read_original_file_contents
117
+ end
118
+
119
+ before (:each) do
120
+ sut.run
121
+ end
122
+
123
+ it "should output the correct contents" do
124
+ contents = IO.read(target)
125
+ contents.should == "first\nsecond\nthird\n"
126
+ end
127
+ end
128
+
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ module Expansions
4
+ describe File do
5
+ let(:file_system){RelativeFileSystem.new}
6
+
7
+ context "when opening a file for read" do
8
+ let(:file){RelativeFileSystem.file_name("blah.rb")}
9
+ before (:each) do
10
+ file_system.write_file("blah.rb","sdf\nnsfsf")
11
+ @number_of_lines_visited = 0
12
+ end
13
+
14
+ after (:each) do
15
+ file_system.teardown
16
+ end
17
+
18
+ before (:each) do
19
+ File.open_for_read(file){|line| @number_of_lines_visited +=1}
20
+ end
21
+
22
+ it "should open the file and run the block against each line in the file" do
23
+ @number_of_lines_visited.should == 2
24
+ end
25
+ end
26
+
27
+ context "when reading all the text in a file" do
28
+ let(:file){RelativeFileSystem.file_name("blah.rb")}
29
+ let(:contents){<<-content
30
+ hello
31
+ this is
32
+ a file
33
+ content
34
+ }
35
+ before (:each) do
36
+ file_system.write_file("blah.rb",contents)
37
+ end
38
+
39
+ after (:each) do
40
+ file_system.teardown
41
+ end
42
+
43
+ before (:each) do
44
+ @result = File.read_all_text(file)
45
+ end
46
+
47
+ it "should return the total contents" do
48
+ @result.should == contents
49
+ end
50
+ end
51
+
52
+ context "when reading all the text in a file after skipping lines" do
53
+ let(:file){RelativeFileSystem.file_name("blah.rb")}
54
+ let(:start){"this is the file\n"}
55
+ let(:contents){<<-content
56
+ hello
57
+ this is
58
+ a file
59
+ content
60
+ }
61
+ let(:all_contents){"#{start}#{contents}"}
62
+ before (:each) do
63
+ file_system.write_file("blah.rb",all_contents)
64
+ end
65
+
66
+ after (:each) do
67
+ file_system.teardown
68
+ end
69
+
70
+ before (:each) do
71
+ @result = File.read_all_text_after_skipping_lines(file,1)
72
+ end
73
+
74
+ it "should return the contents except the skipped lines" do
75
+ @result.should == contents
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ module Kernel
4
+ describe Kernel do
5
+ context "when globbing for files" do
6
+ before (:each) do
7
+ @file_system = RelativeFileSystem.new
8
+ @file_system.write_file("1.rb","sdfsdfsdf")
9
+ @file_system.write_file("2.rb","sdfsdfsdf")
10
+ (1..2).each do|item|
11
+ @file_system.write_file("#{item}.rb","sdfsdf")
12
+ @file_system.write_file(".#{item}","sdfsdf")
13
+ (1..2).each do|folder|
14
+ new_folder = RelativeFileSystem.file_name("#{folder}")
15
+ file = File.join(new_folder,"#{folder}.rb")
16
+ @file_system.write_file(file,"sdfsd")
17
+ end
18
+ end
19
+ end
20
+
21
+ after (:each) do
22
+ @file_system.teardown
23
+ end
24
+
25
+ context "and no block is given" do
26
+ before (:each) do
27
+ @result = glob(File.join(RelativeFileSystem.base_path,"**/*"))
28
+ end
29
+
30
+ it "should return all files in path including dotfiles" do
31
+ @result.count.should == 38
32
+ end
33
+ end
34
+ context "and a block is given" do
35
+ before (:each) do
36
+ @items_visited = 0
37
+ end
38
+ before (:each) do
39
+ @result = glob(File.join(RelativeFileSystem.base_path,"**/*")) do |file|
40
+ @items_visited += 1
41
+ end
42
+ end
43
+
44
+ it "should return all files in path including dotfiles" do
45
+ @result.count.should == 38
46
+ end
47
+
48
+ it "should have run the block against each file" do
49
+ @items_visited.should == 38
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ require 'configatron'
3
+
4
+ module Expansions
5
+ describe MustacheTemplateFile do
6
+ before (:each) do
7
+ @original_template = <<-original
8
+ This is the first line {{ item }}
9
+ This is the second line {{ item }}
10
+ original
11
+ end
12
+ context "when processing" do
13
+ let(:item){"yo"}
14
+ let(:sut){MustacheTemplateFile.new}
15
+
16
+ configatron.configure_from_hash :hello => "world"
17
+
18
+ before (:each) do
19
+ @filesystem = RelativeFileSystem.new
20
+ @output = RelativeFileSystem.file_name("out.rb")
21
+ @file_name = RelativeFileSystem.file_name("blah.rb")
22
+
23
+ @filesystem.write_file("blah.rb",@original_template)
24
+
25
+ configatron.stub(:to_hash).and_return(:item => item)
26
+ File.stub(:read_all_text).with(@file_name).and_return(@original_template)
27
+ end
28
+
29
+ after(:each) do
30
+ @filesystem.teardown
31
+ end
32
+
33
+ before (:each) do
34
+ sut.process(:input => @file_name,:output => @output)
35
+ end
36
+
37
+ it "should expand everything" do
38
+ @expected = <<-template
39
+ This is the first line #{item}
40
+ This is the second line #{item}
41
+ template
42
+
43
+ IO.read(@output).should == @expected
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ module Expansions
4
+ describe ShellActionAgainstFile do
5
+ context "when processing a file name" do
6
+ let(:file){"blah.rb"}
7
+ let(:shell){fake}
8
+ let(:sut){ShellActionAgainstFile.new("rm")}
9
+
10
+ before (:each) do
11
+ Shell.stub(:instance).and_return(shell)
12
+ end
13
+
14
+ before (:each) do
15
+ sut.run_using(file)
16
+ end
17
+ it "should run the shell action against the file" do
18
+ shell.should have_received(:run,"rm #{file}")
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ module Expansions
4
+ describe String do
5
+ context "when formatted as a home item" do
6
+ it "should return the item as a path with the home directory as the root" do
7
+ "hello".as_home_file.should == File.join(ENV["HOME"],"hello")
8
+ end
9
+ end
10
+ end
11
+ end