From f7c48b334b1386bc5ab0f706fbcd6df8496a87fc Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Mon, 10 Aug 2020 10:13:47 -0700 Subject: [PATCH] Migrate to null safety (#99) --- .travis.yml | 36 ++++++++++++++++++++++++------------ CHANGELOG.md | 11 +++++++++++ analysis_options.yaml | 4 ++++ example/example.dart | 2 +- lib/src/digest_sink.dart | 11 ++++------- lib/src/hmac.dart | 2 +- pubspec.yaml | 18 +++++++++++------- test/hmac_sha2_test.dart | 14 +++++++------- test/sha_monte_test.dart | 2 +- test/utils.dart | 2 +- 10 files changed, 65 insertions(+), 37 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6d2f9960bb483..b9d111765d531 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,23 +1,35 @@ language: dart dart: - - 2.3.0 - - stable + - dev -dart_task: - - test: -p vm - - test: -p chrome - - dartanalyzer: --fatal-warnings --fatal-infos . - -matrix: +jobs: include: - # Only validate formatting using the dev release - - dart: dev - dart_task: dartfmt + - stage: analyze_and_format + name: "Analyze" + os: linux + script: dartanalyzer --fatal-warnings --fatal-infos . + - stage: analyze_and_format + name: "Format" + os: linux + script: dartfmt -n --set-exit-if-changed . + - stage: test + name: "Vm Tests" + os: linux + script: pub run --enable-experiment=non-nullable test -p vm + - stage: test + name: "Web Tests" + os: linux + script: pub run --enable-experiment=non-nullable test -p chrome + +stages: + - analyze_and_format + - test + # Only building master means that we don't run two builds for each pull request. branches: - only: [master] + only: [master, null_safety] cache: directories: diff --git a/CHANGELOG.md b/CHANGELOG.md index c7e6aff7a50c7..35e4760fb1a3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,14 @@ +## 2.2.0-nullsafety + +Pre-release for the null safety migration of this package. + +Note that 2.2.0 may not be the final stable null safety release version, we +reserve the right to release it as a 3.0.0 breaking change. + +This release will be pinned to only allow pre-release sdk versions starting from +2.10.0-2.0.dev, which is the first version where this package will appear in the +null safety allow list. + ## 2.1.5 * Improve example and package description to address package site maintenance diff --git a/analysis_options.yaml b/analysis_options.yaml index 0711acad5d1c7..20b4e835373a3 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -2,6 +2,10 @@ include: package:pedantic/analysis_options.yaml analyzer: strong-mode: implicit-casts: false + + enable-experiment: + - non-nullable + linter: rules: - avoid_empty_else diff --git a/example/example.dart b/example/example.dart index 387c0148d64c0..9686ad91fc919 100644 --- a/example/example.dart +++ b/example/example.dart @@ -10,7 +10,7 @@ import 'package:crypto/crypto.dart'; final _usage = 'Usage: dart hash.dart '; Future main(List args) async { - if (args == null || args.length != 2) { + if (args.length != 2) { print(_usage); exitCode = 64; // Command was used incorrectly. return; diff --git a/lib/src/digest_sink.dart b/lib/src/digest_sink.dart index 48c85f5a157f4..39b865bd6773d 100644 --- a/lib/src/digest_sink.dart +++ b/lib/src/digest_sink.dart @@ -7,24 +7,21 @@ import 'digest.dart'; /// A sink used to get a digest value out of `Hash.startChunkedConversion`. class DigestSink extends Sink { /// The value added to the sink, if any. - Digest get value { - assert(_value != null); - return _value; - } + Digest get value => _value; - Digest _value; + late final Digest _value; /// Adds [value] to the sink. /// /// Unlike most sinks, this may only be called once. @override void add(Digest value) { - assert(_value == null); _value = value; } @override void close() { - assert(_value != null); + // Ensure late final field was assigned before closing. + assert((_value as dynamic) != null); } } diff --git a/lib/src/hmac.dart b/lib/src/hmac.dart index bbb2a4d847c5b..a16bd3a08815f 100644 --- a/lib/src/hmac.dart +++ b/lib/src/hmac.dart @@ -60,7 +60,7 @@ class _HmacSink extends ByteConversionSink { final _innerResultSink = DigestSink(); /// The sink for the inner hash computation. - ByteConversionSink _innerSink; + late final ByteConversionSink _innerSink; /// Whether [close] has been called. bool _isClosed = false; diff --git a/pubspec.yaml b/pubspec.yaml index 420fb87c12ebe..a144b06d9bcee 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,16 +1,20 @@ name: crypto -version: 2.1.5 +version: 2.2.0-nullsafety description: Implementations of SHA, MD5, and HMAC cryptographic functions homepage: https://www.github.com/dart-lang/crypto environment: - sdk: '>=2.3.0 <3.0.0' + sdk: '>=2.10.0-2.0.dev <2.10.0' dependencies: - collection: '^1.0.0' - convert: '>=1.0.0 <3.0.0' - typed_data: '^1.0.0' + collection: ^1.15.0-nullsafety + convert: ^2.2.0-nullsafety + typed_data: ^1.3.0-nullsafety dev_dependencies: - pedantic: ^1.0.0 - test: ^1.0.0 + pedantic: ^1.10.0-nullsafety + test: ^1.16.0-nullsafety + +dependency_overrides: + convert: + git: git://github.com/dart-lang/convert.git diff --git a/test/hmac_sha2_test.dart b/test/hmac_sha2_test.dart index f4e7bdae35a7a..4f70331f20d1f 100644 --- a/test/hmac_sha2_test.dart +++ b/test/hmac_sha2_test.dart @@ -103,13 +103,13 @@ void main() { } void testCase({ - String name, - String key, - String data, - String hmacSha224, - String hmacSha256, - String hmacSha384, - String hmacSha512, + required String name, + required String key, + required String data, + required String hmacSha224, + required String hmacSha256, + required String hmacSha384, + required String hmacSha512, bool truncation = false, }) { test(name, () { diff --git a/test/sha_monte_test.dart b/test/sha_monte_test.dart index a8511309f74cc..a41b0b8a64207 100644 --- a/test/sha_monte_test.dart +++ b/test/sha_monte_test.dart @@ -65,7 +65,7 @@ void monteTest(String name, Hash hash, String seed, List expected) { md0 = (Uint8List.fromList(_seed)); md1 = (Uint8List.fromList(_seed)); md2 = (Uint8List.fromList(_seed)); - Digest mdI; + late Digest mdI; for (var i = 3; i < 1003; i++) { var mI = [...md0, ...md1, ...md2]; mdI = hash.convert(mI); diff --git a/test/utils.dart b/test/utils.dart index 8ac12157e68f4..2e46d40902754 100644 --- a/test/utils.dart +++ b/test/utils.dart @@ -18,7 +18,7 @@ final toupleMatch = RegExp('([0-9a-fA-F]{2})'); Uint8List bytesFromHexString(String message) { var bytes = []; for (var match in toupleMatch.allMatches(message)) { - bytes.add(int.parse(match.group(0), radix: 16)); + bytes.add(int.parse(match.group(0)!, radix: 16)); } return Uint8List.fromList(bytes); }