-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from jxstxn1/add-missing-field-to-pubspec
Add missing field to pubspec
- Loading branch information
Showing
3 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import 'package:pubspec/pubspec.dart'; | ||
|
||
/// {@template pubspec_extensions} | ||
/// Extensions on [PubSpec] to provide additional functionality. | ||
/// | ||
/// Following the pubspec file definition on dart.dev | ||
/// https://dart.dev/tools/pub/pubspec | ||
/// {@endtemplate} | ||
extension PubspecExtensions on PubSpec { | ||
/// Optional. URL pointing to the package's source code repository. | ||
String? repository() => unParsedYaml?['repository']; | ||
|
||
/// Optional. URL pointing to an issue tracker for the package. | ||
String? issueTracker() => unParsedYaml?['issue_tracker']; | ||
|
||
/// Optional. List of URLs where users can sponsor development of the package. | ||
List? funding() => unParsedYaml?['funding']; | ||
|
||
/// Optional. Specify files to ignore when conducting a pre-publishing search | ||
/// for potential leaks of secrets. | ||
List? falseSecrets() => unParsedYaml?['false_secrets']; | ||
|
||
/// Optional. Specify a list of screenshot files to display on pub.dev | ||
List<Screenshot>? screenshots() { | ||
final screenShotList = unParsedYaml?['screenshots']; | ||
if (screenShotList == null) { | ||
return null; | ||
} | ||
final List<Screenshot> screenshots = []; | ||
for (final screenShot in screenShotList) { | ||
screenshots.add(Screenshot.fromJson(screenShot)); | ||
} | ||
return screenshots; | ||
} | ||
|
||
/// Optional field to list the topics that this packages belongs to. | ||
List? topics() => unParsedYaml?['topics']; | ||
|
||
/// Optional. List of ignored security advisories. | ||
List? ignoredAdvisories() => unParsedYaml?['ignored_advisories']; | ||
} | ||
|
||
/// {@template screenshot} | ||
/// A screenshot of the package to display on pub.dev. | ||
/// {@endtemplate} | ||
class Screenshot { | ||
const Screenshot(this.path, this.description); | ||
|
||
final String path; | ||
final String description; | ||
|
||
factory Screenshot.fromJson(Map json) => Screenshot( | ||
json['path'] as String, | ||
json['description'] as String, | ||
); | ||
|
||
@override | ||
String toString() => 'Screenshot(path: $path, description: $description)'; | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
if (identical(this, other)) return true; | ||
|
||
return other is Screenshot && | ||
other.path == path && | ||
other.description == description; | ||
} | ||
|
||
@override | ||
int get hashCode => path.hashCode ^ description.hashCode; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import 'package:pub_api_client/pub_api_client.dart'; | ||
import 'package:pubspec/pubspec.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test('Should be able to parse repository field', () async { | ||
expect(pubspec.repository(), 'https://foo.bar/repo'); | ||
}); | ||
|
||
test('Should be able to parse issue tracker field', () async { | ||
expect(pubspec.issueTracker(), 'https://foo.bar/issues'); | ||
}); | ||
|
||
test('Should be able to parse funding field', () async { | ||
final sponsors = pubspec.funding(); | ||
expect(sponsors, isA<List>()); | ||
expect(sponsors, hasLength(2)); | ||
expect(sponsors?[0], 'https://foo.bar/sponsor1'); | ||
expect(sponsors?[1], 'https://foo.bar/sponsor2'); | ||
}); | ||
|
||
test('Should be able to parse false secrets field', () async { | ||
final falseSecrets = pubspec.falseSecrets(); | ||
expect(falseSecrets, isA<List>()); | ||
expect(falseSecrets, hasLength(2)); | ||
expect(falseSecrets?[0], '/lib/foo/bar.dart'); | ||
expect(falseSecrets?[1], '/lib/bar/foo.dart'); | ||
}); | ||
|
||
test('Should be able to parse screenshots field', () async { | ||
final screenshots = pubspec.screenshots(); | ||
|
||
expect(screenshots, isA<List<Screenshot>>()); | ||
expect(screenshots, hasLength(1)); | ||
|
||
final firstScreenShot = screenshots?[0]; | ||
expect(firstScreenShot, isA<Screenshot>()); | ||
expect(firstScreenShot?.description, 'foo-bar screenshot'); | ||
expect(firstScreenShot?.path, 'screenshots/foo-bar.png'); | ||
}); | ||
|
||
test('Should be able to parse topics field', () async { | ||
final topics = pubspec.topics(); | ||
expect(topics, isA<List>()); | ||
expect(topics, hasLength(2)); | ||
expect(topics?[0], 'bar'); | ||
expect(topics?[1], 'foo'); | ||
}); | ||
|
||
test('Should be able to parse ignoredAdvisories field', () async { | ||
final ignoredAdvisories = pubspec.ignoredAdvisories(); | ||
expect(ignoredAdvisories, isA<List>()); | ||
expect(ignoredAdvisories, hasLength(2)); | ||
expect(ignoredAdvisories?[0], 'foo-bar'); | ||
expect(ignoredAdvisories?[1], 'bar-foo'); | ||
}); | ||
} | ||
|
||
const pubspec = PubSpec( | ||
unParsedYaml: { | ||
'repository': 'https://foo.bar/repo', | ||
'issue_tracker': 'https://foo.bar/issues', | ||
'funding': ['https://foo.bar/sponsor1', 'https://foo.bar/sponsor2'], | ||
'false_secrets': ['/lib/foo/bar.dart', '/lib/bar/foo.dart'], | ||
'topics': ['bar', 'foo'], | ||
'ignored_advisories': ['foo-bar', 'bar-foo'], | ||
'screenshots': [ | ||
{ | ||
'description': 'foo-bar screenshot', | ||
'path': 'screenshots/foo-bar.png', | ||
} | ||
], | ||
}, | ||
); |