libgit2dart/test/worktree_test.dart
Aleksey Kulikov 3a0fa75929 feat(repository)!: add more aliases for api methods
BREAKING CHANGE: Make repository entry point for most operations
2021-10-11 20:06:36 +03:00

106 lines
3 KiB
Dart

import 'dart:io';
import 'package:test/test.dart';
import 'package:libgit2dart/libgit2dart.dart';
import 'helpers/util.dart';
void main() {
late Repository repo;
late Directory tmpDir;
final worktreeDir = Directory('${Directory.systemTemp.path}/worktree');
const worktreeName = 'worktree';
setUp(() {
if (worktreeDir.existsSync()) {
worktreeDir.deleteSync(recursive: true);
}
tmpDir = setupRepo(Directory('test/assets/testrepo/'));
repo = Repository.open(tmpDir.path);
});
tearDown(() {
repo.free();
tmpDir.deleteSync(recursive: true);
if (worktreeDir.existsSync()) {
worktreeDir.deleteSync(recursive: true);
}
});
group('Worktree', () {
test('successfully creates worktree at provided path', () {
expect(repo.worktrees, []);
final worktree = repo.createWorktree(
name: worktreeName,
path: worktreeDir.path,
);
final lookedupWorktree = repo.lookupWorktree(worktreeName);
final branches = repo.branches;
expect(repo.worktrees, [worktreeName]);
expect(branches.any((branch) => branch.name == worktreeName), true);
expect(worktree.name, worktreeName);
expect(lookedupWorktree.name, worktreeName);
expect(worktree.path, worktreeDir.path);
expect(lookedupWorktree.path, worktreeDir.path);
expect(File('${worktreeDir.path}/.git').existsSync(), true);
for (final branch in branches) {
branch.free();
}
lookedupWorktree.free();
worktree.free();
});
test(
'successfully creates worktree at provided path from provided reference',
() {
final head = repo.revParseSingle('HEAD');
final worktreeBranch = repo.createBranch(name: 'v1', target: head);
final ref = repo.lookupReference('refs/heads/v1');
expect(repo.worktrees, []);
final worktree = repo.createWorktree(
name: worktreeName,
path: worktreeDir.path,
ref: ref,
);
final branches = repo.branches;
expect(repo.worktrees, [worktreeName]);
expect(branches.any((branch) => branch.name == 'v1'), true);
expect(branches.any((branch) => branch.name == worktreeName), false);
expect(worktreeBranch.isCheckedOut, true);
worktreeDir.deleteSync(recursive: true);
worktree.prune();
expect(repo.worktrees, []);
expect(worktreeBranch.isCheckedOut, false);
expect(branches.any((branch) => branch.name == 'v1'), true);
for (final branch in branches) {
branch.free();
}
worktreeBranch.free();
ref.free();
head.free();
worktree.free();
});
test('successfully prunes worktree', () {
expect(repo.worktrees, []);
final worktree = repo.createWorktree(
name: worktreeName,
path: worktreeDir.path,
);
expect(repo.worktrees, [worktreeName]);
worktreeDir.deleteSync(recursive: true);
worktree.prune();
expect(repo.worktrees, []);
worktree.free();
});
});
}