Skip to content

Commit

Permalink
[kbnArchiver] handle archives which have \r\n (#102118)
Browse files Browse the repository at this point in the history
Co-authored-by: spalger <spalger@users.noreply.github.com>
  • Loading branch information
Spencer and spalger authored Jun 14, 2021
1 parent 2dbf680 commit 7cee2ee
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { parseArchive } from './parse_archive';

jest.mock('fs/promises', () => ({
readFile: jest.fn(),
}));

const mockReadFile = jest.requireMock('fs/promises').readFile;

beforeEach(() => {
jest.clearAllMocks();
});

it('parses archives with \\n', async () => {
mockReadFile.mockResolvedValue(
`{
"foo": "abc"
}\n\n{
"foo": "xyz"
}`
);

const archive = await parseArchive('mock');
expect(archive).toMatchInlineSnapshot(`
Array [
Object {
"foo": "abc",
},
Object {
"foo": "xyz",
},
]
`);
});

it('parses archives with \\r\\n', async () => {
mockReadFile.mockResolvedValue(
`{
"foo": "123"
}\r\n\r\n{
"foo": "456"
}`
);

const archive = await parseArchive('mock');
expect(archive).toMatchInlineSnapshot(`
Array [
Object {
"foo": "123",
},
Object {
"foo": "456",
},
]
`);
});
22 changes: 22 additions & 0 deletions packages/kbn-test/src/kbn_client/import_export/parse_archive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import Fs from 'fs/promises';

export interface SavedObject {
id: string;
type: string;
[key: string]: unknown;
}

export async function parseArchive(path: string): Promise<SavedObject[]> {
return (await Fs.readFile(path, 'utf-8'))
.split(/\r?\n\r?\n/)
.filter((line) => !!line)
.map((line) => JSON.parse(line));
}
15 changes: 1 addition & 14 deletions packages/kbn-test/src/kbn_client/kbn_client_import_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,12 @@ import { ToolingLog, isAxiosResponseError, createFailError, REPO_ROOT } from '@k

import { KbnClientRequester, uriencode, ReqOptions } from './kbn_client_requester';
import { KbnClientSavedObjects } from './kbn_client_saved_objects';
import { parseArchive } from './import_export/parse_archive';

interface ImportApiResponse {
success: boolean;
[key: string]: unknown;
}

interface SavedObject {
id: string;
type: string;
[key: string]: unknown;
}

async function parseArchive(path: string): Promise<SavedObject[]> {
return (await Fs.readFile(path, 'utf-8'))
.split('\n\n')
.filter((line) => !!line)
.map((line) => JSON.parse(line));
}

export class KbnClientImportExport {
constructor(
public readonly log: ToolingLog,
Expand Down

0 comments on commit 7cee2ee

Please sign in to comment.