diff --git a/app/pubspec.lock b/app/pubspec.lock index 272257b32..bd5332da5 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -1818,7 +1818,7 @@ packages: source: hosted version: "3.2.1" random_string: - dependency: "direct dev" + dependency: transitive description: name: random_string sha256: "03b52435aae8cbdd1056cf91bfc5bf845e9706724dd35ae2e99fa14a1ef79d02" @@ -2192,6 +2192,13 @@ packages: url: "https://pub.dev" source: hosted version: "0.5.9" + test_randomness: + dependency: "direct dev" + description: + path: "../lib/test_randomness" + relative: true + source: path + version: "0.0.1" time: dependency: "direct main" description: diff --git a/app/pubspec.yaml b/app/pubspec.yaml index d721b9881..e01da80c6 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -215,7 +215,8 @@ dev_dependencies: integration_test: sdk: flutter mockito: ^5.4.4 - random_string: ^2.0.1 + test_randomness: + path: ../lib/test_randomness bloc_test: ^9.1.4 bloc_presentation_test: ^1.0.0 path: ^1.8.3 diff --git a/app/test/dashboard/update_reminder_bloc_test.dart b/app/test/dashboard/update_reminder_bloc_test.dart index 8770e38e2..3804174db 100644 --- a/app/test/dashboard/update_reminder_bloc_test.dart +++ b/app/test/dashboard/update_reminder_bloc_test.dart @@ -6,12 +6,11 @@ // // SPDX-License-Identifier: EUPL-1.2 -import 'dart:math'; - import 'package:flutter_test/flutter_test.dart'; +import 'package:sharezone/changelog/change.dart'; import 'package:sharezone/dashboard/update_reminder/release.dart'; import 'package:sharezone/dashboard/update_reminder/update_reminder_bloc.dart'; -import 'package:sharezone/changelog/change.dart'; +import 'package:test_randomness/test_randomness.dart'; void main() { group('UpdateReminderBloc', () { @@ -140,5 +139,5 @@ Release _releaseWith({required String version, DateTime? releaseTime}) { return Release( version: Version.parse(name: version), releaseDate: - releaseTime ?? DateTime(2020, 02, Random.secure().nextInt(25) + 1)); + releaseTime ?? DateTime(2020, 02, szTestRandom.nextInt(25) + 1)); } diff --git a/app/test/feedback/feedback_bloc_test.dart b/app/test/feedback/feedback_bloc_test.dart index b068d024c..26e49aa1b 100644 --- a/app/test/feedback/feedback_bloc_test.dart +++ b/app/test/feedback/feedback_bloc_test.dart @@ -9,12 +9,12 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:key_value_store/in_memory_key_value_store.dart'; import 'package:key_value_store/key_value_store.dart'; -import 'package:random_string/random_string.dart' as random; import 'package:sharezone/feedback/src/analytics/feedback_analytics.dart'; import 'package:sharezone/feedback/src/bloc/feedback_bloc.dart'; import 'package:sharezone/feedback/src/cache/cooldown_exception.dart'; import 'package:sharezone/feedback/src/cache/feedback_cache.dart'; import 'package:sharezone/feedback/src/models/user_feedback.dart'; +import 'package:test_randomness/test_randomness.dart' as random; import 'mock_feedback_api.dart'; import 'mock_platform_information_retreiver.dart'; diff --git a/app/test/feedback/feedback_page_test.dart b/app/test/feedback/feedback_page_test.dart index 3cb889340..dbae9eb7b 100644 --- a/app/test/feedback/feedback_page_test.dart +++ b/app/test/feedback/feedback_page_test.dart @@ -10,12 +10,12 @@ import 'package:bloc_provider/bloc_provider.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:key_value_store/in_memory_key_value_store.dart'; -import 'package:random_string/random_string.dart' as random; import 'package:sharezone/feedback/feedback_box_page.dart'; import 'package:sharezone/feedback/src/bloc/feedback_bloc.dart'; import 'package:sharezone/feedback/src/cache/cooldown_exception.dart'; import 'package:sharezone/feedback/src/cache/feedback_cache.dart'; import 'package:sharezone/feedback/src/models/user_feedback.dart'; +import 'package:test_randomness/test_randomness.dart' as random; import 'feedback_bloc_test.dart'; import 'mock_feedback_api.dart'; diff --git a/app/test/groups/group_join_select_courses_bloc_test.dart b/app/test/groups/group_join_select_courses_bloc_test.dart index f851888ca..1b88c3be6 100644 --- a/app/test/groups/group_join_select_courses_bloc_test.dart +++ b/app/test/groups/group_join_select_courses_bloc_test.dart @@ -13,12 +13,12 @@ import 'package:design/design.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:group_domain_models/group_domain_models.dart'; import 'package:mockito/annotations.dart'; -import 'package:random_string/random_string.dart'; import 'package:sharezone/groups/group_join/bloc/group_join_bloc.dart'; import 'package:sharezone/groups/group_join/bloc/group_join_select_courses_bloc.dart'; import 'package:sharezone/groups/group_join/models/group_info_with_selection_state.dart'; import 'package:sharezone/groups/group_join/models/group_join_result.dart'; import 'package:sharezone/util/api/connections_gateway.dart'; +import 'package:test_randomness/test_randomness.dart'; import 'group_join_select_courses_bloc_test.mocks.dart'; diff --git a/app/test/holidays/holiday_bloc_unit_test.dart b/app/test/holidays/holiday_bloc_unit_test.dart index 0c777f29e..ec065701e 100644 --- a/app/test/holidays/holiday_bloc_unit_test.dart +++ b/app/test/holidays/holiday_bloc_unit_test.dart @@ -11,7 +11,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:holidays/holidays.dart'; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; -import 'package:random_string/random_string.dart' as rdm; +import 'package:test_randomness/test_randomness.dart' as rdm; import 'package:rxdart/subjects.dart'; import 'package:sharezone/holidays/holiday_bloc.dart'; import 'package:user/user.dart'; diff --git a/app/test/homework/homework_dialog_test.dart b/app/test/homework/homework_dialog_test.dart index d05504437..395b3a03d 100644 --- a/app/test/homework/homework_dialog_test.dart +++ b/app/test/homework/homework_dialog_test.dart @@ -25,7 +25,7 @@ import 'package:group_domain_models/group_domain_models.dart'; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; import 'package:path/path.dart'; -import 'package:random_string/random_string.dart'; +import 'package:test_randomness/test_randomness.dart'; import 'package:sharezone/homework/homework_dialog/homework_dialog.dart'; import 'package:sharezone/homework/homework_dialog/homework_dialog_bloc.dart'; import 'package:sharezone/main/application_bloc.dart'; diff --git a/app/test/homework/teacher/teacher_homework_page_widget_test.dart b/app/test/homework/teacher/teacher_homework_page_widget_test.dart index 5e5e48515..c1059dbc8 100644 --- a/app/test/homework/teacher/teacher_homework_page_widget_test.dart +++ b/app/test/homework/teacher/teacher_homework_page_widget_test.dart @@ -13,13 +13,12 @@ import 'package:analytics/null_analytics_backend.dart'; import 'package:bloc_provider/bloc_provider.dart'; import 'package:bloc_provider/multi_bloc_provider.dart'; import 'package:common_domain_models/common_domain_models.dart'; -import 'package:flutter/material.dart' hide Color; import 'package:flutter/material.dart' as flutter show Color; +import 'package:flutter/material.dart' hide Color; import 'package:flutter_test/flutter_test.dart'; import 'package:hausaufgabenheft_logik/color.dart'; import 'package:hausaufgabenheft_logik/hausaufgabenheft_logik_lehrer.dart'; import 'package:provider/provider.dart'; -import 'package:random_string/random_string.dart'; import 'package:sharezone/homework/shared/shared.dart'; import 'package:sharezone/homework/teacher/src/teacher_archived_homework_list.dart'; import 'package:sharezone/homework/teacher/src/teacher_homework_bottom_action_bar.dart'; @@ -27,6 +26,7 @@ import 'package:sharezone/homework/teacher/src/teacher_homework_tile.dart'; import 'package:sharezone/homework/teacher/src/teacher_open_homework_list.dart'; import 'package:sharezone/homework/teacher/teacher_homework_page.dart'; import 'package:sharezone/navigation/logic/navigation_bloc.dart'; +import 'package:test_randomness/test_randomness.dart'; class MockTeacherHomeworkPageBloc extends TeacherHomeworkPageBloc { final _queuedStates = Queue(); diff --git a/app/test/timetable/school_class_filter_view_test.dart b/app/test/timetable/school_class_filter_view_test.dart index 938f484d1..75a5528a4 100644 --- a/app/test/timetable/school_class_filter_view_test.dart +++ b/app/test/timetable/school_class_filter_view_test.dart @@ -8,9 +8,8 @@ import 'package:common_domain_models/common_domain_models.dart'; import 'package:flutter_test/flutter_test.dart'; - -import 'package:random_string/random_string.dart'; import 'package:sharezone/timetable/timetable_page/school_class_filter/school_class_filter_view.dart'; +import 'package:test_randomness/test_randomness.dart'; void main() { group('SchoolClassFilterView', () { diff --git a/app/test/timetable/timetable_bloc_test.dart b/app/test/timetable/timetable_bloc_test.dart index 9a43162d1..7d33f05e6 100644 --- a/app/test/timetable/timetable_bloc_test.dart +++ b/app/test/timetable/timetable_bloc_test.dart @@ -6,8 +6,6 @@ // // SPDX-License-Identifier: EUPL-1.2 -import 'dart:math'; - import 'package:clock/clock.dart'; import 'package:common_domain_models/common_domain_models.dart'; import 'package:date/src/date.dart'; @@ -19,6 +17,7 @@ import 'package:sharezone/calendrical_events/models/calendrical_event.dart'; import 'package:sharezone/timetable/src/bloc/timetable_bloc.dart'; import 'package:sharezone/timetable/src/models/lesson.dart'; import 'package:sharezone/timetable/timetable_page/school_class_filter/school_class_filter_view.dart'; +import 'package:test_randomness/test_randomness.dart'; import 'package:time/time.dart'; import 'mock/mock_course_gateway.dart'; @@ -50,7 +49,7 @@ void main() { endTime: Time(hour: 10, minute: 0), groupID: groupId, groupType: GroupType.course, - lessonID: Random().nextInt(200).toString(), + lessonID: szTestRandom.nextInt(200).toString(), place: "", teacher: "", weekday: WeekDay.monday, diff --git a/lib/abgabe/abgabe_client_lib/pubspec.lock b/lib/abgabe/abgabe_client_lib/pubspec.lock index c3cac07d4..d51b23efe 100644 --- a/lib/abgabe/abgabe_client_lib/pubspec.lock +++ b/lib/abgabe/abgabe_client_lib/pubspec.lock @@ -1122,7 +1122,7 @@ packages: source: hosted version: "3.2.1" random_string: - dependency: "direct dev" + dependency: transitive description: name: random_string sha256: "03b52435aae8cbdd1056cf91bfc5bf845e9706724dd35ae2e99fa14a1ef79d02" @@ -1290,6 +1290,13 @@ packages: url: "https://pub.dev" source: hosted version: "0.6.1" + test_randomness: + dependency: "direct dev" + description: + path: "../../test_randomness" + relative: true + source: path + version: "0.0.1" time: dependency: transitive description: diff --git a/lib/abgabe/abgabe_client_lib/pubspec.yaml b/lib/abgabe/abgabe_client_lib/pubspec.yaml index f3b83f6db..535a1897f 100644 --- a/lib/abgabe/abgabe_client_lib/pubspec.yaml +++ b/lib/abgabe/abgabe_client_lib/pubspec.yaml @@ -46,6 +46,7 @@ dev_dependencies: sdk: flutter async: ^2.4.1 mockito: ^5.4.4 - random_string: ^2.0.1 + test_randomness: + path: ../../test_randomness sharezone_lints: path: ../../sharezone_lints diff --git a/lib/abgabe/abgabe_client_lib/test/bloc_test.dart b/lib/abgabe/abgabe_client_lib/test/bloc_test.dart index b2d6d0e46..c7724e5a2 100644 --- a/lib/abgabe/abgabe_client_lib/test/bloc_test.dart +++ b/lib/abgabe/abgabe_client_lib/test/bloc_test.dart @@ -25,8 +25,8 @@ import 'package:common_domain_models/common_domain_models.dart'; import 'package:files_basics/files_models.dart'; import 'package:files_basics/local_file.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:random_string/random_string.dart'; import 'package:rxdart/subjects.dart'; +import 'package:test_randomness/test_randomness.dart'; void main() { group( diff --git a/lib/firebase_hausaufgabenheft_logik/pubspec.lock b/lib/firebase_hausaufgabenheft_logik/pubspec.lock index bb40c7407..afb1c27f2 100644 --- a/lib/firebase_hausaufgabenheft_logik/pubspec.lock +++ b/lib/firebase_hausaufgabenheft_logik/pubspec.lock @@ -423,6 +423,13 @@ packages: url: "https://pub.dev" source: hosted version: "0.6.1" + test_randomness: + dependency: transitive + description: + path: "../test_randomness" + relative: true + source: path + version: "0.0.1" time: dependency: "direct main" description: diff --git a/lib/hausaufgabenheft_logik/lib/src/lehrer/teacher_homework_page_bloc.dart b/lib/hausaufgabenheft_logik/lib/src/lehrer/teacher_homework_page_bloc.dart index 6daf21807..ce0ec760d 100644 --- a/lib/hausaufgabenheft_logik/lib/src/lehrer/teacher_homework_page_bloc.dart +++ b/lib/hausaufgabenheft_logik/lib/src/lehrer/teacher_homework_page_bloc.dart @@ -14,7 +14,7 @@ import 'package:common_domain_models/common_domain_models.dart'; import 'package:hausaufgabenheft_logik/color.dart'; import 'package:hausaufgabenheft_logik/hausaufgabenheft_logik_lehrer.dart'; import 'package:meta/meta.dart'; -import 'package:random_string/random_string.dart'; +import 'package:test_randomness/test_randomness.dart'; export 'events.dart'; export 'states.dart'; diff --git a/lib/hausaufgabenheft_logik/pubspec.lock b/lib/hausaufgabenheft_logik/pubspec.lock index 7749177ac..af545a917 100644 --- a/lib/hausaufgabenheft_logik/pubspec.lock +++ b/lib/hausaufgabenheft_logik/pubspec.lock @@ -295,7 +295,7 @@ packages: source: hosted version: "2.1.4" random_string: - dependency: "direct main" + dependency: transitive description: name: random_string sha256: "03b52435aae8cbdd1056cf91bfc5bf845e9706724dd35ae2e99fa14a1ef79d02" @@ -437,6 +437,13 @@ packages: url: "https://pub.dev" source: hosted version: "0.5.6" + test_randomness: + dependency: "direct main" + description: + path: "../test_randomness" + relative: true + source: path + version: "0.0.1" typed_data: dependency: transitive description: diff --git a/lib/hausaufgabenheft_logik/pubspec.yaml b/lib/hausaufgabenheft_logik/pubspec.yaml index c42253741..a39969ea1 100644 --- a/lib/hausaufgabenheft_logik/pubspec.yaml +++ b/lib/hausaufgabenheft_logik/pubspec.yaml @@ -28,7 +28,8 @@ dependencies: path: ../bloc_base # For teacher fake bloc currently in use (can be removed when bloc is implemented) # https://gitlab.com/codingbrain/sharezone/sharezone-app/-/issues/1460 - random_string: ^2.0.1 + test_randomness: + path: ../test_randomness clock: ^1.1.1 dev_dependencies: diff --git a/lib/hausaufgabenheft_logik/test/create_homework_util.dart b/lib/hausaufgabenheft_logik/test/create_homework_util.dart index bb5b9e571..6aa2d2631 100644 --- a/lib/hausaufgabenheft_logik/test/create_homework_util.dart +++ b/lib/hausaufgabenheft_logik/test/create_homework_util.dart @@ -6,12 +6,11 @@ // // SPDX-License-Identifier: EUPL-1.2 -import 'dart:math'; - import 'package:common_domain_models/common_domain_models.dart'; import 'package:hausaufgabenheft_logik/src/models/homework/homework.dart'; import 'package:hausaufgabenheft_logik/src/models/homework/models_used_by_homework.dart'; import 'package:hausaufgabenheft_logik/src/views/color.dart'; +import 'package:test_randomness/test_randomness.dart'; HomeworkReadModel createHomework( {Date todoDate = const Date(day: 1, month: 1, year: 2019), @@ -22,7 +21,7 @@ HomeworkReadModel createHomework( bool withSubmissions = false, Color? subjectColor, String abbreviation = 'Abb'}) { - id = id == 'willBeRandom' ? randomString(5) : id; + id = id == 'willBeRandom' ? randomAlphaNumeric(5) : id; return HomeworkReadModel( id: HomeworkId(id), todoDate: todoDate.asDateTime(), @@ -32,12 +31,3 @@ HomeworkReadModel createHomework( status: done ? CompletionStatus.completed : CompletionStatus.open, ); } - -String randomString(int length) { - var rand = Random(); - var codeUnits = List.generate(length, (index) { - return rand.nextInt(33) + 89; - }); - - return String.fromCharCodes(codeUnits); -} diff --git a/lib/sharezone_utils/test/random_string/random_string_test.dart b/lib/sharezone_utils/test/random_string/random_string_test.dart deleted file mode 100644 index f152922d5..000000000 --- a/lib/sharezone_utils/test/random_string/random_string_test.dart +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2022 Sharezone UG (haftungsbeschränkt) -// Licensed under the EUPL-1.2-or-later. -// -// You may obtain a copy of the Licence at: -// https://joinup.ec.europa.eu/software/page/eupl -// -// SPDX-License-Identifier: EUPL-1.2 - -import 'package:flutter_test/flutter_test.dart'; -import 'package:sharezone_utils/random_string.dart'; - -void main() { - test('randomString()', () { - for (int i = 0; i < 50; i++) { - expect(randomString(10), hasLength(10)); - } - }); - - test('randomIDString()', () { - for (int i = 0; i < 50; i++) { - expect(randomIDString(10), hasLength(10)); - } - }); -} diff --git a/lib/test_randomness/.gitignore b/lib/test_randomness/.gitignore new file mode 100644 index 000000000..3132dc5ff --- /dev/null +++ b/lib/test_randomness/.gitignore @@ -0,0 +1,73 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +**/doc/api/ +.dart_tool/ +.flutter-plugins +.packages +.pub-cache/ +.pub/ +build/ + +# Android related +**/android/**/gradle-wrapper.jar +**/android/.gradle +**/android/captures/ +**/android/gradlew +**/android/gradlew.bat +**/android/local.properties +**/android/**/GeneratedPluginRegistrant.java + +# iOS/XCode related +**/ios/**/*.mode1v3 +**/ios/**/*.mode2v3 +**/ios/**/*.moved-aside +**/ios/**/*.pbxuser +**/ios/**/*.perspectivev3 +**/ios/**/*sync/ +**/ios/**/.sconsign.dblite +**/ios/**/.tags* +**/ios/**/.vagrant/ +**/ios/**/DerivedData/ +**/ios/**/Icon? +**/ios/**/Pods/ +**/ios/**/.symlinks/ +**/ios/**/profile +**/ios/**/xcuserdata +**/ios/.generated/ +**/ios/Flutter/App.framework +**/ios/Flutter/Flutter.framework +**/ios/Flutter/Generated.xcconfig +**/ios/Flutter/app.flx +**/ios/Flutter/app.zip +**/ios/Flutter/flutter_assets/ +**/ios/Flutter/flutter_export_environment.sh +**/ios/ServiceDefinitions.json +**/ios/Runner/GeneratedPluginRegistrant.* + +# Exceptions to above rules. +!**/ios/**/default.mode1v3 +!**/ios/**/default.mode2v3 +!**/ios/**/default.pbxuser +!**/ios/**/default.perspectivev3 +!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages diff --git a/lib/test_randomness/.metadata b/lib/test_randomness/.metadata new file mode 100644 index 000000000..f917a52ed --- /dev/null +++ b/lib/test_randomness/.metadata @@ -0,0 +1,10 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: e70236e36ce1d32067dc68eb55519ec3e14b6b01 + channel: beta + +project_type: package diff --git a/lib/test_randomness/analysis_options.yaml b/lib/test_randomness/analysis_options.yaml new file mode 100644 index 000000000..1e445c14e --- /dev/null +++ b/lib/test_randomness/analysis_options.yaml @@ -0,0 +1,9 @@ +# Copyright (c) 2023 Sharezone UG (haftungsbeschränkt) +# Licensed under the EUPL-1.2-or-later. +# +# You may obtain a copy of the Licence at: +# https://joinup.ec.europa.eu/software/page/eupl +# +# SPDX-License-Identifier: EUPL-1.2 + +include: package:sharezone_lints/analysis_options.yaml diff --git a/lib/test_randomness/lib/src/test_randomness.dart b/lib/test_randomness/lib/src/test_randomness.dart new file mode 100644 index 000000000..3adfc9fc1 --- /dev/null +++ b/lib/test_randomness/lib/src/test_randomness.dart @@ -0,0 +1,60 @@ +// Copyright (c) 2024 Sharezone UG (haftungsbeschränkt) +// Licensed under the EUPL-1.2-or-later. +// +// You may obtain a copy of the Licence at: +// https://joinup.ec.europa.eu/software/page/eupl +// +// SPDX-License-Identifier: EUPL-1.2 + +import 'dart:math'; + +import 'package:random_string/random_string.dart' as lib; + +/// The seed to use for randomness. +/// +/// This is used to ensure that tests are deterministic. +/// The `TEST_RANDOMNESS_SEED` environment variable will be provided when +/// running `sz test`. +const randomnessSeed = + int.fromEnvironment('TEST_RANDOMNESS_SEED', defaultValue: 0); + +/// The [Random] that should be used for randomness in tests. +/// +/// This is a [Random] that is seeded with [randomnessSeed]. +/// This is used to ensure that tests are deterministic. +final szTestRandom = Random(randomnessSeed); + +final _randomProvider = lib.CoreRandomProvider.from(szTestRandom); + +/// Generates a random integer where [from] <= [to] inclusive +/// where 0 <= from <= to <= 999999999999999 +int randomBetween( + int from, + int to, +) => + lib.randomBetween(from, to, provider: _randomProvider); + +/// Generates a random string of [length] with characters +/// between ascii [from] to [to]. +/// Defaults to characters of ascii '!' to '~'. +String randomString( + int length, { + int from = lib.asciiStart, + int to = lib.asciiEnd, +}) => + lib.randomString(length, from: from, to: to, provider: _randomProvider); + +/// Generates a random string of [length] with only numeric characters. +String randomNumeric(int length) => + lib.randomNumeric(length, provider: _randomProvider); + +/// Generates a random string of [length] with only alpha characters. +String randomAlpha(int length) => + lib.randomAlpha(length, provider: _randomProvider); + +/// Generates a random string of [length] with alpha-numeric characters. +String randomAlphaNumeric(int length) => + lib.randomAlphaNumeric(length, provider: _randomProvider); + +/// Merge [a] with [b] and shuffle. +String randomMerge(String a, String b) => lib.randomMerge(a, b); diff --git a/lib/test_randomness/lib/test_randomness.dart b/lib/test_randomness/lib/test_randomness.dart new file mode 100644 index 000000000..636fa7b37 --- /dev/null +++ b/lib/test_randomness/lib/test_randomness.dart @@ -0,0 +1,11 @@ +// Copyright (c) 2024 Sharezone UG (haftungsbeschränkt) +// Licensed under the EUPL-1.2-or-later. +// +// You may obtain a copy of the Licence at: +// https://joinup.ec.europa.eu/software/page/eupl +// +// SPDX-License-Identifier: EUPL-1.2 + +library test_randomness; + +export 'src/test_randomness.dart'; diff --git a/lib/test_randomness/pubspec.lock b/lib/test_randomness/pubspec.lock new file mode 100644 index 000000000..a9c652dce --- /dev/null +++ b/lib/test_randomness/pubspec.lock @@ -0,0 +1,36 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: + flutter_lints: + dependency: transitive + description: + name: flutter_lints + sha256: e2a421b7e59244faef694ba7b30562e489c2b489866e505074eb005cd7060db7 + url: "https://pub.dev" + source: hosted + version: "3.0.1" + lints: + dependency: transitive + description: + name: lints + sha256: cbf8d4b858bb0134ef3ef87841abdf8d63bfc255c266b7bf6b39daa1085c4290 + url: "https://pub.dev" + source: hosted + version: "3.0.0" + random_string: + dependency: "direct main" + description: + name: random_string + sha256: "03b52435aae8cbdd1056cf91bfc5bf845e9706724dd35ae2e99fa14a1ef79d02" + url: "https://pub.dev" + source: hosted + version: "2.3.1" + sharezone_lints: + dependency: "direct dev" + description: + path: "../sharezone_lints" + relative: true + source: path + version: "1.0.0" +sdks: + dart: ">=3.0.0 <4.0.0" diff --git a/lib/test_randomness/pubspec.yaml b/lib/test_randomness/pubspec.yaml new file mode 100644 index 000000000..346934f7a --- /dev/null +++ b/lib/test_randomness/pubspec.yaml @@ -0,0 +1,22 @@ +# Copyright (c) 2022 Sharezone UG (haftungsbeschränkt) +# Licensed under the EUPL-1.2-or-later. +# +# You may obtain a copy of the Licence at: +# https://joinup.ec.europa.eu/software/page/eupl +# +# SPDX-License-Identifier: EUPL-1.2 + +name: test_randomness +description: A new Flutter package project. +version: 0.0.1 +publish_to: none + +environment: + sdk: ">=2.12.0 <3.0.0" + +dependencies: + random_string: ^2.3.1 + +dev_dependencies: + sharezone_lints: + path: ../sharezone_lints diff --git a/licenses_config.yaml b/licenses_config.yaml index dbb07ddba..178e63ea7 100644 --- a/licenses_config.yaml +++ b/licenses_config.yaml @@ -85,5 +85,4 @@ packageLicenseOverride: url_launcher_extended: EUPL-1.2-or-later user: EUPL-1.2-or-later util: EUPL-1.2-or-later - - + test_randomness: EUPL-1.2-or-later \ No newline at end of file diff --git a/tools/sz_repo_cli/lib/src/commands/src/test_command.dart b/tools/sz_repo_cli/lib/src/commands/src/test_command.dart index 8a1612e7d..b306e0626 100644 --- a/tools/sz_repo_cli/lib/src/commands/src/test_command.dart +++ b/tools/sz_repo_cli/lib/src/commands/src/test_command.dart @@ -7,6 +7,8 @@ // SPDX-License-Identifier: EUPL-1.2 import 'dart:async'; +import 'dart:io'; +import 'dart:math'; import 'package:process_runner/process_runner.dart'; import 'package:sz_repo_cli/src/common/common.dart'; @@ -91,8 +93,14 @@ Future runTests( required bool excludeGoldens, required bool onlyGoldens, required bool updateGoldens, - required String testRandomizeOrderingSeed, + String testRandomizeOrderingSeed = 'random', }) { + // Copied from https://github.com/dart-lang/test/blob/ba64bbbaa26f09e139c26f9ad6409995806aac6e/pkgs/test_core/lib/src/runner/configuration/args.dart#L277 + final seed = testRandomizeOrderingSeed == 'random' + ? Random().nextInt(4294967295) + : int.parse(testRandomizeOrderingSeed).toUnsigned(32); + stdout.writeln('Using seed for test randomization: $seed'); + if (package.isFlutterPackage) { return _runTestsFlutter( processRunner, @@ -100,7 +108,7 @@ Future runTests( excludeGoldens: excludeGoldens, onlyGoldens: onlyGoldens, updateGoldens: updateGoldens, - testRandomizeOrderingSeed: testRandomizeOrderingSeed, + testRandomizeOrderingSeed: seed, ); } else { return _runTestsDart( @@ -108,7 +116,7 @@ Future runTests( package, excludeGoldens: excludeGoldens, onlyGoldens: onlyGoldens, - testRandomizeOrderingSeed: testRandomizeOrderingSeed, + testRandomizeOrderingSeed: seed, ); } } @@ -120,7 +128,7 @@ Future _runTestsDart( // don't have golden tests. required bool excludeGoldens, required bool onlyGoldens, - required String testRandomizeOrderingSeed, + required int testRandomizeOrderingSeed, }) async { if (onlyGoldens) { // Golden tests are only run in the flutter package. @@ -133,9 +141,13 @@ Future _runTestsDart( [ 'fvm', 'dart', + '--define=TEST_RANDOMNESS_SEED=$testRandomizeOrderingSeed', 'test', '--test-randomize-ordering-seed', - testRandomizeOrderingSeed + '$testRandomizeOrderingSeed', + // --define only works with this flag, seems to be a bug in Dart. + // See: https://github.com/dart-lang/test/issues/1794 + '--use-data-isolate-strategy', ], workingDirectory: package.location, ); @@ -147,7 +159,7 @@ Future _runTestsFlutter( required bool excludeGoldens, required bool onlyGoldens, required bool updateGoldens, - required String testRandomizeOrderingSeed, + required int testRandomizeOrderingSeed, }) async { if (onlyGoldens || !excludeGoldens) { if (!package.hasGoldenTestsDirectory) { @@ -162,7 +174,7 @@ Future _runTestsFlutter( 'test_goldens', if (updateGoldens) '--update-goldens', '--test-randomize-ordering-seed', - testRandomizeOrderingSeed, + '$testRandomizeOrderingSeed', ], workingDirectory: package.location, ); @@ -179,7 +191,8 @@ Future _runTestsFlutter( 'flutter', 'test', '--test-randomize-ordering-seed', - testRandomizeOrderingSeed, + '$testRandomizeOrderingSeed', + '--dart-define=TEST_RANDOMNESS_SEED=$testRandomizeOrderingSeed', ], workingDirectory: package.location, ); @@ -199,7 +212,8 @@ Future _runTestsFlutter( // Directory for unit and widget tests. 'test', '--test-randomize-ordering-seed', - testRandomizeOrderingSeed, + '$testRandomizeOrderingSeed', + '--dart-define=TEST_RANDOMNESS_SEED=$testRandomizeOrderingSeed', ], workingDirectory: package.location, );