@@ -26,17 +26,17 @@ describe("getCacheDirectory", () => {
2626
2727 test ( "returns ELECTRON_BUILDER_CACHE when set" , ( { expect } ) => {
2828 vi . stubEnv ( "ELECTRON_BUILDER_CACHE" , "/custom/cache" )
29- expect ( getCacheDirectory ( ) ) . toBe ( "/custom/cache" )
29+ expect ( getCacheDirectory ( { allowEnvVarOverride : true } ) ) . toBe ( "/custom/cache" )
3030 } )
3131
3232 test ( "trims whitespace from ELECTRON_BUILDER_CACHE" , ( { expect } ) => {
3333 vi . stubEnv ( "ELECTRON_BUILDER_CACHE" , " /padded/path " )
34- expect ( getCacheDirectory ( ) ) . toBe ( "/padded/path" )
34+ expect ( getCacheDirectory ( { allowEnvVarOverride : true } ) ) . toBe ( "/padded/path" )
3535 } )
3636
3737 test ( "returns platform-appropriate default when env var is absent" , ( { expect } ) => {
3838 vi . stubEnv ( "ELECTRON_BUILDER_CACHE" , "" )
39- const result = getCacheDirectory ( )
39+ const result = getCacheDirectory ( { allowEnvVarOverride : true } )
4040 expect ( typeof result ) . toBe ( "string" )
4141 expect ( result . length ) . toBeGreaterThan ( 0 )
4242 if ( process . platform === "darwin" ) {
@@ -55,19 +55,66 @@ describe("getCacheDirectory", () => {
5555 }
5656 vi . stubEnv ( "ELECTRON_BUILDER_CACHE" , "" )
5757 vi . stubEnv ( "XDG_CACHE_HOME" , "/xdg/cache" )
58- expect ( getCacheDirectory ( ) ) . toBe ( "/xdg/cache/electron-builder" )
58+ expect ( getCacheDirectory ( { allowEnvVarOverride : true } ) ) . toBe ( "/xdg/cache/electron-builder" )
5959 } )
6060
61- test ( "isAvoidSystemOnWindows falls back to tmpdir for system users " , ( { expect } ) => {
61+ test ( "falls back to tmpdir when LOCALAPPDATA is absent on Windows " , ( { expect } ) => {
6262 if ( process . platform !== "win32" ) {
6363 expect ( true ) . toBe ( true )
6464 return
6565 }
66- vi . stubEnv ( "ELECTRON_BUILDER_CACHE" , "" )
6766 vi . stubEnv ( "LOCALAPPDATA" , "" )
68- const result = getCacheDirectory ( true )
67+ const result = getCacheDirectory ( { isAvoidSystemOnWindows : true , allowEnvVarOverride : false } )
68+ expect ( result ) . toContain ( os . tmpdir ( ) )
69+ } )
70+
71+ test ( "allowEnvVarOverride:false ignores ELECTRON_BUILDER_CACHE even when set" , ( { expect } ) => {
72+ vi . stubEnv ( "ELECTRON_BUILDER_CACHE" , "/custom/cache" )
73+ const result = getCacheDirectory ( { allowEnvVarOverride : false } )
74+ expect ( result ) . not . toBe ( "/custom/cache" )
75+ expect ( result ) . toContain ( "electron-builder" )
76+ } )
77+
78+ test ( "ignores ELECTRON_BUILDER_CACHE when value has no filesystem root (relative path)" , ( { expect } ) => {
79+ vi . stubEnv ( "ELECTRON_BUILDER_CACHE" , "relative/path/no-root" )
80+ const result = getCacheDirectory ( { allowEnvVarOverride : true } )
81+ expect ( result ) . not . toBe ( "relative/path/no-root" )
82+ expect ( result ) . toContain ( "electron-builder" )
83+ } )
84+
85+ test ( "falls back to tmpdir when USERNAME is 'system' (isAvoidSystemOnWindows defaults to true)" , ( { expect } ) => {
86+ if ( process . platform !== "win32" ) {
87+ expect ( true ) . toBe ( true )
88+ return
89+ }
90+ vi . stubEnv ( "LOCALAPPDATA" , "C:\\Users\\system\\AppData\\Local" )
91+ vi . stubEnv ( "USERNAME" , "system" )
92+ const result = getCacheDirectory ( { allowEnvVarOverride : false } )
6993 expect ( result ) . toContain ( os . tmpdir ( ) )
7094 } )
95+
96+ test ( "falls back to tmpdir when LOCALAPPDATA path contains \\windows\\system32\\" , ( { expect } ) => {
97+ if ( process . platform !== "win32" ) {
98+ expect ( true ) . toBe ( true )
99+ return
100+ }
101+ vi . stubEnv ( "LOCALAPPDATA" , "C:\\Windows\\System32\\config\\systemprofile\\AppData\\Local" )
102+ vi . stubEnv ( "USERNAME" , "not-system" )
103+ const result = getCacheDirectory ( { allowEnvVarOverride : false } )
104+ expect ( result ) . toContain ( os . tmpdir ( ) )
105+ } )
106+
107+ test ( "isAvoidSystemOnWindows:false does not fall back to tmpdir for USERNAME=system" , ( { expect } ) => {
108+ if ( process . platform !== "win32" ) {
109+ expect ( true ) . toBe ( true )
110+ return
111+ }
112+ vi . stubEnv ( "LOCALAPPDATA" , "C:\\Users\\system\\AppData\\Local" )
113+ vi . stubEnv ( "USERNAME" , "system" )
114+ const result = getCacheDirectory ( { isAvoidSystemOnWindows : false , allowEnvVarOverride : false } )
115+ expect ( result ) . not . toContain ( os . tmpdir ( ) )
116+ expect ( result ) . toContain ( "electron-builder" )
117+ } )
71118} )
72119
73120describe ( "getBinariesMirrorUrl" , ( ) => {
0 commit comments