11import { describe , expect , it } from "vite-plus/test" ;
2+ import { spawnSync } from "node:child_process" ;
3+ import {
4+ chmodSync ,
5+ existsSync ,
6+ mkdirSync ,
7+ mkdtempSync ,
8+ readFileSync ,
9+ rmSync ,
10+ writeFileSync ,
11+ } from "node:fs" ;
12+ import { tmpdir } from "node:os" ;
13+ import { join } from "node:path" ;
214import { getInstallScriptCommand , parseVitePlusDirs , supportsVitePlusDirs } from "./vp-dirs.js" ;
315
416describe ( "Vite+ directory resolution" , ( ) => {
@@ -53,6 +65,90 @@ describe("Vite+ directory resolution", () => {
5365 expect ( command . args [ 1 ] ) . toContain ( '>> "$SETUP_VP_DIRS_FILE"' ) ;
5466 } ) ;
5567
68+ it . skipIf ( process . platform === "win32" ) . each ( [ true , false ] ) (
69+ "isolates inherited nounset and preserves installer failures (detectDirs: %s)" ,
70+ ( detectDirs ) => {
71+ const fixture = mkdtempSync ( join ( tmpdir ( ) , "setup-vp-shell-options-" ) ) ;
72+ const bin = join ( fixture , "bin" ) ;
73+ const installer = join ( fixture , "installer.sh" ) ;
74+ const dirsFile = join ( fixture , "dirs" ) ;
75+ const continued = join ( fixture , "continued" ) ;
76+ mkdirSync ( bin ) ;
77+ writeFileSync (
78+ join ( bin , "curl" ) ,
79+ `#!/usr/bin/env bash
80+ if [ "$#" -eq 8 ]; then
81+ cp "$SETUP_VP_TEST_INSTALLER" "$8"
82+ else
83+ cat "$SETUP_VP_TEST_INSTALLER"
84+ fi
85+ ` ,
86+ ) ;
87+ writeFileSync (
88+ join ( bin , "vp" ) ,
89+ `#!/usr/bin/env bash
90+ if [ "\${VP_DUMP_DIRS:-}" = "1" ]; then
91+ printf 'data\\t/data\\nbin\\t/bin\\ncache\\t/cache\\nconfig\\t/config\\nstate\\t/state\\n'
92+ else
93+ printf 'vp v0.3.0\\n'
94+ fi
95+ ` ,
96+ ) ;
97+ chmodSync ( join ( bin , "curl" ) , 0o755 ) ;
98+ chmodSync ( join ( bin , "vp" ) , 0o755 ) ;
99+ const command = getInstallScriptCommand (
100+ "https://example.com/install.sh" ,
101+ "linux" ,
102+ detectDirs ,
103+ ) ;
104+ const runInstaller = ( ) =>
105+ spawnSync ( command . command , command . args , {
106+ encoding : "utf8" ,
107+ env : {
108+ ...process . env ,
109+ PATH : `${ bin } :${ process . env . PATH } ` ,
110+ SHELLOPTS : "nounset" ,
111+ SETUP_VP_DIRS_FILE : dirsFile ,
112+ SETUP_VP_TEST_INSTALLER : installer ,
113+ SETUP_VP_TEST_SHIM_DIR : bin ,
114+ SETUP_VP_TEST_CONTINUED : continued ,
115+ } ,
116+ } ) ;
117+
118+ try {
119+ writeFileSync (
120+ installer ,
121+ `set -e
122+ setup_vp_test_optional() { local optional="$4"; }
123+ setup_vp_test_optional one two three
124+ SHIM_DIR="$SETUP_VP_TEST_SHIM_DIR"
125+ printf 'installer completed\\n'
126+ ` ,
127+ ) ;
128+ const success = runInstaller ( ) ;
129+ expect ( success . status , success . stderr ) . toBe ( 0 ) ;
130+ expect ( success . stderr ) . toBe ( "" ) ;
131+ expect ( success . stdout ) . toContain ( "installer completed" ) ;
132+ if ( detectDirs ) {
133+ expect ( parseVitePlusDirs ( readFileSync ( dirsFile , "utf8" ) ) ?. bin ) . toBe ( "/bin" ) ;
134+ }
135+
136+ writeFileSync ( installer , "exit 23\n" ) ;
137+ expect ( runInstaller ( ) . status ) . toBe ( 23 ) ;
138+ if ( detectDirs ) {
139+ writeFileSync ( installer , "return 23\n" ) ;
140+ expect ( runInstaller ( ) . status ) . toBe ( 23 ) ;
141+ }
142+
143+ writeFileSync ( installer , 'set -e\nfalse\nprintf continued > "$SETUP_VP_TEST_CONTINUED"\n' ) ;
144+ expect ( runInstaller ( ) . status ) . not . toBe ( 0 ) ;
145+ expect ( existsSync ( continued ) ) . toBe ( false ) ;
146+ } finally {
147+ rmSync ( fixture , { recursive : true , force : true } ) ;
148+ }
149+ } ,
150+ ) ;
151+
56152 it ( "dumps directories from the installer-resolved Windows shim" , ( ) => {
57153 const command = getInstallScriptCommand ( "https://example.com/install.ps1" , "win32" ) ;
58154
@@ -61,6 +157,8 @@ describe("Vite+ directory resolution", () => {
61157 expect ( command . args [ 1 ] ) . toContain ( "Join-Path $vpDir 'vp.exe'" ) ;
62158 expect ( command . args [ 1 ] ) . toContain ( "& $vpPath --version" ) ;
63159 expect ( command . args [ 1 ] ) . toContain ( "$env:VP_DUMP_DIRS = '1'" ) ;
160+ expect ( command . args [ 1 ] ) . toContain ( "Set-Content -LiteralPath $dirsFile -Encoding UTF8" ) ;
161+ expect ( command . args [ 1 ] ) . toContain ( "Add-Content -LiteralPath $dirsFile -Encoding UTF8" ) ;
64162 } ) ;
65163
66164 it . each ( [
0 commit comments