11import { join , relative , resolve } from 'path'
22import { appendFileSync , ensureDirSync , ensureFileSync , remove } from 'fs-extra'
3+ import { TextDocument , window , workspace } from 'vscode'
34import {
45 exists ,
56 findAbsoluteDvcRootPath ,
@@ -26,6 +27,13 @@ jest.mock('fs-extra', () => {
2627
2728const mockedAppendFileSync = jest . mocked ( appendFileSync )
2829const mockedEnsureFileSync = jest . mocked ( ensureFileSync )
30+ const mockedWorkspace = jest . mocked ( workspace )
31+ const mockedWindow = jest . mocked ( window )
32+ const mockedOpenTextDocument = jest . fn ( )
33+ const mockedShowTextDocument = jest . fn ( )
34+
35+ mockedWorkspace . openTextDocument = mockedOpenTextDocument
36+ mockedWindow . showTextDocument = mockedShowTextDocument
2937
3038beforeEach ( ( ) => {
3139 jest . resetAllMocks ( )
@@ -169,7 +177,7 @@ describe('getModifiedTime', () => {
169177describe ( 'findOrCreateDvcYamlFile' , ( ) => {
170178 it ( 'should make sure a dvc.yaml file exists' , ( ) => {
171179 const cwd = '/cwd'
172- findOrCreateDvcYamlFile ( cwd , '/my/training/script.py' , 'train' )
180+ void findOrCreateDvcYamlFile ( cwd , '/my/training/script.py' , 'train' )
173181
174182 expect ( mockedEnsureFileSync ) . toHaveBeenCalledWith ( `${ cwd } /dvc.yaml` )
175183 } )
@@ -187,16 +195,29 @@ describe('findOrCreateDvcYamlFile', () => {
187195
188196 it ( 'should add the training script as a train stage in the dvc.yaml file' , ( ) => {
189197 const cwd = '/cwd'
190- findOrCreateDvcYamlFile ( cwd , '/my/training/script.py' , 'train' )
198+ void findOrCreateDvcYamlFile ( cwd , '/my/training/script.py' , 'train' )
191199
192200 expect ( mockedAppendFileSync ) . toHaveBeenCalledWith (
193201 `${ cwd } /dvc.yaml` ,
194- expect . stringMatching ( / ^ \s + s t a g e s : \s + t r a i n : / )
202+ expect . stringMatching ( / s t a g e s : \s + t r a i n : / )
203+ )
204+ } )
205+
206+ it ( 'should add a comment to direct the user towards the dvc.yaml stages documentation' , ( ) => {
207+ const cwd = '/cwd'
208+ void findOrCreateDvcYamlFile ( cwd , '/my/training/script.py' , 'train' )
209+
210+ expect ( mockedAppendFileSync ) . toHaveBeenCalledWith (
211+ `${ cwd } /dvc.yaml` ,
212+ expect . stringContaining (
213+ `# Read about DVC pipeline configuration (https://dvc.org/doc/user-guide/project-structure/dvcyaml-files#stages)
214+ # to customize your stages even more`
215+ )
195216 )
196217 } )
197218
198219 it ( 'should add the training script as a relative path to the cwd' , ( ) => {
199- findOrCreateDvcYamlFile (
220+ void findOrCreateDvcYamlFile (
200221 '/dir/my_project/' ,
201222 '/dir/my_project/src/training/train.py' ,
202223 'train'
@@ -207,7 +228,7 @@ describe('findOrCreateDvcYamlFile', () => {
207228 expect . stringContaining ( join ( 'src' , 'training' , 'train.py' ) )
208229 )
209230
210- findOrCreateDvcYamlFile (
231+ void findOrCreateDvcYamlFile (
211232 '/dir/my_project/' ,
212233 '/dir/my_other_project/train.py' ,
213234 'train'
@@ -220,7 +241,7 @@ describe('findOrCreateDvcYamlFile', () => {
220241 } )
221242
222243 it ( 'should use the jupyter nbconvert command if the training script is a Jupyter notebook' , ( ) => {
223- findOrCreateDvcYamlFile ( '/' , '/train.ipynb' , 'train' )
244+ void findOrCreateDvcYamlFile ( '/' , '/train.ipynb' , 'train' )
224245
225246 expect ( mockedAppendFileSync ) . toHaveBeenCalledWith (
226247 expect . anything ( ) ,
@@ -233,7 +254,7 @@ describe('findOrCreateDvcYamlFile', () => {
233254 } )
234255
235256 it ( 'should use the python command if the training script is not a Jupyter notebook' , ( ) => {
236- findOrCreateDvcYamlFile ( '/' , '/train.py' , 'train' )
257+ void findOrCreateDvcYamlFile ( '/' , '/train.py' , 'train' )
237258
238259 expect ( mockedAppendFileSync ) . not . toHaveBeenCalledWith (
239260 expect . anything ( ) ,
@@ -244,4 +265,18 @@ describe('findOrCreateDvcYamlFile', () => {
244265 expect . stringContaining ( scriptCommand . PYTHON )
245266 )
246267 } )
268+
269+ it ( 'should open the dvc.yaml file in the editor' , ( ) => {
270+ mockedOpenTextDocument . mockResolvedValue ( { } as TextDocument )
271+
272+ void findOrCreateDvcYamlFile ( '/' , '/train.py' , 'train' )
273+
274+ expect ( mockedOpenTextDocument ) . toHaveBeenCalledWith (
275+ expect . objectContaining ( {
276+ authority : 'dvc.yaml' ,
277+ path : '/' ,
278+ scheme : 'file'
279+ } )
280+ )
281+ } )
247282} )
0 commit comments