Skip to content

Commit

Permalink
Merge pull request rancher#10168 from rak-phillip/bugfix/9841-configm…
Browse files Browse the repository at this point in the history
…ap-yaml

Prevent mutating yaml when a header isn't found
  • Loading branch information
rak-phillip authored Dec 18, 2023
2 parents 2e69430 + a9d7d4b commit 3bd3cd3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
22 changes: 22 additions & 0 deletions shell/utils/__tests__/create-yaml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,26 @@ skipGeometric=true`

expect(result).toStrictEqual(expectedResult);
});

it('should not attempt to replace indicators when a header cannot be found', () => {
const data = {
a: 'a\nb\tc',
b: 'a\nb\tc',
c: `a
b c`
};

const expectedResult = `a: "a\\nb\\tc"\nb: "a\\nb\\tc"\nc: |+\n a\n b c\n`;

const yamlModifiers = {
lineWidth: -1,
a: { chomping: '+' },
b: { chomping: '+' },
c: { chomping: '+' },
};

const result = dumpBlock(data, yamlModifiers);

expect(result).toStrictEqual(expectedResult);
});
});
4 changes: 3 additions & 1 deletion shell/utils/create-yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,9 @@ export function dumpBlock(data, options = {}) {
/**
* Replace the original block indicators with the ones provided in the options param
*/
out = out.replace(header, `${ key }: ${ scalarStyle }${ chomping }${ indentation }`);
if (header) {
out = out.replace(header, `${ key }: ${ scalarStyle }${ chomping }${ indentation }`);
}
}
}

Expand Down

0 comments on commit 3bd3cd3

Please sign in to comment.