From c28ef62f070afd103e7167ddae21cf04b35bfb7d Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Tue, 12 Feb 2019 02:33:29 -0800 Subject: [PATCH] Import and export Logs Summary: This diff adds import and export support for the logs plugin Reviewed By: danielbuechele Differential Revision: D13958713 fbshipit-source-id: a072de025f0b959302175ef0ccaf763ca41377e9 --- src/devices/ArchivedDevice.js | 20 +++++++-- src/devices/BaseDevice.js | 2 + src/utils/__tests__/exportData.electron.js | 50 +++++++++++++++++++--- src/utils/exportData.js | 6 +++ 4 files changed, 69 insertions(+), 9 deletions(-) diff --git a/src/devices/ArchivedDevice.js b/src/devices/ArchivedDevice.js index 11577a96a..88155f4f5 100644 --- a/src/devices/ArchivedDevice.js +++ b/src/devices/ArchivedDevice.js @@ -5,16 +5,30 @@ * @format */ import BaseDevice from './BaseDevice.js'; -import type {DeviceType, OS, DeviceShell} from './BaseDevice.js'; +import type { + DeviceType, + OS, + DeviceShell, + DeviceLogEntry, +} from './BaseDevice.js'; export default class ArchivedDevice extends BaseDevice { - constructor(serial: string, deviceType: DeviceType, title: string, os: OS) { + constructor( + serial: string, + deviceType: DeviceType, + title: string, + os: OS, + logEntries: Array, + ) { super(serial, deviceType, title); this.os = os; + this.logs = logEntries; } + logs: Array; + getLogs() { - return []; + return this.logs; } spawnShell(): ?DeviceShell { diff --git a/src/devices/BaseDevice.js b/src/devices/BaseDevice.js index 3503471e0..f3ccbb1e3 100644 --- a/src/devices/BaseDevice.js +++ b/src/devices/BaseDevice.js @@ -41,6 +41,7 @@ export type DeviceExport = {| title: string, deviceType: DeviceType, serial: string, + logs: Array, |}; export type OS = 'iOS' | 'Android' | 'Windows'; @@ -80,6 +81,7 @@ export default class BaseDevice { title: this.title, deviceType: this.deviceType, serial: this.serial, + logs: this.getLogs(), }; } diff --git a/src/utils/__tests__/exportData.electron.js b/src/utils/__tests__/exportData.electron.js index 36920a73b..f6e9a9e90 100644 --- a/src/utils/__tests__/exportData.electron.js +++ b/src/utils/__tests__/exportData.electron.js @@ -64,7 +64,13 @@ function generateClientFromDevice( } test('test generateClientIndentifierWithSalt helper function', () => { - const device = new ArchivedDevice('serial', 'emulator', 'TestiPhone', 'iOS'); + const device = new ArchivedDevice( + 'serial', + 'emulator', + 'TestiPhone', + 'iOS', + [], + ); const identifier = generateClientIdentifier(device, 'app'); const saltIdentifier = generateClientIdentifierWithSalt(identifier, 'salt'); expect(saltIdentifier).toEqual('app#iOS#emulator#salt-serial'); @@ -72,7 +78,13 @@ test('test generateClientIndentifierWithSalt helper function', () => { }); test('test generateClientFromClientWithSalt helper function', () => { - const device = new ArchivedDevice('serial', 'emulator', 'TestiPhone', 'iOS'); + const device = new ArchivedDevice( + 'serial', + 'emulator', + 'TestiPhone', + 'iOS', + [], + ); const client = generateClientFromDevice(device, 'app'); const saltedClient = generateClientFromClientWithSalt(client, 'salt'); expect(saltedClient).toEqual({ @@ -96,7 +108,13 @@ test('test generateClientFromClientWithSalt helper function', () => { }); test('test generateClientFromDevice helper function', () => { - const device = new ArchivedDevice('serial', 'emulator', 'TestiPhone', 'iOS'); + const device = new ArchivedDevice( + 'serial', + 'emulator', + 'TestiPhone', + 'iOS', + [], + ); const client = generateClientFromDevice(device, 'app'); expect(client).toEqual({ id: 'app#iOS#emulator#serial', @@ -110,7 +128,13 @@ test('test generateClientFromDevice helper function', () => { }); test('test generateClientIdentifier helper function', () => { - const device = new ArchivedDevice('serial', 'emulator', 'TestiPhone', 'iOS'); + const device = new ArchivedDevice( + 'serial', + 'emulator', + 'TestiPhone', + 'iOS', + [], + ); const identifier = generateClientIdentifier(device, 'app'); expect(identifier).toEqual('app#iOS#emulator#serial'); }); @@ -133,7 +157,7 @@ test('test processStore function for empty state', () => { test('test processStore function for an iOS device connected', () => { const json = processStore( [], - new ArchivedDevice('serial', 'emulator', 'TestiPhone', 'iOS'), + new ArchivedDevice('serial', 'emulator', 'TestiPhone', 'iOS', []), {}, [], new Map(), @@ -157,7 +181,13 @@ test('test processStore function for an iOS device connected', () => { }); test('test processStore function for an iOS device connected with client plugin data', () => { - const device = new ArchivedDevice('serial', 'emulator', 'TestiPhone', 'iOS'); + const device = new ArchivedDevice( + 'serial', + 'emulator', + 'TestiPhone', + 'iOS', + [], + ); const clientIdentifier = generateClientIdentifier(device, 'testapp'); const json = processStore( [], @@ -184,12 +214,14 @@ test('test processStore function to have only the client for the selected device 'emulator', 'TestiPhone', 'iOS', + [], ); const unselectedDevice = new ArchivedDevice( 'identifier', 'emulator', 'TestiPhone', 'iOS', + [], ); const unselectedDeviceClientIdentifier = generateClientIdentifier( @@ -247,6 +279,7 @@ test('test processStore function to have multiple clients for the selected devic 'emulator', 'TestiPhone', 'iOS', + [], ); const clientIdentifierApp1 = generateClientIdentifier( @@ -308,6 +341,7 @@ test('test processStore function for device plugin state and no clients', () => 'emulator', 'TestiPhone', 'iOS', + [], ); const json = processStore( [], @@ -340,6 +374,7 @@ test('test processStore function for unselected device plugin state and no clien 'emulator', 'TestiPhone', 'iOS', + [], ); const json = processStore( [], @@ -369,6 +404,7 @@ test('test processStore function for notifications for selected device', () => { 'emulator', 'TestiPhone', 'iOS', + [], ); const client = generateClientFromDevice(selectedDevice, 'testapp1'); const notification = generateNotifications( @@ -416,12 +452,14 @@ test('test processStore function for notifications for unselected device', () => 'emulator', 'TestiPhone', 'iOS', + [], ); const unselectedDevice = new ArchivedDevice( 'identifier', 'emulator', 'TestiPhone', 'iOS', + [], ); const client = generateClientFromDevice(selectedDevice, 'testapp1'); diff --git a/src/utils/exportData.js b/src/utils/exportData.js index f4e584741..ccf7d44fb 100644 --- a/src/utils/exportData.js +++ b/src/utils/exportData.js @@ -93,6 +93,7 @@ const addSaltToDeviceSerial = ( device.deviceType, device.title, device.os, + device.getLogs(), ); const updatedClients = clients.map((client: ClientExport) => { return { @@ -215,11 +216,16 @@ export const importFileToStore = (file: string, store: Store) => { } const json = JSON.parse(data); const {device, clients} = json; + const updatedLogs = device.logs.map(log => { + // During the export, Date is exported as string + return {...log, date: new Date(log.date)}; + }); const archivedDevice = new ArchivedDevice( device.serial, device.deviceType, device.title, device.os, + updatedLogs, ); store.dispatch({ type: 'REGISTER_DEVICE',