Skip to content

Commit 068119b

Browse files
authored
[pubspec_parse] Support tag_pattern for git dependencies (#2565)
Add support for the `tag_pattern` key on git dependencies, introduced in Dart 3.9. See https://dart.dev/tools/pub/dependencies#git-packages - Add nullable `tagPattern` and `version` fields to `GitDependency`. The existing constructor is unchanged, and `GitDependency.fromData` gains only an optional named `version` parameter. - Parse the `version` key next to a `git` source into `GitDependency.version` instead of silently dropping it, matching the pub client, which reads the constraint for every source. An invalid constraint is now reported as a parse error. - Validate `tag_pattern` the same way as the pub client: it must contain a single `{{version}}` marker and cannot be combined with `ref`. Errors point at the `tag_pattern` key. As with the rest of this package, nothing is gated on the language version, so `tag_pattern` parses under an SDK constraint older than 3.9 even though pub rejects it. - Include `tag_pattern` and `version` in `GitDependency.toJson` when present so that `Pubspec.toJson` round-trips. - Add tests for parsing, validation errors, and `toJson` round-tripping. Parsing tests are cross-checked against the pub client like the rest of the suite. Fixes #2155
1 parent 7d859ed commit 068119b

6 files changed

Lines changed: 252 additions & 14 deletions

File tree

‎pkgs/pubspec_parse/CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
## 1.6.1
1+
## 1.7.0-wip
22

3+
- Added `tagPattern` and `version` fields to `GitDependency`, to support the
4+
`tag_pattern` key for git dependencies introduced in Dart 3.9.
35
- Added a usage example.
46

57
## 1.6.0

‎pkgs/pubspec_parse/lib/src/dependency.dart‎

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ Dependency? _fromJson(Object? data, String name) {
8181
final key = matchedKeys.single;
8282

8383
return switch (key) {
84-
'git' => GitDependency.fromData(data[key]),
84+
'git' => GitDependency.fromData(
85+
data[key],
86+
version: _optionalConstraintFromData(data),
87+
),
8588
'path' => PathDependency.fromData(data[key]),
8689
'sdk' => _$SdkDependencyFromJson(data),
8790
'hosted' => _$HostedDependencyFromJson(
@@ -131,39 +134,95 @@ class GitDependency extends Dependency {
131134
final String? ref;
132135
final String? path;
133136

134-
GitDependency(this.url, {this.ref, this.path});
137+
/// A pattern used to find version tagged commits in the repository.
138+
///
139+
/// Must contain a single `{{version}}` marker, for example `v{{version}}`.
140+
/// Cannot be combined with [ref].
141+
///
142+
/// See https://dart.dev/tools/pub/dependencies#git-packages.
143+
final String? tagPattern;
144+
145+
/// The version constraint specified next to the `git` source.
146+
///
147+
/// `null` if no constraint is specified, which pub treats as any version.
148+
/// This is mostly useful together with [tagPattern].
149+
@JsonKey(includeFromJson: false)
150+
final VersionConstraint? version;
151+
152+
GitDependency(this.url, {this.ref, this.path, this.tagPattern, this.version});
135153

136-
factory GitDependency.fromData(Object? data) {
154+
factory GitDependency.fromData(Object? data, {VersionConstraint? version}) {
137155
if (data is String) {
138156
data = {'url': data};
139157
}
140158

141159
if (data is Map) {
142-
return _$GitDependencyFromJson(data);
160+
final parsed = _$GitDependencyFromJson(data);
161+
_validateTagPattern(data, parsed);
162+
return GitDependency(
163+
parsed.url,
164+
ref: parsed.ref,
165+
path: parsed.path,
166+
tagPattern: parsed.tagPattern,
167+
version: version,
168+
);
143169
}
144170

145171
throw ArgumentError.value(data, 'git', 'Must be a String or a Map.');
146172
}
147173

174+
static void _validateTagPattern(Map data, GitDependency parsed) {
175+
final tagPattern = parsed.tagPattern;
176+
if (tagPattern == null) {
177+
return;
178+
}
179+
if (tagPattern.split(_tagPatternVersionMarker).length != 2) {
180+
throw CheckedFromJsonException(
181+
data,
182+
'tag_pattern',
183+
'GitDependency',
184+
'Must contain a single "$_tagPatternVersionMarker".',
185+
);
186+
}
187+
if (parsed.ref != null) {
188+
throw CheckedFromJsonException(
189+
data,
190+
'tag_pattern',
191+
'GitDependency',
192+
'Cannot be used together with "ref".',
193+
);
194+
}
195+
}
196+
148197
@override
149198
bool operator ==(Object other) =>
150199
other is GitDependency &&
151200
other.url == url &&
152201
other.ref == ref &&
153-
other.path == path;
202+
other.path == path &&
203+
other.tagPattern == tagPattern &&
204+
other.version == version;
154205

155206
@override
156-
int get hashCode => Object.hash(url, ref, path);
207+
int get hashCode => Object.hash(url, ref, path, tagPattern, version);
157208

158209
@override
159210
String toString() => 'GitDependency: url@$url';
160211

161212
@override
162213
Map<String, dynamic> toJson() => {
163-
'git': {'url': url.toString(), 'ref': ?ref, 'path': ?path},
214+
'git': {
215+
'url': url.toString(),
216+
'ref': ?ref,
217+
'path': ?path,
218+
'tag_pattern': ?tagPattern,
219+
},
220+
'version': ?version?.toString(),
164221
};
165222
}
166223

224+
const _tagPatternVersionMarker = '{{version}}';
225+
167226
Uri? _parseUriOrNull(String? value) => value == null ? null : Uri.parse(value);
168227

169228
Uri parseGitUri(String value) => _tryParseScpUri(value) ?? Uri.parse(value);
@@ -306,3 +365,23 @@ class HostedDetails {
306365

307366
VersionConstraint _constraintFromString(String? input) =>
308367
input == null ? VersionConstraint.any : VersionConstraint.parse(input);
368+
369+
VersionConstraint? _optionalConstraintFromData(Map data) {
370+
final value = data['version'];
371+
if (value == null) {
372+
return null;
373+
}
374+
if (value is! String) {
375+
throw CheckedFromJsonException(
376+
data,
377+
'version',
378+
'GitDependency',
379+
'`$value` is not a String.',
380+
);
381+
}
382+
try {
383+
return VersionConstraint.parse(value);
384+
} on FormatException catch (e) {
385+
throw CheckedFromJsonException(data, 'version', 'GitDependency', e.message);
386+
}
387+
}

‎pkgs/pubspec_parse/lib/src/dependency.g.dart‎

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkgs/pubspec_parse/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: pubspec_parse
2-
version: 1.6.1
2+
version: 1.7.0-wip
33
description: >-
44
Simple package for parsing pubspec.yaml files with a type-safe API and rich
55
error reporting.

‎pkgs/pubspec_parse/test/dependency_test.dart‎

Lines changed: 140 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import 'package:test/test.dart';
1010

1111
import 'test_utils.dart';
1212

13+
/// The pub client only accepts `tag_pattern` from Dart 3.9, so the
14+
/// cross-check against it is skipped on older SDKs.
15+
final _sdkSupportsTagPattern =
16+
Version.parse(Platform.version.split(' ').first) >= Version(3, 9, 0);
17+
1318
void main() {
1419
group('hosted', _hostedDependency);
1520
group('git', _gitDependency);
@@ -247,7 +252,7 @@ void _gitDependency() {
247252
expect(dep.toString(), 'GitDependency: url@url');
248253
});
249254

250-
test('string with version key is ignored', () async {
255+
test('string with version key', () async {
251256
// Regression test for https://github.com/dart-lang/pubspec_parse/issues/13
252257
final dep = await _dependency<GitDependency>({
253258
'git': 'url',
@@ -256,9 +261,42 @@ void _gitDependency() {
256261
expect(dep.url.toString(), 'url');
257262
expect(dep.path, isNull);
258263
expect(dep.ref, isNull);
264+
expect(dep.tagPattern, isNull);
265+
expect(dep.version, VersionConstraint.parse('^1.2.3'));
259266
expect(dep.toString(), 'GitDependency: url@url');
260267
});
261268

269+
test('string without version key', () async {
270+
final dep = await _dependency<GitDependency>({'git': 'url'});
271+
expect(dep.version, isNull);
272+
});
273+
274+
test('string with invalid version key fails', () {
275+
_expectThrows(
276+
{'git': 'url', 'version': 'not a version'},
277+
r'''
278+
line 6, column 15: Unsupported value for "version". Could not parse version "not a version". Unknown text at "not a version".
279+
╷
280+
6 │ "version": "not a version"
281+
│ ^^^^^^^^^^^^^^^
282+
╵''',
283+
);
284+
});
285+
286+
test('string with non-String version key fails', () {
287+
_expectThrows(
288+
{'git': 'url', 'version': 42},
289+
r'''
290+
line 6, column 15: Unsupported value for "version". `42` is not a String.
291+
╷
292+
6 │ "version": 42
293+
│ ┌───────────────^
294+
7 │ │ }
295+
│ └──^
296+
╵''',
297+
);
298+
});
299+
262300
test('string with user@ URL', () async {
263301
final skipTryParse = Platform.environment.containsKey('TRAVIS');
264302
if (skipTryParse) {
@@ -292,9 +330,96 @@ line 6, column 4: Unrecognized keys: [bob]; supported keys: [sdk, git, path, hos
292330
expect(dep.url.toString(), 'url');
293331
expect(dep.path, 'path');
294332
expect(dep.ref, 'ref');
333+
expect(dep.tagPattern, isNull);
334+
expect(dep.version, isNull);
335+
expect(dep.toString(), 'GitDependency: url@url');
336+
});
337+
338+
test('map with tag_pattern and version', () async {
339+
final dep = await _dependency<GitDependency>(
340+
{
341+
'git': {'url': 'url', 'path': 'path', 'tag_pattern': 'v{{version}}'},
342+
'version': '^1.2.3',
343+
},
344+
sdkConstraint: '^3.9.0',
345+
skipTryPub: !_sdkSupportsTagPattern,
346+
);
347+
expect(dep.url.toString(), 'url');
348+
expect(dep.path, 'path');
349+
expect(dep.ref, isNull);
350+
expect(dep.tagPattern, 'v{{version}}');
351+
expect(dep.version, VersionConstraint.parse('^1.2.3'));
295352
expect(dep.toString(), 'GitDependency: url@url');
296353
});
297354

355+
test('map with tag_pattern without version', () async {
356+
final dep = await _dependency<GitDependency>(
357+
{
358+
'git': {'url': 'url', 'tag_pattern': 'v{{version}}'},
359+
},
360+
sdkConstraint: '^3.9.0',
361+
skipTryPub: !_sdkSupportsTagPattern,
362+
);
363+
expect(dep.tagPattern, 'v{{version}}');
364+
expect(dep.version, isNull);
365+
});
366+
367+
test('map with tag_pattern and ref fails', () {
368+
_expectThrows(
369+
{
370+
'git': {'url': 'url', 'ref': 'ref', 'tag_pattern': 'v{{version}}'},
371+
},
372+
r'''
373+
line 11, column 20: Unsupported value for "tag_pattern". Cannot be used together with "ref".
374+
╷
375+
11 │ "tag_pattern": "v{{version}}"
376+
│ ^^^^^^^^^^^^^^
377+
╵''',
378+
sdkConstraint: '^3.9.0',
379+
);
380+
});
381+
382+
test('map with tag_pattern missing {{version}} fails', () {
383+
_expectThrows(
384+
{
385+
'git': {'url': 'url', 'tag_pattern': 'v'},
386+
},
387+
r'''
388+
line 10, column 20: Unsupported value for "tag_pattern". Must contain a single "{{version}}".
389+
╷
390+
10 │ "tag_pattern": "v"
391+
│ ^^^
392+
╵''',
393+
sdkConstraint: '^3.9.0',
394+
);
395+
});
396+
397+
test('map with tag_pattern containing {{version}} twice fails', () {
398+
_expectThrows(
399+
{
400+
'git': {'url': 'url', 'tag_pattern': '{{version}}-{{version}}'},
401+
},
402+
r'''
403+
line 10, column 20: Unsupported value for "tag_pattern". Must contain a single "{{version}}".
404+
╷
405+
10 │ "tag_pattern": "{{version}}-{{version}}"
406+
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
407+
╵''',
408+
sdkConstraint: '^3.9.0',
409+
);
410+
});
411+
412+
test('map with non-String tag_pattern fails', () {
413+
_expectThrowsContaining(
414+
{
415+
'git': {'url': 'url', 'tag_pattern': 42},
416+
},
417+
'Unsupported value for "tag_pattern". '
418+
"type 'int' is not a subtype of type 'String?' in type cast",
419+
sdkConstraint: '^3.9.0',
420+
);
421+
});
422+
298423
test('git - null content', () {
299424
_expectThrows(
300425
{'git': null},
@@ -399,26 +524,38 @@ line 5, column 12: Unsupported value for "path". Must be a String.
399524
});
400525
}
401526

402-
void _expectThrows(Object content, String expectedError) {
527+
void _expectThrows(
528+
Object content,
529+
String expectedError, {
530+
String? sdkConstraint,
531+
}) {
403532
expectParseThrows({
404533
'name': 'sample',
534+
if (sdkConstraint != null) 'environment': {'sdk': sdkConstraint},
405535
'dependencies': {'dep': content},
406536
}, expectedError);
407537
}
408538

409-
void _expectThrowsContaining(Object content, String errorText) {
539+
void _expectThrowsContaining(
540+
Object content,
541+
String errorText, {
542+
String? sdkConstraint,
543+
}) {
410544
expectParseThrowsContaining({
411545
'name': 'sample',
546+
if (sdkConstraint != null) 'environment': {'sdk': sdkConstraint},
412547
'dependencies': {'dep': content},
413548
}, errorText);
414549
}
415550

416551
Future<T> _dependency<T extends Dependency>(
417552
Object? content, {
418553
bool skipTryPub = false,
554+
String? sdkConstraint,
419555
}) async {
420556
final value = await parse({
421557
...defaultPubspec,
558+
if (sdkConstraint != null) 'environment': {'sdk': sdkConstraint},
422559
'dependencies': {'dep': content},
423560
}, skipTryPub: skipTryPub);
424561
expect(value.name, 'sample');

0 commit comments

Comments
 (0)