1
- import path from 'node:path ' ;
1
+ import { execa } from 'execa ' ;
2
2
import fs from 'node:fs/promises' ;
3
+ import { tmpdir } from 'node:os' ;
4
+ import path from 'node:path' ;
3
5
import { afterAll , beforeAll , expect , test } from 'vitest' ;
4
- import { execa } from 'execa' ;
5
6
6
- const tmpDir = path . join ( __dirname , '.tmp' ) ;
7
+ // on CI on windows we want to make sure to use the same drive, so we use a custom logic
8
+ const tmpDir =
9
+ process . platform === 'win32'
10
+ ? path . join ( path . resolve ( __dirname , '../../../..' ) , '.tmp' )
11
+ : await fs . mkdtemp ( path . join ( tmpdir ( ) , 'tk-test-' ) ) ;
7
12
const baseDir = path . resolve ( __dirname , '../../..' ) ;
8
13
9
14
const cli = path . join ( baseDir , 'packages/cli/dist/index.js' ) ;
@@ -14,7 +19,9 @@ beforeAll(async () => {
14
19
} ) ;
15
20
16
21
afterAll ( async ( ) => {
17
- await fs . rm ( tmpDir , { force : true , recursive : true } ) ;
22
+ if ( process . platform !== 'win32' || ! process . env . CI ) {
23
+ await fs . rm ( tmpDir , { force : true , recursive : true } ) ;
24
+ }
18
25
} ) ;
19
26
20
27
test ( 'cannot create project without installing but with starting' , async ( context ) => {
@@ -23,9 +30,6 @@ test('cannot create project without installing but with starting', async (contex
23
30
await expect (
24
31
execa ( 'node' , [ cli , 'create' , name , '--no-install' , '--start' ] , {
25
32
cwd : tmpDir ,
26
- env : {
27
- TK_DIRECTORY : baseDir ,
28
- } ,
29
33
} ) ,
30
34
) . rejects . toThrow ( 'Cannot start project without installing dependencies.' ) ;
31
35
} ) ;
@@ -36,9 +40,6 @@ test('create a project', async (context) => {
36
40
37
41
await execa ( 'node' , [ cli , 'create' , name , '--no-install' , '--no-git' , '--defaults' ] , {
38
42
cwd : tmpDir ,
39
- env : {
40
- TK_DIRECTORY : baseDir ,
41
- } ,
42
43
} ) ;
43
44
44
45
const projectFiles = await fs . readdir ( dest , { recursive : true } ) ;
@@ -50,14 +51,13 @@ test('create and build a project', async (context) => {
50
51
const name = context . task . id ;
51
52
const dest = path . join ( tmpDir , name ) ;
52
53
53
- await execa ( 'node' , [ cli , 'create' , name , '--no-git' , '--no-start' , '--defaults' ] , {
54
+ await execa ( 'node' , [ cli , 'create' , name , '--no-git' , '--no-install' , '--no- start', '--defaults' ] , {
54
55
cwd : tmpDir ,
55
- env : {
56
- TK_DIRECTORY : baseDir ,
57
- } ,
58
56
} ) ;
59
57
60
- await execa ( 'npm' , [ 'run' , 'build' ] , {
58
+ await runPnpmInstall ( dest , baseDir ) ;
59
+
60
+ await execa ( 'pnpm' , [ 'run' , 'build' ] , {
61
61
cwd : dest ,
62
62
} ) ;
63
63
@@ -73,24 +73,25 @@ test('create and eject a project', async (context) => {
73
73
const name = context . task . id ;
74
74
const dest = path . join ( tmpDir , name ) ;
75
75
76
- await execa ( 'node' , [ cli , 'create' , name , '--no-git' , '--no-start' , '--defaults' ] , {
76
+ await execa ( 'node' , [ cli , 'create' , name , '--no-git' , '--no-install' , '--no- start', '--defaults' ] , {
77
77
cwd : tmpDir ,
78
- env : {
79
- TK_DIRECTORY : baseDir ,
80
- } ,
81
78
} ) ;
82
79
80
+ await runPnpmInstall ( dest , baseDir ) ;
81
+
83
82
await execa ( 'node' , [ cli , 'eject' , name , '--force' , '--defaults' ] , {
84
83
cwd : tmpDir ,
85
- env : {
86
- TK_DIRECTORY : baseDir ,
87
- } ,
88
84
} ) ;
89
85
90
- // remove `node_modules` before taking the snapshot
91
- await fs . rm ( path . join ( dest , 'node_modules' ) , { force : true , recursive : true } ) ;
86
+ if ( process . platform !== 'win32' ) {
87
+ await fs . rm ( path . join ( dest , 'node_modules' ) , { force : true , recursive : true , maxRetries : 5 } ) ;
88
+ }
92
89
93
- const projectFiles = await fs . readdir ( dest , { recursive : true } ) ;
90
+ let projectFiles = await fs . readdir ( dest , { recursive : true } ) ;
91
+
92
+ if ( process . platform === 'win32' ) {
93
+ projectFiles = projectFiles . filter ( ( filePath ) => ! filePath . startsWith ( 'node_modules' ) ) ;
94
+ }
94
95
95
96
expect ( projectFiles . map ( normaliseSlash ) . sort ( ) ) . toMatchSnapshot ( ) ;
96
97
expect ( await fs . readFile ( path . join ( dest , 'astro.config.ts' ) , 'utf-8' ) ) . toMatchSnapshot ( ) ;
@@ -100,25 +101,21 @@ test('create, eject and build a project', async (context) => {
100
101
const name = context . task . id ;
101
102
const dest = path . join ( tmpDir , name ) ;
102
103
103
- await execa ( 'node' , [ cli , 'create' , name , '--no-git' , '--no-start' , '--defaults' ] , {
104
+ await execa ( 'node' , [ cli , 'create' , name , '--no-git' , '--no-install' , '--no- start', '--defaults' ] , {
104
105
cwd : tmpDir ,
105
- env : {
106
- TK_DIRECTORY : baseDir ,
107
- } ,
108
106
} ) ;
109
107
108
+ await runPnpmInstall ( dest , baseDir ) ;
109
+
110
110
await execa ( 'node' , [ cli , 'eject' , name , '--force' , '--defaults' ] , {
111
111
cwd : tmpDir ,
112
- env : {
113
- TK_DIRECTORY : baseDir ,
114
- } ,
115
112
} ) ;
116
113
117
- await execa ( 'npm ' , [ 'install' ] , {
114
+ await execa ( 'pnpm ' , [ 'install' , '--no-frozen-lockfile '] , {
118
115
cwd : dest ,
119
116
} ) ;
120
117
121
- await execa ( 'npm ' , [ 'run' , 'build' ] , {
118
+ await execa ( 'pnpm ' , [ 'run' , 'build' ] , {
122
119
cwd : dest ,
123
120
} ) ;
124
121
@@ -139,9 +136,6 @@ test('cannot eject on an empty folder', async (context) => {
139
136
await expect (
140
137
execa ( 'node' , [ cli , 'eject' , name , '--force' , '--defaults' ] , {
141
138
cwd : tmpDir ,
142
- env : {
143
- TK_DIRECTORY : baseDir ,
144
- } ,
145
139
} ) ,
146
140
) . rejects . toThrow ( 'package.json does not exists!' ) ;
147
141
} ) ;
@@ -157,9 +151,6 @@ test('cannot eject on a node project that is not an Astro project', async (conte
157
151
await expect (
158
152
execa ( 'node' , [ cli , 'eject' , name , '--force' , '--defaults' ] , {
159
153
cwd : tmpDir ,
160
- env : {
161
- TK_DIRECTORY : baseDir ,
162
- } ,
163
154
} ) ,
164
155
) . rejects . toThrow ( 'astro.config.ts does not exists!' ) ;
165
156
} ) ;
@@ -188,9 +179,6 @@ test('cannot eject on an astro project that is not using TutorialKit', async (co
188
179
await expect (
189
180
execa ( 'node' , [ cli , 'eject' , name , '--force' , '--defaults' ] , {
190
181
cwd : tmpDir ,
191
- env : {
192
- TK_DIRECTORY : baseDir ,
193
- } ,
194
182
} ) ,
195
183
) . rejects . toThrow ( `@tutorialkit${ path . sep } astro does not exists!` ) ;
196
184
} ) ;
@@ -216,20 +204,39 @@ test('cannot eject on an astro project that is not using TutorialKit 2', async (
216
204
` ,
217
205
) ;
218
206
219
- await execa ( 'npm ' , [ 'install' ] , {
207
+ await execa ( 'pnpm ' , [ 'install' ] , {
220
208
cwd : dest ,
221
209
} ) ;
222
210
223
211
await expect (
224
212
execa ( 'node' , [ cli , 'eject' , name , '--force' , '--defaults' ] , {
225
213
cwd : tmpDir ,
226
- env : {
227
- TK_DIRECTORY : baseDir ,
228
- } ,
229
214
} ) ,
230
215
) . rejects . toThrow ( `Could not find import to '@tutorialkit/astro'` ) ;
231
216
} ) ;
232
217
233
218
function normaliseSlash ( filePath : string ) {
234
219
return filePath . replace ( / \\ / g, '/' ) ;
235
220
}
221
+
222
+ async function runPnpmInstall ( dest : string , baseDir : string ) {
223
+ const packageJsonPath = path . join ( dest , 'package.json' ) ;
224
+ const packageJson = JSON . parse ( await fs . readFile ( packageJsonPath , 'utf-8' ) ) ;
225
+
226
+ packageJson . pnpm = {
227
+ overrides : {
228
+ '@astrojs/language-server' : '2.11.1' ,
229
+ '@tutorialkit/astro' : `file:${ baseDir } /packages/astro` ,
230
+ '@tutorialkit/components-react' : `file:${ baseDir } /packages/components/react` ,
231
+ '@tutorialkit/runtime' : `file:${ baseDir } /packages/runtime` ,
232
+ '@tutorialkit/theme' : `file:${ baseDir } /packages/theme` ,
233
+ '@tutorialkit/types' : `file:${ baseDir } /packages/types` ,
234
+ } ,
235
+ } ;
236
+
237
+ await fs . writeFile ( packageJsonPath , JSON . stringify ( packageJson , undefined , 2 ) , 'utf8' ) ;
238
+
239
+ await execa ( 'pnpm' , [ 'install' ] , {
240
+ cwd : dest ,
241
+ } ) ;
242
+ }
0 commit comments