refactor!: use Finalizer to automatically free allocated memory for objects (#48)

BREAKING CHANGE: signature change for remote and repository callbacks during repository clone operation.
This commit is contained in:
Aleksey Kulikov 2022-04-28 11:04:48 +03:00 committed by GitHub
parent 94c40f9a94
commit a3213a88a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
103 changed files with 2278 additions and 2595 deletions

View file

@ -34,8 +34,6 @@ void main() {
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();
}
});
@ -47,8 +45,6 @@ void main() {
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();
}
});
@ -60,8 +56,6 @@ void main() {
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();
}
});
@ -75,7 +69,6 @@ void main() {
test('returns a branch with provided name', () {
final branch = Branch.lookup(repo: repo, name: 'master');
expect(branch.target.sha, lastCommit.sha);
branch.free();
});
test('throws when provided name not found', () {
@ -95,88 +88,66 @@ void main() {
});
test('checks if branch is current head', () {
final masterBranch = Branch.lookup(repo: repo, name: 'master');
final featureBranch = Branch.lookup(repo: repo, name: 'feature');
expect(masterBranch.isHead, true);
expect(featureBranch.isHead, false);
masterBranch.free();
featureBranch.free();
expect(Branch.lookup(repo: repo, name: 'master').isHead, true);
expect(Branch.lookup(repo: repo, name: 'feature').isHead, false);
});
test('throws when checking if branch is current head and error occurs', () {
final nullBranch = Branch(nullptr);
expect(
() => nullBranch.isHead,
() => Branch(nullptr).isHead,
throwsA(isA<LibGit2Error>()),
);
});
test('checks if branch is checked out', () {
final masterBranch = Branch.lookup(repo: repo, name: 'master');
final featureBranch = Branch.lookup(repo: repo, name: 'feature');
expect(masterBranch.isCheckedOut, true);
expect(featureBranch.isCheckedOut, false);
masterBranch.free();
featureBranch.free();
expect(Branch.lookup(repo: repo, name: 'master').isCheckedOut, true);
expect(Branch.lookup(repo: repo, name: 'feature').isCheckedOut, false);
});
test('throws when checking if branch is checked out and error occurs', () {
final nullBranch = Branch(nullptr);
expect(
() => nullBranch.isCheckedOut,
() => Branch(nullptr).isCheckedOut,
throwsA(isA<LibGit2Error>()),
);
});
test('returns name', () {
final branch = Branch.lookup(repo: repo, name: 'master');
expect(branch.name, 'master');
branch.free();
expect(Branch.lookup(repo: repo, name: 'master').name, 'master');
});
test('throws when getting name and error occurs', () {
final nullBranch = Branch(nullptr);
expect(() => nullBranch.name, throwsA(isA<LibGit2Error>()));
expect(() => Branch(nullptr).name, throwsA(isA<LibGit2Error>()));
});
test('returns remote name of a remote-tracking branch', () {
final branch = Branch.list(repo: repo, type: GitBranch.remote).first;
expect(branch.remoteName, 'origin');
branch.free();
});
test(
'throws when getting remote name of a remote-tracking branch and '
'error occurs', () {
final branch = Branch.lookup(repo: repo, name: 'master');
expect(() => branch.remoteName, throwsA(isA<LibGit2Error>()));
expect(
() => Branch.lookup(repo: repo, name: 'master').remoteName,
throwsA(isA<LibGit2Error>()),
);
});
test('returns upstream of a local branch', () {
final branch = Branch.lookup(repo: repo, name: 'master');
final upstream = branch.upstream;
final upstream = Branch.lookup(repo: repo, name: 'master').upstream;
expect(upstream.isRemote, true);
expect(upstream.name, 'refs/remotes/origin/master');
upstream.free();
branch.free();
});
test('throws when trying to get upstream of a remote branch', () {
final branch = Branch.list(repo: repo, type: GitBranch.remote).first;
expect(() => branch.upstream, throwsA(isA<LibGit2Error>()));
branch.free();
});
test('sets upstream of a branch', () {
final branch = Branch.lookup(repo: repo, name: 'master');
var upstream = branch.upstream;
expect(upstream.name, 'refs/remotes/origin/master');
expect(branch.upstream.name, 'refs/remotes/origin/master');
final ref = Reference.create(
repo: repo,
@ -185,24 +156,15 @@ void main() {
);
branch.setUpstream(ref.shorthand);
upstream = branch.upstream;
expect(upstream.name, 'refs/remotes/origin/new');
ref.free();
upstream.free();
branch.free();
expect(branch.upstream.name, 'refs/remotes/origin/new');
});
test('unsets upstream of a branch', () {
final branch = Branch.lookup(repo: repo, name: 'master');
final upstream = branch.upstream;
expect(upstream.name, 'refs/remotes/origin/master');
expect(branch.upstream.name, 'refs/remotes/origin/master');
branch.setUpstream(null);
expect(() => branch.upstream, throwsA(isA<LibGit2Error>()));
upstream.free();
branch.free();
});
test('throws when trying to set upstream of a branch and error occurs', () {
@ -211,64 +173,61 @@ void main() {
() => branch.setUpstream('some/upstream'),
throwsA(isA<LibGit2Error>()),
);
branch.free();
});
test('returns upstream name of a local branch', () {
final branch = Branch.lookup(repo: repo, name: 'master');
expect(branch.upstreamName, 'refs/remotes/origin/master');
branch.free();
expect(
Branch.lookup(repo: repo, name: 'master').upstreamName,
'refs/remotes/origin/master',
);
});
test('throws when trying to get upstream name of a branch and error occurs',
() {
final branch = Branch.lookup(repo: repo, name: 'feature');
expect(() => branch.upstreamName, throwsA(isA<LibGit2Error>()));
branch.free();
expect(
() => Branch.lookup(repo: repo, name: 'feature').upstreamName,
throwsA(isA<LibGit2Error>()),
);
});
test('returns upstream remote of a local branch', () {
final branch = Branch.lookup(repo: repo, name: 'master');
expect(branch.upstreamRemote, 'origin');
branch.free();
expect(
Branch.lookup(repo: repo, name: 'master').upstreamRemote,
'origin',
);
});
test('throws when trying to get upstream remote of a remote branch', () {
final branch = Branch.list(repo: repo, type: GitBranch.remote).first;
expect(() => branch.upstreamRemote, throwsA(isA<LibGit2Error>()));
branch.free();
expect(
() => Branch.list(repo: repo, type: GitBranch.remote)
.first
.upstreamRemote,
throwsA(isA<LibGit2Error>()),
);
});
test('returns upstream merge of a local branch', () {
final branch = Branch.lookup(repo: repo, name: 'master');
expect(branch.upstreamMerge, 'refs/heads/master');
branch.free();
expect(
Branch.lookup(repo: repo, name: 'master').upstreamMerge,
'refs/heads/master',
);
});
test('throws when trying to get upstream merge of a remote branch', () {
final branch = Branch.list(repo: repo, type: GitBranch.remote).first;
expect(() => branch.upstreamMerge, throwsA(isA<LibGit2Error>()));
branch.free();
});
group('create()', () {
test('creates branch', () {
final commit = Commit.lookup(repo: repo, oid: lastCommit);
final branch = Branch.create(
repo: repo,
name: 'testing',
target: commit,
target: Commit.lookup(repo: repo, oid: lastCommit),
);
final branches = Branch.list(repo: repo);
expect(branches.length, 4);
expect(Branch.list(repo: repo).length, 4);
expect(branch.target, lastCommit);
for (final branch in branches) {
branch.free();
}
branch.free();
commit.free();
});
test('throws when name already exists', () {
@ -278,28 +237,18 @@ void main() {
() => Branch.create(repo: repo, name: 'feature', target: commit),
throwsA(isA<LibGit2Error>()),
);
commit.free();
});
test('creates branch with force flag when name already exists', () {
final commit = Commit.lookup(repo: repo, oid: lastCommit);
final branch = Branch.create(
repo: repo,
name: 'feature',
target: commit,
target: Commit.lookup(repo: repo, oid: lastCommit),
force: true,
);
final localBranches = Branch.list(repo: repo, type: GitBranch.local);
expect(localBranches.length, 2);
expect(Branch.list(repo: repo, type: GitBranch.local).length, 2);
expect(branch.target, lastCommit);
for (final branch in localBranches) {
branch.free();
}
branch.free();
commit.free();
});
});
@ -324,20 +273,16 @@ void main() {
group('rename()', () {
test('renames branch', () {
Branch.rename(repo: repo, oldName: 'feature', newName: 'renamed');
final branch = Branch.lookup(repo: repo, name: 'renamed');
final branches = Branch.list(repo: repo);
expect(branches.length, 3);
expect(Branch.list(repo: repo).length, 3);
expect(
() => Branch.lookup(repo: repo, name: 'feature'),
throwsA(isA<LibGit2Error>()),
);
expect(branch.target, featureCommit);
for (final branch in branches) {
branch.free();
}
branch.free();
expect(
Branch.lookup(repo: repo, name: 'renamed').target,
featureCommit,
);
});
test('throws when name already exists', () {
@ -358,11 +303,8 @@ void main() {
newName: 'feature',
force: true,
);
final branch = Branch.lookup(repo: repo, name: 'feature');
expect(branch.target, lastCommit);
branch.free();
expect(Branch.lookup(repo: repo, name: 'feature').target, lastCommit);
});
test('throws when name is invalid', () {
@ -377,10 +319,14 @@ void main() {
});
});
test('manually releases allocated memory', () {
final branch = Branch.lookup(repo: repo, name: 'master');
expect(() => branch.free(), returnsNormally);
});
test('returns string representation of Branch object', () {
final branch = Branch.lookup(repo: repo, name: 'master');
expect(branch.toString(), contains('Branch{'));
branch.free();
});
});
}