Skip to content

Commit

Permalink
Clang-tidy: Fixed math on shard-id validator. (flutter#37433)
Browse files Browse the repository at this point in the history
Clang-tidy: Fixed math on shard-id validator.
  • Loading branch information
gaaclarke authored and naudzghebre committed Nov 9, 2022
1 parent 1471086 commit 8d4911c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tools/clang_tidy/lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class Options {
}
final String? shardIdString = argResults['shard-id'] as String?;
final int? shardId = shardIdString == null ? null : int.parse(shardIdString);
if (shardId != null && (shardId >= shardCommands.length || shardId < 0)) {
if (shardId != null && (shardId > shardCommands.length || shardId < 0)) {
return Options._error('Invalid shard-id value: $shardId.', errSink: errSink);
}
return Options._fromArgResults(
Expand Down
46 changes: 45 additions & 1 deletion tools/clang_tidy/test/clang_tidy_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io' as io show File, Platform, stderr;
import 'dart:io' as io show Directory, File, Platform, stderr;

import 'package:clang_tidy/clang_tidy.dart';
import 'package:clang_tidy/src/command.dart';
import 'package:clang_tidy/src/options.dart';
import 'package:litetest/litetest.dart';
import 'package:path/path.dart' as path;
import 'package:process_runner/process_runner.dart';

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

void _withTempFile(String prefix, void Function(String path) func) {
final String filePath =
path.join(io.Directory.systemTemp.path, '$prefix-temp-file');
final io.File file = io.File(filePath);
file.createSync();
try {
func(file.path);
} finally {
file.deleteSync();
}
}

Future<int> main(List<String> args) async {
if (args.isEmpty) {
io.stderr.writeln(
Expand Down Expand Up @@ -115,6 +128,37 @@ Future<int> main(List<String> args) async {
));
});

test('shard-id valid', () async {
_withTempFile('shard-id-valid', (String path) {
final Options options = Options.fromCommandLine( <String>[
'--compile-commands=$path',
'--shard-variants=variant',
'--shard-id=1',
],);
expect(options.errorMessage, isNull);
expect(options.shardId, equals(1));
});
});

test('shard-id invalid', () async {
_withTempFile('shard-id-valid', (String path) {
final StringBuffer errBuffer = StringBuffer();
final Options options = Options.fromCommandLine(<String>[
'--compile-commands=$path',
'--shard-variants=variant',
'--shard-id=2',
], errSink: errBuffer);
expect(options.errorMessage, isNotNull);
expect(options.shardId, isNull);
print('foo ${options.errorMessage}');
expect(
options.errorMessage,
contains(
'Invalid shard-id value',
));
});
});

test('Error when --compile-commands path does not exist', () async {
final StringBuffer outBuffer = StringBuffer();
final StringBuffer errBuffer = StringBuffer();
Expand Down

0 comments on commit 8d4911c

Please sign in to comment.