refactor(checkout)!: move checkout related methods into Checkout class (#42)

This commit is contained in:
Aleksey Kulikov 2022-01-25 20:16:46 +03:00 committed by GitHub
parent 570c696269
commit 9918ab0905
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 238 additions and 135 deletions

View file

@ -27,8 +27,8 @@ void main() {
File(p.join(tmpDir.path, 'feature_file')).writeAsStringSync('edit');
expect(repo.status, contains('feature_file'));
repo.checkout(
target: 'HEAD',
Checkout.head(
repo: repo,
strategy: {GitCheckout.force},
paths: ['feature_file'],
);
@ -39,8 +39,8 @@ void main() {
'throws when trying to checkout head with invalid alternative '
'directory', () {
expect(
() => repo.checkout(
target: 'HEAD',
() => Checkout.head(
repo: repo,
directory: 'not/there',
),
throwsA(isA<LibGit2Error>()),
@ -51,7 +51,8 @@ void main() {
File(p.join(repo.workdir, 'feature_file')).writeAsStringSync('edit');
expect(repo.status, contains('feature_file'));
repo.checkout(
Checkout.index(
repo: repo,
strategy: {
GitCheckout.force,
GitCheckout.conflictStyleMerge,
@ -65,7 +66,7 @@ void main() {
'throws when trying to checkout index with invalid alternative '
'directory', () {
expect(
() => repo.checkout(directory: 'not/there'),
() => Checkout.index(repo: repo, directory: 'not/there'),
throwsA(isA<LibGit2Error>()),
);
});
@ -78,12 +79,12 @@ void main() {
false,
);
repo.checkout(target: 'refs/heads/feature');
Checkout.reference(repo: repo, name: 'refs/heads/feature');
final featureHead = Commit.lookup(repo: repo, oid: repo['5aecfa0']);
final featureTree = featureHead.tree;
final repoHead = repo.head;
expect(repoHead.target, featureHead.oid);
expect(repo.status, isEmpty);
// does not change HEAD
expect(repoHead.target, isNot(featureHead.oid));
expect(
featureTree.entries.any((e) => e.name == 'another_feature_file'),
true,
@ -100,8 +101,9 @@ void main() {
'throws when trying to checkout reference with invalid alternative '
'directory', () {
expect(
() => repo.checkout(
target: 'refs/heads/feature',
() => Checkout.reference(
repo: repo,
name: 'refs/heads/feature',
directory: 'not/there',
),
throwsA(isA<LibGit2Error>()),
@ -113,11 +115,11 @@ void main() {
expect(index.find('another_feature_file'), equals(false));
final featureHead = Commit.lookup(repo: repo, oid: repo['5aecfa0']);
repo.checkout(target: featureHead.oid);
Checkout.commit(repo: repo, commit: featureHead);
final repoHead = repo.head;
expect(repoHead.target, featureHead.oid);
expect(repo.status, isEmpty);
// does not change HEAD
expect(repoHead.target, isNot(featureHead.oid));
expect(index.find('another_feature_file'), equals(true));
repoHead.free();
@ -127,10 +129,14 @@ void main() {
test('checkouts commit with provided path', () {
final featureHead = Commit.lookup(repo: repo, oid: repo['5aecfa0']);
repo.checkout(target: featureHead.oid, paths: ['another_feature_file']);
Checkout.commit(
repo: repo,
commit: featureHead,
paths: ['another_feature_file'],
);
final repoHead = repo.head;
// When path is provided HEAD will not be set to target;
// does not change HEAD
expect(repoHead.target, isNot(featureHead.oid));
expect(
repo.status,
@ -143,6 +149,23 @@ void main() {
featureHead.free();
});
test(
'throws when trying to checkout commit with invalid alternative '
'directory', () {
final commit = Commit.lookup(repo: repo, oid: repo['5aecfa0']);
expect(
() => Checkout.commit(
repo: repo,
commit: commit,
directory: 'not/there',
),
throwsA(isA<LibGit2Error>()),
);
commit.free();
});
test('checkouts with alrenative directory', () {
final altDir = Directory(p.join(Directory.systemTemp.path, 'alt_dir'));
// making sure there is no directory
@ -152,7 +175,11 @@ void main() {
altDir.createSync();
expect(altDir.listSync().length, 0);
repo.checkout(target: 'refs/heads/feature', directory: altDir.path);
Checkout.reference(
repo: repo,
name: 'refs/heads/feature',
directory: altDir.path,
);
expect(altDir.listSync().length, isNot(0));
altDir.deleteSync(recursive: true);
@ -165,8 +192,9 @@ void main() {
).target;
expect(repo.status, isEmpty);
repo.checkout(
target: 'refs/heads/feature',
Checkout.reference(
repo: repo,
name: 'refs/heads/feature',
paths: ['another_feature_file'],
);
expect(
@ -175,7 +203,7 @@ void main() {
'another_feature_file': {GitStatus.indexNew}
},
);
// When path is provided HEAD will not be set to target;
// does not change HEAD
expect(repo.head.target, isNot(featureTip));
});
@ -185,8 +213,9 @@ void main() {
final file = File(p.join(repo.workdir, 'another_feature_file'));
expect(file.existsSync(), false);
repo.checkout(
target: 'refs/heads/feature',
Checkout.reference(
repo: repo,
name: 'refs/heads/feature',
strategy: {GitCheckout.dryRun},
);
expect(index.length, 4);
@ -194,9 +223,5 @@ void main() {
index.free();
});
test('throws when provided target is not String or Oid', () {
expect(() => repo.checkout(target: 1), throwsA(isA<ArgumentError>()));
});
});
}