Skip to content

Commit

Permalink
git: test import of remote-only branch
Browse files Browse the repository at this point in the history
This test coverage will become more important when we make changes to
remote branch importing.
  • Loading branch information
chooglen committed Jan 30, 2023
1 parent 056591a commit 0169a69
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/tests/test_git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ fn test_import_refs() {
let commit3 = empty_git_commit(&git_repo, "refs/heads/feature1", &[&commit2]);
let commit4 = empty_git_commit(&git_repo, "refs/heads/feature2", &[&commit2]);
let commit5 = empty_git_commit(&git_repo, "refs/tags/v1.0", &[&commit1]);
let commit6 = empty_git_commit(&git_repo, "refs/remotes/origin/feature3", &[&commit1]);
// Should not be imported
empty_git_commit(&git_repo, "refs/notes/x", &[&commit2]);
empty_git_commit(&git_repo, "refs/remotes/origin/HEAD", &[&commit2]);
Expand All @@ -87,7 +88,8 @@ fn test_import_refs() {
let expected_heads = hashset! {
jj_id(&commit3),
jj_id(&commit4),
jj_id(&commit5)
jj_id(&commit5),
jj_id(&commit6)
};
assert_eq!(*view.heads(), expected_heads);

Expand Down Expand Up @@ -117,13 +119,23 @@ fn test_import_refs() {
view.branches().get("feature2"),
Some(expected_feature2_branch).as_ref()
);
let expected_feature3_branch = BranchTarget {
local_target: Some(RefTarget::Normal(jj_id(&commit6))),
remote_targets: btreemap! {
"origin".to_string() => RefTarget::Normal(jj_id(&commit6)),
},
};
assert_eq!(
view.branches().get("feature3"),
Some(expected_feature3_branch).as_ref()
);

assert_eq!(
view.tags().get("v1.0"),
Some(RefTarget::Normal(jj_id(&commit5))).as_ref()
);

assert_eq!(view.git_refs().len(), 5);
assert_eq!(view.git_refs().len(), 6);
assert_eq!(
view.git_refs().get("refs/heads/main"),
Some(RefTarget::Normal(jj_id(&commit2))).as_ref()
Expand All @@ -140,6 +152,10 @@ fn test_import_refs() {
view.git_refs().get("refs/remotes/origin/main"),
Some(RefTarget::Normal(jj_id(&commit1))).as_ref()
);
assert_eq!(
view.git_refs().get("refs/remotes/origin/feature3"),
Some(RefTarget::Normal(jj_id(&commit6))).as_ref()
);
assert_eq!(
view.git_refs().get("refs/tags/v1.0"),
Some(RefTarget::Normal(jj_id(&commit5))).as_ref()
Expand Down
44 changes: 44 additions & 0 deletions tests/test_git_import_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// 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 std::path::Path;

use crate::common::{get_stderr_string, TestEnvironment};

Expand All @@ -37,3 +38,46 @@ fn test_git_export_conflicting_git_refs() {
export or their "parent" branches.
"###);
}

#[test]
fn test_git_import_remote_only_branch() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");

// Create non-empty git repo to add as a remote
let git_repo_path = test_env.env_root().join("git-repo");
let git_repo = git2::Repository::init(git_repo_path).unwrap();
let signature =
git2::Signature::new("Some One", "[email protected]", &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();
test_env.jj_cmd_success(
&repo_path,
&["git", "remote", "add", "origin", "../git-repo"],
);

git_repo
.commit(
Some("refs/heads/feature1"),
&signature,
&signature,
"message",
&tree,
&[],
)
.unwrap();
test_env.jj_cmd_success(&repo_path, &["git", "fetch", "--remote=origin"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
feature1: 9f01a0e04879 message
"###);
}

fn get_branch_output(test_env: &TestEnvironment, repo_path: &Path) -> String {
test_env.jj_cmd_success(repo_path, &["branch", "list"])
}

0 comments on commit 0169a69

Please sign in to comment.