Skip to content

Allow accessing the current test/suite metadata #2010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion pkgs/test_api/lib/src/backend/declarer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class Declarer {
/// The current zone-scoped declarer.
static Declarer? get current => Zone.current[#test.declarer] as Declarer?;

static Metadata? get currentMetadata => current?._metadata;

/// All the test and group names that have been declared in the entire suite.
///
/// If duplicate test names are allowed, this is not tracked and it will be
Expand Down Expand Up @@ -184,7 +186,9 @@ class Declarer {
return;
}

final newName = [if (_metadata.name != null) ..._metadata.name!, name];
var newMetadata = Metadata.parse(
name: newName,
testOn: testOn,
timeout: timeout,
skip: skip,
Expand Down Expand Up @@ -216,7 +220,18 @@ class Declarer {
},
// Make the declarer visible to running tests so that they'll throw
// useful errors when calling `test()` and `group()` within a test.
zoneValues: {#test.declarer: this});
zoneValues: {
#test.declarer: Declarer._(
_parent,
_name,
metadata,
_platformVariables,
_collectTraces,
_trace,
_noRetry,
_fullTestName,
_seenNames),
});
}, trace: _collectTraces ? Trace.current(2) : null, guarded: false));

if (solo) {
Expand All @@ -241,6 +256,7 @@ class Declarer {
}

var newMetadata = Metadata.parse(
name: [if (_metadata.name != null) ..._metadata.name!, name],
testOn: testOn,
timeout: timeout,
skip: skip,
Expand Down
52 changes: 38 additions & 14 deletions pkgs/test_api/lib/src/backend/metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:boolean_selector/boolean_selector.dart';
import 'package:collection/collection.dart';
import 'package:test_api/src/backend/declarer.dart';

import 'configuration/skip.dart';
import 'configuration/timeout.dart';
Expand Down Expand Up @@ -72,6 +73,17 @@ class Metadata {
/// Only available for test suites and not individual tests.
final String? languageVersionComment;

/// The name of the current test group, including the name of any parent
/// groups.
///
/// This is `null` if this is the root group.
final List<String>? name;

/// The metadata for the current test/suite.
///
/// This is `null` if not in a test/suite.
static Metadata? get current => Declarer.currentMetadata;

/// Parses a user-provided map into the value for [onPlatform].
static Map<PlatformSelector, Metadata> _parseOnPlatform(
Map<String, dynamic>? onPlatform) {
Expand Down Expand Up @@ -146,7 +158,8 @@ class Metadata {
/// included inline in the returned value. The values directly passed to the
/// constructor take precedence over tag-specific metadata.
factory Metadata(
{PlatformSelector? testOn,
{List<String>? name,
PlatformSelector? testOn,
Timeout? timeout,
bool? skip,
bool? verboseTrace,
Expand All @@ -159,6 +172,7 @@ class Metadata {
String? languageVersionComment}) {
// Returns metadata without forTag resolved at all.
Metadata unresolved() => Metadata._(
name: name,
testOn: testOn,
timeout: timeout,
skip: skip,
Expand Down Expand Up @@ -194,6 +208,7 @@ class Metadata {
///
/// Unlike [Metadata], this assumes [forTag] is already resolved.
Metadata._({
this.name,
PlatformSelector? testOn,
Timeout? timeout,
bool? skip,
Expand Down Expand Up @@ -223,17 +238,18 @@ class Metadata {
/// where applicable.
///
/// Throws a [FormatException] if any field is invalid.
Metadata.parse(
{String? testOn,
Timeout? timeout,
dynamic skip,
bool? verboseTrace,
bool? chainStackTraces,
int? retry,
Map<String, dynamic>? onPlatform,
tags,
this.languageVersionComment})
: testOn = testOn == null
Metadata.parse({
this.name,
String? testOn,
Timeout? timeout,
dynamic skip,
bool? verboseTrace,
bool? chainStackTraces,
int? retry,
Map<String, dynamic>? onPlatform,
tags,
this.languageVersionComment,
}) : testOn = testOn == null
? PlatformSelector.all
: PlatformSelector.parse(testOn),
timeout = timeout ?? const Timeout.factor(1),
Expand All @@ -256,7 +272,10 @@ class Metadata {

/// Deserializes the result of [Metadata.serialize] into a new [Metadata].
Metadata.deserialize(serialized)
: testOn = serialized['testOn'] == null
: name = (serialized['name'] as List? ?? [])
.map((s) => s as String)
.toList(),
testOn = serialized['testOn'] == null
? PlatformSelector.all
: PlatformSelector.parse(serialized['testOn'] as String),
timeout = _deserializeTimeout(serialized['timeout']),
Expand Down Expand Up @@ -318,6 +337,7 @@ class Metadata {
/// either has a [forTag] metadata for one of the other's tags, that metadata
/// is merged as well.
Metadata merge(Metadata other) => Metadata(
name: other.name ?? name,
testOn: testOn.intersection(other.testOn),
timeout: timeout.merge(other.timeout),
skip: other._skip ?? _skip,
Expand All @@ -335,7 +355,8 @@ class Metadata {

/// Returns a copy of [this] with the given fields changed.
Metadata change(
{PlatformSelector? testOn,
{List<String>? name,
PlatformSelector? testOn,
Timeout? timeout,
bool? skip,
bool? verboseTrace,
Expand All @@ -346,6 +367,7 @@ class Metadata {
Set<String>? tags,
Map<BooleanSelector, Metadata>? forTag,
String? languageVersionComment}) {
name ??= this.name;
testOn ??= this.testOn;
timeout ??= this.timeout;
skip ??= _skip;
Expand All @@ -358,6 +380,7 @@ class Metadata {
forTag ??= this.forTag;
languageVersionComment ??= this.languageVersionComment;
return Metadata(
name: name,
testOn: testOn,
timeout: timeout,
skip: skip,
Expand Down Expand Up @@ -394,6 +417,7 @@ class Metadata {
});

return {
'name': name ?? [],
'testOn': testOn == PlatformSelector.all ? null : testOn.toString(),
'timeout': _serializeTimeout(timeout),
'skip': _skip,
Expand Down
39 changes: 39 additions & 0 deletions pkgs/test_api/test/backend/metadata_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,45 @@ import 'package:test_api/src/backend/runtime.dart';
import 'package:test_api/src/backend/suite_platform.dart';

void main() {
group('current', () {
final originalMetadata = Metadata.current;
group(
'tags',
() {
test(
'inherited from the parent group',
() {
expect(Metadata.current?.tags, equals(['tag-1', 'tag-2', 'tag-3']));
},
tags: ['tag-3'],
);
test(
'inherited from the parent group (2)',
() {
expect(Metadata.current?.tags, equals(['tag-1', 'tag-2']));
},
);
},
tags: ['tag-1', 'tag-2'],
);
group('group 1', () {
group('group 1.a', () {
group('group 1.a.I', () {
test('name is correct', () {
expect(
Metadata.current?.name,
equals([
...originalMetadata?.name ?? [],
'group 1',
'group 1.a',
'group 1.a.I',
'name is correct'
]));
});
});
});
});
});
group('tags', () {
test('parses an Iterable', () {
expect(
Expand Down