import 'dart:io'; import 'package:libgit2dart/libgit2dart.dart'; import 'package:path/path.dart' as p; import 'package:test/test.dart'; import 'helpers/util.dart'; void main() { late Repository originRepo; late Repository clonedRepo; late Directory tmpDir; late Remote remote; final cloneDir = Directory( p.join(Directory.systemTemp.path, 'remote_prune_cloned'), ); setUp(() { tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo'))); if (cloneDir.existsSync()) { cloneDir.deleteSync(recursive: true); } originRepo = Repository.open(tmpDir.path); clonedRepo = Repository.clone( url: tmpDir.path, localPath: cloneDir.path, ); Branch.delete(repo: originRepo, name: 'feature'); remote = Remote.lookup(repo: clonedRepo, name: 'origin'); }); tearDown(() { remote.free(); originRepo.free(); clonedRepo.free(); tmpDir.deleteSync(recursive: true); if (cloneDir.existsSync()) { cloneDir.deleteSync(recursive: true); } }); group('Remote', () { test('fetch() does not prune branch by default', () { remote.fetch(); final branches = clonedRepo.branches; expect(branches.any((branch) => branch.name == 'origin/feature'), true); for (final branch in branches) { branch.free(); } }); test('fetch() prunes branch with provided flag', () { remote.fetch(prune: GitFetchPrune.prune); final branches = clonedRepo.branches; expect(branches.any((branch) => branch.name == 'origin/feature'), false); for (final branch in branches) { branch.free(); } }); test('fetch() does not prune branch with provided flag', () { remote.fetch(prune: GitFetchPrune.noPrune); final branches = clonedRepo.branches; expect(branches.any((branch) => branch.name == 'origin/feature'), true); for (final branch in branches) { branch.free(); } }); test('prune() prunes branches', () { final pruned = []; void updateTips(String refname, Oid oldOid, Oid newOid) { pruned.add(refname); } final callbacks = Callbacks(updateTips: updateTips); remote.fetch(prune: GitFetchPrune.noPrune); var branches = clonedRepo.branches; expect(branches.any((branch) => branch.name == 'origin/feature'), true); for (final branch in branches) { branch.free(); } remote.prune(callbacks); branches = clonedRepo.branches; expect(pruned, contains('refs/remotes/origin/feature')); expect(branches.any((branch) => branch.name == 'origin/feature'), false); for (final branch in branches) { branch.free(); } }); test( 'throws when trying to prune remote refs and remote has never ' 'connected', () { expect(() => remote.prune(), throwsA(isA())); }); }); }