Skip to content

Commit

Permalink
revsets: add a test for ghrevset
Browse files Browse the repository at this point in the history
Summary: Adds an integration test for the features added in D42221073

Reviewed By: bolinfest

Differential Revision: D42625035

fbshipit-source-id: 40a00f36ce25bd2250637bca3b42f16076be1304
  • Loading branch information
sggutier authored and facebook-github-bot committed Jan 23, 2023
1 parent 6fde922 commit a8fb908
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
47 changes: 47 additions & 0 deletions eden/scm/tests/github/mock_ghrevset.py
Original file line number Diff line number Diff line change
@@ -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)
64 changes: 64 additions & 0 deletions eden/scm/tests/test-ext-github-ghrevset.t
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a8fb908

Please sign in to comment.