libgit2dart/test/reset_test.dart
Viktor Borisov bb75589a14 fix tests on windows platform
I had some problems with SSH for windows and I skipped them for the specific  platform:

- clones repository with provided keypair
- clones repository with provided keypair from memory
- throws when provided keypair from memory is incorrect
2023-02-24 19:58:12 +07:00

117 lines
3.2 KiB
Dart

import 'dart:convert';
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 repo;
late Directory tmpDir;
late File file;
const sha = '6cbc22e509d72758ab4c8d9f287ea846b90c448b';
const splitter = LineSplitter();
setUp(() {
tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo')));
file = File(p.join(tmpDir.path, 'feature_file'));
repo = Repository.open(tmpDir.path);
});
tearDown(() {
tmpDir.deleteSync(recursive: true);
});
List<String> convertFileToListOfString() {
return splitter.convert(file.readAsStringSync());
}
group('Reset', () {
test('resets with hard', () {
expect(convertFileToListOfString(), ['Feature edit']);
repo.reset(oid: repo[sha], resetType: GitReset.hard);
expect(file.readAsStringSync(), isEmpty);
});
test('resets with soft', () {
expect(convertFileToListOfString(), ['Feature edit']);
repo.reset(oid: repo[sha], resetType: GitReset.soft);
expect(convertFileToListOfString(), ['Feature edit']);
final diff = Diff.indexToWorkdir(repo: repo, index: repo.index);
expect(diff.deltas, isEmpty);
});
test('resets with mixed', () {
expect(convertFileToListOfString(), ['Feature edit']);
repo.reset(oid: repo[sha], resetType: GitReset.mixed);
expect(convertFileToListOfString(), ['Feature edit']);
final diff = Diff.indexToWorkdir(repo: repo, index: repo.index);
expect(diff.deltas.length, 1);
});
test('resets with provided checkout options', () {
expect(convertFileToListOfString(), ['Feature edit']);
repo.reset(
oid: repo[sha],
resetType: GitReset.hard,
strategy: {GitCheckout.conflictStyleZdiff3},
pathspec: ['feature_file'],
);
expect(convertFileToListOfString(), <String>[]);
});
test(
'throws when trying to reset and error occurs',
testOn: '!windows',
() {
expect(
() => repo.reset(
oid: repo[sha],
resetType: GitReset.hard,
checkoutDirectory: '',
),
throwsA(isA<LibGit2Error>()),
);
},
);
group('resetDefault', () {
test('updates entry in the index', () {
file.writeAsStringSync('new edit');
repo.index.add('feature_file');
expect(repo.status['feature_file'], {GitStatus.indexModified});
repo.resetDefault(oid: repo.head.target, pathspec: ['feature_file']);
expect(repo.status['feature_file'], {GitStatus.wtModified});
});
test('removes entry in the index when null oid is provided', () {
const fileName = 'new_file.txt';
File(p.join(tmpDir.path, fileName)).createSync();
repo.index.add(fileName);
expect(repo.status[fileName], {GitStatus.indexNew});
repo.resetDefault(oid: null, pathspec: [fileName]);
expect(repo.status[fileName], {GitStatus.wtNew});
});
test('throws when pathspec list is empty', () {
expect(
() => repo.resetDefault(oid: repo.head.target, pathspec: []),
throwsA(isA<LibGit2Error>()),
);
});
});
});
}