11import { expect } from "@std/expect" ;
2- import { initProject , InitStep , type MockTTY } from "./init.ts" ;
2+ import {
3+ CONFIRM_TAILWIND_MESSAGE ,
4+ CONFIRM_VSCODE_MESSAGE ,
5+ initProject ,
6+ } from "./init.ts" ;
37import * as path from "@std/path" ;
48import { getStdOutput , withBrowser } from "../../tests/test_utils.tsx" ;
59import { waitForText } from "../../tests/test_utils.tsx" ;
610import { withChildProcessServer } from "../../tests/test_utils.tsx" ;
11+ import { stub } from "@std/testing/mock" ;
12+
13+ function stubPrompt ( result : string ) {
14+ return stub ( globalThis , "prompt" , ( ) => result ) ;
15+ }
16+
17+ function stubConfirm ( steps : Record < string , boolean > = { } ) {
18+ return stub (
19+ globalThis ,
20+ "confirm" ,
21+ ( message ) => message ? steps [ message ] : false ,
22+ ) ;
23+ }
724
825async function withTmpDir ( fn : ( dir : string ) => void | Promise < void > ) {
926 const hash = crypto . randomUUID ( ) . replaceAll ( / - / g, "" ) ;
@@ -32,29 +49,6 @@ async function patchProject(dir: string): Promise<void> {
3249 await Deno . writeTextFile ( jsonPath , JSON . stringify ( json , null , 2 ) + "\n" ) ;
3350}
3451
35- function mockUserInput ( steps : Record < string , unknown > ) {
36- const errorOutput : unknown [ ] [ ] = [ ] ;
37- const tty : MockTTY = {
38- confirm ( step , _msg ) {
39- return Boolean ( steps [ step ] ) ;
40- } ,
41- prompt ( step , _msg , def ) {
42- const setting = typeof steps [ step ] === "string"
43- ? steps [ step ] as string
44- : null ;
45- return setting ?? def ?? null ;
46- } ,
47- log : ( ) => { } ,
48- logError : ( ...args ) => {
49- errorOutput . push ( args ) ;
50- } ,
51- } ;
52- return {
53- errorOutput,
54- tty,
55- } ;
56- }
57-
5852async function expectProjectFile ( dir : string , pathname : string ) {
5953 const filePath = path . join ( dir , ...pathname . split ( "/" ) . filter ( Boolean ) ) ;
6054 const stat = await Deno . stat ( filePath ) ;
@@ -71,15 +65,17 @@ async function readProjectFile(dir: string, pathname: string): Promise<string> {
7165
7266Deno . test ( "init - new project" , async ( ) => {
7367 await withTmpDir ( async ( dir ) => {
74- const mock = mockUserInput ( { } ) ;
75- await initProject ( dir , [ ] , { } , mock . tty ) ;
68+ using _promptStub = stubPrompt ( "fresh-init" ) ;
69+ using _confirmStub = stubConfirm ( ) ;
70+ await initProject ( dir , [ ] , { } ) ;
7671 } ) ;
7772} ) ;
7873
7974Deno . test ( "init - create project dir" , async ( ) => {
8075 await withTmpDir ( async ( dir ) => {
81- const mock = mockUserInput ( { [ InitStep . ProjectName ] : "fresh-init" } ) ;
82- await initProject ( dir , [ ] , { } , mock . tty ) ;
76+ using _promptStub = stubPrompt ( "fresh-init" ) ;
77+ using _confirmStub = stubConfirm ( ) ;
78+ await initProject ( dir , [ ] , { } ) ;
8379
8480 const root = path . join ( dir , "fresh-init" ) ;
8581 await expectProjectFile ( root , "deno.json" ) ;
@@ -92,11 +88,11 @@ Deno.test("init - create project dir", async () => {
9288
9389Deno . test ( "init - with tailwind" , async ( ) => {
9490 await withTmpDir ( async ( dir ) => {
95- const mock = mockUserInput ( {
96- [ InitStep . ProjectName ] : "." ,
97- [ InitStep . Tailwind ] : true ,
91+ using _promptStub = stubPrompt ( "." ) ;
92+ using _confirmStub = stubConfirm ( {
93+ [ CONFIRM_TAILWIND_MESSAGE ] : true ,
9894 } ) ;
99- await initProject ( dir , [ ] , { } , mock . tty ) ;
95+ await initProject ( dir , [ ] , { } ) ;
10096
10197 const css = await readProjectFile ( dir , "static/styles.css" ) ;
10298 expect ( css ) . toMatch ( / @ t a i l w i n d / ) ;
@@ -110,11 +106,11 @@ Deno.test("init - with tailwind", async () => {
110106
111107Deno . test ( "init - with vscode" , async ( ) => {
112108 await withTmpDir ( async ( dir ) => {
113- const mock = mockUserInput ( {
114- [ InitStep . ProjectName ] : "." ,
115- [ InitStep . VSCode ] : true ,
109+ using _promptStub = stubPrompt ( "." ) ;
110+ using _confirmStub = stubConfirm ( {
111+ [ CONFIRM_VSCODE_MESSAGE ] : true ,
116112 } ) ;
117- await initProject ( dir , [ ] , { } , mock . tty ) ;
113+ await initProject ( dir , [ ] , { } ) ;
118114
119115 await expectProjectFile ( dir , ".vscode/settings.json" ) ;
120116 await expectProjectFile ( dir , ".vscode/extensions.json" ) ;
@@ -128,10 +124,9 @@ Deno.test({
128124 ignore : Deno . version . deno . includes ( "+" ) ,
129125 fn : async ( ) => {
130126 await withTmpDir ( async ( dir ) => {
131- const mock = mockUserInput ( {
132- [ InitStep . ProjectName ] : "." ,
133- } ) ;
134- await initProject ( dir , [ ] , { } , mock . tty ) ;
127+ using _promptStub = stubPrompt ( "." ) ;
128+ using _confirmStub = stubConfirm ( ) ;
129+ await initProject ( dir , [ ] , { } ) ;
135130 await expectProjectFile ( dir , "main.ts" ) ;
136131 await expectProjectFile ( dir , "dev.ts" ) ;
137132
@@ -150,11 +145,12 @@ Deno.test({
150145
151146Deno . test ( "init with tailwind - fmt, lint, and type check project" , async ( ) => {
152147 await withTmpDir ( async ( dir ) => {
153- const mock = mockUserInput ( {
154- [ InitStep . ProjectName ] : "." ,
155- [ InitStep . Tailwind ] : true ,
148+ using _promptStub = stubPrompt ( "." ) ;
149+ using _confirmStub = stubConfirm ( {
150+ [ CONFIRM_TAILWIND_MESSAGE ] : true ,
156151 } ) ;
157- await initProject ( dir , [ ] , { } , mock . tty ) ;
152+
153+ await initProject ( dir , [ ] , { } ) ;
158154 await expectProjectFile ( dir , "main.ts" ) ;
159155 await expectProjectFile ( dir , "dev.ts" ) ;
160156
@@ -172,10 +168,9 @@ Deno.test("init with tailwind - fmt, lint, and type check project", async () =>
172168
173169Deno . test ( "init - can start dev server" , async ( ) => {
174170 await withTmpDir ( async ( dir ) => {
175- const mock = mockUserInput ( {
176- [ InitStep . ProjectName ] : "." ,
177- } ) ;
178- await initProject ( dir , [ ] , { } , mock . tty ) ;
171+ using _promptStub = stubPrompt ( "." ) ;
172+ using _confirmStub = stubConfirm ( ) ;
173+ await initProject ( dir , [ ] , { } ) ;
179174 await expectProjectFile ( dir , "main.ts" ) ;
180175 await expectProjectFile ( dir , "dev.ts" ) ;
181176
@@ -196,10 +191,9 @@ Deno.test("init - can start dev server", async () => {
196191
197192Deno . test ( "init - can start built project" , async ( ) => {
198193 await withTmpDir ( async ( dir ) => {
199- const mock = mockUserInput ( {
200- [ InitStep . ProjectName ] : "." ,
201- } ) ;
202- await initProject ( dir , [ ] , { } , mock . tty ) ;
194+ using _promptStub = stubPrompt ( "." ) ;
195+ using _confirmStub = stubConfirm ( ) ;
196+ await initProject ( dir , [ ] , { } ) ;
203197 await expectProjectFile ( dir , "main.ts" ) ;
204198 await expectProjectFile ( dir , "dev.ts" ) ;
205199
@@ -230,10 +224,9 @@ Deno.test("init - can start built project", async () => {
230224
231225Deno . test ( "init - errors on missing build cache in prod" , async ( ) => {
232226 await withTmpDir ( async ( dir ) => {
233- const mock = mockUserInput ( {
234- [ InitStep . ProjectName ] : "." ,
235- } ) ;
236- await initProject ( dir , [ ] , { } , mock . tty ) ;
227+ using _promptStub = stubPrompt ( "." ) ;
228+ using _confirmStub = stubConfirm ( ) ;
229+ await initProject ( dir , [ ] , { } ) ;
237230 await expectProjectFile ( dir , "main.ts" ) ;
238231 await expectProjectFile ( dir , "dev.ts" ) ;
239232
0 commit comments