Catch Stopwatch with static analysis (#140019)
I did not include the `'// flutter_ignore_for_file: stopwatch (see analyze.dart)'` directive since it's currently not used, and adding that shouldn't be too difficult.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import '../../foo/stopwatch_external_lib.dart' as externallib;
|
||||
|
||||
typedef ExternalStopwatchConstructor = externallib.MyStopwatch Function();
|
||||
|
||||
class StopwatchAtHome extends Stopwatch {
|
||||
StopwatchAtHome();
|
||||
StopwatchAtHome.create(): this();
|
||||
|
||||
Stopwatch get stopwatch => this;
|
||||
}
|
||||
|
||||
void testNoStopwatches(Stopwatch stopwatch) {
|
||||
stopwatch.runtimeType; // OK for now, but we probably want to catch public APIs that take a Stopwatch?
|
||||
final Stopwatch localVariable = Stopwatch(); // Bad: introducing Stopwatch from dart:core.
|
||||
Stopwatch().runtimeType; // Bad: introducing Stopwatch from dart:core.
|
||||
|
||||
(localVariable..runtimeType) // OK: not directly introducing Stopwatch.
|
||||
.runtimeType;
|
||||
|
||||
StopwatchAtHome().runtimeType; // Bad: introducing a Stopwatch subclass.
|
||||
|
||||
Stopwatch anothorStopwatch = stopwatch; // OK: not directly introducing Stopwatch.
|
||||
StopwatchAtHome Function() constructor = StopwatchAtHome.new; // Bad: introducing a Stopwatch constructor.
|
||||
assert(() {
|
||||
anothorStopwatch = constructor()..runtimeType;
|
||||
constructor = StopwatchAtHome.create; // Bad: introducing a Stopwatch constructor.
|
||||
anothorStopwatch = constructor()..runtimeType;
|
||||
return true;
|
||||
}());
|
||||
anothorStopwatch.runtimeType;
|
||||
|
||||
externallib.MyStopwatch.create(); // Bad: introducing an external Stopwatch constructor.
|
||||
ExternalStopwatchConstructor? externalConstructor;
|
||||
|
||||
assert(() {
|
||||
externalConstructor = externallib.MyStopwatch.new; // Bad: introducing an external Stopwatch constructor.
|
||||
return true;
|
||||
}());
|
||||
externalConstructor?.call();
|
||||
|
||||
externallib.stopwatch.runtimeType; // Bad: introducing an external Stopwatch.
|
||||
externallib.createMyStopwatch().runtimeType; // Bad: calling an external function that returns a Stopwatch.
|
||||
externallib.createStopwatch().runtimeType; // Bad: calling an external function that returns a Stopwatch.
|
||||
externalConstructor = externallib.createMyStopwatch; // Bad: introducing the tear-off form of an external function that returns a Stopwatch.
|
||||
|
||||
constructor.call().stopwatch; // OK: existing instance.
|
||||
}
|
||||
|
||||
void testStopwatchIgnore(Stopwatch stopwatch) {
|
||||
Stopwatch().runtimeType; // flutter_ignore: stopwatch (see analyze.dart)
|
||||
Stopwatch().runtimeType; // flutter_ignore: some_other_ignores, stopwatch (see analyze.dart)
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
/// Sample Code
|
||||
///
|
||||
/// No analysis failures should be found.
|
||||
///
|
||||
/// {@tool snippet}
|
||||
/// Sample invocations of [Stopwatch].
|
||||
///
|
||||
/// ```dart
|
||||
/// Stopwatch();
|
||||
/// ```
|
||||
/// {@end-tool}
|
||||
String? foo;
|
||||
// Other comments
|
||||
// Stopwatch();
|
||||
|
||||
String literal = 'Stopwatch()'; // flutter_ignore: stopwatch (see analyze.dart)
|
||||
@@ -0,0 +1,43 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// External Library that creates Stopwatches. This file will not be analyzed but
|
||||
// its symbols will be imported by tests.
|
||||
|
||||
class MyStopwatch implements Stopwatch {
|
||||
MyStopwatch();
|
||||
MyStopwatch.create(): this();
|
||||
|
||||
@override
|
||||
Duration get elapsed => throw UnimplementedError();
|
||||
|
||||
@override
|
||||
int get elapsedMicroseconds => throw UnimplementedError();
|
||||
|
||||
@override
|
||||
int get elapsedMilliseconds => throw UnimplementedError();
|
||||
|
||||
@override
|
||||
int get elapsedTicks => throw UnimplementedError();
|
||||
|
||||
@override
|
||||
int get frequency => throw UnimplementedError();
|
||||
|
||||
@override
|
||||
bool get isRunning => throw UnimplementedError();
|
||||
|
||||
@override
|
||||
void reset() { }
|
||||
|
||||
@override
|
||||
void start() { }
|
||||
|
||||
@override
|
||||
void stop() { }
|
||||
}
|
||||
|
||||
final MyStopwatch stopwatch = MyStopwatch.create();
|
||||
|
||||
MyStopwatch createMyStopwatch() => MyStopwatch();
|
||||
Stopwatch createStopwatch() => Stopwatch();
|
||||
@@ -1,14 +0,0 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// This should fail analysis.
|
||||
|
||||
void main() {
|
||||
Stopwatch();
|
||||
|
||||
// Identify more than one in a file.
|
||||
Stopwatch myStopwatch;
|
||||
myStopwatch = Stopwatch();
|
||||
myStopwatch.reset();
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// This would fail analysis, but it is ignored
|
||||
// flutter_ignore_for_file: stopwatch (see analyze.dart)
|
||||
|
||||
void main() {
|
||||
Stopwatch();
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import 'package:path/path.dart' as path;
|
||||
import '../analyze.dart';
|
||||
import '../custom_rules/analyze.dart';
|
||||
import '../custom_rules/no_double_clamp.dart';
|
||||
import '../custom_rules/no_stop_watches.dart';
|
||||
import '../utils.dart';
|
||||
import 'common.dart';
|
||||
|
||||
@@ -94,24 +95,6 @@ void main() {
|
||||
expect(result[result.length - 1], ''); // trailing newline
|
||||
});
|
||||
|
||||
test('analyze.dart - verifyNoStopwatches', () async {
|
||||
final List<String> result = (await capture(() => verifyNoStopwatches(testRootPath, minimumMatches: 6), shouldHaveErrors: true)).split('\n');
|
||||
final List<String> lines = <String>[
|
||||
'║ \ttest/analyze-test-input/root/packages/foo/stopwatch_fail.dart:8',
|
||||
'║ \ttest/analyze-test-input/root/packages/foo/stopwatch_fail.dart:12',
|
||||
]
|
||||
.map((String line) => line.replaceAll('/', Platform.isWindows ? r'\' : '/'))
|
||||
.toList();
|
||||
expect(result.length, 6 + lines.length, reason: 'output had unexpected number of lines:\n${result.join('\n')}');
|
||||
expect(result[0], '╔═╡ERROR╞═══════════════════════════════════════════════════════════════════════');
|
||||
expect(result[1], '║ Stopwatch use was found in the following files:');
|
||||
expect(result.getRange(2, result.length - 4).toSet(), lines.toSet());
|
||||
expect(result[result.length - 4], '║ Stopwatches introduce flakes by falling out of sync with the FakeAsync used in testing.');
|
||||
expect(result[result.length - 3], '║ A Stopwatch that stays in sync with FakeAsync is available through the Gesture or Test bindings, through samplingClock.');
|
||||
expect(result[result.length - 2], '╚═══════════════════════════════════════════════════════════════════════════════');
|
||||
expect(result[result.length - 1], ''); // trailing newline
|
||||
});
|
||||
|
||||
test('analyze.dart - verifyNoMissingLicense', () async {
|
||||
final String result = await capture(() => verifyNoMissingLicense(testRootPath, checkMinimums: false), shouldHaveErrors: true);
|
||||
final String file = 'test/analyze-test-input/root/packages/foo/foo.dart'
|
||||
@@ -230,9 +213,10 @@ void main() {
|
||||
});
|
||||
|
||||
test('analyze.dart - clampDouble', () async {
|
||||
final String result = await capture(() => analyzeFrameworkWithRules(
|
||||
final String result = await capture(() => analyzeWithRules(
|
||||
testRootPath,
|
||||
<AnalyzeRule>[noDoubleClamp],
|
||||
includePaths: <String>['packages/flutter/lib'],
|
||||
), shouldHaveErrors: true);
|
||||
final String lines = <String>[
|
||||
'║ packages/flutter/lib/bar.dart:37: input.clamp(0.0, 2)',
|
||||
@@ -252,4 +236,35 @@ void main() {
|
||||
'╚═══════════════════════════════════════════════════════════════════════════════\n'
|
||||
);
|
||||
});
|
||||
|
||||
test('analyze.dart - stopwatch', () async {
|
||||
final String result = await capture(() => analyzeWithRules(
|
||||
testRootPath,
|
||||
<AnalyzeRule>[noStopwatches],
|
||||
includePaths: <String>['packages/flutter/lib'],
|
||||
), shouldHaveErrors: true);
|
||||
final String lines = <String>[
|
||||
'║ packages/flutter/lib/stopwatch.dart:18: Stopwatch()',
|
||||
'║ packages/flutter/lib/stopwatch.dart:19: Stopwatch()',
|
||||
'║ packages/flutter/lib/stopwatch.dart:24: StopwatchAtHome()',
|
||||
'║ packages/flutter/lib/stopwatch.dart:27: StopwatchAtHome.new',
|
||||
'║ packages/flutter/lib/stopwatch.dart:30: StopwatchAtHome.create',
|
||||
'║ packages/flutter/lib/stopwatch.dart:36: externallib.MyStopwatch.create()',
|
||||
'║ packages/flutter/lib/stopwatch.dart:40: externallib.MyStopwatch.new',
|
||||
'║ packages/flutter/lib/stopwatch.dart:45: externallib.stopwatch',
|
||||
'║ packages/flutter/lib/stopwatch.dart:46: externallib.createMyStopwatch()',
|
||||
'║ packages/flutter/lib/stopwatch.dart:47: externallib.createStopwatch()',
|
||||
'║ packages/flutter/lib/stopwatch.dart:48: externallib.createMyStopwatch'
|
||||
]
|
||||
.map((String line) => line.replaceAll('/', Platform.isWindows ? r'\' : '/'))
|
||||
.join('\n');
|
||||
expect(result,
|
||||
'╔═╡ERROR╞═══════════════════════════════════════════════════════════════════════\n'
|
||||
'$lines\n'
|
||||
'║ \n'
|
||||
'║ Stopwatches introduce flakes by falling out of sync with the FakeAsync used in testing.\n'
|
||||
'║ A Stopwatch that stays in sync with FakeAsync is available through the Gesture or Test bindings, through samplingClock.\n'
|
||||
'╚═══════════════════════════════════════════════════════════════════════════════\n'
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user