Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions extension/src/fileSystem/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Copy link
Contributor

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.

Copy link
Contributor Author

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.

const mockPlotYamlContent = [
'',
'- simple_plot:',
' template: simple',
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 (x: recall in this example) which doesn't always tell you the plots actual indent level.

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', () => {
Expand Down
18 changes: 12 additions & 6 deletions extension/src/fileSystem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) => {
Expand Down