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 { ...@@ -423,11 +423,18 @@ class FuchsiaDevice extends Device {
TargetPlatform _targetPlatform; TargetPlatform _targetPlatform;
Future<TargetPlatform> _queryTargetPlatform() async { 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'); final RunResult result = await shell('uname -m');
if (result.exitCode != 0) { if (result.exitCode != 0) {
globals.printError('Could not determine Fuchsia target platform type:\n$result\n' globals.printError('Could not determine Fuchsia target platform type:\n$result\n'
'Defaulting to arm64.'); 'Defaulting to arm64.');
return TargetPlatform.fuchsia_arm64; return defaultTargetPlatform;
} }
final String machine = result.stdout.trim(); final String machine = result.stdout.trim();
switch (machine) { switch (machine) {
...@@ -438,7 +445,7 @@ class FuchsiaDevice extends Device { ...@@ -438,7 +445,7 @@ class FuchsiaDevice extends Device {
default: default:
globals.printError('Unknown Fuchsia target platform "$machine". ' globals.printError('Unknown Fuchsia target platform "$machine". '
'Defaulting to arm64.'); 'Defaulting to arm64.');
return TargetPlatform.fuchsia_arm64; return defaultTargetPlatform;
} }
} }
...@@ -480,16 +487,22 @@ class FuchsiaDevice extends Device { ...@@ -480,16 +487,22 @@ class FuchsiaDevice extends Device {
@override @override
Future<String> get sdkNameAndVersion async { 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'; const String versionPath = '/pkgfs/packages/build-info/0/data/version';
final RunResult catResult = await shell('cat $versionPath'); final RunResult catResult = await shell('cat $versionPath');
if (catResult.exitCode != 0) { if (catResult.exitCode != 0) {
globals.printTrace('Failed to cat $versionPath: ${catResult.stderr}'); globals.printTrace('Failed to cat $versionPath: ${catResult.stderr}');
return 'Fuchsia'; return defaultName;
} }
final String version = catResult.stdout.trim(); final String version = catResult.stdout.trim();
if (version.isEmpty) { if (version.isEmpty) {
globals.printTrace('$versionPath was empty'); globals.printTrace('$versionPath was empty');
return 'Fuchsia'; return defaultName;
} }
return 'Fuchsia $version'; return 'Fuchsia $version';
} }
......
...@@ -156,4 +156,7 @@ class FuchsiaArtifacts { ...@@ -156,4 +156,7 @@ class FuchsiaArtifacts {
/// The pm tool. /// The pm tool.
final File pm; 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() { ...@@ -43,6 +43,7 @@ void main() {
setUp(() { setUp(() {
memoryFileSystem = MemoryFileSystem(); memoryFileSystem = MemoryFileSystem();
sshConfig = MockFile(); sshConfig = MockFile();
when(sshConfig.existsSync()).thenReturn(true);
when(sshConfig.absolute).thenReturn(sshConfig); when(sshConfig.absolute).thenReturn(sshConfig);
}); });
...@@ -115,6 +116,15 @@ void main() { ...@@ -115,6 +116,15 @@ void main() {
ProcessManager: () => FakeProcessManager.any(), 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 { testUsingContext('targetPlatform arm64 works', () async {
when(globals.processManager.run(any)).thenAnswer((Invocation _) async { when(globals.processManager.run(any)).thenAnswer((Invocation _) async {
return ProcessResult(1, 0, 'aarch64', ''); return ProcessResult(1, 0, 'aarch64', '');
...@@ -888,6 +898,7 @@ void main() { ...@@ -888,6 +898,7 @@ void main() {
setUp(() { setUp(() {
sshConfig = MockFile(); sshConfig = MockFile();
when(sshConfig.existsSync()).thenReturn(true);
when(sshConfig.absolute).thenReturn(sshConfig); when(sshConfig.absolute).thenReturn(sshConfig);
mockSuccessProcessManager = MockProcessManager(); mockSuccessProcessManager = MockProcessManager();
...@@ -915,6 +926,15 @@ void main() { ...@@ -915,6 +926,15 @@ void main() {
when<String>(emptyStdoutProcessResult.stderr as String).thenReturn(''); 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 { testUsingContext('returns what we get from the device on success', () async {
final FuchsiaDevice device = FuchsiaDevice('123'); final FuchsiaDevice device = FuchsiaDevice('123');
expect(await device.sdkNameAndVersion, equals('Fuchsia version')); 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