Skip to content

Commit c725f10

Browse files
authored
[flutter_tools] More Linux version detail (flutter#50739)
1 parent 766bd70 commit c725f10

File tree

3 files changed

+178
-83
lines changed

3 files changed

+178
-83
lines changed

packages/flutter_tools/lib/src/base/os.dart

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -251,21 +251,48 @@ class _PosixUtils extends OperatingSystemUtils {
251251

252252
@override
253253
String get name {
254-
if (_name == null) {
255-
if (_platform.isMacOS) {
256-
final List<RunResult> results = <RunResult>[
257-
_processUtils.runSync(<String>['sw_vers', '-productName']),
258-
_processUtils.runSync(<String>['sw_vers', '-productVersion']),
259-
_processUtils.runSync(<String>['sw_vers', '-buildVersion']),
260-
];
261-
if (results.every((RunResult result) => result.exitCode == 0)) {
262-
_name = '${results[0].stdout.trim()} ${results[1].stdout
263-
.trim()} ${results[2].stdout.trim()}';
264-
}
265-
}
266-
_name ??= super.name;
254+
if (_name != null) {
255+
return _name;
267256
}
268-
return _name;
257+
if (_platform.isMacOS) {
258+
return _name = _macName;
259+
} else if (_platform.isLinux) {
260+
return _name = _linuxName;
261+
}
262+
return _name = super.name;
263+
}
264+
265+
String get _macName {
266+
final List<RunResult> results = <RunResult>[
267+
_processUtils.runSync(<String>['sw_vers', '-productName']),
268+
_processUtils.runSync(<String>['sw_vers', '-productVersion']),
269+
_processUtils.runSync(<String>['sw_vers', '-buildVersion']),
270+
];
271+
if (results.every((RunResult result) => result.exitCode == 0)) {
272+
return '${results[0].stdout.trim()} '
273+
'${results[1].stdout.trim()} '
274+
'${results[2].stdout.trim()}';
275+
}
276+
return super.name;
277+
}
278+
279+
String get _linuxName {
280+
final String fullVersion = _platform.operatingSystemVersion;
281+
// This is formatted as 'Linux version build'. The 'build' part can be
282+
// somewhat long and is not very informative, so omit it.
283+
final List<String> versionParts = fullVersion.split(' ');
284+
if (versionParts.length < 2) {
285+
// The version string didn't have the expected format. Just return the
286+
// full string.
287+
return fullVersion;
288+
}
289+
final String system = versionParts[0];
290+
final String version = versionParts[1];
291+
if (system != 'Linux') {
292+
// If the system name isn't 'Linux', then just return the full string.
293+
return fullVersion;
294+
}
295+
return '$system $version';
269296
}
270297

271298
@override

packages/flutter_tools/test/general.shard/base/os_test.dart

Lines changed: 137 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44

55
import 'package:file/file.dart';
66
import 'package:file/memory.dart';
7-
import 'package:flutter_tools/src/base/io.dart';
87
import 'package:flutter_tools/src/base/file_system.dart';
98
import 'package:flutter_tools/src/base/logger.dart';
109
import 'package:flutter_tools/src/base/os.dart';
10+
import 'package:flutter_tools/src/globals.dart' as globals;
1111
import 'package:mockito/mockito.dart';
1212
import 'package:process/process.dart';
1313
import 'package:platform/platform.dart';
1414

1515
import '../../src/common.dart';
1616
import '../../src/context.dart';
17+
import '../../src/fake_process_manager.dart';
1718

1819
const String kExecutable = 'foo';
1920
const String kPath1 = '/bar/bin/$kExecutable';
@@ -22,40 +23,51 @@ const String kPath2 = '/another/bin/$kExecutable';
2223
class MockLogger extends Mock implements Logger {}
2324

2425
void main() {
25-
MockProcessManager mockProcessManager;
26-
27-
setUp(() {
28-
mockProcessManager = MockProcessManager();
29-
});
26+
FakeProcessManager fakeProcessManager;
3027

3128
OperatingSystemUtils createOSUtils(Platform platform) {
3229
return OperatingSystemUtils(
3330
fileSystem: MemoryFileSystem(),
3431
logger: MockLogger(),
3532
platform: platform,
36-
processManager: mockProcessManager,
33+
processManager: fakeProcessManager,
3734
);
3835
}
3936

4037
group('which on POSIX', () {
4138
testWithoutContext('returns null when executable does not exist', () async {
42-
when(mockProcessManager.runSync(<String>['which', kExecutable]))
43-
.thenReturn(ProcessResult(0, 1, null, null));
44-
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
39+
fakeProcessManager = FakeProcessManager.list(<FakeCommand>[
40+
const FakeCommand(command: <String>['which', kExecutable], exitCode: 1),
41+
]);
42+
final OperatingSystemUtils utils = createOSUtils(
43+
FakePlatform(operatingSystem: 'linux'),
44+
);
4545
expect(utils.which(kExecutable), isNull);
4646
});
4747

4848
testWithoutContext('returns exactly one result', () async {
49-
when(mockProcessManager.runSync(<String>['which', 'foo']))
50-
.thenReturn(ProcessResult(0, 0, kPath1, null));
51-
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
49+
fakeProcessManager = FakeProcessManager.list(<FakeCommand>[
50+
const FakeCommand(
51+
command: <String>['which', kExecutable],
52+
stdout: kPath1,
53+
),
54+
]);
55+
final OperatingSystemUtils utils = createOSUtils(
56+
FakePlatform(operatingSystem: 'linux'),
57+
);
5258
expect(utils.which(kExecutable).path, kPath1);
5359
});
5460

5561
testWithoutContext('returns all results for whichAll', () async {
56-
when(mockProcessManager.runSync(<String>['which', '-a', kExecutable]))
57-
.thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
58-
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
62+
fakeProcessManager = FakeProcessManager.list(<FakeCommand>[
63+
const FakeCommand(
64+
command: <String>['which', '-a', kExecutable],
65+
stdout: '$kPath1\n$kPath2\n',
66+
),
67+
]);
68+
final OperatingSystemUtils utils = createOSUtils(
69+
FakePlatform(operatingSystem: 'linux'),
70+
);
5971
final List<File> result = utils.whichAll(kExecutable);
6072
expect(result, hasLength(2));
6173
expect(result[0].path, kPath1);
@@ -65,29 +77,129 @@ void main() {
6577

6678
group('which on Windows', () {
6779
testWithoutContext('returns null when executable does not exist', () async {
68-
when(mockProcessManager.runSync(<String>['where', kExecutable]))
69-
.thenReturn(ProcessResult(0, 1, null, null));
70-
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
80+
fakeProcessManager = FakeProcessManager.list(<FakeCommand>[
81+
const FakeCommand(
82+
command: <String>['where', kExecutable],
83+
exitCode: 1,
84+
),
85+
]);
86+
final OperatingSystemUtils utils = createOSUtils(
87+
FakePlatform(operatingSystem: 'windows'),
88+
);
7189
expect(utils.which(kExecutable), isNull);
7290
});
7391

7492
testWithoutContext('returns exactly one result', () async {
75-
when(mockProcessManager.runSync(<String>['where', 'foo']))
76-
.thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
77-
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
93+
fakeProcessManager = FakeProcessManager.list(<FakeCommand>[
94+
const FakeCommand(
95+
command: <String>['where', kExecutable],
96+
stdout: '$kPath1\n$kPath2\n',
97+
),
98+
]);
99+
final OperatingSystemUtils utils = createOSUtils(
100+
FakePlatform(operatingSystem: 'windows'),
101+
);
78102
expect(utils.which(kExecutable).path, kPath1);
79103
});
80104

81105
testWithoutContext('returns all results for whichAll', () async {
82-
when(mockProcessManager.runSync(<String>['where', kExecutable]))
83-
.thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
84-
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
106+
fakeProcessManager = FakeProcessManager.list(<FakeCommand>[
107+
const FakeCommand(
108+
command: <String>['where', kExecutable],
109+
stdout: '$kPath1\n$kPath2\n',
110+
),
111+
]);
112+
final OperatingSystemUtils utils = createOSUtils(
113+
FakePlatform(operatingSystem: 'windows'),
114+
);
85115
final List<File> result = utils.whichAll(kExecutable);
86116
expect(result, hasLength(2));
87117
expect(result[0].path, kPath1);
88118
expect(result[1].path, kPath2);
89119
});
90120
});
121+
122+
group('name', () {
123+
testWithoutContext('on Linux', () {
124+
final FakePlatform platform = FakePlatform(
125+
operatingSystem: 'linux',
126+
operatingSystemVersion: 'Linux 5.2.17-amd64 '
127+
'#1 SMP Debian 5.2.17 (2019-10-21 > 2018)',
128+
);
129+
final OperatingSystemUtils utils = createOSUtils(platform);
130+
expect(utils.name, 'Linux 5.2.17-amd64');
131+
});
132+
133+
testWithoutContext('on Mac', () {
134+
fakeProcessManager = FakeProcessManager.list(<FakeCommand>[
135+
const FakeCommand(
136+
command: <String>['sw_vers', '-productName'],
137+
stdout: 'Mac OS X\n',
138+
),
139+
const FakeCommand(
140+
command: <String>['sw_vers', '-productVersion'],
141+
stdout: '10.14.6\n',
142+
),
143+
const FakeCommand(
144+
command: <String>['sw_vers', '-buildVersion'],
145+
stdout: '16G2128\n',
146+
),
147+
]);
148+
final FakePlatform platform = FakePlatform(
149+
operatingSystem: 'macos',
150+
);
151+
final OperatingSystemUtils utils = createOSUtils(platform);
152+
expect(utils.name, 'Mac OS X 10.14.6 16G2128');
153+
});
154+
155+
testWithoutContext('on Windows', () {
156+
fakeProcessManager = FakeProcessManager.list(<FakeCommand>[
157+
const FakeCommand(
158+
command: <String>['ver'],
159+
stdout: 'Microsoft Windows [Version 10.0.17763.740]',
160+
),
161+
]);
162+
final FakePlatform platform = FakePlatform(
163+
operatingSystem: 'windows',
164+
);
165+
final OperatingSystemUtils utils = createOSUtils(platform);
166+
expect(utils.name, 'Microsoft Windows [Version 10.0.17763.740]');
167+
});
168+
});
169+
170+
group('makeExecutable', () {
171+
Directory tempDir;
172+
173+
setUp(() {
174+
tempDir = globals.fs.systemTempDirectory.createTempSync(
175+
'flutter_tools_os_utils_test.',
176+
);
177+
});
178+
179+
tearDown(() {
180+
tryToDelete(tempDir);
181+
});
182+
183+
testUsingContext('makeExecutable', () async {
184+
final File file = globals.fs.file(globals.fs.path.join(
185+
tempDir.path,
186+
'foo.script',
187+
));
188+
file.writeAsStringSync('hello world');
189+
globals.os.makeExecutable(file);
190+
191+
final String mode = file.statSync().modeString();
192+
// rwxr--r--
193+
expect(mode.substring(0, 3), endsWith('x'));
194+
}, overrides: <Type, Generator>{
195+
OperatingSystemUtils: () => OperatingSystemUtils(
196+
fileSystem: globals.fs,
197+
logger: globals.logger,
198+
platform: globals.platform,
199+
processManager: globals.processManager,
200+
),
201+
}, skip: const LocalPlatform().isWindows);
202+
});
91203
}
92204

93205
class MockProcessManager extends Mock implements ProcessManager {}

packages/flutter_tools/test/general.shard/base/os_utils_test.dart

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)