11import fs from 'fs' ;
2- import { describe , expect , test , vi } from 'vitest' ;
2+ import os from 'os' ;
3+ import pathlib from 'path' ;
4+ import { describe , expect , test , vi , type MockInstance } from 'vitest' ;
35import { generateStructure } from '../structure' ;
46import { generateTree } from '../tree' ;
57import { isRootYamlObject , isYamlObject } from '../types' ;
@@ -9,6 +11,8 @@ const mockStatSync = vi.spyOn(fs, 'statSync').mockReturnValue({
911 isDirectory : ( ) => true
1012} as any ) ;
1113
14+ const isWindows = os . platform ( ) === 'win32' ;
15+
1216describe ( 'Test isYamlObject' , ( ) => {
1317 const testCases : [ string , unknown , boolean ] [ ] = [
1418 [ 'Name property is required' , { } , false ] ,
@@ -75,16 +79,26 @@ test('structure generation', () => {
7579} ) ;
7680
7781describe ( 'Test tree validation' , ( ) => {
78- const validPaths = [
79- '/' ,
80- '/real_item0' ,
81- '/real_item1' ,
82- '/real_item1/real_item2' ,
83- ] ;
84-
85- mockExistsSync . mockImplementation ( path => validPaths . includes ( path as string ) ) ;
82+ interface Fixtures {
83+ rootDir : string ;
84+ validPaths : string [ ]
85+ mockExistsSync : MockInstance < typeof fs . existsSync >
86+ }
87+
88+ const treeTest = test . extend < Fixtures > ( {
89+ rootDir : ( { } , use ) => use ( isWindows ? '\\' : '/' ) ,
90+ validPaths : ( { rootDir } , use ) => use ( [
91+ rootDir ,
92+ pathlib . join ( rootDir , 'real_item0' ) ,
93+ pathlib . join ( rootDir , 'real_item1' ) ,
94+ pathlib . join ( rootDir , 'real_item1' , 'real_item2' ) ,
95+ ] ) ,
96+ mockExistsSync : ( { validPaths } , use ) => use (
97+ mockExistsSync . mockImplementation ( path => validPaths . includes ( path as string ) )
98+ )
99+ } ) ;
86100
87- test ( 'Successful validation' , ( ) => {
101+ treeTest ( 'Successful validation' , ( { rootDir , mockExistsSync } ) => {
88102 const [ , , warnings ] = generateStructure ( {
89103 path : '.' ,
90104 name : 'root' ,
@@ -95,24 +109,23 @@ describe('Test tree validation', () => {
95109 children : [ 'real_item2' ]
96110 }
97111 ]
98- } , '/' ) ;
99- console . log ( warnings ) ;
112+ } , rootDir ) ;
100113 expect ( warnings . length ) . toEqual ( 0 ) ;
101- expect ( fs . existsSync ) . toHaveBeenCalledTimes ( 4 ) ;
114+ expect ( mockExistsSync ) . toHaveBeenCalledTimes ( 4 ) ;
102115 } ) ;
103116
104- test ( 'Unsuccessful validation when child item doesn\'t exist' , ( ) => {
117+ treeTest ( 'Unsuccessful validation when child item doesn\'t exist' , ( { rootDir , mockExistsSync } ) => {
105118 const [ , , warnings ] = generateStructure ( {
106119 path : '.' ,
107120 name : 'root' ,
108121 children : [ 'fake_item0' ]
109- } , '/' ) ;
122+ } , rootDir ) ;
110123
111124 expect ( warnings . length ) . toEqual ( 1 ) ;
112- expect ( fs . existsSync ) . toHaveBeenCalledTimes ( 2 ) ;
125+ expect ( mockExistsSync ) . toHaveBeenCalledTimes ( 2 ) ;
113126 } ) ;
114127
115- test ( 'Unsuccessful validation when item with children is not a folder' , ( ) => {
128+ treeTest ( 'Unsuccessful validation when item with children is not a folder' , ( { rootDir , mockExistsSync } ) => {
116129 mockStatSync . mockReturnValueOnce ( {
117130 isDirectory : ( ) => false
118131 } as any ) ;
@@ -124,14 +137,14 @@ describe('Test tree validation', () => {
124137 name : 'real_item0' ,
125138 children : [ 'real_item2' ]
126139 } ]
127- } , '/' ) ;
140+ } , rootDir ) ;
128141
129142 console . log ( warnings ) ;
130143 expect ( warnings . length ) . toEqual ( 1 ) ;
131- expect ( fs . existsSync ) . toHaveBeenCalledTimes ( 3 ) ;
144+ expect ( mockExistsSync ) . toHaveBeenCalledTimes ( 3 ) ;
132145 } ) ;
133146
134- test ( 'Not providing a path means no validation is run' , ( ) => {
147+ treeTest ( 'Not providing a path means no validation is run' , ( { mockExistsSync } ) => {
135148 const [ , , warnings ] = generateStructure ( {
136149 path : '.' ,
137150 name : 'root' ,
@@ -142,7 +155,7 @@ describe('Test tree validation', () => {
142155 } ) ;
143156
144157 expect ( warnings . length ) . toEqual ( 0 ) ;
145- expect ( fs . existsSync ) . not . toHaveBeenCalled ( ) ;
158+ expect ( mockExistsSync ) . not . toHaveBeenCalled ( ) ;
146159 expect ( fs . statSync ) . not . toHaveBeenCalled ( ) ;
147160 } ) ;
148161} ) ;
0 commit comments