feat(reference): add ability to create direct reference

This commit is contained in:
Aleksey Kulikov 2021-08-05 19:48:58 +03:00
parent 6643527f2d
commit 9190ed2e0f
16 changed files with 669 additions and 60 deletions

View file

@ -1,5 +1,6 @@
import 'dart:io';
import 'package:libgit2dart/src/reflog.dart';
import 'package:test/test.dart';
import 'package:libgit2dart/src/repository.dart';
import 'package:libgit2dart/src/reference.dart';
@ -11,8 +12,8 @@ void main() {
const lastCommit = '78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8';
group('Reference', () {
late Repository repo;
final tmpDir = '${Directory.systemTemp.path}/testrepo/';
late final Repository repo;
final tmpDir = '${Directory.systemTemp.path}/ref_testrepo/';
setUpAll(() async {
if (await Directory(tmpDir).exists()) {
@ -30,23 +31,150 @@ void main() {
await Directory(tmpDir).delete(recursive: true);
});
group('.createDirect()', () {
test('successfully creates with Oid as target', () {
final ref = repo.getReference('refs/heads/master');
final refFromOid = repo.createReference(
name: 'refs/tags/from.oid',
target: ref.target,
);
expect(repo.references, contains('refs/tags/from.oid'));
refFromOid.delete();
refFromOid.free();
ref.free();
});
test('successfully creates with SHA hash as target', () {
final refFromHash = repo.createReference(
name: 'refs/tags/from.hash',
target: lastCommit,
);
expect(repo.references, contains('refs/tags/from.hash'));
refFromHash.delete();
refFromHash.free();
});
test('successfully creates with short SHA hash as target', () {
final refFromHash = repo.createReference(
name: 'refs/tags/from.short.hash',
target: '78b8bf',
);
expect(repo.references, contains('refs/tags/from.short.hash'));
refFromHash.delete();
refFromHash.free();
});
test('successfully creates with log message', () {
repo.setIdentity(name: 'name', email: 'email');
final ref = repo.createReference(
name: 'refs/heads/log.message',
target: lastCommit,
logMessage: 'log message',
);
final reflog = RefLog(ref);
final reflogEntry = reflog.entryAt(0);
expect(reflogEntry.message, 'log message');
expect(reflogEntry.committer, {'name': 'name', 'email': 'email'});
reflog.free();
ref.delete();
ref.free();
});
test('throws if target is not valid', () {
expect(
() => repo.createReference(
name: 'refs/tags/invalid',
target: '78b',
),
throwsA(isA<LibGit2Error>()),
);
});
test('throws if name is not valid', () {
expect(
() => repo.createReference(
name: 'refs/tags/invalid~',
target: lastCommit,
),
throwsA(isA<LibGit2Error>()),
);
});
test('successfully creates with force flag if name already exists', () {
final ref = repo.createReference(
name: 'refs/tags/test',
target: lastCommit,
);
final forceRef = repo.createReference(
name: 'refs/tags/test',
target: lastCommit,
force: true,
);
expect(forceRef.target.sha, lastCommit);
forceRef.delete();
ref.free();
forceRef.free();
});
test('throws if name already exists', () {
final ref = repo.createReference(
name: 'refs/tags/test',
target: lastCommit,
);
expect(
() => repo.createReference(
name: 'refs/tags/test',
target: lastCommit,
),
throwsA(isA<LibGit2Error>()),
);
ref.delete();
ref.free();
});
});
test('successfully deletes reference', () {
final ref = repo.createReference(
name: 'refs/tags/test',
target: lastCommit,
);
expect(repo.references, contains('refs/tags/test'));
ref.delete();
expect(repo.references, isNot(contains('refs/tags/test')));
});
test('returns correct type of reference', () {
expect(repo.head.type, ReferenceType.direct);
repo.head.free();
final ref = Reference.lookup(repo, 'HEAD');
final ref = repo.getReference('HEAD');
expect(ref.type, ReferenceType.symbolic);
ref.free();
});
test('returns SHA hex of direct reference', () {
expect(repo.head.target, lastCommit);
expect(repo.head.target.sha, lastCommit);
repo.head.free();
});
test('returns SHA hex of symbolic reference', () {
final ref = Reference.lookup(repo, 'HEAD');
expect(ref.target, lastCommit);
final ref = repo.getReference('HEAD');
expect(ref.target.sha, lastCommit);
ref.free();
});
@ -67,44 +195,50 @@ void main() {
});
test('checks if reflog exists for the reference', () {
expect(Reference.hasLog(repo, 'refs/heads/master'), true);
expect(Reference.hasLog(repo, 'refs/heads/not/there'), false);
expect(repo.referenceHasLog('refs/heads/master'), true);
expect(repo.referenceHasLog('refs/tags/v0.1'), false);
});
test('checks if reference is a local branch', () {
final ref = Reference.lookup(repo, 'refs/heads/feature');
final ref = repo.getReference('refs/heads/feature');
expect(ref.isBranch, true);
ref.free();
});
test('checks if reference is a note', () {
final ref = Reference.lookup(repo, 'refs/heads/master');
final ref = repo.getReference('refs/heads/master');
expect(ref.isNote, false);
ref.free();
});
test('checks if reference is a remote branch', () {
final ref = Reference.lookup(repo, 'refs/heads/master');
expect(ref.isRemote, false);
final ref = repo.createReference(
name: 'refs/remotes/origin/master',
target: lastCommit,
);
expect(ref.isRemote, true);
ref.delete();
ref.free();
});
test('checks if reference is a tag', () {
final ref = Reference.lookup(repo, 'refs/tags/v0.1');
final ref = repo.getReference('refs/tags/v0.1');
expect(ref.isTag, true);
ref.free();
});
group('.lookup()', () {
test('finds a reference with provided name', () {
final ref = Reference.lookup(repo, 'refs/heads/master');
expect(ref.target, lastCommit);
final ref = repo.getReference('refs/heads/master');
expect(ref.target.sha, lastCommit);
ref.free();
});
test('throws when error occured', () {
expect(
() => Reference.lookup(repo, 'refs/heads/not/there'),
() => repo.getReference('refs/heads/not/there'),
throwsA(isA<LibGit2Error>()),
);
});