Unverified Commit 53dc8db0 authored by Dan Field's avatar Dan Field Committed by GitHub

Do not throw when trying to discover a fuchsia device and the sshConfig is invalid (#52858)

* Do not throw when trying to discover a fuchsia device and the sshConfig is invalid
parent 6c2a29dd
......@@ -423,11 +423,18 @@ class FuchsiaDevice extends Device {
TargetPlatform _targetPlatform;
Future<TargetPlatform> _queryTargetPlatform() async {
const TargetPlatform defaultTargetPlatform = TargetPlatform.fuchsia_arm64;
if (!globals.fuchsiaArtifacts.hasSshConfig) {
globals.printTrace('Could not determine Fuchsia target platform because '
'Fuchsia ssh configuration is missing.\n'
'Defaulting to arm64.');
return defaultTargetPlatform;
}
final RunResult result = await shell('uname -m');
if (result.exitCode != 0) {
globals.printError('Could not determine Fuchsia target platform type:\n$result\n'
'Defaulting to arm64.');
return TargetPlatform.fuchsia_arm64;
return defaultTargetPlatform;
}
final String machine = result.stdout.trim();
switch (machine) {
......@@ -438,7 +445,7 @@ class FuchsiaDevice extends Device {
default:
globals.printError('Unknown Fuchsia target platform "$machine". '
'Defaulting to arm64.');
return TargetPlatform.fuchsia_arm64;
return defaultTargetPlatform;
}
}
......@@ -480,16 +487,22 @@ class FuchsiaDevice extends Device {
@override
Future<String> get sdkNameAndVersion async {
const String defaultName = 'Fuchsia';
if (!globals.fuchsiaArtifacts.hasSshConfig) {
globals.printTrace('Could not determine Fuchsia sdk name or version '
'because Fuchsia ssh configuration is missing.');
return defaultName;
}
const String versionPath = '/pkgfs/packages/build-info/0/data/version';
final RunResult catResult = await shell('cat $versionPath');
if (catResult.exitCode != 0) {
globals.printTrace('Failed to cat $versionPath: ${catResult.stderr}');
return 'Fuchsia';
return defaultName;
}
final String version = catResult.stdout.trim();
if (version.isEmpty) {
globals.printTrace('$versionPath was empty');
return 'Fuchsia';
return defaultName;
}
return 'Fuchsia $version';
}
......
......@@ -156,4 +156,7 @@ class FuchsiaArtifacts {
/// The pm tool.
final File pm;
/// Returns true if the [sshConfig] file is not null and exists.
bool get hasSshConfig => sshConfig != null && sshConfig.existsSync();
}
......@@ -43,6 +43,7 @@ void main() {
setUp(() {
memoryFileSystem = MemoryFileSystem();
sshConfig = MockFile();
when(sshConfig.existsSync()).thenReturn(true);
when(sshConfig.absolute).thenReturn(sshConfig);
});
......@@ -115,6 +116,15 @@ void main() {
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('targetPlatform does not throw when sshConfig is missing', () async {
final FuchsiaDevice device = FuchsiaDevice('123');
expect(await device.targetPlatform, TargetPlatform.fuchsia_arm64);
}, overrides: <Type, Generator>{
FuchsiaArtifacts: () => FuchsiaArtifacts(sshConfig: null),
FuchsiaSdk: () => MockFuchsiaSdk(),
ProcessManager: () => MockProcessManager(),
});
testUsingContext('targetPlatform arm64 works', () async {
when(globals.processManager.run(any)).thenAnswer((Invocation _) async {
return ProcessResult(1, 0, 'aarch64', '');
......@@ -888,6 +898,7 @@ void main() {
setUp(() {
sshConfig = MockFile();
when(sshConfig.existsSync()).thenReturn(true);
when(sshConfig.absolute).thenReturn(sshConfig);
mockSuccessProcessManager = MockProcessManager();
......@@ -915,6 +926,15 @@ void main() {
when<String>(emptyStdoutProcessResult.stderr as String).thenReturn('');
});
testUsingContext('does not throw on non-existant ssh config', () async {
final FuchsiaDevice device = FuchsiaDevice('123');
expect(await device.sdkNameAndVersion, equals('Fuchsia'));
}, overrides: <Type, Generator>{
ProcessManager: () => mockSuccessProcessManager,
FuchsiaArtifacts: () => FuchsiaArtifacts(sshConfig: null),
FuchsiaSdk: () => MockFuchsiaSdk(),
});
testUsingContext('returns what we get from the device on success', () async {
final FuchsiaDevice device = FuchsiaDevice('123');
expect(await device.sdkNameAndVersion, equals('Fuchsia version'));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment