Test: add yaml (and other formats) parsing tests (#3586)

This commit is contained in:
Erez Rokah 2020-04-12 19:40:44 +03:00 committed by GitHub
parent 625a9980f1
commit 39e00261a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 469 additions and 328 deletions

View File

@ -1,5 +1,6 @@
import { fromJS } from 'immutable'; import { fromJS } from 'immutable';
import { applyDefaults, detectProxyServer, handleLocalBackend } from '../config'; import { stripIndent } from 'common-tags';
import { parseConfig, applyDefaults, detectProxyServer, handleLocalBackend } from '../config';
jest.spyOn(console, 'log').mockImplementation(() => {}); jest.spyOn(console, 'log').mockImplementation(() => {});
jest.mock('coreSrc/backend', () => { jest.mock('coreSrc/backend', () => {
@ -9,6 +10,82 @@ jest.mock('coreSrc/backend', () => {
}); });
describe('config', () => { describe('config', () => {
describe('parseConfig', () => {
it('can parse simple yaml config', () => {
const file = stripIndent`
backend:
name: test-repo
media_folder: static/images
`;
expect(parseConfig(file)).toEqual({
backend: { name: 'test-repo' },
media_folder: 'static/images',
});
});
it('should merge yaml aliases', () => {
const file = stripIndent`
backend:
name: github
repo: netlify/netlify-cms
squash_merges: true
open_authoring: true
local_backend: true
site_url: https://www.netlifycms.org
publish_mode: editorial_workflow
media_folder: website/static/img
public_folder: img
docs_collection: &docs_collection
folder: website/content/docs
create: true
preview_path: 'docs/{{slug}}'
fields:
- { label: Title, name: title }
- { label: Body, name: body, widget: markdown }
collections:
- <<: *docs_collection
name: docs_start
label: 'Docs: Quick Start'
`;
expect(parseConfig(file)).toEqual({
backend: {
name: 'github',
repo: 'netlify/netlify-cms',
squash_merges: true,
open_authoring: true,
},
local_backend: true,
site_url: 'https://www.netlifycms.org',
publish_mode: 'editorial_workflow',
media_folder: 'website/static/img',
public_folder: 'img',
docs_collection: {
folder: 'website/content/docs',
create: true,
preview_path: 'docs/{{slug}}',
fields: [
{ label: 'Title', name: 'title' },
{ label: 'Body', name: 'body', widget: 'markdown' },
],
},
collections: [
{
folder: 'website/content/docs',
create: true,
preview_path: 'docs/{{slug}}',
fields: [
{ label: 'Title', name: 'title' },
{ label: 'Body', name: 'body', widget: 'markdown' },
],
name: 'docs_start',
label: 'Docs: Quick Start',
},
],
});
});
});
describe('applyDefaults', () => { describe('applyDefaults', () => {
describe('publish_mode', () => { describe('publish_mode', () => {
it('should set publish_mode if not set', () => { it('should set publish_mode if not set', () => {

View File

@ -118,7 +118,7 @@ function mergePreloadedConfig(preloadedConfig, loadedConfig) {
return preloadedConfig ? preloadedConfig.mergeDeep(map) : map; return preloadedConfig ? preloadedConfig.mergeDeep(map) : map;
} }
function parseConfig(data) { export function parseConfig(data) {
const config = yaml.parse(data, { maxAliasCount: -1, prettyErrors: true, merge: true }); const config = yaml.parse(data, { maxAliasCount: -1, prettyErrors: true, merge: true });
if (typeof CMS_ENV === 'string' && config[CMS_ENV]) { if (typeof CMS_ENV === 'string' && config[CMS_ENV]) {
Object.keys(config[CMS_ENV]).forEach(key => { Object.keys(config[CMS_ENV]).forEach(key => {

View File

@ -6,348 +6,412 @@ import {
} from '../frontmatter'; } from '../frontmatter';
describe('Frontmatter', () => { describe('Frontmatter', () => {
it('should parse YAML with --- delimiters', () => { describe('yaml', () => {
expect( it('should parse YAML with --- delimiters', () => {
FrontmatterInfer.fromFile('---\ntitle: YAML\ndescription: Something longer\n---\nContent'), expect(
).toEqual({ FrontmatterInfer.fromFile('---\ntitle: YAML\ndescription: Something longer\n---\nContent'),
title: 'YAML', ).toEqual({
description: 'Something longer',
body: 'Content',
});
});
it('should parse YAML with --- delimiters when it is explicitly set as the format without a custom delimiter', () => {
expect(
frontmatterYAML().fromFile('---\ntitle: YAML\ndescription: Something longer\n---\nContent'),
).toEqual({
title: 'YAML',
description: 'Something longer',
body: 'Content',
});
});
it('should parse YAML with custom delimiters when it is explicitly set as the format with a custom delimiter', () => {
expect(
frontmatterYAML('~~~').fromFile(
'~~~\ntitle: YAML\ndescription: Something longer\n~~~\nContent',
),
).toEqual({
title: 'YAML',
description: 'Something longer',
body: 'Content',
});
});
it('should parse YAML with custom delimiters when it is explicitly set as the format with different custom delimiters', () => {
expect(
frontmatterYAML(['~~~', '^^^']).fromFile(
'~~~\ntitle: YAML\ndescription: Something longer\n^^^\nContent',
),
).toEqual({
title: 'YAML',
description: 'Something longer',
body: 'Content',
});
});
it('should parse YAML with ---yaml delimiters', () => {
expect(
FrontmatterInfer.fromFile(
'---yaml\ntitle: YAML\ndescription: Something longer\n---\nContent',
),
).toEqual({
title: 'YAML',
description: 'Something longer',
body: 'Content',
});
});
it('should overwrite any body param in the front matter', () => {
expect(
FrontmatterInfer.fromFile('---\ntitle: The Title\nbody: Something longer\n---\nContent'),
).toEqual({
title: 'The Title',
body: 'Content',
});
});
it('should parse TOML with +++ delimiters', () => {
expect(
FrontmatterInfer.fromFile('+++\ntitle = "TOML"\ndescription = "Front matter"\n+++\nContent'),
).toEqual({
title: 'TOML',
description: 'Front matter',
body: 'Content',
});
});
it('should parse TOML with 0.5 style dates', () => {
expect(
FrontmatterInfer.fromFile('+++\ntitle = "TOML"\ndate = 2018-12-24\n+++\nContent'),
).toEqual({
title: 'TOML',
date: new Date('2018-12-24T00:00:00.000Z'),
body: 'Content',
});
});
it('should parse TOML with +++ delimiters when it is explicitly set as the format without a custom delimiter', () => {
expect(
frontmatterTOML('~~~').fromFile(
'~~~\ntitle = "TOML"\ndescription = "Front matter"\n~~~\nContent',
),
).toEqual({
title: 'TOML',
description: 'Front matter',
body: 'Content',
});
});
it('should parse TOML with ---toml delimiters', () => {
expect(
FrontmatterInfer.fromFile(
'---toml\ntitle = "TOML"\ndescription = "Something longer"\n---\nContent',
),
).toEqual({
title: 'TOML',
description: 'Something longer',
body: 'Content',
});
});
it('should parse JSON with { } delimiters', () => {
expect(
FrontmatterInfer.fromFile(
'{\n"title": "The Title",\n"description": "Something longer"\n}\nContent',
),
).toEqual({
title: 'The Title',
description: 'Something longer',
body: 'Content',
});
});
it('should parse JSON with { } delimiters when it is explicitly set as the format without a custom delimiter', () => {
expect(
frontmatterJSON().fromFile(
'{\n"title": "The Title",\n"description": "Something longer"\n}\nContent',
),
).toEqual({
title: 'The Title',
description: 'Something longer',
body: 'Content',
});
});
it('should parse JSON with { } delimiters when it is explicitly set as the format with a custom delimiter', () => {
expect(
frontmatterJSON('~~~').fromFile(
'~~~\n"title": "The Title",\n"description": "Something longer"\n~~~\nContent',
),
).toEqual({
title: 'The Title',
description: 'Something longer',
body: 'Content',
});
});
it('should parse JSON with ---json delimiters', () => {
expect(
FrontmatterInfer.fromFile(
'---json\n{\n"title": "The Title",\n"description": "Something longer"\n}\n---\nContent',
),
).toEqual({
title: 'The Title',
description: 'Something longer',
body: 'Content',
});
});
it('should stringify YAML with --- delimiters', () => {
expect(
FrontmatterInfer.toFile({
body: 'Some content\nOn another line',
tags: ['front matter', 'yaml'],
title: 'YAML', title: 'YAML',
}), description: 'Something longer',
).toEqual( body: 'Content',
[ });
'---', });
'tags:',
' - front matter',
' - yaml',
'title: YAML',
'---',
'Some content',
'On another line',
].join('\n'),
);
});
it('should stringify YAML with missing body', () => { it('should parse YAML with --- delimiters when it is explicitly set as the format without a custom delimiter', () => {
expect(FrontmatterInfer.toFile({ tags: ['front matter', 'yaml'], title: 'YAML' })).toEqual( expect(
['---', 'tags:', ' - front matter', ' - yaml', 'title: YAML', '---', ''].join('\n'), frontmatterYAML().fromFile('---\ntitle: YAML\ndescription: Something longer\n---\nContent'),
); ).toEqual({
});
it('should stringify YAML with --- delimiters when it is explicitly set as the format without a custom delimiter', () => {
expect(
frontmatterYAML().toFile({
body: 'Some content\nOn another line',
tags: ['front matter', 'yaml'],
title: 'YAML', title: 'YAML',
}), description: 'Something longer',
).toEqual( body: 'Content',
[ });
'---', });
'tags:',
' - front matter',
' - yaml',
'title: YAML',
'---',
'Some content',
'On another line',
].join('\n'),
);
});
it('should stringify YAML with --- delimiters when it is explicitly set as the format with a custom delimiter', () => { it('should parse YAML with custom delimiters when it is explicitly set as the format with a custom delimiter', () => {
expect( expect(
frontmatterYAML('~~~').toFile({ frontmatterYAML('~~~').fromFile(
body: 'Some content\nOn another line', '~~~\ntitle: YAML\ndescription: Something longer\n~~~\nContent',
tags: ['front matter', 'yaml'], ),
).toEqual({
title: 'YAML', title: 'YAML',
}), description: 'Something longer',
).toEqual( body: 'Content',
[ });
'~~~', });
'tags:',
' - front matter',
' - yaml',
'title: YAML',
'~~~',
'Some content',
'On another line',
].join('\n'),
);
});
it('should stringify YAML with --- delimiters when it is explicitly set as the format with different custom delimiters', () => { it('should parse YAML with custom delimiters when it is explicitly set as the format with different custom delimiters', () => {
expect( expect(
frontmatterYAML(['~~~', '^^^']).toFile({ frontmatterYAML(['~~~', '^^^']).fromFile(
body: 'Some content\nOn another line', '~~~\ntitle: YAML\ndescription: Something longer\n^^^\nContent',
tags: ['front matter', 'yaml'], ),
).toEqual({
title: 'YAML', title: 'YAML',
}), description: 'Something longer',
).toEqual( body: 'Content',
[ });
'~~~', });
'tags:',
' - front matter', it('should parse YAML with ---yaml delimiters', () => {
' - yaml', expect(
'title: YAML', FrontmatterInfer.fromFile(
'^^^', '---yaml\ntitle: YAML\ndescription: Something longer\n---\nContent',
'Some content', ),
'On another line', ).toEqual({
].join('\n'), title: 'YAML',
); description: 'Something longer',
body: 'Content',
});
});
it('should overwrite any body param in the front matter', () => {
expect(
FrontmatterInfer.fromFile('---\ntitle: The Title\nbody: Something longer\n---\nContent'),
).toEqual({
title: 'The Title',
body: 'Content',
});
});
it('should stringify YAML with --- delimiters', () => {
expect(
FrontmatterInfer.toFile({
body: 'Some content\nOn another line',
tags: ['front matter', 'yaml'],
title: 'YAML',
}),
).toEqual(
[
'---',
'tags:',
' - front matter',
' - yaml',
'title: YAML',
'---',
'Some content',
'On another line',
].join('\n'),
);
});
it('should stringify YAML with missing body', () => {
expect(FrontmatterInfer.toFile({ tags: ['front matter', 'yaml'], title: 'YAML' })).toEqual(
['---', 'tags:', ' - front matter', ' - yaml', 'title: YAML', '---', ''].join('\n'),
);
});
it('should stringify YAML with --- delimiters when it is explicitly set as the format without a custom delimiter', () => {
expect(
frontmatterYAML().toFile({
body: 'Some content\nOn another line',
tags: ['front matter', 'yaml'],
title: 'YAML',
}),
).toEqual(
[
'---',
'tags:',
' - front matter',
' - yaml',
'title: YAML',
'---',
'Some content',
'On another line',
].join('\n'),
);
});
it('should stringify YAML with --- delimiters when it is explicitly set as the format with a custom delimiter', () => {
expect(
frontmatterYAML('~~~').toFile({
body: 'Some content\nOn another line',
tags: ['front matter', 'yaml'],
title: 'YAML',
}),
).toEqual(
[
'~~~',
'tags:',
' - front matter',
' - yaml',
'title: YAML',
'~~~',
'Some content',
'On another line',
].join('\n'),
);
});
it('should stringify YAML with --- delimiters when it is explicitly set as the format with different custom delimiters', () => {
expect(
frontmatterYAML(['~~~', '^^^']).toFile({
body: 'Some content\nOn another line',
tags: ['front matter', 'yaml'],
title: 'YAML',
}),
).toEqual(
[
'~~~',
'tags:',
' - front matter',
' - yaml',
'title: YAML',
'^^^',
'Some content',
'On another line',
].join('\n'),
);
});
it('should trim last line break if added by grey-matter', () => {
expect(
frontmatterYAML().toFile({
body: 'noLineBreak',
}),
).toEqual('noLineBreak');
});
it('should not trim last line break if not added by grey-matter', () => {
expect(
frontmatterYAML().toFile({
body: 'withLineBreak\n',
}),
).toEqual('withLineBreak\n');
});
it('should keep field types', () => {
const frontmatter = frontmatterYAML();
const file = frontmatter.toFile({
number: 1,
string: 'Hello World!',
date: new Date('2020-01-01'),
array: ['1', new Date('2020-01-01')],
body: 'Content',
});
expect(frontmatter.fromFile(file)).toEqual({
number: 1,
string: 'Hello World!',
date: new Date('2020-01-01'),
array: ['1', new Date('2020-01-01')],
body: 'Content',
});
});
}); });
it('should stringify TOML with +++ delimiters when it is explicitly set as the format without a custom delimiter', () => { describe('toml', () => {
expect( it('should parse TOML with +++ delimiters', () => {
frontmatterTOML().toFile({ expect(
body: 'Some content\nOn another line', FrontmatterInfer.fromFile(
tags: ['front matter', 'toml'], '+++\ntitle = "TOML"\ndescription = "Front matter"\n+++\nContent',
),
).toEqual({
title: 'TOML', title: 'TOML',
}), description: 'Front matter',
).toEqual( body: 'Content',
[ });
'+++', });
'tags = ["front matter", "toml"]',
'title = "TOML"',
'+++',
'Some content',
'On another line',
].join('\n'),
);
});
it('should stringify TOML with +++ delimiters when it is explicitly set as the format with a custom delimiter', () => { it('should parse TOML with 0.5 style dates', () => {
expect( expect(
frontmatterTOML('~~~').toFile({ FrontmatterInfer.fromFile('+++\ntitle = "TOML"\ndate = 2018-12-24\n+++\nContent'),
body: 'Some content\nOn another line', ).toEqual({
tags: ['front matter', 'toml'],
title: 'TOML', title: 'TOML',
}), date: new Date('2018-12-24T00:00:00.000Z'),
).toEqual( body: 'Content',
[ });
'~~~', });
'tags = ["front matter", "toml"]',
'title = "TOML"', it('should parse TOML with +++ delimiters when it is explicitly set as the format without a custom delimiter', () => {
'~~~', expect(
'Some content', frontmatterTOML('~~~').fromFile(
'On another line', '~~~\ntitle = "TOML"\ndescription = "Front matter"\n~~~\nContent',
].join('\n'), ),
); ).toEqual({
title: 'TOML',
description: 'Front matter',
body: 'Content',
});
});
it('should parse TOML with ---toml delimiters', () => {
expect(
FrontmatterInfer.fromFile(
'---toml\ntitle = "TOML"\ndescription = "Something longer"\n---\nContent',
),
).toEqual({
title: 'TOML',
description: 'Something longer',
body: 'Content',
});
});
it('should stringify TOML with +++ delimiters when it is explicitly set as the format without a custom delimiter', () => {
expect(
frontmatterTOML().toFile({
body: 'Some content\nOn another line',
tags: ['front matter', 'toml'],
title: 'TOML',
}),
).toEqual(
[
'+++',
'tags = ["front matter", "toml"]',
'title = "TOML"',
'+++',
'Some content',
'On another line',
].join('\n'),
);
});
it('should stringify TOML with +++ delimiters when it is explicitly set as the format with a custom delimiter', () => {
expect(
frontmatterTOML('~~~').toFile({
body: 'Some content\nOn another line',
tags: ['front matter', 'toml'],
title: 'TOML',
}),
).toEqual(
[
'~~~',
'tags = ["front matter", "toml"]',
'title = "TOML"',
'~~~',
'Some content',
'On another line',
].join('\n'),
);
});
it('should keep field types', () => {
const frontmatter = frontmatterTOML();
const file = frontmatter.toFile({
number: 1,
string: 'Hello World!',
date: new Date('2020-01-01'),
// in toml arrays must contain the same type
array: ['1', new Date('2020-01-01').toISOString()],
body: 'Content',
});
expect(frontmatter.fromFile(file)).toEqual({
number: 1,
string: 'Hello World!',
date: new Date('2020-01-01'),
array: ['1', new Date('2020-01-01').toISOString()],
body: 'Content',
});
});
}); });
it('should stringify JSON with { } delimiters when it is explicitly set as the format without a custom delimiter', () => { describe('json', () => {
expect( it('should parse JSON with { } delimiters', () => {
frontmatterJSON().toFile({ expect(
body: 'Some content\nOn another line', FrontmatterInfer.fromFile(
tags: ['front matter', 'json'], '{\n"title": "The Title",\n"description": "Something longer"\n}\nContent',
title: 'JSON', ),
}), ).toEqual({
).toEqual( title: 'The Title',
[ description: 'Something longer',
'{', body: 'Content',
'"tags": [', });
' "front matter",', });
' "json"',
' ],',
' "title": "JSON"',
'}',
'Some content',
'On another line',
].join('\n'),
);
});
it('should stringify JSON with { } delimiters when it is explicitly set as the format with a custom delimiter', () => { it('should parse JSON with { } delimiters when it is explicitly set as the format without a custom delimiter', () => {
expect( expect(
frontmatterJSON('~~~').toFile({ frontmatterJSON().fromFile(
body: 'Some content\nOn another line', '{\n"title": "The Title",\n"description": "Something longer"\n}\nContent',
tags: ['front matter', 'json'], ),
title: 'JSON', ).toEqual({
}), title: 'The Title',
).toEqual( description: 'Something longer',
[ body: 'Content',
'~~~', });
'"tags": [', });
' "front matter",',
' "json"',
' ],',
' "title": "JSON"',
'~~~',
'Some content',
'On another line',
].join('\n'),
);
});
it('should trim last line break if added by grey-matter', () => { it('should parse JSON with { } delimiters when it is explicitly set as the format with a custom delimiter', () => {
expect( expect(
frontmatterYAML().toFile({ frontmatterJSON('~~~').fromFile(
body: 'noLineBreak', '~~~\n"title": "The Title",\n"description": "Something longer"\n~~~\nContent',
}), ),
).toEqual('noLineBreak'); ).toEqual({
}); title: 'The Title',
description: 'Something longer',
body: 'Content',
});
});
it('should not trim last line break if not added by grey-matter', () => { it('should parse JSON with ---json delimiters', () => {
expect( expect(
frontmatterYAML().toFile({ FrontmatterInfer.fromFile(
body: 'withLineBreak\n', '---json\n{\n"title": "The Title",\n"description": "Something longer"\n}\n---\nContent',
}), ),
).toEqual('withLineBreak\n'); ).toEqual({
title: 'The Title',
description: 'Something longer',
body: 'Content',
});
});
it('should stringify JSON with { } delimiters when it is explicitly set as the format without a custom delimiter', () => {
expect(
frontmatterJSON().toFile({
body: 'Some content\nOn another line',
tags: ['front matter', 'json'],
title: 'JSON',
}),
).toEqual(
[
'{',
'"tags": [',
' "front matter",',
' "json"',
' ],',
' "title": "JSON"',
'}',
'Some content',
'On another line',
].join('\n'),
);
});
it('should stringify JSON with { } delimiters when it is explicitly set as the format with a custom delimiter', () => {
expect(
frontmatterJSON('~~~').toFile({
body: 'Some content\nOn another line',
tags: ['front matter', 'json'],
title: 'JSON',
}),
).toEqual(
[
'~~~',
'"tags": [',
' "front matter",',
' "json"',
' ],',
' "title": "JSON"',
'~~~',
'Some content',
'On another line',
].join('\n'),
);
});
it('should keep field types', () => {
const frontmatter = frontmatterJSON();
const file = frontmatter.toFile({
number: 1,
string: 'Hello World!',
// no way to represent date in JSON
date: new Date('2020-01-01').toISOString(),
array: ['1', new Date('2020-01-01').toISOString()],
body: 'Content',
});
expect(frontmatter.fromFile(file)).toEqual({
number: 1,
string: 'Hello World!',
date: new Date('2020-01-01').toISOString(),
array: ['1', new Date('2020-01-01').toISOString()],
body: 'Content',
});
});
}); });
}); });