Skip to content

Commit dc72485

Browse files
Generator tests: prevent parallel running tests from sending events
1 parent 327e866 commit dc72485

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

generator/lib/src/analysis/analysis.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@ import 'build_properties.dart';
1818
/// Requires [tokenFilePath] to exist, otherwise does nothing. See the
1919
/// associated test (analysis_test.dart) on how to create this file.
2020
class ObjectBoxAnalysis {
21-
static const _debug = false;
22-
2321
/// Path is relative to lib folder.
24-
static const tokenFilePath = "assets/analysis-token.txt";
22+
static const defaultTokenFilePath = "assets/analysis-token.txt";
2523

2624
static const _url = "api.mixpanel.com";
2725
static const _path = "track";
2826

27+
final bool _debug;
28+
final String _tokenFilePath;
29+
30+
ObjectBoxAnalysis(
31+
{bool debug = false, String tokenFilePath = defaultTokenFilePath})
32+
: _debug = debug,
33+
_tokenFilePath = tokenFilePath;
34+
2935
/// Builds a Build event and sends it with [sendEvent]. May not send if it
3036
/// fails to store a unique identifier and last time sent, or if no valid API
3137
/// token is found.
@@ -126,7 +132,7 @@ class ObjectBoxAnalysis {
126132
}
127133

128134
Future<String?> _getToken() async {
129-
final uri = Uri.parse("package:objectbox_generator/$tokenFilePath");
135+
final uri = Uri.parse("package:objectbox_generator/$_tokenFilePath");
130136
final resolvedUri = await Isolate.resolvePackageUri(uri);
131137
if (resolvedUri != null) {
132138
final file = File.fromUri(resolvedUri);

generator/test/analysis_test.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,41 @@ void main() {
2424
var obfuscatedToken = _obfuscateToken(token);
2525
final keyBase64 = obfuscatedToken.keyBase64;
2626
final dataBase64 = obfuscatedToken.dataBase64;
27-
print("Store this in generator/lib/${ObjectBoxAnalysis.tokenFilePath}:");
27+
print(
28+
"Store this in generator/lib/${ObjectBoxAnalysis.defaultTokenFilePath}:");
2829
print("$keyBase64\n$dataBase64");
2930

30-
final decryptedToken =
31-
ObjectBoxAnalysis().decryptAndVerifyToken(keyBase64, dataBase64);
31+
final decryptedToken = ObjectBoxAnalysis(debug: true)
32+
.decryptAndVerifyToken(keyBase64, dataBase64);
3233
expect(decryptedToken, equals(token));
3334
}, skip: true);
3435

3536
test("send test event", () async {
36-
// Create a token file just for this test (delete right after to avoid
37-
// CI sending events).
37+
// Create a token file just for this test
3838
final token = Platform.environment["DART_ANALYSIS_TOKEN"];
3939
if (token == null) {
4040
markTestSkipped("DART_ANALYSIS_TOKEN not set");
4141
return;
4242
}
4343
var obfuscatedToken = _obfuscateToken(token);
44-
final tokenFile = File("lib/${ObjectBoxAnalysis.tokenFilePath}");
44+
// Use a token file different from the default to prevent parallel running
45+
// tests from sending events.
46+
final tokenFilePath = 'assets/test-analysis-token.txt';
47+
final tokenFile = File("lib/$tokenFilePath");
4548
await tokenFile.writeAsString(
4649
"${obfuscatedToken.keyBase64}\n${obfuscatedToken.dataBase64}");
50+
addTearDown(() async => tokenFile.delete());
4751

4852
final testPubspec = Pubspec("test", dependencies: {
4953
"flutter": SdkDependency("flutter"),
5054
"objectbox": HostedDependency(version: VersionConstraint.parse("^1.2.3"))
5155
});
5256

53-
final analysis = ObjectBoxAnalysis();
57+
final analysis =
58+
ObjectBoxAnalysis(tokenFilePath: tokenFilePath, debug: true);
5459
final event = analysis.buildEvent("Test Event", "test-uid", testPubspec);
5560
final response = await analysis.sendEvent(event);
5661

57-
// Delete token before test may fail to ensure CI does not send events.
58-
await tokenFile.delete();
59-
6062
expect(response!.statusCode, 200);
6163
expect(response.body, "1");
6264
});

0 commit comments

Comments
 (0)