1- import { describe , expect , it } from 'vitest'
1+ import { beforeEach , describe , expect , it , vi } from 'vitest'
2+ import { fs , vol } from 'memfs'
23
3- import { createMemoryEnvironment } from '../src/environment.js'
4+ vi . mock ( 'node:fs' , ( ) => fs )
5+ vi . mock ( 'node:fs/promises' , ( ) => fs . promises )
6+
7+ beforeEach ( ( ) => {
8+ vol . reset ( )
9+ } )
10+
11+ import {
12+ createMemoryEnvironment ,
13+ createDefaultEnvironment ,
14+ } from '../src/environment.js'
415
516describe ( 'createMemoryEnvironment' , ( ) => {
617 it ( 'should handle basic file operations' , async ( ) => {
718 const { environment, output } = createMemoryEnvironment ( )
819
920 environment . startRun ( )
21+
22+ await environment . writeFile ( '/test.txt' , 'test' )
23+
24+ environment . deleteFile ( '/test.txt' )
25+ expect ( environment . exists ( '/test.txt' ) ) . toBe ( false )
26+
1027 await environment . writeFile ( '/test.txt' , 'test' )
28+
1129 environment . finishRun ( )
1230
1331 expect ( output . files [ '/test.txt' ] ) . toEqual ( 'test' )
@@ -25,3 +43,54 @@ describe('createMemoryEnvironment', () => {
2543 expect ( output . commands [ 0 ] . args ) . toEqual ( [ 'test' ] )
2644 } )
2745} )
46+
47+ describe ( 'createDefaultEnvironment' , ( ) => {
48+ it ( 'should create a default environment' , async ( ) => {
49+ const environment = createDefaultEnvironment ( )
50+ expect ( environment ) . toBeDefined ( )
51+ } )
52+
53+ it ( 'should write to the file system' , async ( ) => {
54+ const environment = createDefaultEnvironment ( )
55+ environment . startRun ( )
56+ await environment . writeFile ( '/test.txt' , 'test' )
57+ await environment . appendFile ( '/test.txt' , 'test2' )
58+ await environment . copyFile ( '/test.txt' , '/test2.txt' )
59+ environment . finishRun ( )
60+
61+ expect ( fs . readFileSync ( '/test.txt' , 'utf8' ) ) . toEqual ( 'testtest2' )
62+ } )
63+
64+ it ( 'should allow deletes' , async ( ) => {
65+ const environment = createDefaultEnvironment ( )
66+ await environment . writeFile ( '/test.txt' , 'test' )
67+ expect ( fs . readFileSync ( '/test.txt' , 'utf8' ) ) . toEqual ( 'test' )
68+ await environment . deleteFile ( '/test.txt' )
69+ expect ( environment . exists ( '/test.txt' ) ) . toBe ( false )
70+ } )
71+
72+ it ( 'should record errors' , async ( ) => {
73+ const environment = createDefaultEnvironment ( )
74+ environment . startRun ( )
75+ await environment . execute ( 'command-that-does-not-exist' , [ 'test' ] , '' )
76+ environment . finishRun ( )
77+ expect ( environment . getErrors ( ) ) . toEqual ( [
78+ 'Command "command-that-does-not-exist test" did not run successfully. Please run this manually in your project.' ,
79+ ] )
80+ } )
81+
82+ it ( 'should have UI methods' , async ( ) => {
83+ const environment = createDefaultEnvironment ( )
84+ environment . startRun ( )
85+ environment . intro ( 'test' )
86+ environment . outro ( 'test' )
87+ environment . info ( 'test' )
88+ environment . error ( 'test' )
89+ environment . warn ( 'test' )
90+ const s = environment . spinner ( )
91+ s . start ( 'foo' )
92+ s . stop ( 'bar' )
93+ environment . finishRun ( )
94+ expect ( await environment . confirm ( 'test' ) ) . toEqual ( true )
95+ } )
96+ } )
0 commit comments