Skip to content

Commit e122039

Browse files
[tool] Remove --against-pub flag (#11550)
We haven't used `--against-pub` in CI in a very long time, and we don't expect to need it locally. We report pub versions as part of the publish step, but the source of truth for validation is the repo, not pub. This removes the flag to simplify the tool.
1 parent 624a8be commit e122039

2 files changed

Lines changed: 10 additions & 195 deletions

File tree

script/tool/lib/src/version_check_command.dart

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// found in the LICENSE file.
44

55
import 'package:file/file.dart';
6-
import 'package:http/http.dart' as http;
76
import 'package:meta/meta.dart';
87
import 'package:path/path.dart' as p;
98
import 'package:pub_semver/pub_semver.dart';
@@ -12,7 +11,6 @@ import 'common/git_version_finder.dart';
1211
import 'common/output_utils.dart';
1312
import 'common/package_looping_command.dart';
1413
import 'common/package_state_utils.dart';
15-
import 'common/pub_version_finder.dart';
1614
import 'common/repository_package.dart';
1715

1816
/// Categories of version change types.
@@ -98,17 +96,7 @@ class VersionCheckCommand extends PackageLoopingCommand {
9896
super.processRunner,
9997
super.platform,
10098
super.gitDir,
101-
http.Client? httpClient,
102-
}) : _pubVersionFinder = PubVersionFinder(
103-
httpClient: httpClient ?? http.Client(),
104-
) {
105-
argParser.addFlag(
106-
_againstPubFlag,
107-
help:
108-
'Whether the version check should run against the version on pub.\n'
109-
'Defaults to false, which means the version check only run against '
110-
'the previous version in code.',
111-
);
99+
}) {
112100
argParser.addOption(
113101
_prLabelsArg,
114102
help:
@@ -140,7 +128,6 @@ class VersionCheckCommand extends PackageLoopingCommand {
140128
);
141129
}
142130

143-
static const String _againstPubFlag = 'against-pub';
144131
static const String _prLabelsArg = 'pr-labels';
145132
static const String _checkForMissingChanges = 'check-for-missing-changes';
146133
static const String _ignorePlatformInterfaceBreaks =
@@ -161,8 +148,6 @@ class VersionCheckCommand extends PackageLoopingCommand {
161148
static const String _missingChangelogChangeOverrideLabel =
162149
'override: no changelog needed';
163150

164-
final PubVersionFinder _pubVersionFinder;
165-
166151
late final GitVersionFinder _gitVersionFinder;
167152

168153
late final Set<String> _prLabels = _getPRLabels();
@@ -184,7 +169,7 @@ class VersionCheckCommand extends PackageLoopingCommand {
184169
final String description =
185170
'Checks if the versions of packages have been incremented per pub specification.\n'
186171
'Also checks if the latest version in CHANGELOG matches the version in pubspec.\n\n'
187-
'This command requires "pub" and "flutter" to be in your path.';
172+
'This command requires "flutter" to be in your path.';
188173

189174
@override
190175
bool get hasLongOutput => false;
@@ -303,33 +288,6 @@ class VersionCheckCommand extends PackageLoopingCommand {
303288
: PackageResult.fail(errors);
304289
}
305290

306-
@override
307-
Future<void> completeRun() async {
308-
_pubVersionFinder.httpClient.close();
309-
}
310-
311-
/// Returns the previous published version of [package].
312-
///
313-
/// [packageName] must be the actual name of the package as published (i.e.,
314-
/// the name from pubspec.yaml, not the on disk name if different.)
315-
Future<Version?> _fetchPreviousVersionFromPub(String packageName) async {
316-
final PubVersionFinderResponse pubVersionFinderResponse =
317-
await _pubVersionFinder.getPackageVersion(packageName: packageName);
318-
switch (pubVersionFinderResponse.result) {
319-
case PubVersionFinderResult.success:
320-
return pubVersionFinderResponse.versions.first;
321-
case PubVersionFinderResult.fail:
322-
printError('''
323-
${indentation}Error fetching version on pub for $packageName.
324-
${indentation}HTTP Status ${pubVersionFinderResponse.httpResponse.statusCode}
325-
${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body}
326-
''');
327-
return null;
328-
case PubVersionFinderResult.noPackageFound:
329-
return Version.none;
330-
}
331-
}
332-
333291
/// Returns the version of [package] from git at the base comparison hash.
334292
Future<Version?> _getPreviousVersionFromGit(RepositoryPackage package) async {
335293
final File pubspecFile = package.pubspecFile;
@@ -352,28 +310,12 @@ ${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body}
352310
}) async {
353311
// This method isn't called unless `version` is non-null.
354312
final Version currentVersion = pubspec.version!;
355-
Version? previousVersion;
356-
String previousVersionSource;
357-
if (getBoolArg(_againstPubFlag)) {
358-
previousVersionSource = 'pub';
359-
previousVersion = await _fetchPreviousVersionFromPub(pubspec.name);
360-
if (previousVersion == null) {
361-
return _CurrentVersionState.unknown;
362-
}
363-
if (previousVersion != Version.none) {
364-
print(
365-
'$indentation${pubspec.name}: Current largest version on pub: $previousVersion',
366-
);
367-
}
368-
} else {
369-
previousVersionSource = baseSha;
370-
previousVersion =
371-
await _getPreviousVersionFromGit(package) ?? Version.none;
372-
}
313+
final Version previousVersion =
314+
await _getPreviousVersionFromGit(package) ?? Version.none;
373315
if (previousVersion == Version.none) {
374316
print(
375317
'${indentation}Unable to find previous version '
376-
'${getBoolArg(_againstPubFlag) ? 'on pub server' : 'at git base'}.',
318+
'at git base.',
377319
);
378320
logWarning(
379321
'${indentation}If this package is not new, something has gone wrong.',
@@ -387,7 +329,7 @@ ${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body}
387329
}
388330

389331
// Check for reverts when doing local validation.
390-
if (!getBoolArg(_againstPubFlag) && currentVersion < previousVersion) {
332+
if (currentVersion < previousVersion) {
391333
// Since this skips validation, try to ensure that it really is likely
392334
// to be a revert rather than a typo by checking that the transition
393335
// from the lower version to the new version would have been valid.
@@ -414,7 +356,7 @@ ${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body}
414356
} else {
415357
printError(
416358
'${indentation}Incorrectly updated version.\n'
417-
'${indentation}HEAD: $currentVersion, $previousVersionSource: $previousVersion.\n'
359+
'${indentation}HEAD: $currentVersion, $baseSha: $previousVersion.\n'
418360
'${indentation}Allowed versions: $allowedNextVersions',
419361
);
420362
return _CurrentVersionState.invalidChange;

script/tool/test/version_check_command_test.dart

Lines changed: 3 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:convert';
6-
75
import 'package:args/command_runner.dart';
86
import 'package:file/file.dart';
97
import 'package:flutter_plugin_tools/src/common/core.dart';
108
import 'package:flutter_plugin_tools/src/version_check_command.dart';
119
import 'package:git/git.dart';
12-
import 'package:http/http.dart' as http;
13-
import 'package:http/testing.dart';
1410
import 'package:pub_semver/pub_semver.dart';
1511
import 'package:test/test.dart';
1612

@@ -40,15 +36,11 @@ void testAllowedVersion(
4036
}
4137

4238
void main() {
43-
const indentation = ' ';
4439
group('VersionCheckCommand', () {
4540
late MockPlatform mockPlatform;
4641
late Directory packagesDir;
4742
late CommandRunner<void> runner;
4843
late RecordingProcessRunner gitProcessRunner;
49-
// Ignored if mockHttpResponse is set.
50-
int mockHttpStatus;
51-
Map<String, dynamic>? mockHttpResponse;
5244

5345
setUp(() {
5446
mockPlatform = MockPlatform();
@@ -57,22 +49,11 @@ void main() {
5749
(:packagesDir, :processRunner, :gitProcessRunner, :gitDir) =
5850
configureBaseCommandMocks(platform: mockPlatform);
5951

60-
// Default to simulating the plugin never having been published.
61-
mockHttpStatus = 404;
62-
mockHttpResponse = null;
63-
final mockClient = MockClient((http.Request request) async {
64-
return http.Response(
65-
json.encode(mockHttpResponse),
66-
mockHttpResponse == null ? mockHttpStatus : 200,
67-
);
68-
});
69-
7052
final command = VersionCheckCommand(
7153
packagesDir,
7254
processRunner: processRunner,
7355
platform: mockPlatform,
7456
gitDir: gitDir,
75-
httpClient: mockClient,
7657
);
7758

7859
runner = CommandRunner<void>(
@@ -448,7 +429,7 @@ void main() {
448429
Error? commandError;
449430
final List<String> output = await runCapturingPrint(
450431
runner,
451-
<String>['version-check', '--base-sha=main', '--against-pub'],
432+
<String>['version-check', '--base-sha=main'],
452433
errorHandler: (Error e) {
453434
commandError = e;
454435
},
@@ -594,7 +575,7 @@ void main() {
594575
var hasError = false;
595576
final List<String> output = await runCapturingPrint(
596577
runner,
597-
<String>['version-check', '--base-sha=main', '--against-pub'],
578+
<String>['version-check', '--base-sha=main'],
598579
errorHandler: (Error e) {
599580
expect(e, isA<ToolExit>());
600581
hasError = true;
@@ -669,7 +650,7 @@ void main() {
669650
var hasError = false;
670651
final List<String> output = await runCapturingPrint(
671652
runner,
672-
<String>['version-check', '--base-sha=main', '--against-pub'],
653+
<String>['version-check', '--base-sha=main'],
673654
errorHandler: (Error e) {
674655
expect(e, isA<ToolExit>());
675656
hasError = true;
@@ -1652,114 +1633,6 @@ packages/plugin/lib/plugin.dart
16521633
});
16531634
});
16541635

1655-
test('allows valid against pub', () async {
1656-
mockHttpResponse = <String, dynamic>{
1657-
'name': 'some_package',
1658-
'versions': <String>['0.0.1', '0.0.2', '1.0.0'],
1659-
};
1660-
1661-
createFakePlugin('plugin', packagesDir, version: '2.0.0');
1662-
final List<String> output = await runCapturingPrint(runner, <String>[
1663-
'version-check',
1664-
'--base-sha=main',
1665-
'--against-pub',
1666-
]);
1667-
1668-
expect(
1669-
output,
1670-
containsAllInOrder(<Matcher>[
1671-
contains('plugin: Current largest version on pub: 1.0.0'),
1672-
]),
1673-
);
1674-
});
1675-
1676-
test('denies invalid against pub', () async {
1677-
mockHttpResponse = <String, dynamic>{
1678-
'name': 'some_package',
1679-
'versions': <String>['0.0.1', '0.0.2'],
1680-
};
1681-
1682-
createFakePlugin('plugin', packagesDir, version: '2.0.0');
1683-
1684-
var hasError = false;
1685-
final List<String> result = await runCapturingPrint(
1686-
runner,
1687-
<String>['version-check', '--base-sha=main', '--against-pub'],
1688-
errorHandler: (Error e) {
1689-
expect(e, isA<ToolExit>());
1690-
hasError = true;
1691-
},
1692-
);
1693-
expect(hasError, isTrue);
1694-
1695-
expect(
1696-
result,
1697-
containsAllInOrder(<Matcher>[
1698-
contains(
1699-
'''
1700-
${indentation}Incorrectly updated version.
1701-
${indentation}HEAD: 2.0.0, pub: 0.0.2.
1702-
${indentation}Allowed versions: {1.0.0: NextVersionType.BREAKING_MAJOR, 0.1.0: NextVersionType.MINOR, 0.0.3: NextVersionType.PATCH}''',
1703-
),
1704-
]),
1705-
);
1706-
});
1707-
1708-
test(
1709-
'throw and print error message if http request failed when checking against pub',
1710-
() async {
1711-
mockHttpStatus = 400;
1712-
1713-
createFakePlugin('plugin', packagesDir, version: '2.0.0');
1714-
var hasError = false;
1715-
final List<String> result = await runCapturingPrint(
1716-
runner,
1717-
<String>['version-check', '--base-sha=main', '--against-pub'],
1718-
errorHandler: (Error e) {
1719-
expect(e, isA<ToolExit>());
1720-
hasError = true;
1721-
},
1722-
);
1723-
expect(hasError, isTrue);
1724-
1725-
expect(
1726-
result,
1727-
containsAllInOrder(<Matcher>[
1728-
contains('''
1729-
${indentation}Error fetching version on pub for plugin.
1730-
${indentation}HTTP Status 400
1731-
${indentation}HTTP response: null
1732-
'''),
1733-
]),
1734-
);
1735-
},
1736-
);
1737-
1738-
test(
1739-
'when checking against pub, allow any version if http status is 404.',
1740-
() async {
1741-
mockHttpStatus = 404;
1742-
1743-
createFakePlugin('plugin', packagesDir, version: '2.0.0');
1744-
gitProcessRunner.mockProcessesForExecutable['git-show'] =
1745-
<FakeProcessInfo>[
1746-
FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')),
1747-
];
1748-
final List<String> result = await runCapturingPrint(runner, <String>[
1749-
'version-check',
1750-
'--base-sha=main',
1751-
'--against-pub',
1752-
]);
1753-
1754-
expect(
1755-
result,
1756-
containsAllInOrder(<Matcher>[
1757-
contains('Unable to find previous version on pub server.'),
1758-
]),
1759-
);
1760-
},
1761-
);
1762-
17631636
group('prelease versions', () {
17641637
test(
17651638
'allow an otherwise-valid transition that also adds a pre-release component',

0 commit comments

Comments
 (0)