FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Improve `test_merge_conflict` via new `prompt_yes_or_no` by ianthomas23 · Pull Request #147 · QuantStack/git2cpp · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cpp  (2) .hpp  (1) .py  (4) .ts  (1) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
7 changes: 3 additions & 4 deletions src/subcommand/merge_subcommand.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <git2/types.h>
#include <termcolor/termcolor.hpp>

#include "../utils/input_output.hpp"
#include "../wrapper/status_wrapper.hpp"

merge_subcommand::merge_subcommand(const libgit2_object&, CLI::App& app)
Expand Down Expand Up @@ -181,10 +182,8 @@ void merge_subcommand::run()

std::cout << "Warning: 'merge --abort' is not implemented yet. A 'reset --hard HEAD' will be executed."
<< std::endl;
std::cout << "Do you want to continue [y/N] ?" << std::endl;
std::string answer;
std::cin >> answer;
if (answer == "y")
auto answer = prompt_yes_or_no("Do you want to continue [y/N] ? ", false);
if (answer)
{
repo.state_cleanup();
index.conflict_cleanup();
Expand Down
22 changes: 22 additions & 0 deletions src/utils/input_output.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,25 @@ std::string prompt_input(const std::string_view prompt, bool echo /* = true */)
// Maybe sanitise input, removing escape codes?
return input;
}

bool prompt_yes_or_no(const std::string_view prompt, bool default_return)
{
while (true)
{
auto input = prompt_input(prompt);
if (input.empty())
{
return default_return;
}
auto first_char = std::tolower(input.front());
if (first_char == 'y')
{
return true;
}
else if (first_char == 'n')
{
return false;
}
// Repeat prompt.
}
}
6 changes: 6 additions & 0 deletions src/utils/input_output.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ class echo_control : noncopyable_nonmovable
// stdin from the user. The `echo` argument controls whether stdin is echoed
// to stdout, use `false` for passwords.
std::string prompt_input(const std::string_view prompt, bool echo = true);

// Display a prompt on stdout and accept a yes or no answer on stdin.
// Return true if the first character on stdin is 'y' or 'Y', false if it is
// 'n' or 'N', or default_return if the input is empty which occurs if the user
// presses enter only. Otherwise the input is ambiguous so repeat the prompt.
bool prompt_yes_or_no(const std::string_view prompt, bool default_return);
2 changes: 1 addition & 1 deletion test/conftest.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def commit_env_config(monkeypatch):


@pytest.fixture
def repo_init_with_commit(commit_env_config, git2cpp_path, tmp_path, run_in_tmp_path):
def repo_init_with_commit(commit_env_config, git2cpp_path, tmp_path):
cmd_init = [git2cpp_path, "init", ".", "-b", "main"]
p_init = subprocess.run(cmd_init, capture_output=True, cwd=tmp_path, text=True)
assert p_init.returncode == 0
Expand Down
2 changes: 1 addition & 1 deletion test/conftest_wasm.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def run_web_server():
cwd = pathlib.Path(__file__).parent.parent / "wasm/test"
proc = subprocess.Popen(["npm", "run", "serve"], stdout=f, stderr=f, cwd=cwd)
# Wait a bit until server ready to receive connections.
time.sleep(0.3)
time.sleep(0.5)
yield
proc.terminate()

Expand Down
36 changes: 36 additions & 0 deletions test/test_fixtures.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,39 @@ def test_env_vars():
assert git_lines == ["GIT_CORS_PROXY=http://localhost:8881/"]
else:
assert git_lines == []


def test_repo_init_with_commit(repo_init_with_commit, git2cpp_path, tmp_path):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

New test to check that repo_init_with_commit is correct. I thought at one point that is was causing problems but it is fine. We may as well keep the test.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Let's keep it, it cannot hurt.

assert (tmp_path / "initial.txt").exists()
assert (tmp_path / "initial.txt").is_file()
assert (tmp_path / "initial.txt").read_text() == "initial"

git_dir = tmp_path / ".git"
assert git_dir.exists()
assert git_dir.is_dir()

assert sorted(tmp_path.iterdir()) == [
git_dir,
tmp_path / "initial.txt",
]

assert sorted(git_dir.iterdir()) == [
git_dir / "HEAD",
git_dir / "config",
git_dir / "description",
git_dir / "hooks",
git_dir / "index",
git_dir / "info",
git_dir / "logs",
git_dir / "objects",
git_dir / "refs",
]

cmd = [git2cpp_path, "log"]
p = subprocess.run(cmd, capture_output=True, cwd=tmp_path, text=True)
assert p.returncode == 0
lines = p.stdout.splitlines()
assert "commit" in lines[0]
assert lines[1].startswith("Author:")
assert lines[2].startswith("Date:")
assert "Initial commit" in lines[4]
41 changes: 27 additions & 14 deletions test/test_merge.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,21 @@ def test_merge_commit(repo_init_with_commit, commit_env_config, git2cpp_path, tm
assert p_merge_2.stdout == "Already up-to-date\n"


@pytest.mark.parametrize("flag", ["--abort", "--quit", "--continue"])
def test_merge_conflict(repo_init_with_commit, commit_env_config, git2cpp_path, tmp_path, flag):
@pytest.mark.parametrize(
"flag,abort_input",
[
("--abort", "y"),
("--abort", "Y"),
("--abort", "n"),
("--abort", "N"),
("--abort", ""),
("--quit", None),
("--continue", None),
],
)
def test_merge_conflict(
repo_init_with_commit, commit_env_config, git2cpp_path, tmp_path, flag, abort_input
):
assert (tmp_path / "initial.txt").exists()

checkout_cmd = [git2cpp_path, "checkout", "-b", "foregone"]
Expand Down Expand Up @@ -140,18 +153,18 @@ def test_merge_conflict(repo_init_with_commit, commit_env_config, git2cpp_path,

flag_cmd = [git2cpp_path, "merge", flag]
if flag == "--abort":
for answer in {"y", ""}:
p_abort = subprocess.run(
flag_cmd, input=answer, capture_output=True, cwd=tmp_path, text=True
)
assert p_abort.returncode == 0
assert (tmp_path / "mook_file.txt").exists()
text = (tmp_path / "mook_file.txt").read_text()
if answer == "y":
assert "BLA" in text
assert "bla" not in text
else:
assert "Abort." in p_abort.stdout
p_abort = subprocess.run(
flag_cmd, input=abort_input, capture_output=True, cwd=tmp_path, text=True
)

assert p_abort.returncode == 0
assert (tmp_path / "mook_file.txt").exists()
text = (tmp_path / "mook_file.txt").read_text()
if abort_input.lower() == "y":
assert "BLA" in text
assert "bla" not in text
else:
assert "Abort." in p_abort.stdout

elif flag == "--quit":
pass
Expand Down
5 changes: 4 additions & 1 deletion wasm/test/src/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ async function shellRun(

if (input !== undefined && input !== null) {
async function delayThenStdin(): Promise<void> {
const chars = input! + '\x04'; // EOT
let chars = input!;
if (!chars.endsWith('\n')) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is an improvement in the wasm handling following on from cockle now implementing line buffered stdin.

chars += '\n';
}
await delay(100);
for (const char of chars) {
await shell.input(char);
Expand Down

Back | FazBrowse Home | New Git URL