refactor(repository)!: use Finalizer to automatically free allocated memory (#51)

BREAKING CHANGE: Return value of identity getter changed from Map<String, String> to Identity
This commit is contained in:
Aleksey Kulikov 2022-05-02 15:33:31 +03:00 committed by GitHub
parent aef440e345
commit 4e55d0f06c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 109 additions and 151 deletions

View file

@ -19,7 +19,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -52,7 +52,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -20,7 +20,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -21,7 +21,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -16,7 +16,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -34,7 +34,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -5,21 +5,6 @@ import 'package:path/path.dart' as p;
import 'package:test/test.dart';
void main() {
final cloneDir = Directory(
p.join(Directory.systemTemp.path, 'credentials_cloned'),
);
setUp(() {
if (cloneDir.existsSync()) {
cloneDir.deleteSync(recursive: true);
}
});
tearDown(() {
if (cloneDir.existsSync()) {
cloneDir.deleteSync(recursive: true);
}
});
group('Credentials', () {
test('initializes username/password credentials', () {
final credentials = const UserPass(
@ -78,6 +63,7 @@ void main() {
});
test('throws when provided username and password are incorrect', () {
final cloneDir = Directory.systemTemp.createTempSync('clone');
final callbacks = const Callbacks(
credentials: UserPass(
username: 'libgit2',
@ -93,11 +79,14 @@ void main() {
),
throwsA(isA<LibGit2Error>()),
);
cloneDir.deleteSync(recursive: true);
});
test(
'clones repository with provided keypair',
() {
final cloneDir = Directory.systemTemp.createTempSync('clone');
final keypair = Keypair(
username: 'git',
pubKey: p.join('test', 'assets', 'keys', 'id_rsa.pub'),
@ -114,12 +103,16 @@ void main() {
expect(repo.isEmpty, false);
repo.free();
if (Platform.isLinux || Platform.isMacOS) {
cloneDir.deleteSync(recursive: true);
}
},
testOn: '!linux',
);
test('throws when no credentials is provided', () {
final cloneDir = Directory.systemTemp.createTempSync('clone');
expect(
() => Repository.clone(
url: 'ssh://git@github.com/libgit2/TestGitRepository',
@ -127,9 +120,12 @@ void main() {
),
throwsA(isA<LibGit2Error>()),
);
cloneDir.deleteSync(recursive: true);
});
test('throws when provided keypair is invalid', () {
final cloneDir = Directory.systemTemp.createTempSync('clone');
final keypair = const Keypair(
username: 'git',
pubKey: 'invalid.pub',
@ -146,9 +142,12 @@ void main() {
),
throwsA(isA<LibGit2Error>()),
);
cloneDir.deleteSync(recursive: true);
});
test('throws when provided keypair is incorrect', () {
final cloneDir = Directory.systemTemp.createTempSync('clone');
final keypair = Keypair(
username: 'git',
pubKey: p.join('test', 'assets', 'keys', 'id_rsa.pub'),
@ -165,9 +164,12 @@ void main() {
),
throwsA(isA<LibGit2Error>()),
);
cloneDir.deleteSync(recursive: true);
});
test('throws when provided credential type is invalid', () {
final cloneDir = Directory.systemTemp.createTempSync('clone');
final callbacks = const Callbacks(
credentials: UserPass(
username: 'libgit2',
@ -183,11 +185,14 @@ void main() {
),
throwsA(isA<LibGit2Error>()),
);
cloneDir.deleteSync(recursive: true);
});
test(
'clones repository with provided keypair from memory',
() {
final cloneDir = Directory.systemTemp.createTempSync('clone');
final pubKey = File(p.join('test', 'assets', 'keys', 'id_rsa.pub'))
.readAsStringSync();
final privateKey =
@ -208,12 +213,15 @@ void main() {
expect(repo.isEmpty, false);
repo.free();
if (Platform.isLinux || Platform.isMacOS) {
cloneDir.deleteSync(recursive: true);
}
},
testOn: '!linux',
);
test('throws when provided keypair from memory is incorrect', () {
final cloneDir = Directory.systemTemp.createTempSync('clone');
final pubKey = File(p.join('test', 'assets', 'keys', 'id_rsa.pub'))
.readAsStringSync();
final keypair = KeypairFromMemory(
@ -232,9 +240,12 @@ void main() {
),
throwsA(isA<LibGit2Error>()),
);
cloneDir.deleteSync(recursive: true);
});
test('throws when provided keypair from agent is incorrect', () {
final cloneDir = Directory.systemTemp.createTempSync('clone');
final callbacks = const Callbacks(credentials: KeypairFromAgent('git'));
expect(
@ -245,6 +256,8 @@ void main() {
),
throwsA(isA<LibGit2Error>()),
);
cloneDir.deleteSync(recursive: true);
});
});
}

View file

@ -17,7 +17,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -127,7 +127,6 @@ index e69de29..c217c63 100644
});
tearDown(() {
repo.free();
if (Platform.isLinux || Platform.isMacOS) {
tmpDir.deleteSync(recursive: true);
}

View file

@ -20,7 +20,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});
@ -152,8 +151,6 @@ void main() {
p.join('test', 'assets', 'empty_bare.git'),
);
expect(() => bare.index.add('config'), throwsA(isA<LibGit2Error>()));
bare.free();
});
});
@ -203,8 +200,6 @@ void main() {
p.join('test', 'assets', 'empty_bare.git'),
);
expect(() => bare.index.addAll([]), throwsA(isA<LibGit2Error>()));
bare.free();
});
});
@ -229,8 +224,6 @@ void main() {
() => bare.index.updateAll(['not_there']),
throwsA(isA<LibGit2Error>()),
);
bare.free();
});
});
@ -316,7 +309,6 @@ void main() {
expect(() => repo.index.writeTree(), throwsA(isA<LibGit2Error>()));
repo.free();
tmpDir.deleteSync(recursive: true);
});
@ -356,7 +348,6 @@ void main() {
expect(conflictedFile.their?.path, 'feature_file');
expect(conflictedFile.toString(), contains('ConflictEntry{'));
conflictRepo.free();
repoDir.deleteSync(recursive: true);
});
@ -381,7 +372,6 @@ void main() {
expect(conflictedFile.their?.path, 'conflict_file');
expect(conflictedFile.toString(), contains('ConflictEntry{'));
conflictRepo.free();
repoDir.deleteSync(recursive: true);
});
@ -407,7 +397,6 @@ void main() {
expect(conflictedFile.their?.path, 'feature_file');
expect(conflictedFile.toString(), contains('ConflictEntry{'));
conflictRepo.free();
repoDir.deleteSync(recursive: true);
});
@ -432,7 +421,6 @@ void main() {
expect(conflictedFile.their?.path, null);
expect(conflictedFile.toString(), contains('ConflictEntry{'));
conflictRepo.free();
repoDir.deleteSync(recursive: true);
});
@ -465,7 +453,6 @@ void main() {
expect(index.conflicts, isEmpty);
expect(index.conflicts['conflict_file'], null);
conflictRepo.free();
repoDir.deleteSync(recursive: true);
});
@ -502,7 +489,6 @@ void main() {
expect(index.hasConflicts, false);
expect(index.conflicts, isEmpty);
conflictRepo.free();
repoDir.deleteSync(recursive: true);
});

