@@ -20,9 +20,15 @@ describe("config", () => {
2020 } )
2121
2222 test ( "read returns null for missing file" , async ( ) => {
23- const { read } = await import ( "../src/config" )
24- const result = await read ( )
25- expect ( result ) . toBeNull ( )
23+ const origCwd = process . cwd ( )
24+ process . chdir ( dir )
25+ try {
26+ const { read } = await import ( "../src/config" )
27+ const result = await read ( )
28+ expect ( result ) . toBeNull ( )
29+ } finally {
30+ process . chdir ( origCwd )
31+ }
2632 } )
2733
2834 test ( "write and read round-trip" , async ( ) => {
@@ -76,6 +82,31 @@ describe("config", () => {
7682 }
7783 } )
7884
85+ test ( "read falls back to auto-discovery on malformed config file" , async ( ) => {
86+ const { read } = await import ( "../src/config" )
87+ // Write a malformed JSON config file
88+ const configDir = join ( dir , ".altimate-code" )
89+ await mkdir ( configDir , { recursive : true } )
90+ await writeFile ( join ( configDir , "dbt.json" ) , "{ invalid json !!!" )
91+
92+ // Create a dbt project so auto-discovery has something to find
93+ await writeFile ( join ( dir , "dbt_project.yml" ) , "name: test" )
94+ const binDir = join ( dir , ".venv" , "bin" )
95+ await mkdir ( binDir , { recursive : true } )
96+ await writeFile ( join ( binDir , "python3" ) , "#!/bin/sh" )
97+
98+ const origCwd = process . cwd ( )
99+ process . chdir ( dir )
100+ try {
101+ const result = await read ( )
102+ // Should fall through to auto-discovery instead of crashing
103+ expect ( result ) . not . toBeNull ( )
104+ expect ( result ! . dbtIntegration ) . toBe ( "corecommand" )
105+ } finally {
106+ process . chdir ( origCwd )
107+ }
108+ } )
109+
79110 test ( "read returns null when no config file and no dbt_project.yml in cwd" , async ( ) => {
80111 // dir has no dbt_project.yml and HOME has no config file
81112 const origCwd = process . cwd ( )
@@ -138,6 +169,32 @@ describe("discoverPython", () => {
138169 await rm ( dir , { recursive : true , force : true } )
139170 } )
140171
172+ test ( "ALTIMATE_CODE_PYTHON_PATH takes highest priority" , async ( ) => {
173+ const { discoverPython } = await import ( "../src/config" )
174+
175+ const altPythonBin = join ( dir , "alt-python" , "bin" )
176+ await mkdir ( altPythonBin , { recursive : true } )
177+ await writeFile ( join ( altPythonBin , "python3" ) , "#!/bin/sh" )
178+
179+ const localBin = join ( dir , "project" , ".venv" , "bin" )
180+ await mkdir ( localBin , { recursive : true } )
181+ await writeFile ( join ( localBin , "python3" ) , "#!/bin/sh" )
182+
183+ const origPython = process . env . ALTIMATE_CODE_PYTHON_PATH
184+ const origVenv = process . env . ALTIMATE_CODE_VIRTUAL_ENV
185+ process . env . ALTIMATE_CODE_PYTHON_PATH = join ( altPythonBin , "python3" )
186+ process . env . ALTIMATE_CODE_VIRTUAL_ENV = join ( dir , "project" , ".venv" )
187+ try {
188+ const result = discoverPython ( join ( dir , "project" ) )
189+ expect ( result ) . toBe ( join ( altPythonBin , "python3" ) )
190+ } finally {
191+ if ( origPython !== undefined ) process . env . ALTIMATE_CODE_PYTHON_PATH = origPython
192+ else delete process . env . ALTIMATE_CODE_PYTHON_PATH
193+ if ( origVenv !== undefined ) process . env . ALTIMATE_CODE_VIRTUAL_ENV = origVenv
194+ else delete process . env . ALTIMATE_CODE_VIRTUAL_ENV
195+ }
196+ } )
197+
141198 test ( "ALTIMATE_CODE_VIRTUAL_ENV takes priority over project-local .venv" , async ( ) => {
142199 const { discoverPython } = await import ( "../src/config" )
143200
@@ -163,8 +220,10 @@ describe("discoverPython", () => {
163220 test ( "falls back to project-local .venv/bin/python3" , async ( ) => {
164221 const { discoverPython } = await import ( "../src/config" )
165222
166- const orig = process . env . ALTIMATE_CODE_VIRTUAL_ENV
223+ const origVenv = process . env . ALTIMATE_CODE_VIRTUAL_ENV
224+ const origPython = process . env . ALTIMATE_CODE_PYTHON_PATH
167225 delete process . env . ALTIMATE_CODE_VIRTUAL_ENV
226+ delete process . env . ALTIMATE_CODE_PYTHON_PATH
168227
169228 const binDir = join ( dir , ".venv" , "bin" )
170229 await mkdir ( binDir , { recursive : true } )
@@ -174,15 +233,18 @@ describe("discoverPython", () => {
174233 const result = discoverPython ( dir )
175234 expect ( result ) . toBe ( join ( binDir , "python3" ) )
176235 } finally {
177- if ( orig !== undefined ) process . env . ALTIMATE_CODE_VIRTUAL_ENV = orig
236+ if ( origVenv !== undefined ) process . env . ALTIMATE_CODE_VIRTUAL_ENV = origVenv
237+ if ( origPython !== undefined ) process . env . ALTIMATE_CODE_PYTHON_PATH = origPython
178238 }
179239 } )
180240
181241 test ( "tries python3 before python in each location" , async ( ) => {
182242 const { discoverPython } = await import ( "../src/config" )
183243
184- const orig = process . env . ALTIMATE_CODE_VIRTUAL_ENV
244+ const origVenv = process . env . ALTIMATE_CODE_VIRTUAL_ENV
245+ const origPython = process . env . ALTIMATE_CODE_PYTHON_PATH
185246 delete process . env . ALTIMATE_CODE_VIRTUAL_ENV
247+ delete process . env . ALTIMATE_CODE_PYTHON_PATH
186248
187249 const binDir = join ( dir , ".venv" , "bin" )
188250 await mkdir ( binDir , { recursive : true } )
@@ -193,7 +255,8 @@ describe("discoverPython", () => {
193255 const result = discoverPython ( dir )
194256 expect ( result ) . toBe ( join ( binDir , "python3" ) )
195257 } finally {
196- if ( orig !== undefined ) process . env . ALTIMATE_CODE_VIRTUAL_ENV = orig
258+ if ( origVenv !== undefined ) process . env . ALTIMATE_CODE_VIRTUAL_ENV = origVenv
259+ if ( origPython !== undefined ) process . env . ALTIMATE_CODE_PYTHON_PATH = origPython
197260 }
198261 } )
199262} )
0 commit comments