Skip to content

Commit 4a14be4

Browse files
authored
Remove check for stages in plot wizard (#4904)
1 parent b534cd7 commit 4a14be4

File tree

4 files changed

+98
-34
lines changed

4 files changed

+98
-34
lines changed

extension/src/fileSystem/index.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,29 @@ describe('addPlotToDvcYamlFile', () => {
640640
' y:',
641641
' data.json: accuracy'
642642
]
643+
644+
it('should add a plots list with the new plot if the dvc.yaml file does not exist', () => {
645+
const mockPlotYamlContent = ['', 'plots:', ...mockNewPlotLines, ''].join(
646+
'\n'
647+
)
648+
mockedReadFileSync.mockReturnValueOnce('')
649+
mockedReadFileSync.mockReturnValueOnce('')
650+
651+
addPlotToDvcYamlFile('/', {
652+
template: 'simple',
653+
title: 'Simple Plot',
654+
x: { 'data.json': ['epochs'] },
655+
y: { 'data.json': ['accuracy'] }
656+
})
657+
658+
expect(mockedEnsureFileSync).toHaveBeenCalledWith('//dvc.yaml')
659+
expect(mockedOpenTextDocument).toHaveBeenCalledTimes(1)
660+
expect(mockedWriteFileSync).toHaveBeenCalledWith(
661+
'//dvc.yaml',
662+
mockPlotYamlContent
663+
)
664+
})
665+
643666
it('should add a plots list with the new plot if the dvc.yaml file has no plots', () => {
644667
const mockDvcYamlContent = mockStagesLines.join('\n')
645668
const mockPlotYamlContent = ['', 'plots:', ...mockNewPlotLines, ''].join(
@@ -655,6 +678,7 @@ describe('addPlotToDvcYamlFile', () => {
655678
y: { 'data.json': ['accuracy'] }
656679
})
657680

681+
expect(mockedOpenTextDocument).toHaveBeenCalledTimes(1)
658682
expect(mockedWriteFileSync).toHaveBeenCalledWith(
659683
'//dvc.yaml',
660684
mockDvcYamlContent + mockPlotYamlContent
@@ -684,6 +708,7 @@ describe('addPlotToDvcYamlFile', () => {
684708
y: { 'acc.json': ['accuracy'] }
685709
})
686710

711+
expect(mockedOpenTextDocument).toHaveBeenCalledTimes(1)
687712
expect(mockedWriteFileSync).toHaveBeenCalledWith(
688713
'//dvc.yaml',
689714
mockDvcYamlContent + mockPlotYamlContent
@@ -714,6 +739,7 @@ describe('addPlotToDvcYamlFile', () => {
714739
y: { 'data.json': ['accuracy', 'epochs'] }
715740
})
716741

742+
expect(mockedOpenTextDocument).toHaveBeenCalledTimes(1)
717743
expect(mockedWriteFileSync).toHaveBeenCalledWith(
718744
'//dvc.yaml',
719745
mockDvcYamlContent + mockPlotYamlContent
@@ -735,6 +761,7 @@ describe('addPlotToDvcYamlFile', () => {
735761

736762
mockDvcYamlContent.splice(7, 0, ...mockPlotYamlContent)
737763

764+
expect(mockedOpenTextDocument).toHaveBeenCalledTimes(1)
738765
expect(mockedWriteFileSync).toHaveBeenCalledWith(
739766
'//dvc.yaml',
740767
mockDvcYamlContent.join('\n')
@@ -756,6 +783,7 @@ describe('addPlotToDvcYamlFile', () => {
756783
y: { 'data.json': ['accuracy'] }
757784
})
758785

786+
expect(mockedOpenTextDocument).toHaveBeenCalledTimes(1)
759787
expect(mockedWriteFileSync).toHaveBeenCalledWith(
760788
'//dvc.yaml',
761789
mockDvcYamlContent + mockPlotYamlContent
@@ -794,6 +822,7 @@ describe('addPlotToDvcYamlFile', () => {
794822
y: { 'data.json': ['accuracy'] }
795823
})
796824

825+
expect(mockedOpenTextDocument).toHaveBeenCalledTimes(1)
797826
expect(mockedWriteFileSync).toHaveBeenCalledWith(
798827
'//dvc.yaml',
799828
mockDvcYamlContent + mockPlotYamlContent

extension/src/fileSystem/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ const getPlotsYaml = (plotObj: PlotConfigData, indentSearchLines: string[]) => {
262262

263263
export const addPlotToDvcYamlFile = (cwd: string, plotObj: PlotConfigData) => {
264264
const dvcYamlFile = `${cwd}/dvc.yaml`
265+
ensureFileSync(dvcYamlFile)
265266
const dvcYamlDoc = loadYamlAsDoc(dvcYamlFile)
266267

267268
if (!dvcYamlDoc) {
@@ -276,8 +277,9 @@ export const addPlotToDvcYamlFile = (cwd: string, plotObj: PlotConfigData) => {
276277
if (!plots?.range) {
277278
const plotYaml = getPlotsYaml(plotObj, dvcYamlLines)
278279
dvcYamlLines.push(...plotYaml)
279-
writeFileSync(dvcYamlFile, dvcYamlLines.join('\n'))
280-
return
280+
281+
void openFileInEditor(dvcYamlFile)
282+
return writeFileSync(dvcYamlFile, dvcYamlLines.join('\n'))
281283
}
282284

283285
const plotsEndPos = lineCounter.linePos(plots.range[2]).line

extension/src/pipeline/index.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,28 +83,8 @@ export class Pipeline extends DeferredDisposable {
8383
}
8484

8585
public async getCwd() {
86-
const focusedPipeline = this.getFocusedPipeline()
87-
if (focusedPipeline) {
88-
return focusedPipeline
89-
}
90-
9186
await this.checkOrAddPipeline()
92-
93-
const pipelines = this.model.getPipelines()
94-
if (!pipelines?.size) {
95-
return
96-
}
97-
if (pipelines.has(this.dvcRoot)) {
98-
return this.dvcRoot
99-
}
100-
if (pipelines.size === 1) {
101-
return [...pipelines][0]
102-
}
103-
104-
return quickPickOne(
105-
[...pipelines],
106-
'Select a Pipeline to Run Command Against'
107-
)
87+
return this.findPipelineCwd()
10888
}
10989

11090
public async checkOrAddPipeline() {
@@ -158,11 +138,7 @@ export class Pipeline extends DeferredDisposable {
158138
}
159139

160140
public async addDataSeriesPlot() {
161-
const cwd = await this.getCwd()
162-
163-
if (!cwd) {
164-
return
165-
}
141+
const cwd = (await this.findPipelineCwd()) || this.dvcRoot
166142

167143
const plotConfiguration = await pickPlotConfiguration(cwd)
168144

@@ -177,6 +153,29 @@ export class Pipeline extends DeferredDisposable {
177153
return appendFileSync(join(this.dvcRoot, TEMP_DAG_FILE), '\n')
178154
}
179155

156+
private findPipelineCwd() {
157+
const focusedPipeline = this.getFocusedPipeline()
158+
if (focusedPipeline) {
159+
return focusedPipeline
160+
}
161+
162+
const pipelines = this.model.getPipelines()
163+
if (!pipelines?.size) {
164+
return
165+
}
166+
if (pipelines.has(this.dvcRoot)) {
167+
return this.dvcRoot
168+
}
169+
if (pipelines.size === 1) {
170+
return [...pipelines][0]
171+
}
172+
173+
return quickPickOne(
174+
[...pipelines],
175+
'Select a Pipeline to Run Command Against'
176+
)
177+
}
178+
180179
private async initialize() {
181180
this.dispose.track(
182181
this.data.onDidUpdate(({ dag, stages }) => {

extension/src/test/suite/pipeline/index.test.ts

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,9 @@ suite('Pipeline Test Suite', () => {
323323
})
324324
})
325325

326-
it('should add a data series plot', async () => {
326+
it('should add a data series plot when a dvc.yaml file exists', async () => {
327327
const { pipeline } = buildPipeline({
328-
disposer: disposable,
329-
dvcRoot: dvcDemoPath
328+
disposer: disposable
330329
})
331330
const mockPickPlotConfiguration = stub(
332331
PipelineQuickPick,
@@ -340,7 +339,7 @@ suite('Pipeline Test Suite', () => {
340339

341340
await pipeline.addDataSeriesPlot()
342341

343-
expect(mockPickPlotConfiguration).to.be.calledOnce
342+
expect(mockPickPlotConfiguration).to.be.calledOnceWithExactly(dvcDemoPath)
344343
expect(mockAddPlotToDvcFile).not.to.be.called
345344

346345
const mockPlotConfig: PipelineQuickPick.PlotConfigData = {
@@ -354,8 +353,43 @@ suite('Pipeline Test Suite', () => {
354353

355354
await pipeline.addDataSeriesPlot()
356355

357-
expect(mockPickPlotConfiguration).to.be.calledTwice
358-
expect(mockAddPlotToDvcFile).to.be.called
356+
expect(mockPickPlotConfiguration).to.be.calledWithExactly(dvcDemoPath)
357+
expect(mockAddPlotToDvcFile).to.be.calledOnceWithExactly(
358+
dvcDemoPath,
359+
mockPlotConfig
360+
)
361+
})
362+
363+
it('should add a data series plot without trying to add a missing dvc.yaml file or stage', async () => {
364+
const { pipeline } = buildPipeline({
365+
disposer: disposable,
366+
dvcYamls: []
367+
})
368+
const mockPickPlotConfiguration = stub(
369+
PipelineQuickPick,
370+
'pickPlotConfiguration'
371+
)
372+
const mockAddPlotToDvcFile = stub(FileSystem, 'addPlotToDvcYamlFile')
373+
const mockCheckOrAddPipeline = stub(pipeline, 'checkOrAddPipeline')
374+
const mockPlotConfig: PipelineQuickPick.PlotConfigData = {
375+
template: 'simple',
376+
title: 'Great Plot Name',
377+
x: { 'results.json': ['step'] },
378+
y: { 'results.json': ['acc'] }
379+
}
380+
381+
mockPickPlotConfiguration.onFirstCall().resolves(mockPlotConfig)
382+
383+
await pipeline.isReady()
384+
await pipeline.addDataSeriesPlot()
385+
386+
expect(mockCheckOrAddPipeline, 'should not check for a pipeline stage').not
387+
.to.be.called
388+
expect(mockPickPlotConfiguration).to.be.calledOnceWithExactly(dvcDemoPath)
389+
expect(mockAddPlotToDvcFile).to.be.calledOnceWithExactly(
390+
dvcDemoPath,
391+
mockPlotConfig
392+
)
359393
})
360394

361395
it('should set the appropriate context value when a dvc.yaml is open in the active editor', async () => {

0 commit comments

Comments
 (0)