mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-05 04:39:07 -04:00
test: improve coverage
This commit is contained in:
parent
bad5e20581
commit
4ac0c10237
17 changed files with 171 additions and 40 deletions
|
@ -347,18 +347,10 @@ Pointer<git_oid> treeOid(Pointer<git_commit> commit) {
|
|||
}
|
||||
|
||||
/// Get the tree pointed to by a commit.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Pointer<git_tree> tree(Pointer<git_commit> commit) {
|
||||
final out = calloc<Pointer<git_tree>>();
|
||||
final error = libgit2.git_commit_tree(out, commit);
|
||||
|
||||
if (error < 0) {
|
||||
calloc.free(out);
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
} else {
|
||||
libgit2.git_commit_tree(out, commit);
|
||||
return out.value;
|
||||
}
|
||||
}
|
||||
|
||||
/// Reverts the given commit, producing changes in the index and working
|
||||
|
|
|
@ -525,9 +525,5 @@ void conflictCleanup(Pointer<git_index> index) {
|
|||
}
|
||||
}
|
||||
|
||||
/// Get the repository this index relates to.
|
||||
Pointer<git_repository> owner(Pointer<git_index> index) =>
|
||||
libgit2.git_index_owner(index);
|
||||
|
||||
/// Free an existing index object.
|
||||
void free(Pointer<git_index> index) => libgit2.git_index_free(index);
|
||||
|
|
|
@ -26,10 +26,6 @@ Pointer<git_tree> lookup({
|
|||
}
|
||||
}
|
||||
|
||||
/// Get the repository that contains the tree.
|
||||
Pointer<git_repository> owner(Pointer<git_tree> tree) =>
|
||||
libgit2.git_tree_owner(tree);
|
||||
|
||||
/// Lookup a tree entry by its position in the tree.
|
||||
///
|
||||
/// This returns a tree entry that is owned by the tree. You don't have to free
|
||||
|
|
|
@ -3,7 +3,7 @@ import 'package:libgit2dart/src/bindings/checkout.dart' as bindings;
|
|||
import 'package:libgit2dart/src/bindings/object.dart' as object_bindings;
|
||||
|
||||
class Checkout {
|
||||
const Checkout._();
|
||||
const Checkout._(); // coverage:ignore-line
|
||||
|
||||
/// Updates files in the index and the working tree to match the content of
|
||||
/// the commit pointed at by HEAD.
|
||||
|
|
|
@ -314,11 +314,11 @@ class Commit {
|
|||
///
|
||||
/// Note that a commit is not considered a descendant of itself, in contrast
|
||||
/// to `git merge-base --is-ancestor`.
|
||||
bool descendantOf(Commit ancestor) {
|
||||
bool descendantOf(Oid ancestor) {
|
||||
return graph_bindings.descendantOf(
|
||||
repoPointer: bindings.owner(_commitPointer),
|
||||
commitPointer: bindings.id(_commitPointer),
|
||||
ancestorPointer: bindings.id(ancestor.pointer),
|
||||
ancestorPointer: ancestor.pointer,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ import 'package:libgit2dart/libgit2dart.dart';
|
|||
import 'package:libgit2dart/src/util.dart';
|
||||
|
||||
class Libgit2 {
|
||||
Libgit2._();
|
||||
Libgit2._(); // coverage:ignore-line
|
||||
|
||||
/// Returns libgit2 version number.
|
||||
static String get version {
|
||||
|
|
|
@ -5,7 +5,7 @@ import 'package:libgit2dart/src/bindings/merge.dart' as bindings;
|
|||
import 'package:libgit2dart/src/util.dart';
|
||||
|
||||
class Merge {
|
||||
const Merge._();
|
||||
const Merge._(); // coverage:ignore-line
|
||||
|
||||
/// Finds a merge base between [commits].
|
||||
///
|
||||
|
|
|
@ -29,30 +29,39 @@ void main() {
|
|||
test('returns a list of all branches', () {
|
||||
const branchesExpected = ['feature', 'master', 'origin/master'];
|
||||
final branches = Branch.list(repo: repo);
|
||||
final aliasBranches = repo.branches;
|
||||
|
||||
for (var i = 0; i < branches.length; i++) {
|
||||
expect(branches[i].name, branchesExpected[i]);
|
||||
expect(aliasBranches[i].name, branchesExpected[i]);
|
||||
branches[i].free();
|
||||
aliasBranches[i].free();
|
||||
}
|
||||
});
|
||||
|
||||
test('returns a list of local branches', () {
|
||||
const branchesExpected = ['feature', 'master'];
|
||||
final branches = Branch.list(repo: repo, type: GitBranch.local);
|
||||
final aliasBranches = repo.branchesLocal;
|
||||
|
||||
for (var i = 0; i < branches.length; i++) {
|
||||
expect(branches[i].name, branchesExpected[i]);
|
||||
expect(aliasBranches[i].name, branchesExpected[i]);
|
||||
branches[i].free();
|
||||
aliasBranches[i].free();
|
||||
}
|
||||
});
|
||||
|
||||
test('returns a list of remote branches', () {
|
||||
const branchesExpected = ['origin/master'];
|
||||
final branches = Branch.list(repo: repo, type: GitBranch.remote);
|
||||
final aliasBranches = repo.branchesRemote;
|
||||
|
||||
for (var i = 0; i < branches.length; i++) {
|
||||
expect(branches[i].name, branchesExpected[i]);
|
||||
expect(aliasBranches[i].name, branchesExpected[i]);
|
||||
branches[i].free();
|
||||
aliasBranches[i].free();
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -226,7 +235,7 @@ void main() {
|
|||
|
||||
test('throws when trying to get upstream remote of a remote branch', () {
|
||||
final branch = Branch.list(repo: repo, type: GitBranch.remote).first;
|
||||
expect(() => branch.upstreamName, throwsA(isA<LibGit2Error>()));
|
||||
expect(() => branch.upstreamRemote, throwsA(isA<LibGit2Error>()));
|
||||
branch.free();
|
||||
});
|
||||
|
||||
|
|
|
@ -107,6 +107,18 @@ void main() {
|
|||
);
|
||||
});
|
||||
|
||||
test('checks if commit is a descendant of another commit', () {
|
||||
final commit1 = Commit.lookup(repo: repo, oid: repo['821ed6e8']);
|
||||
final commit2 = Commit.lookup(repo: repo, oid: repo['78b8bf12']);
|
||||
|
||||
expect(commit1.descendantOf(commit2.oid), true);
|
||||
expect(commit1.descendantOf(commit1.oid), false);
|
||||
expect(commit2.descendantOf(commit1.oid), false);
|
||||
|
||||
commit1.free();
|
||||
commit2.free();
|
||||
});
|
||||
|
||||
test('creates commit', () {
|
||||
final parent = Commit.lookup(repo: repo, oid: tip);
|
||||
final oid = Commit.create(
|
||||
|
@ -139,6 +151,33 @@ void main() {
|
|||
parent.free();
|
||||
});
|
||||
|
||||
test('writes commit without parents into the buffer', () {
|
||||
final parent = Commit.lookup(repo: repo, oid: tip);
|
||||
final commit = Commit.createBuffer(
|
||||
repo: repo,
|
||||
updateRef: 'HEAD',
|
||||
message: message,
|
||||
author: author,
|
||||
committer: committer,
|
||||
tree: tree,
|
||||
parents: [],
|
||||
);
|
||||
|
||||
const expected = """
|
||||
tree a8ae3dd59e6e1802c6f78e05e301bfd57c9f334f
|
||||
author Author Name <author@email.com> 123 +0000
|
||||
committer Commiter <commiter@email.com> 124 +0000
|
||||
|
||||
Commit message.
|
||||
|
||||
Some description.
|
||||
""";
|
||||
|
||||
expect(commit, expected);
|
||||
|
||||
parent.free();
|
||||
});
|
||||
|
||||
test('writes commit into the buffer', () {
|
||||
final parent = Commit.lookup(repo: repo, oid: tip);
|
||||
final commit = Commit.createBuffer(
|
||||
|
|
|
@ -86,6 +86,20 @@ void main() {
|
|||
'subdir/modified_file',
|
||||
];
|
||||
|
||||
const treeToEmptyTree = [
|
||||
'current_file',
|
||||
'file_deleted',
|
||||
'modified_file',
|
||||
'staged_changes',
|
||||
'staged_changes_file_deleted',
|
||||
'staged_changes_file_modified',
|
||||
'staged_delete',
|
||||
'staged_delete_file_modified',
|
||||
'subdir/current_file',
|
||||
'subdir/deleted_file',
|
||||
'subdir/modified_file',
|
||||
];
|
||||
|
||||
const patchText = """
|
||||
diff --git a/subdir/modified_file b/subdir/modified_file
|
||||
index e69de29..c217c63 100644
|
||||
|
@ -150,6 +164,25 @@ index e69de29..c217c63 100644
|
|||
index.free();
|
||||
});
|
||||
|
||||
test('returns diff between index and empty tree', () {
|
||||
final index = repo.index;
|
||||
final head = repo.head;
|
||||
final commit = Commit.lookup(repo: repo, oid: head.target);
|
||||
final tree = commit.tree;
|
||||
final diff = Diff.treeToIndex(repo: repo, tree: null, index: index);
|
||||
|
||||
expect(diff.length, 12);
|
||||
for (var i = 0; i < diff.deltas.length; i++) {
|
||||
expect(diff.deltas[i].newFile.path, indexToIndex[i]);
|
||||
}
|
||||
|
||||
commit.free();
|
||||
head.free();
|
||||
tree.free();
|
||||
diff.free();
|
||||
index.free();
|
||||
});
|
||||
|
||||
test('returns diff between tree and workdir', () {
|
||||
final head = repo.head;
|
||||
final commit = Commit.lookup(repo: repo, oid: head.target);
|
||||
|
@ -223,6 +256,42 @@ index e69de29..c217c63 100644
|
|||
diff.free();
|
||||
});
|
||||
|
||||
test('returns diff between tree and empty tree', () {
|
||||
final head = repo.head;
|
||||
final commit = Commit.lookup(repo: repo, oid: head.target);
|
||||
final tree = commit.tree;
|
||||
|
||||
final diff = Diff.treeToTree(repo: repo, oldTree: tree, newTree: null);
|
||||
|
||||
expect(diff.length, 11);
|
||||
for (var i = 0; i < diff.deltas.length; i++) {
|
||||
expect(diff.deltas[i].newFile.path, treeToEmptyTree[i]);
|
||||
}
|
||||
|
||||
commit.free();
|
||||
head.free();
|
||||
tree.free();
|
||||
diff.free();
|
||||
});
|
||||
|
||||
test('returns diff between empty tree and tree', () {
|
||||
final head = repo.head;
|
||||
final commit = Commit.lookup(repo: repo, oid: head.target);
|
||||
final tree = commit.tree;
|
||||
|
||||
final diff = Diff.treeToTree(repo: repo, oldTree: null, newTree: tree);
|
||||
|
||||
expect(diff.length, 11);
|
||||
for (var i = 0; i < diff.deltas.length; i++) {
|
||||
expect(diff.deltas[i].newFile.path, treeToEmptyTree[i]);
|
||||
}
|
||||
|
||||
commit.free();
|
||||
head.free();
|
||||
tree.free();
|
||||
diff.free();
|
||||
});
|
||||
|
||||
test('throws when trying to diff between tree and tree and error occurs',
|
||||
() {
|
||||
final nullTree = Tree(nullptr);
|
||||
|
@ -405,6 +474,19 @@ index e69de29..c217c63 100644
|
|||
diff.free();
|
||||
});
|
||||
|
||||
test('does not apply hunk with non existing index', () {
|
||||
final diff = Diff.parse(patchText);
|
||||
final file = File(p.join(tmpDir.path, 'subdir', 'modified_file'));
|
||||
|
||||
Checkout.head(repo: repo, strategy: {GitCheckout.force});
|
||||
expect(file.readAsStringSync(), '');
|
||||
|
||||
diff.apply(repo: repo, hunkIndex: 10);
|
||||
expect(file.readAsStringSync(), '');
|
||||
|
||||
diff.free();
|
||||
});
|
||||
|
||||
test('applies diff to tree', () {
|
||||
final diff = Diff.parse(patchText);
|
||||
|
||||
|
|
|
@ -341,6 +341,7 @@ void main() {
|
|||
test('adds conflict entry', () {
|
||||
expect(index.conflicts, isEmpty);
|
||||
index.addConflict(
|
||||
ancestorEntry: index['file'],
|
||||
ourEntry: index['file'],
|
||||
theirEntry: index['feature_file'],
|
||||
);
|
||||
|
|
|
@ -152,6 +152,19 @@ index e69de29..0000000
|
|||
patch.free();
|
||||
});
|
||||
|
||||
test('creates from empty blob and buffer', () {
|
||||
final patch = Patch.fromBlobAndBuffer(
|
||||
blob: null,
|
||||
buffer: newBuffer,
|
||||
blobPath: path,
|
||||
bufferPath: path,
|
||||
);
|
||||
|
||||
expect(patch.text, blobPatchAdd);
|
||||
|
||||
patch.free();
|
||||
});
|
||||
|
||||
test('throws when trying to create from diff and error occurs', () {
|
||||
expect(
|
||||
() => Patch.fromDiff(diff: Diff(nullptr), index: 0),
|
||||
|
|
|
@ -141,6 +141,17 @@ void main() {
|
|||
expect(newReflog.length, 3);
|
||||
});
|
||||
|
||||
test('throws when trying to write reflog to disk and error occurs', () {
|
||||
final ref = Reference.lookup(repo: repo, name: 'refs/heads/feature');
|
||||
final reflog = ref.log;
|
||||
Reference.delete(repo: repo, name: ref.name);
|
||||
|
||||
expect(() => reflog.write(), throwsA(isA<LibGit2Error>()));
|
||||
|
||||
reflog.free();
|
||||
ref.free();
|
||||
});
|
||||
|
||||
test('returns string representation of RefLogEntry object', () {
|
||||
expect(reflog[0].toString(), contains('RefLogEntry{'));
|
||||
});
|
||||
|
|
|
@ -11,7 +11,9 @@ void main() {
|
|||
late Repository clonedRepo;
|
||||
late Directory tmpDir;
|
||||
late Remote remote;
|
||||
final cloneDir = Directory(p.join(Directory.systemTemp.path, 'cloned'));
|
||||
final cloneDir = Directory(
|
||||
p.join(Directory.systemTemp.path, 'remote_prune_cloned'),
|
||||
);
|
||||
|
||||
setUp(() {
|
||||
tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo')));
|
||||
|
|
|
@ -11,7 +11,9 @@ import 'helpers/util.dart';
|
|||
void main() {
|
||||
late Repository repo;
|
||||
late Directory tmpDir;
|
||||
final cloneDir = Directory(p.join(Directory.systemTemp.path, 'cloned'));
|
||||
final cloneDir = Directory(
|
||||
p.join(Directory.systemTemp.path, 'repository_cloned'),
|
||||
);
|
||||
|
||||
setUp(() {
|
||||
tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo')));
|
||||
|
|
|
@ -249,18 +249,6 @@ void main() {
|
|||
expect(repo.getAttribute(path: 'file.sh', name: 'eol'), 'lf');
|
||||
});
|
||||
|
||||
test('checks if commit is a descendant of another commit', () {
|
||||
final commit1 = Commit.lookup(repo: repo, oid: repo['821ed6e8']);
|
||||
final commit2 = Commit.lookup(repo: repo, oid: repo['78b8bf12']);
|
||||
|
||||
expect(commit1.descendantOf(commit2), true);
|
||||
expect(commit1.descendantOf(commit1), false);
|
||||
expect(commit2.descendantOf(commit1), false);
|
||||
|
||||
commit1.free();
|
||||
commit2.free();
|
||||
});
|
||||
|
||||
test('returns number of ahead behind commits', () {
|
||||
final commit1 = Commit.lookup(repo: repo, oid: repo['821ed6e']);
|
||||
final commit2 = Commit.lookup(repo: repo, oid: repo['c68ff54']);
|
||||
|
|
|
@ -352,7 +352,7 @@ void main() {
|
|||
repo: repo,
|
||||
tagName: '',
|
||||
target: repo['9c78c21'],
|
||||
targetType: GitObject.commit,
|
||||
targetType: GitObject.any,
|
||||
tagger: Signature(nullptr),
|
||||
message: '',
|
||||
),
|
||||
|
@ -366,7 +366,7 @@ void main() {
|
|||
repo: repo,
|
||||
tagName: '',
|
||||
target: repo['9c78c21'],
|
||||
targetType: GitObject.commit,
|
||||
targetType: GitObject.any,
|
||||
),
|
||||
throwsA(isA<LibGit2Error>()),
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue