##// END OF EJS Templates
scm: mercurial: add unit lib test for the previous changeset isn't the parent (#7253, #7518)....
Toshi MARUYAMA -
r4672:9f1a63075e89
parent child
Show More
@@ -1,156 +1,167
1 require File.expand_path('../../../../../../test_helper', __FILE__)
1 require File.expand_path('../../../../../../test_helper', __FILE__)
2 begin
2 begin
3 require 'mocha'
3 require 'mocha'
4
4
5 class MercurialAdapterTest < ActiveSupport::TestCase
5 class MercurialAdapterTest < ActiveSupport::TestCase
6
6
7 TEMPLATES_DIR = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATES_DIR
7 TEMPLATES_DIR = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATES_DIR
8 TEMPLATE_NAME = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_NAME
8 TEMPLATE_NAME = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_NAME
9 TEMPLATE_EXTENSION = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_EXTENSION
9 TEMPLATE_EXTENSION = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_EXTENSION
10
10
11 REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository'
11 REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository'
12
12
13 if File.directory?(REPOSITORY_PATH)
13 if File.directory?(REPOSITORY_PATH)
14 def setup
14 def setup
15 @adapter = Redmine::Scm::Adapters::MercurialAdapter.new(REPOSITORY_PATH)
15 @adapter = Redmine::Scm::Adapters::MercurialAdapter.new(REPOSITORY_PATH)
16 end
16 end
17
17
18 def test_hgversion
18 def test_hgversion
19 to_test = { "Mercurial Distributed SCM (version 0.9.5)\n" => [0,9,5],
19 to_test = { "Mercurial Distributed SCM (version 0.9.5)\n" => [0,9,5],
20 "Mercurial Distributed SCM (1.0)\n" => [1,0],
20 "Mercurial Distributed SCM (1.0)\n" => [1,0],
21 "Mercurial Distributed SCM (1e4ddc9ac9f7+20080325)\n" => nil,
21 "Mercurial Distributed SCM (1e4ddc9ac9f7+20080325)\n" => nil,
22 "Mercurial Distributed SCM (1.0.1+20080525)\n" => [1,0,1],
22 "Mercurial Distributed SCM (1.0.1+20080525)\n" => [1,0,1],
23 "Mercurial Distributed SCM (1916e629a29d)\n" => nil,
23 "Mercurial Distributed SCM (1916e629a29d)\n" => nil,
24 "Mercurial SCM Distribuito (versione 0.9.5)\n" => [0,9,5],
24 "Mercurial SCM Distribuito (versione 0.9.5)\n" => [0,9,5],
25 "(1.6)\n(1.7)\n(1.8)" => [1,6],
25 "(1.6)\n(1.7)\n(1.8)" => [1,6],
26 "(1.7.1)\r\n(1.8.1)\r\n(1.9.1)" => [1,7,1]}
26 "(1.7.1)\r\n(1.8.1)\r\n(1.9.1)" => [1,7,1]}
27
27
28 to_test.each do |s, v|
28 to_test.each do |s, v|
29 test_hgversion_for(s, v)
29 test_hgversion_for(s, v)
30 end
30 end
31 end
31 end
32
32
33 def test_template_path
33 def test_template_path
34 to_test = { [0,9,5] => "0.9.5",
34 to_test = { [0,9,5] => "0.9.5",
35 [1,0] => "1.0",
35 [1,0] => "1.0",
36 [] => "1.0",
36 [] => "1.0",
37 [1,0,1] => "1.0",
37 [1,0,1] => "1.0",
38 [1,7] => "1.0",
38 [1,7] => "1.0",
39 [1,7,1] => "1.0" }
39 [1,7,1] => "1.0" }
40 to_test.each do |v, template|
40 to_test.each do |v, template|
41 test_template_path_for(v, template)
41 test_template_path_for(v, template)
42 end
42 end
43 end
43 end
44
44
45 def test_diff
45 def test_diff
46 if @adapter.class.client_version_above?([1, 2])
46 if @adapter.class.client_version_above?([1, 2])
47 assert_nil @adapter.diff(nil, '100000')
47 assert_nil @adapter.diff(nil, '100000')
48 end
48 end
49 assert_nil @adapter.diff(nil, '100000', '200000')
49 assert_nil @adapter.diff(nil, '100000', '200000')
50 [2, '400bb8672109', '400', 400].each do |r1|
50 [2, '400bb8672109', '400', 400].each do |r1|
51 diff1 = @adapter.diff(nil, r1)
51 diff1 = @adapter.diff(nil, r1)
52 if @adapter.class.client_version_above?([1, 2])
52 if @adapter.class.client_version_above?([1, 2])
53 assert_equal 28, diff1.size
53 assert_equal 28, diff1.size
54 buf = diff1[24].gsub(/\r\n|\r|\n/, "")
54 buf = diff1[24].gsub(/\r\n|\r|\n/, "")
55 assert_equal "+ return true unless klass.respond_to?('watched_by')", buf
55 assert_equal "+ return true unless klass.respond_to?('watched_by')", buf
56 else
56 else
57 assert_equal 0, diff1.size
57 assert_equal 0, diff1.size
58 end
58 end
59 [4, 'def6d2f1254a'].each do |r2|
59 [4, 'def6d2f1254a'].each do |r2|
60 diff2 = @adapter.diff(nil,r1,r2)
60 diff2 = @adapter.diff(nil,r1,r2)
61 assert_equal 49, diff2.size
61 assert_equal 49, diff2.size
62 buf = diff2[41].gsub(/\r\n|\r|\n/, "")
62 buf = diff2[41].gsub(/\r\n|\r|\n/, "")
63 assert_equal "+class WelcomeController < ApplicationController", buf
63 assert_equal "+class WelcomeController < ApplicationController", buf
64 diff3 = @adapter.diff('sources/watchers_controller.rb', r1, r2)
64 diff3 = @adapter.diff('sources/watchers_controller.rb', r1, r2)
65 assert_equal 20, diff3.size
65 assert_equal 20, diff3.size
66 buf = diff3[12].gsub(/\r\n|\r|\n/, "")
66 buf = diff3[12].gsub(/\r\n|\r|\n/, "")
67 assert_equal "+ @watched.remove_watcher(user)", buf
67 assert_equal "+ @watched.remove_watcher(user)", buf
68 end
68 end
69 end
69 end
70 end
70 end
71
71
72 def test_diff_made_by_revision
73 if @adapter.class.client_version_above?([1, 2])
74 [16, '16', '4cddb4e45f52'].each do |r1|
75 diff1 = @adapter.diff(nil, r1)
76 assert_equal 5, diff1.size
77 buf = diff1[4].gsub(/\r\n|\r|\n/, "")
78 assert_equal '+0885933ad4f68d77c2649cd11f8311276e7ef7ce tag-init-revision', buf
79 end
80 end
81 end
82
72 def test_cat
83 def test_cat
73 [2, '400bb8672109', '400', 400].each do |r|
84 [2, '400bb8672109', '400', 400].each do |r|
74 buf = @adapter.cat('sources/welcome_controller.rb', r)
85 buf = @adapter.cat('sources/welcome_controller.rb', r)
75 assert buf
86 assert buf
76 lines = buf.split("\r\n")
87 lines = buf.split("\r\n")
77 assert_equal 25, lines.length
88 assert_equal 25, lines.length
78 assert_equal 'class WelcomeController < ApplicationController', lines[17]
89 assert_equal 'class WelcomeController < ApplicationController', lines[17]
79 end
90 end
80 assert_nil @adapter.cat('sources/welcome_controller.rb')
91 assert_nil @adapter.cat('sources/welcome_controller.rb')
81 end
92 end
82
93
83 def test_annotate
94 def test_annotate
84 assert_equal [], @adapter.annotate("sources/welcome_controller.rb").lines
95 assert_equal [], @adapter.annotate("sources/welcome_controller.rb").lines
85 [2, '400bb8672109', '400', 400].each do |r|
96 [2, '400bb8672109', '400', 400].each do |r|
86 ann = @adapter.annotate('sources/welcome_controller.rb', r)
97 ann = @adapter.annotate('sources/welcome_controller.rb', r)
87 assert ann
98 assert ann
88 assert_equal '1', ann.revisions[17].revision
99 assert_equal '1', ann.revisions[17].revision
89 assert_equal '9d5b5b004199', ann.revisions[17].identifier
100 assert_equal '9d5b5b004199', ann.revisions[17].identifier
90 assert_equal 'jsmith', ann.revisions[0].author
101 assert_equal 'jsmith', ann.revisions[0].author
91 assert_equal 25, ann.lines.length
102 assert_equal 25, ann.lines.length
92 assert_equal 'class WelcomeController < ApplicationController', ann.lines[17]
103 assert_equal 'class WelcomeController < ApplicationController', ann.lines[17]
93 end
104 end
94 end
105 end
95
106
96 # TODO filesize etc.
107 # TODO filesize etc.
97 def test_entries
108 def test_entries
98 assert_nil @adapter.entries(nil, '100000')
109 assert_nil @adapter.entries(nil, '100000')
99 [2, '400bb8672109', '400', 400].each do |r|
110 [2, '400bb8672109', '400', 400].each do |r|
100 entries1 = @adapter.entries(nil, r)
111 entries1 = @adapter.entries(nil, r)
101 assert entries1
112 assert entries1
102 assert_equal 3, entries1.size
113 assert_equal 3, entries1.size
103 assert_equal 'sources', entries1[1].name
114 assert_equal 'sources', entries1[1].name
104 assert_equal 'sources', entries1[1].path
115 assert_equal 'sources', entries1[1].path
105 assert_equal 'dir', entries1[1].kind
116 assert_equal 'dir', entries1[1].kind
106 assert_equal 'README', entries1[2].name
117 assert_equal 'README', entries1[2].name
107 assert_equal 'README', entries1[2].path
118 assert_equal 'README', entries1[2].path
108 assert_equal 'file', entries1[2].kind
119 assert_equal 'file', entries1[2].kind
109
120
110 entries2 = @adapter.entries('sources', r)
121 entries2 = @adapter.entries('sources', r)
111 assert entries2
122 assert entries2
112 assert_equal 2, entries2.size
123 assert_equal 2, entries2.size
113 assert_equal 'watchers_controller.rb', entries2[0].name
124 assert_equal 'watchers_controller.rb', entries2[0].name
114 assert_equal 'sources/watchers_controller.rb', entries2[0].path
125 assert_equal 'sources/watchers_controller.rb', entries2[0].path
115 assert_equal 'file', entries2[0].kind
126 assert_equal 'file', entries2[0].kind
116 assert_equal 'welcome_controller.rb', entries2[1].name
127 assert_equal 'welcome_controller.rb', entries2[1].name
117 assert_equal 'sources/welcome_controller.rb', entries2[1].path
128 assert_equal 'sources/welcome_controller.rb', entries2[1].path
118 assert_equal 'file', entries2[1].kind
129 assert_equal 'file', entries2[1].kind
119 end
130 end
120 end
131 end
121
132
122 def test_access_by_nodeid
133 def test_access_by_nodeid
123 path = 'sources/welcome_controller.rb'
134 path = 'sources/welcome_controller.rb'
124 assert_equal @adapter.cat(path, 2), @adapter.cat(path, '400bb8672109')
135 assert_equal @adapter.cat(path, 2), @adapter.cat(path, '400bb8672109')
125 end
136 end
126
137
127 def test_access_by_fuzzy_nodeid
138 def test_access_by_fuzzy_nodeid
128 path = 'sources/welcome_controller.rb'
139 path = 'sources/welcome_controller.rb'
129 # falls back to nodeid
140 # falls back to nodeid
130 assert_equal @adapter.cat(path, 2), @adapter.cat(path, '400')
141 assert_equal @adapter.cat(path, 2), @adapter.cat(path, '400')
131 end
142 end
132
143
133 private
144 private
134
145
135 def test_hgversion_for(hgversion, version)
146 def test_hgversion_for(hgversion, version)
136 @adapter.class.expects(:hgversion_from_command_line).returns(hgversion)
147 @adapter.class.expects(:hgversion_from_command_line).returns(hgversion)
137 assert_equal version, @adapter.class.hgversion
148 assert_equal version, @adapter.class.hgversion
138 end
149 end
139
150
140 def test_template_path_for(version, template)
151 def test_template_path_for(version, template)
141 assert_equal "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{template}.#{TEMPLATE_EXTENSION}",
152 assert_equal "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{template}.#{TEMPLATE_EXTENSION}",
142 @adapter.class.template_path_for(version)
153 @adapter.class.template_path_for(version)
143 assert File.exist?(@adapter.class.template_path_for(version))
154 assert File.exist?(@adapter.class.template_path_for(version))
144 end
155 end
145 else
156 else
146 puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
157 puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
147 def test_fake; assert true end
158 def test_fake; assert true end
148 end
159 end
149 end
160 end
150
161
151 rescue LoadError
162 rescue LoadError
152 class MercurialMochaFake < ActiveSupport::TestCase
163 class MercurialMochaFake < ActiveSupport::TestCase
153 def test_fake; assert(false, "Requires mocha to run those tests") end
164 def test_fake; assert(false, "Requires mocha to run those tests") end
154 end
165 end
155 end
166 end
156
167
General Comments 0
You need to be logged in to leave comments. Login now