44 "fmt"
55 "os"
66 "path/filepath"
7+ "runtime"
78 "strings"
89 "testing"
910
@@ -131,7 +132,11 @@ func TestGetCacheDir(t *testing.T) {
131132
132133 t .Run ("valid home directory" , func (t * testing.T ) {
133134 tmpDir := t .TempDir ()
134- os .Setenv ("HOME" , tmpDir )
135+ if runtime .GOOS == "windows" {
136+ os .Setenv ("USERPROFILE" , tmpDir )
137+ } else {
138+ os .Setenv ("HOME" , tmpDir )
139+ }
135140
136141 got , err := getCacheDir ()
137142 if err != nil {
@@ -152,7 +157,11 @@ func TestGetCacheDir(t *testing.T) {
152157
153158 t .Run ("invalid home directory" , func (t * testing.T ) {
154159 // Set HOME to a non-existent directory
155- os .Setenv ("HOME" , "/nonexistent/path" )
160+ if runtime .GOOS == "windows" {
161+ os .Setenv ("USERPROFILE" , "/nonexistent/path" )
162+ } else {
163+ os .Setenv ("HOME" , "/nonexistent/path" )
164+ }
156165
157166 _ , err := getCacheDir ()
158167 if err == nil {
@@ -162,18 +171,34 @@ func TestGetCacheDir(t *testing.T) {
162171}
163172
164173func TestUpdatePkgConfig (t * testing.T ) {
165- t .Run ("valid pkg-config files" , func (t * testing.T ) {
174+ t .Run ("freethreaded pkg-config files" , func (t * testing.T ) {
166175 // Create temporary directory structure
167176 tmpDir := t .TempDir ()
168177 pkgConfigDir := env .GetPythonPkgConfigDir (tmpDir )
169178 if err := os .MkdirAll (pkgConfigDir , 0755 ); err != nil {
170179 t .Fatal (err )
171180 }
172181
173- // Create test .pc files
182+ // Create test .pc files with freethreaded content
174183 testFiles := map [string ]string {
175- "python-3.13t.pc" : "prefix=/install\n libdir=${prefix}/lib\n " ,
176- "python-3.13-embed.pc" : "prefix=/install\n libdir=${prefix}/lib\n " ,
184+ "python-3.13t.pc" : `prefix=/install
185+ libdir=${prefix}/lib
186+ includedir=${prefix}/include
187+
188+ Name: Python
189+ Description: Python library
190+ Version: 3.13
191+ Libs: -L${libdir} -lpython3t
192+ Cflags: -I${includedir}` ,
193+ "python-3.13t-embed.pc" : `prefix=/install
194+ libdir=${prefix}/lib
195+ includedir=${prefix}/include
196+
197+ Name: Python
198+ Description: Embed Python into an application
199+ Version: 3.13
200+ Libs: -L${libdir} -lpython313t
201+ Cflags: -I${includedir}` ,
177202 }
178203
179204 for filename , content := range testFiles {
@@ -189,19 +214,29 @@ func TestUpdatePkgConfig(t *testing.T) {
189214 }
190215
191216 // Verify the generated files
192- expectedFiles := []string {
193- "python-3.13t.pc" ,
194- "python-3.13.pc" ,
195- "python3t.pc" ,
196- "python3.pc" ,
197- "python-3.13-embed.pc" ,
198- "python3-embed.pc" ,
217+ expectedFiles := map [string ]struct {
218+ shouldExist bool
219+ libName string
220+ }{
221+ // Freethreaded versions
222+ "python-3.13t.pc" : {true , "-lpython3t" },
223+ "python3t.pc" : {true , "-lpython3t" },
224+ "python-3.13t-embed.pc" : {true , "-lpython313t" },
225+ "python3t-embed.pc" : {true , "-lpython313t" },
226+ // Non-t versions (same content as freethreaded)
227+ "python-3.13.pc" : {true , "-lpython3t" },
228+ "python3.pc" : {true , "-lpython3t" },
229+ "python-3.13-embed.pc" : {true , "-lpython313t" },
230+ "python3-embed.pc" : {true , "-lpython313t" },
199231 }
200232
201- for _ , filename := range expectedFiles {
233+ absPath , _ := filepath .Abs (filepath .Join (tmpDir , ".deps/python" ))
234+ for filename , expected := range expectedFiles {
202235 path := filepath .Join (pkgConfigDir , filename )
203236 if _ , err := os .Stat (path ); os .IsNotExist (err ) {
204- t .Errorf ("Expected file %s was not created" , filename )
237+ if expected .shouldExist {
238+ t .Errorf ("Expected file %s was not created" , filename )
239+ }
205240 continue
206241 }
207242
@@ -211,11 +246,98 @@ func TestUpdatePkgConfig(t *testing.T) {
211246 continue
212247 }
213248
214- absPath , _ := filepath . Abs ( filepath . Join ( tmpDir , ".deps/python" ))
249+ // Check prefix
215250 expectedPrefix := fmt .Sprintf ("prefix=%s" , absPath )
216251 if ! strings .Contains (string (content ), expectedPrefix ) {
217252 t .Errorf ("File %s does not contain expected prefix %s" , filename , expectedPrefix )
218253 }
254+
255+ // Check library name
256+ if ! strings .Contains (string (content ), expected .libName ) {
257+ t .Errorf ("File %s does not contain expected library name %s" , filename , expected .libName )
258+ }
259+ }
260+ })
261+
262+ t .Run ("non-freethreaded pkg-config files" , func (t * testing.T ) {
263+ // Create temporary directory structure
264+ tmpDir := t .TempDir ()
265+ pkgConfigDir := env .GetPythonPkgConfigDir (tmpDir )
266+ if err := os .MkdirAll (pkgConfigDir , 0755 ); err != nil {
267+ t .Fatal (err )
268+ }
269+
270+ // Create test .pc files with non-freethreaded content
271+ testFiles := map [string ]string {
272+ "python-3.13.pc" : `prefix=/install
273+ libdir=${prefix}/lib
274+ includedir=${prefix}/include
275+
276+ Name: Python
277+ Description: Python library
278+ Version: 3.13
279+ Libs: -L${libdir} -lpython3
280+ Cflags: -I${includedir}` ,
281+ "python-3.13-embed.pc" : `prefix=/install
282+ libdir=${prefix}/lib
283+ includedir=${prefix}/include
284+
285+ Name: Python
286+ Description: Embed Python into an application
287+ Version: 3.13
288+ Libs: -L${libdir} -lpython313
289+ Cflags: -I${includedir}` ,
290+ }
291+
292+ for filename , content := range testFiles {
293+ if err := os .WriteFile (filepath .Join (pkgConfigDir , filename ), []byte (content ), 0644 ); err != nil {
294+ t .Fatal (err )
295+ }
296+ }
297+
298+ // Test updating pkg-config files
299+ if err := updatePkgConfig (tmpDir ); err != nil {
300+ t .Errorf ("updatePkgConfig() error = %v, want nil" , err )
301+ return
302+ }
303+
304+ // Verify the generated files
305+ expectedFiles := map [string ]struct {
306+ shouldExist bool
307+ libName string
308+ }{
309+ "python-3.13.pc" : {true , "-lpython3" },
310+ "python3.pc" : {true , "-lpython3" },
311+ "python-3.13-embed.pc" : {true , "-lpython313" },
312+ "python3-embed.pc" : {true , "-lpython313" },
313+ }
314+
315+ absPath , _ := filepath .Abs (filepath .Join (tmpDir , ".deps/python" ))
316+ for filename , expected := range expectedFiles {
317+ path := filepath .Join (pkgConfigDir , filename )
318+ if _ , err := os .Stat (path ); os .IsNotExist (err ) {
319+ if expected .shouldExist {
320+ t .Errorf ("Expected file %s was not created" , filename )
321+ }
322+ continue
323+ }
324+
325+ content , err := os .ReadFile (path )
326+ if err != nil {
327+ t .Errorf ("Failed to read file %s: %v" , filename , err )
328+ continue
329+ }
330+
331+ // Check prefix
332+ expectedPrefix := fmt .Sprintf ("prefix=%s" , absPath )
333+ if ! strings .Contains (string (content ), expectedPrefix ) {
334+ t .Errorf ("File %s does not contain expected prefix %s" , filename , expectedPrefix )
335+ }
336+
337+ // Check library name
338+ if ! strings .Contains (string (content ), expected .libName ) {
339+ t .Errorf ("File %s does not contain expected library name %s" , filename , expected .libName )
340+ }
219341 }
220342 })
221343
0 commit comments