# HG changeset patch
# User Georges Racinet <georges.racinet@octobus.net>
# Date 1704480394 -3600
#      Fri Jan 05 19:46:34 2024 +0100
# Branch oldstable
# Node ID 7798ad2840207ddb567759b31c8905c5f00fa9ed
# Parent  4bd70d20817947b6818ff362003c87d754ea2430
LastCommitForPath: switched tests to service fixture pattern

diff --git a/hgitaly/service/tests/test_commit.py b/hgitaly/service/tests/test_commit.py
--- a/hgitaly/service/tests/test_commit.py
+++ b/hgitaly/service/tests/test_commit.py
@@ -148,6 +148,17 @@
         request = CommitLanguagesRequest(repository=self.grpc_repo, **kw)
         return list(self.stub.CommitLanguages(request).languages)
 
+    def last_commit_for_path(self, revision, path, **kw):
+        """Call the method, returning directly commit.id, as bytes"""
+        grpc_repo = kw.pop('grpc_repo', self.grpc_repo)
+        request = LastCommitForPathRequest(repository=grpc_repo,
+                                           revision=revision,
+                                           path=path,
+                                           **kw)
+        return pycompat.sysbytes(
+            self.stub.LastCommitForPath(request).commit.id
+        )
+
 
 @pytest.fixture
 def commit_fixture_empty_repo(grpc_channel, server_repos_root):
@@ -657,9 +668,9 @@
     assert do_rpc(topic_hex + b'..branch/default') == 2
 
 
-def test_last_commit_for_path(grpc_channel, server_repos_root):
-    grpc_stub = CommitServiceStub(grpc_channel)
-    wrapper, grpc_repo = make_empty_repo(server_repos_root)
+def test_last_commit_for_path(commit_fixture_empty_repo):
+    fixture = commit_fixture_empty_repo
+    wrapper = fixture.repo_wrapper
 
     # prepare repo as:
     #
@@ -675,61 +686,44 @@
     (wrapper.path / 'sub').mkdir()
     ctx2 = wrapper.commit_file('sub/bar', branch='other', parent=ctx)
 
-    def do_rpc(revision, path, **kw):
-        """Call the method, returning directly commit.id, as bytes"""
-        request = LastCommitForPathRequest(repository=grpc_repo,
-                                           revision=revision,
-                                           path=path,
-                                           **kw)
-        return pycompat.sysbytes(
-            grpc_stub.LastCommitForPath(request).commit.id)
+    lcfp = fixture.last_commit_for_path
+    # case with path resolving to the repo root
+    assert lcfp(revision=b'branch/other', path=b'/') == ctx2.hex()
+    assert lcfp(revision=b'branch/other', path=b'') == ctx2.hex()
 
-    # case with path resolving to the repo root
-    assert do_rpc(revision=b'branch/other', path=b'/') == ctx2.hex()
-    assert do_rpc(revision=b'branch/other', path=b'') == ctx2.hex()
-
-    assert do_rpc(revision=b'branch/default', path=b'foo') == ctx1.hex()
-    assert do_rpc(revision=b'branch/other', path=b'foo') == ctx.hex()
-    assert do_rpc(revision=ctx2.hex(), path=b'foo') == ctx.hex()
-    assert not do_rpc(revision=b'branch/default', path=b'sub/bar')
-    assert do_rpc(revision=b'branch/other', path=b'sub/bar') == ctx2.hex()
+    assert lcfp(revision=b'branch/default', path=b'foo') == ctx1.hex()
+    assert lcfp(revision=b'branch/other', path=b'foo') == ctx.hex()
+    assert lcfp(revision=ctx2.hex(), path=b'foo') == ctx.hex()
+    assert not lcfp(revision=b'branch/default', path=b'sub/bar')
+    assert lcfp(revision=b'branch/other', path=b'sub/bar') == ctx2.hex()
     # recursive directory matching (see Rails tests for validation
     # that we must match on directories)
-    assert do_rpc(revision=b'branch/other', path=b'sub') == ctx2.hex()
+    assert lcfp(revision=b'branch/other', path=b'sub') == ctx2.hex()
     # with trailing slash already provided
-    assert do_rpc(revision=b'branch/other', path=b'sub/') == ctx2.hex()
+    assert lcfp(revision=b'branch/other', path=b'sub/') == ctx2.hex()
     # pathspecs with wildcards
-    assert do_rpc(revision=b'branch/other', path=b's*/bar') == ctx2.hex()
-    assert do_rpc(revision=b'branch/other', path=b's*r') == ctx2.hex()
+    assert lcfp(revision=b'branch/other', path=b's*/bar') == ctx2.hex()
+    assert lcfp(revision=b'branch/other', path=b's*r') == ctx2.hex()
     # literal pathspecs
-    literal_opts = GlobalOptions(literal_pathspecs=True)
-    assert not do_rpc(revision=b'branch/other',
-                      path=b's*/bar',
-                      global_options=literal_opts,
-                      )
-    assert do_rpc(revision=b'branch/other',
-                  path=b'sub',
-                  global_options=literal_opts,
-                  ) == ctx2.hex()
-    assert do_rpc(revision=b'branch/other',
-                  path=b'sub/',
-                  global_options=literal_opts,
-                  ) == ctx2.hex()
+    literal = GlobalOptions(literal_pathspecs=True)
+    assert not lcfp(b'branch/other', b's*/bar', global_options=literal)
+    assert lcfp(b'branch/other', b'sub', global_options=literal) == ctx2.hex()
+    assert lcfp(b'branch/other', b'sub/', global_options=literal) == ctx2.hex()
 
     # with obsolescence
     wrapper.command('amend', message=b'amended')
     assert ctx2.obsolete()
-    assert do_rpc(revision=ctx2.hex(), path=b'foo') == ctx.hex()
+    assert lcfp(revision=ctx2.hex(), path=b'foo') == ctx.hex()
 
     # with a removal
     (wrapper.path / 'sub/bar').unlink()
     ctx_rm = wrapper.commit(rel_paths=['sub/bar'], message="removed",
                             add_remove=True)
     wrapper.commit_file('foo2')
-    assert do_rpc(revision=b'branch/other', path=b'sub') == ctx_rm.hex()
+    assert lcfp(revision=b'branch/other', path=b'sub') == ctx_rm.hex()
 
     # empty response for unresolvable revision
-    assert do_rpc(revision=b'unknown', path=b'foo') == b''
+    assert lcfp(revision=b'unknown', path=b'foo') == b''
 
 
 def test_list_commits_by_oid(grpc_channel, server_repos_root):