feat(commit): add base bindings and api

This commit is contained in:
Aleksey Kulikov 2021-08-24 19:08:12 +03:00
parent 696d55bb3a
commit dc5f510aa5
12 changed files with 485 additions and 17 deletions

79
test/commit_test.dart Normal file
View file

@ -0,0 +1,79 @@
import 'dart:io';
import 'package:test/test.dart';
import 'package:libgit2dart/libgit2dart.dart';
import 'package:libgit2dart/src/commit.dart';
import 'helpers/util.dart';
void main() {
const mergeCommit = '78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8';
group('Commit', () {
late Repository repo;
final tmpDir = '${Directory.systemTemp.path}/ref_testrepo/';
setUp(() async {
if (await Directory(tmpDir).exists()) {
await Directory(tmpDir).delete(recursive: true);
}
await copyRepo(
from: Directory('test/assets/testrepo/'),
to: await Directory(tmpDir).create(),
);
repo = Repository.open(tmpDir);
});
tearDown(() async {
repo.free();
await Directory(tmpDir).delete(recursive: true);
});
group('lookup', () {
test('successful when 40 char sha hex is provided', () {
final commit = repo[mergeCommit];
expect(commit, isA<Commit>());
commit.free();
});
test('successful when sha hex is short', () {
final commit = repo[mergeCommit.substring(0, 5)];
expect(commit, isA<Commit>());
commit.free();
});
test('throws when provided sha hex is invalid', () {
expect(() => repo['invalid'], throwsA(isA<ArgumentError>()));
});
test('throws when nothing found', () {
expect(() => repo['970ae5c'], throwsA(isA<LibGit2Error>()));
});
test('returns with correct fields', () {
final signature = Signature.create(
name: 'Aleksey Kulikov',
email: 'skinny.mind@gmail.com',
time: 1626091184,
offset: 180,
);
final commit = repo[mergeCommit];
expect(commit.messageEncoding, 'utf-8');
expect(commit.message, 'Merge branch \'feature\'\n');
expect(commit.id.sha, mergeCommit);
expect(commit.parents.length, 2);
expect(
commit.parents[0].id.sha,
'c68ff54aabf660fcdd9a2838d401583fe31249e3',
);
expect(commit.time, 1626091184);
expect(commit.committer, signature);
expect(commit.author, signature);
expect(commit.tree.sha, '7796359a96eb722939c24bafdb1afe9f07f2f628');
signature.free();
commit.free();
});
});
});
}

63
test/signature_test.dart Normal file
View file

@ -0,0 +1,63 @@
import 'package:test/test.dart';
import 'package:libgit2dart/libgit2dart.dart';
void main() {
late Signature signature;
group('Signature', () {
const name = 'Some Name';
const email = 'some@email.com';
const time = 1234567890;
const offset = 0;
setUp(() {
signature = Signature.create(
name: name,
email: email,
time: time,
offset: offset,
);
});
tearDown(() {
signature.free();
});
test('successfully creates with provided time and offset', () {
expect(signature, isA<Signature>());
});
test('successfully creates without provided time and offset', () {
final defaultSignature =
Signature.create(name: 'Name', email: 'email@example.com');
expect(defaultSignature, isA<Signature>());
expect(defaultSignature.name, 'Name');
expect(defaultSignature.email, 'email@example.com');
expect(
defaultSignature.time -
(DateTime.now().millisecondsSinceEpoch / 1000).truncate(),
lessThan(5),
);
expect(defaultSignature.offset, 180);
defaultSignature.free();
});
test('returns correct values', () {
expect(signature.name, name);
expect(signature.email, email);
expect(signature.time, time);
expect(signature.offset, offset);
});
test('compares two objects', () {
final otherSignature = Signature.create(
name: name,
email: email,
time: time,
offset: offset,
);
expect(signature == otherSignature, true);
otherSignature.free();
});
});
}