-
Notifications
You must be signed in to change notification settings - Fork 28
Fix "Add Plot" breaking dvc.yaml when there is no list indentation #5126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -828,6 +828,42 @@ describe('addPlotToDvcYamlFile', () => { | |
| mockDvcYamlContent + mockPlotYamlContent | ||
| ) | ||
| }) | ||
|
|
||
| it('should add a new plot when plot list items have no indent', () => { | ||
| const mockDvcYamlContent = [ | ||
| 'plots:', | ||
| '- eval/importance.png', | ||
| '- Precision-Recall:', | ||
| ' x: recall', | ||
| ' y:', | ||
| ' eval/prc/train.json: precision', | ||
| ' eval/prc/test.json: precision' | ||
| ].join('\n') | ||
| const mockPlotYamlContent = [ | ||
| '', | ||
| '- simple_plot:', | ||
| ' template: simple', | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can see in the demo (and these tests) that the new plot yaml still doesn't line up perfectly with the previous plots since we're grabbing the indent level from a nested item ( Since I'm not seeing an simple way to make sure it's all aligned correctly, I chose to aim for just ensuring that the new yaml is valid and doesn't break anything :) |
||
| ' x: epochs', | ||
| ' y:', | ||
| ' data.json: accuracy', | ||
| '' | ||
| ].join('\n') | ||
| mockedReadFileSync.mockReturnValueOnce(mockDvcYamlContent) | ||
| mockedReadFileSync.mockReturnValueOnce(mockDvcYamlContent) | ||
|
|
||
| addPlotToDvcYamlFile('/', { | ||
| template: 'simple', | ||
| title: 'simple_plot', | ||
| x: { 'data.json': ['epochs'] }, | ||
| y: { 'data.json': ['accuracy'] } | ||
| }) | ||
|
|
||
| expect(mockedOpenTextDocument).toHaveBeenCalledTimes(1) | ||
| expect(mockedWriteFileSync).toHaveBeenCalledWith( | ||
| '//dvc.yaml', | ||
| mockDvcYamlContent + mockPlotYamlContent | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('isPathInProject', () => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -250,14 +250,20 @@ const getPlotsYaml = (plotObj: PlotConfigData, indentSearchLines: string[]) => { | |
| const indentReg = /^( +)[^ ]/ | ||
| const indentLine = indentSearchLines.find(line => indentReg.test(line)) || '' | ||
| const spacesMatches = indentLine.match(indentReg) | ||
| const spaces = spacesMatches?.[1] | ||
| const spaces = spacesMatches?.[1].length || 2 | ||
|
|
||
| return yaml | ||
| .stringify( | ||
| { plots: [getPlotYamlObj(plotObj)] }, | ||
| { indent: spaces ? spaces.length : 2 } | ||
| ) | ||
| const newPlotLines = yaml | ||
| .stringify({ plots: [getPlotYamlObj(plotObj)] }, { indent: spaces }) | ||
| .split('\n') | ||
|
|
||
| const doesYamlListItemHaveNoIndent = indentSearchLines.find(line => | ||
| line.startsWith('-') | ||
| ) | ||
| return doesYamlListItemHaveNoIndent | ||
| ? newPlotLines.map(line => | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We check for if the lines have a list item with no indentation and trim our new plot lines to ensure that the new plot also has 0 indentation for its list item. |
||
| line.startsWith(' ') ? line.slice(spaces) : line | ||
| ) | ||
| : newPlotLines | ||
| } | ||
|
|
||
| export const addPlotToDvcYamlFile = (cwd: string, plotObj: PlotConfigData) => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use string literals to test for format (`) instead of creating lines and joining them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I prefer creating lines since I find it easier to keep track of the exact spacing on each line.