|
2 | 2 | require 'annotate/annotate_routes'
|
3 | 3 |
|
4 | 4 | describe AnnotateRoutes do
|
| 5 | + ROUTE_FILE = "config/routes.rb" |
| 6 | + ANNOTATION_ADDED = "#{ROUTE_FILE} annotated." |
| 7 | + ANNOTATION_REMOVED = "Removed annotations from #{ROUTE_FILE}." |
| 8 | + FILE_UNCHANGED = "#{ROUTE_FILE} unchanged." |
5 | 9 |
|
6 | 10 | def mock_file(stubs={})
|
7 | 11 | @mock_file ||= double(File, stubs)
|
8 | 12 | end
|
9 | 13 |
|
10 | 14 | it "should check if routes.rb exists" do
|
11 |
| - expect(File).to receive(:exists?).with("config/routes.rb").and_return(false) |
| 15 | + expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(false) |
12 | 16 | expect(AnnotateRoutes).to receive(:puts).with("Can`t find routes.rb")
|
13 | 17 | AnnotateRoutes.do_annotations
|
14 | 18 | end
|
15 | 19 |
|
16 |
| - describe "When Annotating, with older Rake Versions" do |
| 20 | + describe "When adding" do |
| 21 | + before(:each) do |
| 22 | + expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true) |
| 23 | + expect(AnnotateRoutes).to receive(:`).with("rake routes").and_return("") |
| 24 | + end |
| 25 | + |
| 26 | + it "should insert annotations if file does not contain annotations" do |
| 27 | + expect(File).to receive(:read).with(ROUTE_FILE).and_return("") |
| 28 | + expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file) |
| 29 | + expect(@mock_file).to receive(:puts).with("\n# == Route Map\n#\n") |
| 30 | + expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) |
| 31 | + |
| 32 | + AnnotateRoutes.do_annotations |
| 33 | + end |
| 34 | + |
| 35 | + it "should skip annotations if file does already contain annotation" do |
| 36 | + expect(File).to receive(:read).with(ROUTE_FILE).and_return("\n# == Route Map\n#\n") |
| 37 | + expect(AnnotateRoutes).to receive(:puts).with(FILE_UNCHANGED) |
| 38 | + |
| 39 | + AnnotateRoutes.do_annotations |
| 40 | + end |
| 41 | + |
| 42 | + end |
| 43 | + |
| 44 | + describe "When adding with older Rake versions" do |
17 | 45 |
|
18 | 46 | before(:each) do
|
19 |
| - expect(File).to receive(:exists?).with("config/routes.rb").and_return(true) |
| 47 | + expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true) |
20 | 48 | expect(AnnotateRoutes).to receive(:`).with("rake routes").and_return("(in /bad/line)\ngood line")
|
21 |
| - expect(File).to receive(:open).with("config/routes.rb", "wb").and_yield(mock_file) |
22 |
| - expect(AnnotateRoutes).to receive(:puts).with("Route file annotated.") |
| 49 | + expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file) |
| 50 | + expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) |
23 | 51 | end
|
24 | 52 |
|
25 | 53 | it "should annotate and add a newline!" do
|
26 |
| - expect(File).to receive(:read).with("config/routes.rb").and_return("ActionController::Routing...\nfoo") |
| 54 | + expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo") |
27 | 55 | expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
|
28 | 56 | AnnotateRoutes.do_annotations
|
29 | 57 | end
|
30 | 58 |
|
31 | 59 | it "should not add a newline if there are empty lines" do
|
32 |
| - expect(File).to receive(:read).with("config/routes.rb").and_return("ActionController::Routing...\nfoo\n") |
| 60 | + expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n") |
33 | 61 | expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
|
34 | 62 | AnnotateRoutes.do_annotations
|
35 | 63 | end
|
36 | 64 |
|
37 | 65 | end
|
38 | 66 |
|
39 |
| - describe "When Annotating, with newer Rake Versions" do |
| 67 | + describe "When adding with newer Rake versions" do |
40 | 68 |
|
41 | 69 | before(:each) do
|
42 |
| - expect(File).to receive(:exists?).with("config/routes.rb").and_return(true) |
| 70 | + expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true) |
43 | 71 | expect(AnnotateRoutes).to receive(:`).with("rake routes").and_return("another good line\ngood line")
|
44 |
| - expect(File).to receive(:open).with("config/routes.rb", "wb").and_yield(mock_file) |
45 |
| - expect(AnnotateRoutes).to receive(:puts).with("Route file annotated.") |
| 72 | + expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file) |
| 73 | + expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) |
46 | 74 | end
|
47 | 75 |
|
| 76 | + |
48 | 77 | it "should annotate and add a newline!" do
|
49 |
| - expect(File).to receive(:read).with("config/routes.rb").and_return("ActionController::Routing...\nfoo") |
| 78 | + expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo") |
50 | 79 | expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
|
51 | 80 | AnnotateRoutes.do_annotations
|
52 | 81 | end
|
53 | 82 |
|
54 | 83 | it "should not add a newline if there are empty lines" do
|
55 |
| - expect(File).to receive(:read).with("config/routes.rb").and_return("ActionController::Routing...\nfoo\n") |
| 84 | + expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n") |
56 | 85 | expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
|
57 | 86 | AnnotateRoutes.do_annotations
|
58 | 87 | end
|
59 | 88 |
|
60 | 89 | it "should add a timestamp when :timestamp is passed" do
|
61 |
| - expect(File).to receive(:read).with("config/routes.rb").and_return("ActionController::Routing...\nfoo") |
| 90 | + expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo") |
62 | 91 | expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map \(Updated \d{4}-\d{2}-\d{2} \d{2}:\d{2}\)\n#\n# another good line\n# good line\n/)
|
63 | 92 | AnnotateRoutes.do_annotations :timestamp => true
|
64 | 93 | end
|
65 | 94 |
|
66 | 95 | end
|
67 | 96 |
|
68 |
| - describe "When Removing Annotation" do |
| 97 | + describe "When removing" do |
69 | 98 |
|
70 | 99 | before(:each) do
|
71 |
| - expect(File).to receive(:exists?).with("config/routes.rb").and_return(true) |
72 |
| - expect(File).to receive(:open).with("config/routes.rb", "wb").and_yield(mock_file) |
73 |
| - expect(AnnotateRoutes).to receive(:puts).with("Removed annotations from routes file.") |
| 100 | + expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true) |
| 101 | + expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file) |
| 102 | + expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_REMOVED) |
74 | 103 | end
|
75 | 104 |
|
76 | 105 | it "should remove trailing annotation and trim trailing newlines, but leave leading newlines alone" do
|
77 |
| - expect(File).to receive(:read).with("config/routes.rb").and_return("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n# == Route Map\n#\n# another good line\n# good line\n") |
| 106 | + expect(File).to receive(:read).with(ROUTE_FILE).and_return("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n# == Route Map\n#\n# another good line\n# good line\n") |
78 | 107 | expect(@mock_file).to receive(:puts).with(/\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n/)
|
79 | 108 | AnnotateRoutes.remove_annotations
|
80 | 109 | end
|
81 | 110 |
|
82 | 111 | it "should remove prepended annotation and trim leading newlines, but leave trailing newlines alone" do
|
83 |
| - expect(File).to receive(:read).with("config/routes.rb").and_return("# == Route Map\n#\n# another good line\n# good line\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n") |
| 112 | + expect(File).to receive(:read).with(ROUTE_FILE).and_return("# == Route Map\n#\n# another good line\n# good line\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n") |
84 | 113 | expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n/)
|
85 | 114 | AnnotateRoutes.remove_annotations
|
86 | 115 | end
|
|
0 commit comments