# HG changeset patch
# Parent af9a9e86815f3785d2e2b5708c965d3336b0bb63
# User Toshi MARUYAMA
mercurial: get client_version correctly even if LANG is set

 * Assumes the version number is of the first line of the output.
 * Uses shellout
 * Uses class' instance variables instead of wide-scope class variables,
   because it's only referenced by class method.
 * Changed not to cache template_path because it isn't a heavy operation.

closes #5117

diff --git a/lib/redmine/scm/adapters/mercurial_adapter.rb b/lib/redmine/scm/adapters/mercurial_adapter.rb
--- a/lib/redmine/scm/adapters/mercurial_adapter.rb
+++ b/lib/redmine/scm/adapters/mercurial_adapter.rb
@@ -30,25 +30,26 @@
         
         class << self
           def client_version
-            @@client_version ||= (hgversion || [])
+            @client_version ||= hgversion
           end
           
           def hgversion  
             # The hg version is expressed either as a
             # release number (eg 0.9.5 or 1.0) or as a revision
             # id composed of 12 hexa characters.
-            theversion = hgversion_from_command_line
-            if theversion.match(/^\d+(\.\d+)+/)
-              theversion.split(".").collect(&:to_i)
-            end
+            hgversion_str.to_s.split('.').map { |e| e.to_i }
           end
           
-          def hgversion_from_command_line
-            %x{#{HG_BIN} --version}.match(/\(version (.*)\)/)[1]
+          def hgversion_str
+            hgversion_str_command_line.to_s[/\d+(\.\d+)+/]
+          end
+
+          def hgversion_str_command_line
+            shellout("#{HG_BIN} --version") { |io| io.gets }.to_s
           end
           
           def template_path
-            @@template_path ||= template_path_for(client_version)
+            template_path_for(client_version)
           end
           
           def template_path_for(version)
diff --git a/test/unit/lib/redmine/scm/adapters/mercurial_adapter_test.rb b/test/unit/lib/redmine/scm/adapters/mercurial_adapter_test.rb
--- a/test/unit/lib/redmine/scm/adapters/mercurial_adapter_test.rb
+++ b/test/unit/lib/redmine/scm/adapters/mercurial_adapter_test.rb
@@ -1,3 +1,4 @@
+
 require File.dirname(__FILE__) + '/../../../../../test_helper'
 begin
   require 'mocha'
@@ -9,13 +10,26 @@
     TEMPLATE_EXTENSION = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_EXTENSION
     
     REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository'
+
+    def setup
+      @adapter = Redmine::Scm::Adapters::MercurialAdapter
+    end
     
     def test_hgversion
-      to_test = { "0.9.5" => [0,9,5],
-                  "1.0" => [1,0],
-                  "1e4ddc9ac9f7+20080325" => nil,
-                  "1.0.1+20080525" => [1,0,1],
-                  "1916e629a29d" => nil}
+      to_test = {
+                    "0.9.5"                 => [0,9,5],
+                    "1.0"                   => [1,0],
+                    "1e4ddc9ac9f7+20080325" => [],
+                    "1.0.1+20080525"        => [1,0,1],
+                    "1916e629a29d"          => [] ,
+                    "1.6"                   => [1,6],
+                    "1.6.1"                 => [1,6,1],
+                    "Mercurial Distributed SCM (version 1.6.3)" => [1,6,3],
+                    ## Italian
+                    # $ make local
+                    # $ LANG=it ./hg --version
+                    "Mercurial SCM Distribuito (versione 1.6.3+61-1c9bb7e00f71)" => [1,6,3],
+                  }
       
       to_test.each do |s, v|
         test_hgversion_for(s, v)
@@ -23,10 +37,14 @@
     end
     
     def test_template_path
-      to_test = { [0,9,5] => "0.9.5",
-                  [1,0] => "1.0",
-                  [] => "1.0",
-                  [1,0,1] => "1.0"}
+      to_test = { 
+                    [0,9,5] => "0.9.5",
+                    [1,0]   => "1.0"  ,
+                    []      => "1.0"  ,
+                    [1,0,1] => "1.0"  ,
+                    [1,6]   => "1.0"  ,
+                    [1,6,1] => "1.0"  ,
+                  }
       
       to_test.each do |v, template|
         test_template_path_for(v, template)
@@ -36,15 +54,14 @@
     private
     
     def test_hgversion_for(hgversion, version)
-      Redmine::Scm::Adapters::MercurialAdapter.expects(:hgversion_from_command_line).returns(hgversion)
-      adapter = Redmine::Scm::Adapters::MercurialAdapter
-      assert_equal version, adapter.hgversion
+      @adapter.expects(:hgversion_str_command_line).returns(hgversion)
+      assert_equal version, @adapter.hgversion
     end
     
     def test_template_path_for(version, template)
-      adapter = Redmine::Scm::Adapters::MercurialAdapter
-      assert_equal "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{template}.#{TEMPLATE_EXTENSION}", adapter.template_path_for(version)
-      assert File.exist?(adapter.template_path_for(version))
+      assert_equal "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{template}.#{TEMPLATE_EXTENSION}",
+                   @adapter.template_path_for(version)
+      assert File.exist?(@adapter.template_path_for(version))
     end
   end
   
