From a8fb90880f55f0fbed434c5afd15c47aaaf529b4 Mon Sep 17 00:00:00 2001 From: Saul Gutierrez Date: Mon, 23 Jan 2023 13:11:01 -0800 Subject: [PATCH] revsets: add a test for ghrevset Summary: Adds an integration test for the features added in D42221073 Reviewed By: bolinfest Differential Revision: D42625035 fbshipit-source-id: 40a00f36ce25bd2250637bca3b42f16076be1304 --- eden/scm/tests/github/mock_ghrevset.py | 47 +++++++++++++++++ eden/scm/tests/test-ext-github-ghrevset.t | 64 +++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 eden/scm/tests/github/mock_ghrevset.py create mode 100644 eden/scm/tests/test-ext-github-ghrevset.t diff --git a/eden/scm/tests/github/mock_ghrevset.py b/eden/scm/tests/github/mock_ghrevset.py new file mode 100644 index 0000000000000..2ba17c9484414 --- /dev/null +++ b/eden/scm/tests/github/mock_ghrevset.py @@ -0,0 +1,47 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This software may be used and distributed according to the terms of the +# GNU General Public License version 2. + +from edenscm import extensions +from edenscm.ext.github import submit +from edenscm.ext.github.mock_utils import mock_run_git_command, MockGitHubServer +from edenscm.ext.github.pull_request_body import firstline +from ghstack import github_gh_cli + +# An extension to mock network requests by replacing the `github_gh_cli.make_request` +# and `submit.run_git_command` with the corresponding wrapper functions. Check `uisetup` +# function for how wrapper functions are registered. + + +def setup_mock_github_server(): + """Setup mock GitHub Server for testing happy case of `sl pr submit` command.""" + github_server = MockGitHubServer() + + github_server.expect_get_repository_request().and_respond() + + next_pr_number = 7 + github_server.expect_guess_next_pull_request_number().and_respond() + + body = "neobranch\n" + title = firstline(body) + head = f"pr{next_pr_number}" + github_server.expect_create_pr_request( + body=body, title=title, head=head + ).and_respond(number=next_pr_number) + + pr_id = f"PR_id_{next_pr_number}" + github_server.expect_get_pr_details_request(next_pr_number).and_respond( + pr_id, + head_ref_oid="4ce18fc3106a6f8dc65f0af182bed42275b2c3e6", + ) + + return github_server + + +def uisetup(ui): + mock_github_server = setup_mock_github_server() + extensions.wrapfunction( + github_gh_cli, "_make_request", mock_github_server.make_request + ) + extensions.wrapfunction(submit, "run_git_command", mock_run_git_command) diff --git a/eden/scm/tests/test-ext-github-ghrevset.t b/eden/scm/tests/test-ext-github-ghrevset.t new file mode 100644 index 0000000000000..d7ecb283bdd76 --- /dev/null +++ b/eden/scm/tests/test-ext-github-ghrevset.t @@ -0,0 +1,64 @@ +#debugruntest-compatible +#require git no-windows + + $ . $TESTDIR/git.sh + $ export SL_TEST_GH_URL=https://github.com/facebook/test_github_repo.git + $ setconfig ghrevset.autopull=True + $ enable smartlog amend github ghstack + $ setconfig workingcopy.ruststatus=False + $ setconfig extensions.mock_ghrevset=$TESTDIR/github/mock_ghrevset.py + $ setconfig templatealias.sl_github="\"{desc} {node|short} {if(github_pull_request_url, '#{github_pull_request_number}')}\"" + +Prepare upstream server repo w/ two commits on "main": + + $ git init -q upstream + $ cd upstream + $ git branch -m main + $ echo foo > foo + $ git add foo + $ git commit -qa -m foo + $ git checkout -qb neobranch + $ echo fork-existing-branch > neobranch + $ git add neobranch + $ git commit -qa -m neobranch + $ git checkout main -q + $ echo bar > bar + $ git add bar + $ git commit -qa -m bar + +Clone git repo as Sapling repo + $ cd .. + $ sl clone --git -q file://$TESTTMP/upstream client + $ cd client + $ tglog + @ ada74d65d813 'bar' + │ + o af065c3057b1 'foo' + +Make sure revset works by autopulling by using goto + $ sl goto pr7 -q + $ sl smartlog -T "{sl_github}" + o bar ada74d65d813 + │ + │ @ neobranch 4ce18fc3106a #7 + ├─╯ + o foo af065c3057b1 + +Amending a local commit should maintain PR no + $ echo meh > meh + $ sl amend + $ sl goto main -q + $ sl goto pr7 -q + $ sl smartlog -T "{sl_github}" + o bar ada74d65d813 + │ + │ @ neobranch 902a89e783d4 #7 + ├─╯ + o foo af065c3057b1 + +Looking locally for a commit should be possible even if upstream is not available + $ rm -rf ../upstream + $ sl goto main -q + $ sl goto pr7 -q + $ sl log -l 1 -T "{sl_github}\n" + neobranch 902a89e783d4 #7