Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 583c71c

Browse files
authored
Clang-tidy: Fixed math on shard-id validator. (#37433)
Clang-tidy: Fixed math on shard-id validator.
1 parent c171f27 commit 583c71c

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

tools/clang_tidy/lib/src/options.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class Options {
120120
}
121121
final String? shardIdString = argResults['shard-id'] as String?;
122122
final int? shardId = shardIdString == null ? null : int.parse(shardIdString);
123-
if (shardId != null && (shardId >= shardCommands.length || shardId < 0)) {
123+
if (shardId != null && (shardId > shardCommands.length || shardId < 0)) {
124124
return Options._error('Invalid shard-id value: $shardId.', errSink: errSink);
125125
}
126126
return Options._fromArgResults(

tools/clang_tidy/test/clang_tidy_test.dart

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
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:io' as io show File, Platform, stderr;
5+
import 'dart:io' as io show Directory, File, Platform, stderr;
66

77
import 'package:clang_tidy/clang_tidy.dart';
88
import 'package:clang_tidy/src/command.dart';
99
import 'package:clang_tidy/src/options.dart';
1010
import 'package:litetest/litetest.dart';
11+
import 'package:path/path.dart' as path;
1112
import 'package:process_runner/process_runner.dart';
1213

1314
// Recorded locally from clang-tidy.
@@ -38,6 +39,18 @@ Suppressed 3474 warnings (3466 in non-user code, 8 NOLINT).
3839
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
3940
1 warning treated as error''';
4041

42+
void _withTempFile(String prefix, void Function(String path) func) {
43+
final String filePath =
44+
path.join(io.Directory.systemTemp.path, '$prefix-temp-file');
45+
final io.File file = io.File(filePath);
46+
file.createSync();
47+
try {
48+
func(file.path);
49+
} finally {
50+
file.deleteSync();
51+
}
52+
}
53+
4154
Future<int> main(List<String> args) async {
4255
if (args.isEmpty) {
4356
io.stderr.writeln(
@@ -115,6 +128,37 @@ Future<int> main(List<String> args) async {
115128
));
116129
});
117130

131+
test('shard-id valid', () async {
132+
_withTempFile('shard-id-valid', (String path) {
133+
final Options options = Options.fromCommandLine( <String>[
134+
'--compile-commands=$path',
135+
'--shard-variants=variant',
136+
'--shard-id=1',
137+
],);
138+
expect(options.errorMessage, isNull);
139+
expect(options.shardId, equals(1));
140+
});
141+
});
142+
143+
test('shard-id invalid', () async {
144+
_withTempFile('shard-id-valid', (String path) {
145+
final StringBuffer errBuffer = StringBuffer();
146+
final Options options = Options.fromCommandLine(<String>[
147+
'--compile-commands=$path',
148+
'--shard-variants=variant',
149+
'--shard-id=2',
150+
], errSink: errBuffer);
151+
expect(options.errorMessage, isNotNull);
152+
expect(options.shardId, isNull);
153+
print('foo ${options.errorMessage}');
154+
expect(
155+
options.errorMessage,
156+
contains(
157+
'Invalid shard-id value',
158+
));
159+
});
160+
});
161+
118162
test('Error when --compile-commands path does not exist', () async {
119163
final StringBuffer outBuffer = StringBuffer();
120164
final StringBuffer errBuffer = StringBuffer();

0 commit comments

Comments
 (0)