14406: Merge branch 'master'
[arvados.git] / services / api / test / unit / commit_test.rb
index a8594169fb3c7567aa95a00922be6350ac41ac52..af365b19e2e224b70946982cabe59c2c2fd8cb77 100644 (file)
@@ -1,3 +1,7 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 require 'test_helper'
 require 'helpers/git_test_helper'
 
@@ -22,6 +26,14 @@ class CommitTest < ActiveSupport::TestCase
     end
   end
 
+  def must_pipe(cmd)
+    begin
+      return IO.read("|#{cmd}")
+    ensure
+      assert $?.success?
+    end
+  end
+
   [
    'https://github.com/curoverse/arvados.git',
    'http://github.com/curoverse/arvados.git',
@@ -75,6 +87,47 @@ class CommitTest < ActiveSupport::TestCase
     assert $?.success?
   end
 
+  def with_foo_repository
+    Dir.chdir("#{Rails.configuration.git_repositories_dir}/#{repositories(:foo).uuid}") do
+      must_pipe("git checkout master 2>&1")
+      yield
+    end
+  end
+
+  test 'tag_in_internal_repository, new non-tip sha1 in local repo' do
+    tag = "tag#{rand(10**10)}"
+    sha1 = nil
+    with_foo_repository do
+      must_pipe("git checkout -b branch-#{rand(10**10)} 2>&1")
+      must_pipe("echo -n #{tag.shellescape} >bar")
+      must_pipe("git add bar")
+      must_pipe("git -c user.email=x@x -c user.name=X commit -m -")
+      sha1 = must_pipe("git log -n1 --format=%H").strip
+      must_pipe("git rm bar")
+      must_pipe("git -c user.email=x@x -c user.name=X commit -m -")
+    end
+    Commit.tag_in_internal_repository 'active/foo', sha1, tag
+    gitint = "git --git-dir #{Rails.configuration.git_internal_dir.shellescape}"
+    assert_match(/^commit /, IO.read("|#{gitint} show #{tag.shellescape}"))
+    assert $?.success?
+  end
+
+  test 'tag_in_internal_repository, new unreferenced sha1 in local repo' do
+    tag = "tag#{rand(10**10)}"
+    sha1 = nil
+    with_foo_repository do
+      must_pipe("echo -n #{tag.shellescape} >bar")
+      must_pipe("git add bar")
+      must_pipe("git -c user.email=x@x -c user.name=X commit -m -")
+      sha1 = must_pipe("git log -n1 --format=%H").strip
+      must_pipe("git reset --hard HEAD^")
+    end
+    Commit.tag_in_internal_repository 'active/foo', sha1, tag
+    gitint = "git --git-dir #{Rails.configuration.git_internal_dir.shellescape}"
+    assert_match(/^commit /, IO.read("|#{gitint} show #{tag.shellescape}"))
+    assert $?.success?
+  end
+
   # In active/shabranchnames, "7387838c69a21827834586cc42b467ff6c63293b" is
   # both a commit hash, and the name of a branch that begins from that same
   # commit.