test: improve coverage

This commit is contained in:
Aleksey Kulikov 2022-01-26 15:41:28 +03:00
parent bad5e20581
commit 4ac0c10237
17 changed files with 171 additions and 40 deletions

View file

@ -347,18 +347,10 @@ Pointer<git_oid> treeOid(Pointer<git_commit> commit) {
} }
/// Get the tree pointed to by a commit. /// Get the tree pointed to by a commit.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_tree> tree(Pointer<git_commit> commit) { Pointer<git_tree> tree(Pointer<git_commit> commit) {
final out = calloc<Pointer<git_tree>>(); final out = calloc<Pointer<git_tree>>();
final error = libgit2.git_commit_tree(out, commit); libgit2.git_commit_tree(out, commit);
if (error < 0) {
calloc.free(out);
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value; return out.value;
}
} }
/// Reverts the given commit, producing changes in the index and working /// Reverts the given commit, producing changes in the index and working

View file

@ -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. /// Free an existing index object.
void free(Pointer<git_index> index) => libgit2.git_index_free(index); void free(Pointer<git_index> index) => libgit2.git_index_free(index);

View file

@ -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. /// 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 /// This returns a tree entry that is owned by the tree. You don't have to free

View file

@ -3,7 +3,7 @@ import 'package:libgit2dart/src/bindings/checkout.dart' as bindings;
import 'package:libgit2dart/src/bindings/object.dart' as object_bindings; import 'package:libgit2dart/src/bindings/object.dart' as object_bindings;
class Checkout { class Checkout {
const Checkout._(); const Checkout._(); // coverage:ignore-line
/// Updates files in the index and the working tree to match the content of /// Updates files in the index and the working tree to match the content of
/// the commit pointed at by HEAD. /// the commit pointed at by HEAD.

View file

@ -314,11 +314,11 @@ class Commit {
/// ///
/// Note that a commit is not considered a descendant of itself, in contrast /// Note that a commit is not considered a descendant of itself, in contrast
/// to `git merge-base --is-ancestor`. /// to `git merge-base --is-ancestor`.
bool descendantOf(Commit ancestor) { bool descendantOf(Oid ancestor) {
return graph_bindings.descendantOf( return graph_bindings.descendantOf(
repoPointer: bindings.owner(_commitPointer), repoPointer: bindings.owner(_commitPointer),
commitPointer: bindings.id(_commitPointer), commitPointer: bindings.id(_commitPointer),
ancestorPointer: bindings.id(ancestor.pointer), ancestorPointer: ancestor.pointer,
); );
} }

View file

@ -5,7 +5,7 @@ import 'package:libgit2dart/libgit2dart.dart';
import 'package:libgit2dart/src/util.dart'; import 'package:libgit2dart/src/util.dart';
class Libgit2 { class Libgit2 {
Libgit2._(); Libgit2._(); // coverage:ignore-line
/// Returns libgit2 version number. /// Returns libgit2 version number.
static String get version { static String get version {

View file

@ -5,7 +5,7 @@ import 'package:libgit2dart/src/bindings/merge.dart' as bindings;
import 'package:libgit2dart/src/util.dart'; import 'package:libgit2dart/src/util.dart';
class Merge { class Merge {
const Merge._(); const Merge._(); // coverage:ignore-line
/// Finds a merge base between [commits]. /// Finds a merge base between [commits].
/// ///

View file

@ -29,30 +29,39 @@ void main() {
test('returns a list of all branches', () { test('returns a list of all branches', () {
const branchesExpected = ['feature', 'master', 'origin/master']; const branchesExpected = ['feature', 'master', 'origin/master'];
final branches = Branch.list(repo: repo); final branches = Branch.list(repo: repo);
final aliasBranches = repo.branches;
for (var i = 0; i < branches.length; i++) { for (var i = 0; i < branches.length; i++) {
expect(branches[i].name, branchesExpected[i]); expect(branches[i].name, branchesExpected[i]);
expect(aliasBranches[i].name, branchesExpected[i]);
branches[i].free(); branches[i].free();
aliasBranches[i].free();
} }
}); });
test('returns a list of local branches', () { test('returns a list of local branches', () {
const branchesExpected = ['feature', 'master']; const branchesExpected = ['feature', 'master'];
final branches = Branch.list(repo: repo, type: GitBranch.local); final branches = Branch.list(repo: repo, type: GitBranch.local);
final aliasBranches = repo.branchesLocal;
for (var i = 0; i < branches.length; i++) { for (var i = 0; i < branches.length; i++) {
expect(branches[i].name, branchesExpected[i]); expect(branches[i].name, branchesExpected[i]);
expect(aliasBranches[i].name, branchesExpected[i]);
branches[i].free(); branches[i].free();
aliasBranches[i].free();
} }
}); });
test('returns a list of remote branches', () { test('returns a list of remote branches', () {
const branchesExpected = ['origin/master']; const branchesExpected = ['origin/master'];
final branches = Branch.list(repo: repo, type: GitBranch.remote); final branches = Branch.list(repo: repo, type: GitBranch.remote);
final aliasBranches = repo.branchesRemote;
for (var i = 0; i < branches.length; i++) { for (var i = 0; i < branches.length; i++) {
expect(branches[i].name, branchesExpected[i]); expect(branches[i].name, branchesExpected[i]);
expect(aliasBranches[i].name, branchesExpected[i]);
branches[i].free(); branches[i].free();
aliasBranches[i].free();
} }
}); });
@ -226,7 +235,7 @@ void main() {
test('throws when trying to get upstream remote of a remote branch', () { test('throws when trying to get upstream remote of a remote branch', () {
final branch = Branch.list(repo: repo, type: GitBranch.remote).first; final branch = Branch.list(repo: repo, type: GitBranch.remote).first;
expect(() => branch.upstreamName, throwsA(isA<LibGit2Error>())); expect(() => branch.upstreamRemote, throwsA(isA<LibGit2Error>()));
branch.free(); branch.free();
}); });

View file

@ -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', () { test('creates commit', () {
final parent = Commit.lookup(repo: repo, oid: tip); final parent = Commit.lookup(repo: repo, oid: tip);
final oid = Commit.create( final oid = Commit.create(
@ -139,6 +151,33 @@ void main() {
parent.free(); 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', () { test('writes commit into the buffer', () {
final parent = Commit.lookup(repo: repo, oid: tip); final parent = Commit.lookup(repo: repo, oid: tip);
final commit = Commit.createBuffer( final commit = Commit.createBuffer(

View file

@ -86,6 +86,20 @@ void main() {
'subdir/modified_file', '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 = """ const patchText = """
diff --git a/subdir/modified_file b/subdir/modified_file diff --git a/subdir/modified_file b/subdir/modified_file
index e69de29..c217c63 100644 index e69de29..c217c63 100644
@ -150,6 +164,25 @@ index e69de29..c217c63 100644
index.free(); 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', () { test('returns diff between tree and workdir', () {
final head = repo.head; final head = repo.head;
final commit = Commit.lookup(repo: repo, oid: head.target); final commit = Commit.lookup(repo: repo, oid: head.target);
@ -223,6 +256,42 @@ index e69de29..c217c63 100644
diff.free(); 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', test('throws when trying to diff between tree and tree and error occurs',
() { () {
final nullTree = Tree(nullptr); final nullTree = Tree(nullptr);
@ -405,6 +474,19 @@ index e69de29..c217c63 100644
diff.free(); 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', () { test('applies diff to tree', () {
final diff = Diff.parse(patchText); final diff = Diff.parse(patchText);

View file

@ -341,6 +341,7 @@ void main() {
test('adds conflict entry', () { test('adds conflict entry', () {
expect(index.conflicts, isEmpty); expect(index.conflicts, isEmpty);
index.addConflict( index.addConflict(
ancestorEntry: index['file'],
ourEntry: index['file'], ourEntry: index['file'],
theirEntry: index['feature_file'], theirEntry: index['feature_file'],
); );

View file

@ -152,6 +152,19 @@ index e69de29..0000000
patch.free(); 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', () { test('throws when trying to create from diff and error occurs', () {
expect( expect(
() => Patch.fromDiff(diff: Diff(nullptr), index: 0), () => Patch.fromDiff(diff: Diff(nullptr), index: 0),

View file

@ -141,6 +141,17 @@ void main() {
expect(newReflog.length, 3); 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', () { test('returns string representation of RefLogEntry object', () {
expect(reflog[0].toString(), contains('RefLogEntry{')); expect(reflog[0].toString(), contains('RefLogEntry{'));
}); });

View file

@ -11,7 +11,9 @@ void main() {
late Repository clonedRepo; late Repository clonedRepo;
late Directory tmpDir; late Directory tmpDir;
late Remote remote; late Remote remote;
final cloneDir = Directory(p.join(Directory.systemTemp.path, 'cloned')); final cloneDir = Directory(
p.join(Directory.systemTemp.path, 'remote_prune_cloned'),
);
setUp(() { setUp(() {
tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo'))); tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo')));

View file

@ -11,7 +11,9 @@ import 'helpers/util.dart';
void main() { void main() {
late Repository repo; late Repository repo;
late Directory tmpDir; late Directory tmpDir;
final cloneDir = Directory(p.join(Directory.systemTemp.path, 'cloned')); final cloneDir = Directory(
p.join(Directory.systemTemp.path, 'repository_cloned'),
);
setUp(() { setUp(() {
tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo'))); tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo')));

View file

@ -249,18 +249,6 @@ void main() {
expect(repo.getAttribute(path: 'file.sh', name: 'eol'), 'lf'); 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', () { test('returns number of ahead behind commits', () {
final commit1 = Commit.lookup(repo: repo, oid: repo['821ed6e']); final commit1 = Commit.lookup(repo: repo, oid: repo['821ed6e']);
final commit2 = Commit.lookup(repo: repo, oid: repo['c68ff54']); final commit2 = Commit.lookup(repo: repo, oid: repo['c68ff54']);

View file

@ -352,7 +352,7 @@ void main() {
repo: repo, repo: repo,
tagName: '', tagName: '',
target: repo['9c78c21'], target: repo['9c78c21'],
targetType: GitObject.commit, targetType: GitObject.any,
tagger: Signature(nullptr), tagger: Signature(nullptr),
message: '', message: '',
), ),
@ -366,7 +366,7 @@ void main() {
repo: repo, repo: repo,
tagName: '', tagName: '',
target: repo['9c78c21'], target: repo['9c78c21'],
targetType: GitObject.commit, targetType: GitObject.any,
), ),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );