Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 458b298

Browse files
authored
Refactoring to use ver command instead of systeminfo (#119304)
* refactoring to use `ver` command instead of `systeminfo` * fix tests to match new approach * adding another valid validator using brazil locale text * refactor tests to use generic fake class `ver` arg + showing output from `ver` if unsuccessful * update reason text in test * fix reason text to be hard coded
1 parent 59d80dc commit 458b298

File tree

3 files changed

+33
-255
lines changed

3 files changed

+33
-255
lines changed

packages/flutter_tools/lib/src/doctor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class _DefaultDoctorValidatorsProvider implements DoctorValidatorsProvider {
132132
),
133133
if (platform.isWindows)
134134
WindowsVersionValidator(
135-
processManager: globals.processManager,
135+
operatingSystemUtils: globals.os,
136136
),
137137
if (androidWorkflow!.appliesToHostPlatform)
138138
GroupedValidator(<DoctorValidator>[androidValidator!, androidLicenseValidator!]),

packages/flutter_tools/lib/src/windows/windows_version_validator.dart

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'package:process/process.dart';
6-
7-
import '../base/io.dart';
5+
import '../base/os.dart';
86
import '../doctor_validator.dart';
97

108
/// Flutter only supports development on Windows host machines version 10 and greater.
@@ -16,49 +14,36 @@ const List<String> kUnsupportedVersions = <String>[
1614

1715
/// Regex pattern for identifying line from systeminfo stdout with windows version
1816
/// (ie. 10.5.4123)
19-
const String kWindowsOSVersionSemVerPattern =
20-
r'^(OS )([^:]*:\s*)([0-9]+\.[0-9]+\.[0-9]+)(.*)$';
17+
const String kWindowsOSVersionSemVerPattern = r'([0-9]+)\.([0-9]+)\.([0-9\.]+)';
2118

2219
/// Validator for supported Windows host machine operating system version.
2320
class WindowsVersionValidator extends DoctorValidator {
24-
const WindowsVersionValidator({required ProcessManager processManager})
25-
: _processManager = processManager,
21+
const WindowsVersionValidator({
22+
required OperatingSystemUtils operatingSystemUtils,
23+
}) : _operatingSystemUtils = operatingSystemUtils,
2624
super('Windows Version');
2725

28-
final ProcessManager _processManager;
26+
final OperatingSystemUtils _operatingSystemUtils;
2927

3028
@override
3129
Future<ValidationResult> validate() async {
32-
final ProcessResult result =
33-
await _processManager.run(<String>['systeminfo']);
34-
35-
if (result.exitCode != 0) {
36-
return const ValidationResult(
37-
ValidationType.missing,
38-
<ValidationMessage>[],
39-
statusInfo: 'Exit status from running `systeminfo` was unsuccessful',
40-
);
41-
}
42-
43-
final String resultStdout = result.stdout as String;
44-
4530
final RegExp regex =
4631
RegExp(kWindowsOSVersionSemVerPattern, multiLine: true);
47-
final Iterable<RegExpMatch> matches = regex.allMatches(resultStdout);
32+
final String commandResult = _operatingSystemUtils.name;
33+
final Iterable<RegExpMatch> matches = regex.allMatches(commandResult);
4834

4935
// Use the string split method to extract the major version
5036
// and check against the [kUnsupportedVersions] list
5137
final ValidationType windowsVersionStatus;
5238
final String statusInfo;
5339
if (matches.length == 1 &&
54-
!kUnsupportedVersions
55-
.contains(matches.elementAt(0).group(3)?.split('.').elementAt(0))) {
40+
!kUnsupportedVersions.contains(matches.elementAt(0).group(1))) {
5641
windowsVersionStatus = ValidationType.success;
5742
statusInfo = 'Installed version of Windows is version 10 or higher';
5843
} else {
5944
windowsVersionStatus = ValidationType.missing;
6045
statusInfo =
61-
'Unable to confirm if installed Windows version is 10 or greater';
46+
'Unable to determine Windows version (command `ver` returned $commandResult)';
6247
}
6348

6449
return ValidationResult(

packages/flutter_tools/test/general.shard/windows_version_validator_test.dart

Lines changed: 22 additions & 229 deletions
Original file line numberDiff line numberDiff line change
@@ -2,138 +2,22 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter_tools/src/base/os.dart';
56
import 'package:flutter_tools/src/doctor_validator.dart';
67
import 'package:flutter_tools/src/windows/windows_version_validator.dart';
8+
import 'package:test/fake.dart';
79

810
import '../src/common.dart';
9-
import '../src/fake_process_manager.dart';
1011

11-
/// Example output from `systeminfo` from a Windows 10 host
12-
const String validWindows10StdOut = r'''
13-
Host Name: XXXXXXXXXXXX
14-
OS Name: Microsoft Windows 10 Enterprise
15-
OS Version: 10.0.19044 N/A Build 19044
16-
OS Manufacturer: Microsoft Corporation
17-
OS Configuration: Member Workstation
18-
OS Build Type: Multiprocessor Free
19-
Registered Owner: N/A
20-
Registered Organization: N/A
21-
Product ID: XXXXXXXXXXXX
22-
Original Install Date: 8/4/2022, 2:51:28 PM
23-
System Boot Time: 8/10/2022, 1:03:10 PM
24-
System Manufacturer: Google
25-
System Model: Google Compute Engine
26-
System Type: x64-based PC
27-
Processor(s): 1 Processor(s) Installed.
28-
[01]: AMD64 Family 23 Model 49 Stepping 0 AuthenticAMD ~2250 Mhz
29-
BIOS Version: Google Google, 6/29/2022
30-
Windows Directory: C:\\Windows
31-
System Directory: C:\\Windows\\system32
32-
Boot Device: \\Device\\HarddiskVolume2
33-
System Locale: en-us;English (United States)
34-
Input Locale: en-us;English (United States)
35-
Time Zone: (UTC-08:00) Pacific Time (US & Canada)
36-
Total Physical Memory: 32,764 MB
37-
Available Physical Memory: 17,852 MB
38-
Virtual Memory: Max Size: 33,788 MB
39-
Virtual Memory: Available: 18,063 MB
40-
Virtual Memory: In Use: 15,725 MB
41-
Page File Location(s): C:\\pagefile.sys
42-
Domain: ad.corp.google.com
43-
Logon Server: \\CBF-DC-8
44-
Hotfix(s): 7 Hotfix(s) Installed.
45-
[01]: KB5013624
46-
[02]: KB5003791
47-
[03]: KB5012170
48-
[04]: KB5016616
49-
[05]: KB5014032
50-
[06]: KB5014671
51-
[07]: KB5015895
52-
Hyper-V Requirements: A hypervisor has been detected. Features required for Hyper-V will not be displayed.
53-
''';
54-
55-
const String validWindows11CnStdOut = r'''
56-
主机名: XXXXXXXXXXXX
57-
OS 名称: Microsoft Windows 11 专业版
58-
OS 版本: 10.0.22621 暂缺 Build 22621
59-
OS 制造商: Microsoft Corporation
60-
OS 配置: 独立工作站
61-
OS 构建类型: Multiprocessor Free
62-
注册的所有人: 暂缺
63-
注册的组织: 暂缺
64-
产品 ID: XXXXXXXXXXXX
65-
初始安装日期: 2022/11/9, 13:33:50
66-
系统启动时间: 2022/11/30, 13:36:47
67-
系统制造商: ASUS
68-
系统型号: System Product Name
69-
系统类型: x64-based PC
70-
处理器: 安装了 1 个处理器。
71-
[01]: Intel64 Family 6 Model 151 Stepping 2 GenuineIntel ~3600 Mhz
72-
BIOS 版本: American Megatrends Inc. 2103, 2022/9/30
73-
Windows 目录: C:\WINDOWS
74-
系统目录: C:\WINDOWS\system32
75-
启动设备: \Device\HarddiskVolume1
76-
系统区域设置: zh-cn;中文(中国)
77-
输入法区域设置: zh-cn;中文(中国)
78-
时区: (UTC+08:00) 北京,重庆,香港特别行政区,乌鲁木齐
79-
物理内存总量: 65,277 MB
80-
可用的物理内存: 55,333 MB
81-
虚拟内存: 最大值: 75,005 MB
82-
虚拟内存: 可用: 61,781 MB
83-
虚拟内存: 使用中: 13,224 MB
84-
页面文件位置: C:\pagefile.sys
85-
域: WORKGROUP
86-
登录服务器: \\XXXXXXXXXXXX
87-
修补程序: 安装了 3 个修补程序。
88-
[01]: KB5020622
89-
[02]: KB5019980
90-
[03]: KB5019304
91-
Hyper-V 要求: 已检测到虚拟机监控程序。将不显示 Hyper-V 所需的功能。
92-
''';
12+
/// Fake [_WindowsUtils] to use for testing
13+
class FakeValidOperatingSystemUtils extends Fake
14+
implements OperatingSystemUtils {
15+
FakeValidOperatingSystemUtils(
16+
[this.name = 'Microsoft Windows [Version 11.0.22621.963]']);
9317

94-
/// Example output from `systeminfo` from version != 10
95-
const String invalidWindowsStdOut = r'''
96-
Host Name: XXXXXXXXXXXX
97-
OS Name: Microsoft Windows 8.1 Enterprise
98-
OS Version: 6.3.9600 Build 9600
99-
OS Manufacturer: Microsoft Corporation
100-
OS Configuration: Member Workstation
101-
OS Build Type: Multiprocessor Free
102-
Registered Owner: N/A
103-
Registered Organization: N/A
104-
Product ID: XXXXXXXXXXXX
105-
Original Install Date: 8/4/2022, 2:51:28 PM
106-
System Boot Time: 8/10/2022, 1:03:10 PM
107-
System Manufacturer: Google
108-
System Model: Google Compute Engine
109-
System Type: x64-based PC
110-
Processor(s): 1 Processor(s) Installed.
111-
[01]: AMD64 Family 23 Model 49 Stepping 0 AuthenticAMD ~2250 Mhz
112-
BIOS Version: Google Google, 6/29/2022
113-
Windows Directory: C:\\Windows
114-
System Directory: C:\\Windows\\system32
115-
Boot Device: \\Device\\HarddiskVolume2
116-
System Locale: en-us;English (United States)
117-
Input Locale: en-us;English (United States)
118-
Time Zone: (UTC-08:00) Pacific Time (US & Canada)
119-
Total Physical Memory: 32,764 MB
120-
Available Physical Memory: 17,852 MB
121-
Virtual Memory: Max Size: 33,788 MB
122-
Virtual Memory: Available: 18,063 MB
123-
Virtual Memory: In Use: 15,725 MB
124-
Page File Location(s): C:\\pagefile.sys
125-
Domain: ad.corp.google.com
126-
Logon Server: \\CBF-DC-8
127-
Hotfix(s): 7 Hotfix(s) Installed.
128-
[01]: KB5013624
129-
[02]: KB5003791
130-
[03]: KB5012170
131-
[04]: KB5016616
132-
[05]: KB5014032
133-
[06]: KB5014671
134-
[07]: KB5015895
135-
Hyper-V Requirements: A hypervisor has been detected. Features required for Hyper-V will not be displayed.
136-
''';
18+
@override
19+
final String name;
20+
}
13721

13822
/// The expected validation result object for
13923
/// a passing windows version test
@@ -143,14 +27,6 @@ const ValidationResult validWindows10ValidationResult = ValidationResult(
14327
statusInfo: 'Installed version of Windows is version 10 or higher',
14428
);
14529

146-
/// The expected validation result object for
147-
/// a failing exit code (!= 0)
148-
const ValidationResult failedValidationResult = ValidationResult(
149-
ValidationType.missing,
150-
<ValidationMessage>[],
151-
statusInfo: 'Exit status from running `systeminfo` was unsuccessful',
152-
);
153-
15430
/// The expected validation result object for
15531
/// a passing windows version test
15632
const ValidationResult invalidWindowsValidationResult = ValidationResult(
@@ -159,28 +35,12 @@ const ValidationResult invalidWindowsValidationResult = ValidationResult(
15935
statusInfo: 'Unable to confirm if installed Windows version is 10 or greater',
16036
);
16137

162-
/// Expected return from a nonzero exitcode when
163-
/// running systeminfo
164-
const ValidationResult invalidExitCodeValidationResult = ValidationResult(
165-
ValidationType.missing,
166-
<ValidationMessage>[],
167-
statusInfo: 'Exit status from running `systeminfo` was unsuccessful',
168-
);
169-
17038
void main() {
17139
testWithoutContext('Successfully running windows version check on windows 10',
17240
() async {
17341
final WindowsVersionValidator windowsVersionValidator =
17442
WindowsVersionValidator(
175-
processManager: FakeProcessManager.list(
176-
<FakeCommand>[
177-
const FakeCommand(
178-
command: <String>['systeminfo'],
179-
stdout: validWindows10StdOut,
180-
),
181-
],
182-
),
183-
);
43+
operatingSystemUtils: FakeValidOperatingSystemUtils());
18444

18545
final ValidationResult result = await windowsVersionValidator.validate();
18646

@@ -191,95 +51,31 @@ void main() {
19151
});
19252

19353
testWithoutContext(
194-
'Successfully running windows version check on windows 11 CN',
195-
() async {
196-
final WindowsVersionValidator windowsVersionValidator =
197-
WindowsVersionValidator(
198-
processManager: FakeProcessManager.list(
199-
<FakeCommand>[
200-
const FakeCommand(
201-
command: <String>['systeminfo'],
202-
stdout: validWindows11CnStdOut,
203-
),
204-
],
205-
),
206-
);
207-
208-
final ValidationResult result = await windowsVersionValidator.validate();
209-
210-
expect(
211-
result.type,
212-
validWindows10ValidationResult.type,
213-
reason: 'The ValidationResult type should be the same (installed)',
214-
);
215-
expect(
216-
result.statusInfo,
217-
validWindows10ValidationResult.statusInfo,
218-
reason: 'The ValidationResult statusInfo messages should be the same',
219-
);
220-
},
221-
);
222-
223-
testWithoutContext('Failing to invoke the `systeminfo` command', () async {
54+
'Successfully running windows version check on windows 10 for BR',
55+
() async {
22456
final WindowsVersionValidator windowsVersionValidator =
22557
WindowsVersionValidator(
226-
processManager: FakeProcessManager.list(
227-
<FakeCommand>[
228-
const FakeCommand(
229-
command: <String>['systeminfo'],
230-
stdout: validWindows10StdOut,
231-
exitCode: 1,
232-
),
233-
],
234-
),
235-
);
58+
operatingSystemUtils: FakeValidOperatingSystemUtils(
59+
'Microsoft Windows [versão 10.0.22621.1105]'));
23660

23761
final ValidationResult result = await windowsVersionValidator.validate();
23862

239-
expect(result.type, failedValidationResult.type,
240-
reason: 'The ValidationResult type should be the same (missing)');
241-
expect(result.statusInfo, failedValidationResult.statusInfo,
63+
expect(result.type, validWindows10ValidationResult.type,
64+
reason: 'The ValidationResult type should be the same (installed)');
65+
expect(result.statusInfo, validWindows10ValidationResult.statusInfo,
24266
reason: 'The ValidationResult statusInfo messages should be the same');
24367
});
24468

24569
testWithoutContext('Identifying a windows version before 10', () async {
24670
final WindowsVersionValidator windowsVersionValidator =
24771
WindowsVersionValidator(
248-
processManager: FakeProcessManager.list(
249-
<FakeCommand>[
250-
const FakeCommand(
251-
command: <String>['systeminfo'],
252-
stdout: invalidWindowsStdOut,
253-
),
254-
],
255-
),
256-
);
72+
operatingSystemUtils: FakeValidOperatingSystemUtils(
73+
'Microsoft Windows [Version 8.0.22621.1105]'));
25774

25875
final ValidationResult result = await windowsVersionValidator.validate();
25976

26077
expect(result.type, invalidWindowsValidationResult.type,
26178
reason: 'The ValidationResult type should be the same (missing)');
262-
expect(result.statusInfo, invalidWindowsValidationResult.statusInfo,
263-
reason: 'The ValidationResult statusInfo messages should be the same');
264-
});
265-
266-
testWithoutContext(
267-
'Running into an nonzero exit code from systeminfo command', () async {
268-
final WindowsVersionValidator windowsVersionValidator =
269-
WindowsVersionValidator(
270-
processManager: FakeProcessManager.list(
271-
<FakeCommand>[
272-
const FakeCommand(command: <String>['systeminfo'], exitCode: 1),
273-
],
274-
),
275-
);
276-
277-
final ValidationResult result = await windowsVersionValidator.validate();
278-
279-
expect(result.type, invalidExitCodeValidationResult.type,
280-
reason: 'The ValidationResult type should be the same (missing)');
281-
expect(result.statusInfo, invalidExitCodeValidationResult.statusInfo,
282-
reason: 'The ValidationResult statusInfo messages should be the same');
28379
});
28480

28581
testWithoutContext('Unit testing on a regex pattern validator', () async {
@@ -300,10 +96,7 @@ OS 版本: 10.0.22621 暂缺 Build 22621
30096
);
30197
final Iterable<RegExpMatch> matches = regex.allMatches(testStr);
30298

303-
expect(
304-
matches.length,
305-
3,
306-
reason: 'There should be only two matches for the pattern provided',
307-
);
99+
expect(matches.length, 5,
100+
reason: 'There should be only 5 matches for the pattern provided');
308101
});
309102
}

0 commit comments

Comments
 (0)