Merge pull request #8853 from torchiaf/fix/8655

Test automation around YAML chomping
This commit is contained in:
Francesco Torchia 2023-05-16 10:34:02 +02:00 committed by GitHub
commit 646f2a1159
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 5 deletions

View File

@ -660,6 +660,7 @@ export default {
<CodeMirror
v-else-if="valueMarkdownMultiline"
ref="cm"
data-testid="code-mirror-multiline-field"
:class="{['focus']: codeMirrorFocus[i]}"
:value="row[valueName]"
:as-text-area="true"

View File

@ -15,4 +15,21 @@ describe('component: KeyValue', () => {
expect(inputValue.value).toBe(value);
});
it('should display a markdown-multiline field with new lines visible', () => {
const wrapper = mount(KeyValue, {
propsData: {
value: 'test',
valueMarkdownMultiline: true,
},
mocks: { $store: { getters: { 'i18n/t': jest.fn() } } },
directives: { t }
});
const inputFieldTextArea = wrapper.find('textarea').element;
const inputFieldMultiline = wrapper.find('[data-testid="code-mirror-multiline-field"]').element;
expect(inputFieldTextArea).toBeUndefined();
expect(inputFieldMultiline).toBeDefined();
});
});

View File

@ -0,0 +1,63 @@
import {
getBlockDescriptor,
dumpBlock,
} from '@shell/utils/create-yaml';
const key = 'example';
const randomData = '\n foo\n bar\n';
const scalarStyles = ['>', '|'];
const chomping = ['+', '-', ''];
const indentations = ['4', '2', ''];
describe('fx: getBlockDescriptor', () => {
describe('should parse blocks header for all block indicators combo', () => {
scalarStyles.forEach((scalar) => {
chomping.forEach((chomping) => {
indentations.forEach((indentation) => {
const combo = `${ scalar }${ indentation }${ chomping }`;
it(`combo: ${ combo }`, () => {
const toParse = `${ key }: ${ combo }${ randomData }`;
const desc = getBlockDescriptor(toParse, key);
expect(desc?.header).toBe(`${ key }: ${ combo }`);
expect(desc?.indentation).toBe(indentation);
});
});
});
});
});
});
describe('fx: dumpBlock', () => {
describe('should create a data block replacing indicators with blocks indicator from options', () => {
const key = 'example';
scalarStyles.forEach((scalarStyle) => {
chomping.forEach((chomping) => {
const options = {
[key]: {
chomping,
scalarStyle
}
};
it(`options: { scalarStyle: ${ scalarStyle }, chomping: ${ chomping } } with indentation`, () => {
const data = { [key]: ' foo \n bar \n \n foo\n bar\n ' };
const block = dumpBlock(data, options);
expect(block.includes(`example: ${ scalarStyle }${ chomping }2`)).toBeTruthy();
});
it(`options: { scalarStyle: ${ scalarStyle }, chomping: ${ chomping } } without indentation`, () => {
const data = { [key]: 'foo \nbar \n\nfoo\nbar\n ' };
const block = dumpBlock(data, options);
expect(block.includes(`example: ${ scalarStyle }${ chomping }`)).toBeTruthy();
});
});
});
});
});

View File

@ -188,6 +188,10 @@ export function uniq<T>(ary: T[]): T[] {
return out;
}
export function concatStrings(a: string[], b: string[]) {
return [...a.map(aa => b.map(bb => aa.concat(bb)))].reduce((acc, arr) => [...arr, ...acc], []);
}
interface KubeResource { metadata: { labels: { [name: string]: string} } } // Migrate to central kube types resource when those are brought in
export function getUniqueLabelKeys<T extends KubeResource>(aryResources: T[]): string[] {
const uniqueObj = aryResources.reduce((res, r) => {

View File

@ -370,7 +370,7 @@ function serializeSimpleValue(data) {
return jsyaml.dump(data).trim();
}
function getBlockDescriptor(value, key) {
export function getBlockDescriptor(value, key) {
const header = getBlockHeader(value, key);
return {
@ -451,7 +451,7 @@ export function saferDump(obj) {
* one of '|', '>'
* default '|'
* - chomping:
* one of: null, '-', '+'
* one of: null, '', '-', '+'
* default: null
* @returns the result of jsyaml.dump with the addition of multiline indicators
*/
@ -464,15 +464,15 @@ export function dumpBlock(data, options = {}) {
if (blockFields.length) {
for (const key of blockFields) {
const { header, indentation } = getBlockDescriptor(out, key);
const scalarStyle = options[key]?.scalarStyle ?? '|';
const chomping = options[key]?.chomping ?? '';
const desc = getBlockDescriptor(out, key);
/**
* Replace the original block indicators with the ones provided in the options param
*/
out = out.replace(desc.header, `${ key }: ${ scalarStyle }${ chomping }${ desc.indentation }`);
out = out.replace(header, `${ key }: ${ scalarStyle }${ chomping }${ indentation }`);
}
}