tests: add test for jj git clone

We didn't seem to have any. I included a test for #399.

The tests also showed how the debug formatting (`{:?}`) results in
escaped (e.g.) backslashes on Windows, which is not what we want, so I
also fixed that.
This commit is contained in:
Martin von Zweigbergk 2022-06-29 22:21:35 -07:00 committed by Martin von Zweigbergk
parent 418ab22be0
commit 5d871810a1
2 changed files with 85 additions and 1 deletions

View File

@ -4993,7 +4993,7 @@ fn cmd_git_clone(
let (workspace, repo) = Workspace::init_internal_git(ui.settings(), wc_path.clone())?;
let git_repo = get_git_repo(repo.store())?;
writeln!(ui, "Fetching into new repo in {:?}", wc_path)?;
writeln!(ui, r#"Fetching into new repo in "{}""#, wc_path.display())?;
let mut workspace_command = command.for_loaded_repo(ui, workspace, repo)?;
let remote_name = "origin";
git_repo.remote(remote_name, source).unwrap();

84
tests/test_git_clone.rs Normal file
View File

@ -0,0 +1,84 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::common::TestEnvironment;
pub mod common;
#[test]
fn test_git_clone() {
let test_env = TestEnvironment::default();
let git_repo_path = test_env.env_root().join("source");
let git_repo = git2::Repository::init(&git_repo_path).unwrap();
// Clone an empty repo
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["git", "clone", "source", "empty"]);
insta::assert_snapshot!(stdout.replace(test_env.env_root().join("empty").to_str().unwrap(), "<dest>"), @r###"
Fetching into new repo in "<dest>"
Nothing changed.
"###);
// Clone a non-empty repo
let signature =
git2::Signature::new("Some One", "some.one@example.com", &git2::Time::new(0, 0)).unwrap();
let mut tree_builder = git_repo.treebuilder(None).unwrap();
let file_oid = git_repo.blob(b"content").unwrap();
tree_builder
.insert("file", file_oid, git2::FileMode::Blob.into())
.unwrap();
let tree_oid = tree_builder.write().unwrap();
let tree = git_repo.find_tree(tree_oid).unwrap();
git_repo
.commit(
Some("refs/heads/main"),
&signature,
&signature,
"message",
&tree,
&[],
)
.unwrap();
git_repo.set_head("refs/heads/main").unwrap();
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["git", "clone", "source", "clone"]);
insta::assert_snapshot!(stdout.replace(test_env.env_root().join("clone").to_str().unwrap(), "<dest>"), @r###"
Fetching into new repo in "<dest>"
Working copy now at: 1f0b881a057d (no description set)
Added 1 files, modified 0 files, removed 0 files
"###);
assert!(test_env.env_root().join("clone").join("file").exists());
// Try cloning into an existing workspace
let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["git", "clone", "source", "clone"]);
insta::assert_snapshot!(stderr.replace(test_env.env_root().join("clone").to_str().unwrap(), "<dest>"), @r###"
Error: The target repo already exists
"###);
// Try cloning into an existing file
std::fs::write(test_env.env_root().join("file"), "contents").unwrap();
// TODO: This shouldn't crash (i.e. not exit code 101)
test_env
.jj_cmd(test_env.env_root(), &["git", "clone", "source", "file"])
.assert()
.code(101);
// Try cloning into non-empty, non-workspace directory
std::fs::remove_dir_all(test_env.env_root().join("clone").join(".jj")).unwrap();
// TODO: This should fail (https://github.com/martinvonz/jj/issues/399)
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["git", "clone", "source", "clone"]);
insta::assert_snapshot!(stdout.replace(test_env.env_root().join("clone").to_str().unwrap(), "<dest>"), @r###"
Fetching into new repo in "<dest>"
Working copy now at: b56015e38065 (no description set)
Added 1 files, modified 0 files, removed 0 files
"###);
}