View file

@ -136,7 +136,6 @@ Santa Claus <santa.claus@northpole.xx> <me@company.xx>
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -19,7 +19,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -29,7 +29,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -20,7 +20,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -20,7 +20,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -19,7 +19,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -50,7 +50,6 @@ index e69de29..0000000
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -21,7 +21,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -19,7 +19,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -19,7 +19,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -30,8 +30,6 @@ void main() {
});
tearDown(() {
originRepo.free();
clonedRepo.free();
tmpDir.deleteSync(recursive: true);
if (cloneDir.existsSync()) {
cloneDir.deleteSync(recursive: true);

View file

@ -18,7 +18,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});
@ -474,13 +473,8 @@ Total 69 (delta 0), reused 1 (delta 0), pack-reused 68
);
test('pushes with update reference callback', () {
final originDir =
Directory(p.join(Directory.systemTemp.path, 'origin_testrepo'));
final originDir = Directory.systemTemp.createTempSync('origin');
if (originDir.existsSync()) {
originDir.deleteSync(recursive: true);
}
originDir.createSync();
copyRepo(
from: Directory(p.join('test', 'assets', 'empty_bare.git')),
to: originDir,
@ -505,8 +499,9 @@ Total 69 (delta 0), reused 1 (delta 0), pack-reused 68
);
expect(updateRefOutput, {'refs/heads/master': ''});
originRepo.free();
originDir.delete(recursive: true);
if (Platform.isLinux || Platform.isMacOS) {
originDir.deleteSync(recursive: true);
}
});
test('throws when trying to push to invalid url', () {

View file

@ -37,8 +37,6 @@ void main() {
expect(clonedRepo.isEmpty, false);
expect(clonedRepo.isBare, false);
clonedRepo.free();
});
test('clones repository as bare', () {
@ -50,8 +48,6 @@ void main() {
expect(clonedRepo.isEmpty, false);
expect(clonedRepo.isBare, true);
clonedRepo.free();
});
test('clones repository with provided checkout branch name', () {
@ -65,8 +61,6 @@ void main() {
expect(clonedRepo.isEmpty, false);
expect(clonedRepo.isBare, true);
expect(clonedRepo.head.name, 'refs/heads/feature');
clonedRepo.free();
});
test(
@ -82,8 +76,6 @@ void main() {
expect(clonedRepo.isBare, false);
expect(clonedRepo.remotes, ['test']);
expect(clonedRepo.references, contains('refs/remotes/test/master'));
clonedRepo.free();
});
test('clones repository with provided remote callback ', () {
@ -105,8 +97,6 @@ void main() {
expect(clonedRepo.remotes, ['test']);
expect(clonedRepo.references, contains('refs/remotes/spec/master'));
expect(remote.fetchRefspecs, [fetchRefspec]);
clonedRepo.free();
});
test('throws when cloning repository with invalid remote callback', () {
@ -132,14 +122,16 @@ void main() {
final clonedRepo = Repository.clone(
url: tmpDir.path,
localPath: cloneDir.path,
repositoryCallback: RepositoryCallback(path: callbackPath.path),
repositoryCallback: RepositoryCallback(
path: callbackPath.path,
bare: true,
),
);
expect(clonedRepo.isEmpty, false);
expect(clonedRepo.isBare, false);
expect(clonedRepo.path, contains('/callbackRepo/.git/'));
expect(clonedRepo.isBare, true);
expect(clonedRepo.path, contains('/callbackRepo/'));
clonedRepo.free();
callbackPath.deleteSync(recursive: true);
});

View file

@ -20,10 +20,6 @@ void main() {
repo = Repository.open(p.join('test', 'assets', 'empty_bare.git'));
});
tearDown(() {
repo.free();
});
test('opens successfully', () {
expect(repo, isA<Repository>());
});
@ -52,10 +48,6 @@ void main() {
);
});
tearDown(() {
repo.free();
});
test('opens standart repository from working directory successfully', () {
expect(repo, isA<Repository>());
});
@ -89,12 +81,18 @@ void main() {
test('sets identity ', () {
repo.setIdentity(name: 'name', email: 'email@email.com');
expect(repo.identity, {'name': 'email@email.com'});
final identity = repo.identity;
expect(identity.name, 'name');
expect(identity.email, 'email@email.com');
});
test('unsets identity', () {
repo.setIdentity(name: null, email: null);
expect(repo.identity, isEmpty);
final identity = repo.identity;
expect(identity.name, '');
expect(identity.email, '');
});
test('checks if shallow clone', () {

View file

@ -24,8 +24,6 @@ void main() {
expect(repo.path, contains('init_repo'));
expect(repo.isBare, true);
repo.free();
});
test('creates new standard repo at provided path', () {
@ -34,8 +32,6 @@ void main() {
expect(repo.path, contains('init_repo/.git/'));
expect(repo.isBare, false);
expect(repo.isEmpty, true);
repo.free();
});
test('creates new standard repo with provided options', () {
@ -54,8 +50,6 @@ void main() {
'test repo',
);
expect(Remote.lookup(repo: repo, name: 'origin').url, 'test.url');
repo.free();
});
});
}

View file

@ -19,7 +19,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});
@ -165,8 +164,6 @@ void main() {
final bare = Repository.open(p.join('test', 'assets', 'empty_bare.git'));
expect(() => bare.status, throwsA(isA<LibGit2Error>()));
bare.free();
});
test('cleans up state', () {

View file

@ -19,7 +19,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -18,7 +18,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -24,7 +24,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -24,7 +24,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -20,7 +20,6 @@ void main() {
});
tearDown(() {
repo.free();
try {
tmpDir.deleteSync(recursive: true);
} catch (e) {
@ -99,8 +98,6 @@ void main() {
submodule.workdirOid?.sha,
'49322bb17d3acc9146f98c97d078513228bbf3c0',
);
submoduleRepo.free();
});
test('throws when trying to open repository for not initialized submodule',
@ -120,8 +117,6 @@ void main() {
expect(submodule.path, 'test');
expect(submodule.url, submoduleUrl);
expect(submoduleRepo.isEmpty, false);
submoduleRepo.free();
});
test('throws when trying to add submodule with wrong url', () {
@ -197,9 +192,6 @@ void main() {
updatedSubmRepoConfig['remote.origin.url'].value,
'https://updated.com/',
);
submRepo.free();
updatedSubmRepo.free();
});
test('reloads info', () {

View file

@ -21,7 +21,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -19,7 +19,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -19,7 +19,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
});

View file

@ -22,7 +22,6 @@ void main() {
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
if (worktreeDir.existsSync()) {
worktreeDir.deleteSync(recursive: true);