From 00b93b5732e15668458b9ddcd789dbd131ca41c9 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Tue, 23 Aug 2022 13:44:37 -0400 Subject: [PATCH 1/8] fixing merging error --- packages/flutter_tools/lib/src/doctor.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/doctor.dart b/packages/flutter_tools/lib/src/doctor.dart index 47d9dcb13a61c..6c55b4599a805 100644 --- a/packages/flutter_tools/lib/src/doctor.dart +++ b/packages/flutter_tools/lib/src/doctor.dart @@ -119,7 +119,6 @@ class _DefaultDoctorValidatorsProvider implements DoctorValidatorsProvider { ]; final ProxyValidator proxyValidator = ProxyValidator(platform: platform); _validators = [ - if (platform.isWindows) const WindowsVersionValidator(), FlutterValidator( fileSystem: globals.fs, platform: globals.platform, From b45ae7e5aca93aef0a2d35abcb939f61b8ecb9f3 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Tue, 23 Aug 2022 17:00:32 -0400 Subject: [PATCH 2/8] added 3 tests for each return case for windows version - case 1: successful check (version 10 or greater) - case 2: `systeminfo` process call fails - case 3: version identified is less than 10 --- .../windows/windows_version_validator.dart | 17 +- .../windows_version_validator_test.dart | 190 ++++++++++++++++++ 2 files changed, 199 insertions(+), 8 deletions(-) diff --git a/packages/flutter_tools/lib/src/windows/windows_version_validator.dart b/packages/flutter_tools/lib/src/windows/windows_version_validator.dart index 75cb0d5a5e2f3..d73e690204532 100644 --- a/packages/flutter_tools/lib/src/windows/windows_version_validator.dart +++ b/packages/flutter_tools/lib/src/windows/windows_version_validator.dart @@ -7,6 +7,13 @@ import 'package:process/process.dart'; import '../base/io.dart'; import '../doctor_validator.dart'; +// Define the major versions that are not supported +const List unsupportedVersions = [ + '6', + '7', + '8', +]; + class WindowsVersionValidator extends DoctorValidator { /// Validator to be run with `flutter doctor` to check /// Windows host machines if they are running supported versions, @@ -41,12 +48,6 @@ class WindowsVersionValidator extends DoctorValidator { final String resultStdout = result.stdout as String; - // Define the major versions that are not supported - const List unsupportedVersions = [ - '7', - '8', - ]; - final List systemInfoElements = resultStdout.split('\n'); // Regular expression pattern for identifying @@ -62,14 +63,14 @@ class WindowsVersionValidator extends DoctorValidator { // Use two booleans to identify when you have found // the word 'version' and a version number that matches // the regex pattern above; only once both are found do - // we report back a valid version + // we add that version to the [versionList] bool versionText = false; bool versionSemver = false; String? version; for (final String curLine in systemInfoElements) { final List lineElems = curLine.split(' '); - for (final String elem in lineElems){ + for (final String elem in lineElems) { final bool match = regex.hasMatch(elem); if (match) { diff --git a/packages/flutter_tools/test/general.shard/windows_version_validator_test.dart b/packages/flutter_tools/test/general.shard/windows_version_validator_test.dart index e69de29bb2d1d..ae6192d62109e 100644 --- a/packages/flutter_tools/test/general.shard/windows_version_validator_test.dart +++ b/packages/flutter_tools/test/general.shard/windows_version_validator_test.dart @@ -0,0 +1,190 @@ +// 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 'package:flutter_tools/src/doctor_validator.dart'; +import 'package:flutter_tools/src/windows/windows_version_validator.dart'; + +import '../src/common.dart'; +import '../src/fake_process_manager.dart'; + +/// Example output from `systeminfo` from a Windows 10 host +const String validWindows10StdOut = r''' +Host Name: XXXXXXXXXXXX +OS Name: Microsoft Windows 10 Enterprise +OS Version: 10.0.19044 N/A Build 19044 +OS Manufacturer: Microsoft Corporation +OS Configuration: Member Workstation +OS Build Type: Multiprocessor Free +Registered Owner: N/A +Registered Organization: N/A +Product ID: XXXXXXXXXXXX +Original Install Date: 8/4/2022, 2:51:28 PM +System Boot Time: 8/10/2022, 1:03:10 PM +System Manufacturer: Google +System Model: Google Compute Engine +System Type: x64-based PC +Processor(s): 1 Processor(s) Installed. + [01]: AMD64 Family 23 Model 49 Stepping 0 AuthenticAMD ~2250 Mhz +BIOS Version: Google Google, 6/29/2022 +Windows Directory: C:\\Windows +System Directory: C:\\Windows\\system32 +Boot Device: \\Device\\HarddiskVolume2 +System Locale: en-us;English (United States) +Input Locale: en-us;English (United States) +Time Zone: (UTC-08:00) Pacific Time (US & Canada) +Total Physical Memory: 32,764 MB +Available Physical Memory: 17,852 MB +Virtual Memory: Max Size: 33,788 MB +Virtual Memory: Available: 18,063 MB +Virtual Memory: In Use: 15,725 MB +Page File Location(s): C:\\pagefile.sys +Domain: ad.corp.google.com +Logon Server: \\CBF-DC-8 +Hotfix(s): 7 Hotfix(s) Installed. + [01]: KB5013624 + [02]: KB5003791 + [03]: KB5012170 + [04]: KB5016616 + [05]: KB5014032 + [06]: KB5014671 + [07]: KB5015895 +Hyper-V Requirements: A hypervisor has been detected. Features required for Hyper-V will not be displayed. +'''; + +// TODO (eliasyishak): replace stdout with valid stdout + +/// Example output from `systeminfo` from version != 10 +const String invalidWindowsStdOut = r''' +Host Name: XXXXXXXXXXXX +OS Name: Microsoft Windows 8.1 Enterprise +OS Version: 6.3.9600 Build 9600 +OS Manufacturer: Microsoft Corporation +OS Configuration: Member Workstation +OS Build Type: Multiprocessor Free +Registered Owner: N/A +Registered Organization: N/A +Product ID: XXXXXXXXXXXX +Original Install Date: 8/4/2022, 2:51:28 PM +System Boot Time: 8/10/2022, 1:03:10 PM +System Manufacturer: Google +System Model: Google Compute Engine +System Type: x64-based PC +Processor(s): 1 Processor(s) Installed. + [01]: AMD64 Family 23 Model 49 Stepping 0 AuthenticAMD ~2250 Mhz +BIOS Version: Google Google, 6/29/2022 +Windows Directory: C:\\Windows +System Directory: C:\\Windows\\system32 +Boot Device: \\Device\\HarddiskVolume2 +System Locale: en-us;English (United States) +Input Locale: en-us;English (United States) +Time Zone: (UTC-08:00) Pacific Time (US & Canada) +Total Physical Memory: 32,764 MB +Available Physical Memory: 17,852 MB +Virtual Memory: Max Size: 33,788 MB +Virtual Memory: Available: 18,063 MB +Virtual Memory: In Use: 15,725 MB +Page File Location(s): C:\\pagefile.sys +Domain: ad.corp.google.com +Logon Server: \\CBF-DC-8 +Hotfix(s): 7 Hotfix(s) Installed. + [01]: KB5013624 + [02]: KB5003791 + [03]: KB5012170 + [04]: KB5016616 + [05]: KB5014032 + [06]: KB5014671 + [07]: KB5015895 +Hyper-V Requirements: A hypervisor has been detected. Features required for Hyper-V will not be displayed. +'''; + +/// The expected validation result object for +/// a passing windows version test +const ValidationResult validWindows10ValidationResult = ValidationResult( + ValidationType.installed, + [], + statusInfo: 'Installed version of Windows is version 10 or higher', +); + +/// The expected validation result object for +/// a failing exit code (!= 0) +const ValidationResult failedValidationResult = ValidationResult( + ValidationType.missing, + [], + statusInfo: 'Exit status from running `systeminfo` was unsuccessful', +); + +/// The expected validation result object for +/// a passing windows version test +const ValidationResult invalidWindowsValidationResult = ValidationResult( + ValidationType.missing, + [], + statusInfo: 'Unable to confirm if installed Windows version is 10 or greater', +); + +void main() { + testWithoutContext('Successfully running windows version check on windows 10', + () async { + final WindowsVersionValidator windowsVersionValidator = + WindowsVersionValidator( + processManager: FakeProcessManager.list( + [ + const FakeCommand( + command: ['systeminfo'], + stdout: validWindows10StdOut, + ), + ], + ), + ); + + final ValidationResult result = await windowsVersionValidator.validate(); + + expect(result.type, validWindows10ValidationResult.type, + reason: 'The ValidationResult type should be the same (installed)'); + expect(result.statusInfo, validWindows10ValidationResult.statusInfo, + reason: 'The ValidationResult statusInfo messages should be the same'); + }); + + testWithoutContext('Failing to invoke the `systeminfo` command', () async { + final WindowsVersionValidator windowsVersionValidator = + WindowsVersionValidator( + processManager: FakeProcessManager.list( + [ + const FakeCommand( + command: ['systeminfo'], + stdout: validWindows10StdOut, + exitCode: 1, + ), + ], + ), + ); + + final ValidationResult result = await windowsVersionValidator.validate(); + + expect(result.type, failedValidationResult.type, + reason: 'The ValidationResult type should be the same (missing)'); + expect(result.statusInfo, failedValidationResult.statusInfo, + reason: 'The ValidationResult statusInfo messages should be the same'); + }); + + testWithoutContext('Identifying a windows version before 10', () async { + final WindowsVersionValidator windowsVersionValidator = + WindowsVersionValidator( + processManager: FakeProcessManager.list( + [ + const FakeCommand( + command: ['systeminfo'], + stdout: invalidWindowsStdOut, + ), + ], + ), + ); + + final ValidationResult result = await windowsVersionValidator.validate(); + + expect(result.type, invalidWindowsValidationResult.type, + reason: 'The ValidationResult type should be the same (missing)'); + expect(result.statusInfo, invalidWindowsValidationResult.statusInfo, + reason: 'The ValidationResult statusInfo messages should be the same'); + }); +} From 35fc46f6afc380a9d5ae0c428d53ea4457cd597b Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Tue, 23 Aug 2022 17:16:21 -0400 Subject: [PATCH 3/8] update to documentation + using async process run --- .../lib/src/windows/windows_version_validator.dart | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/flutter_tools/lib/src/windows/windows_version_validator.dart b/packages/flutter_tools/lib/src/windows/windows_version_validator.dart index d73e690204532..96f4fb25de75a 100644 --- a/packages/flutter_tools/lib/src/windows/windows_version_validator.dart +++ b/packages/flutter_tools/lib/src/windows/windows_version_validator.dart @@ -7,17 +7,15 @@ import 'package:process/process.dart'; import '../base/io.dart'; import '../doctor_validator.dart'; -// Define the major versions that are not supported const List unsupportedVersions = [ '6', '7', '8', ]; +/// Validator to be run with `flutter doctor` to check +/// Windows host machines if they are running supported versions. class WindowsVersionValidator extends DoctorValidator { - /// Validator to be run with `flutter doctor` to check - /// Windows host machines if they are running supported versions, - /// current unsupported versions = 7, 8 const WindowsVersionValidator({required ProcessManager processManager}) : _processManager = processManager, super('Windows Version'); @@ -28,7 +26,7 @@ class WindowsVersionValidator extends DoctorValidator { Future validate() async { final ProcessResult result; try { - result = _processManager.runSync(['systeminfo']); + result = await _processManager.run(['systeminfo']); } on ProcessException { return const ValidationResult( ValidationType.missing, From 8939a3fcd6f0888995f40dabc940991733d657bb Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Wed, 24 Aug 2022 14:11:01 -0400 Subject: [PATCH 4/8] simplified string parsing to utilize builtin regex functionality --- .../windows/windows_version_validator.dart | 53 +++---------------- 1 file changed, 7 insertions(+), 46 deletions(-) diff --git a/packages/flutter_tools/lib/src/windows/windows_version_validator.dart b/packages/flutter_tools/lib/src/windows/windows_version_validator.dart index 96f4fb25de75a..01428766d06b2 100644 --- a/packages/flutter_tools/lib/src/windows/windows_version_validator.dart +++ b/packages/flutter_tools/lib/src/windows/windows_version_validator.dart @@ -46,59 +46,20 @@ class WindowsVersionValidator extends DoctorValidator { final String resultStdout = result.stdout as String; - final List systemInfoElements = resultStdout.split('\n'); - // Regular expression pattern for identifying // semantic versioned strings // (ie. 10.5.4123) - final RegExp regex = RegExp(r'^([0-9]+)\.([0-9]+)\.([0-9]+)$'); - - // Define the list that will contain the matches; - // if ran successfully, this list should have only - // one item - final List versionList = []; - - // Use two booleans to identify when you have found - // the word 'version' and a version number that matches - // the regex pattern above; only once both are found do - // we add that version to the [versionList] - bool versionText = false; - bool versionSemver = false; - String? version; - for (final String curLine in systemInfoElements) { - final List lineElems = curLine.split(' '); - - for (final String elem in lineElems) { - final bool match = regex.hasMatch(elem); - - if (match) { - versionSemver = true; - version = elem; - } - - if (elem.toLowerCase().contains('version')) { - versionText = true; - } - } - - // Once both booleans are true, add - // the version to the list that will contain - // at most, one element if ran as anticipated - if (versionText && versionSemver && version != null) { - versionList.add(version); - } - - // Reset the boolean values for the next line - versionText = false; - versionSemver = false; - version = null; - } + final RegExp regex = + RegExp(r'\n(OS Version:\s*)([0-9]+\.[0-9]+\.[0-9]+)(.*)\n'); + final Iterable matches = regex.allMatches(resultStdout); + // Use the string split method to extract the major version + // and check against the [unsupportedVersions] list final ValidationType windowsVersionStatus; String statusInfo; - if (versionList.length == 1 && + if (matches.length == 1 && !unsupportedVersions - .contains(versionList.elementAt(0).split('.').elementAt(0))) { + .contains(matches.elementAt(0).group(2)?.split('.').elementAt(0))) { windowsVersionStatus = ValidationType.installed; statusInfo = 'Installed version of Windows is version 10 or higher'; } else { From a28478833584c3ff615ea285b64324686c02c358 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Wed, 24 Aug 2022 14:31:55 -0400 Subject: [PATCH 5/8] remove todo --- .../test/general.shard/windows_version_validator_test.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/flutter_tools/test/general.shard/windows_version_validator_test.dart b/packages/flutter_tools/test/general.shard/windows_version_validator_test.dart index ae6192d62109e..f08e0bbe098d2 100644 --- a/packages/flutter_tools/test/general.shard/windows_version_validator_test.dart +++ b/packages/flutter_tools/test/general.shard/windows_version_validator_test.dart @@ -52,8 +52,6 @@ Hotfix(s): 7 Hotfix(s) Installed. Hyper-V Requirements: A hypervisor has been detected. Features required for Hyper-V will not be displayed. '''; -// TODO (eliasyishak): replace stdout with valid stdout - /// Example output from `systeminfo` from version != 10 const String invalidWindowsStdOut = r''' Host Name: XXXXXXXXXXXX From add6386baf974382550c632fd28a58d8cdecb67a Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Wed, 24 Aug 2022 15:32:49 -0400 Subject: [PATCH 6/8] add optional indicator for new line chars --- .../lib/src/windows/windows_version_validator.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/windows/windows_version_validator.dart b/packages/flutter_tools/lib/src/windows/windows_version_validator.dart index 01428766d06b2..7e16683747379 100644 --- a/packages/flutter_tools/lib/src/windows/windows_version_validator.dart +++ b/packages/flutter_tools/lib/src/windows/windows_version_validator.dart @@ -50,7 +50,7 @@ class WindowsVersionValidator extends DoctorValidator { // semantic versioned strings // (ie. 10.5.4123) final RegExp regex = - RegExp(r'\n(OS Version:\s*)([0-9]+\.[0-9]+\.[0-9]+)(.*)\n'); + RegExp(r'\n?(OS Version:\s*)([0-9]+\.[0-9]+\.[0-9]+)(.*)\n?'); final Iterable matches = regex.allMatches(resultStdout); // Use the string split method to extract the major version From 3c8c1a3e7abb2957fa7feea9551b274277e8b22f Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Wed, 24 Aug 2022 17:48:33 -0400 Subject: [PATCH 7/8] fixing regex pattern to look for start of line --- .../lib/src/windows/windows_version_validator.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/windows/windows_version_validator.dart b/packages/flutter_tools/lib/src/windows/windows_version_validator.dart index 7e16683747379..0704c880bceed 100644 --- a/packages/flutter_tools/lib/src/windows/windows_version_validator.dart +++ b/packages/flutter_tools/lib/src/windows/windows_version_validator.dart @@ -49,8 +49,10 @@ class WindowsVersionValidator extends DoctorValidator { // Regular expression pattern for identifying // semantic versioned strings // (ie. 10.5.4123) - final RegExp regex = - RegExp(r'\n?(OS Version:\s*)([0-9]+\.[0-9]+\.[0-9]+)(.*)\n?'); + final RegExp regex = RegExp( + r'^(OS Version:\s*)([0-9]+\.[0-9]+\.[0-9]+)(.*)$', + multiLine: true, + ); final Iterable matches = regex.allMatches(resultStdout); // Use the string split method to extract the major version From ad3bf7c1488aa813774c75b1882e3608ea625a8f Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Tue, 6 Sep 2022 15:44:32 -0400 Subject: [PATCH 8/8] added unit test for regex pattern validator --- .../windows/windows_version_validator.dart | 20 +++++++++++++----- .../windows_version_validator_test.dart | 21 +++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/packages/flutter_tools/lib/src/windows/windows_version_validator.dart b/packages/flutter_tools/lib/src/windows/windows_version_validator.dart index 0704c880bceed..27f6cd243dc75 100644 --- a/packages/flutter_tools/lib/src/windows/windows_version_validator.dart +++ b/packages/flutter_tools/lib/src/windows/windows_version_validator.dart @@ -22,6 +22,19 @@ class WindowsVersionValidator extends DoctorValidator { final ProcessManager _processManager; + /// Provide a literal string as the Regex pattern + /// and a string to validate and get a boolean determining + /// if the string has at least one match + static Iterable validateString(String pattern, String str, + {bool multiLine = true}) { + final RegExp regex = RegExp( + pattern, + multiLine: multiLine, + ); + + return regex.allMatches(str); + } + @override Future validate() async { final ProcessResult result; @@ -49,11 +62,8 @@ class WindowsVersionValidator extends DoctorValidator { // Regular expression pattern for identifying // semantic versioned strings // (ie. 10.5.4123) - final RegExp regex = RegExp( - r'^(OS Version:\s*)([0-9]+\.[0-9]+\.[0-9]+)(.*)$', - multiLine: true, - ); - final Iterable matches = regex.allMatches(resultStdout); + final Iterable matches = validateString( + r'^(OS Version:\s*)([0-9]+\.[0-9]+\.[0-9]+)(.*)$', resultStdout); // Use the string split method to extract the major version // and check against the [unsupportedVersions] list diff --git a/packages/flutter_tools/test/general.shard/windows_version_validator_test.dart b/packages/flutter_tools/test/general.shard/windows_version_validator_test.dart index f08e0bbe098d2..da849cab90f77 100644 --- a/packages/flutter_tools/test/general.shard/windows_version_validator_test.dart +++ b/packages/flutter_tools/test/general.shard/windows_version_validator_test.dart @@ -185,4 +185,25 @@ void main() { expect(result.statusInfo, invalidWindowsValidationResult.statusInfo, reason: 'The ValidationResult statusInfo messages should be the same'); }); + + testWithoutContext('Unit testing on a regex pattern validator', () async { + const String regexPattern = + r'^(OS Version:\s*)([0-9]+\.[0-9]+\.[0-9]+)(.*)$'; + const String testStr = r''' +OS Version: 10.0.19044 N/A Build 19044 +OSz Version: 10.0.19044 N/A Build 19044 +OS 6Version: 10.0.19044 N/A Build 19044 +OxS Version: 10.0.19044 N/A Build 19044 +OS Version: 10.19044 N/A Build 19044 +OS Version: 10.x.19044 N/A Build 19044 +OS Version: 10.0.19044 N/A Build 19044 +OS Version: .0.19044 N/A Build 19044 +'''; + + final Iterable matches = + WindowsVersionValidator.validateString(regexPattern, testStr); + + expect(matches.length, 2, + reason: 'There should be only two matches for the pattern provided'); + }); }