@@ -1401,30 +1401,99 @@ public async Task NoResetOnClose(bool noResetOnClose)
14011401 }
14021402
14031403 [ Test ]
1404- [ NonParallelizable ]
1405- public async Task UsePgPassFile ( )
1404+ public async Task Use_pgpass_from_connection_string ( )
14061405 {
14071406 using var resetPassword = SetEnvironmentVariable ( "PGPASSWORD" , null ) ;
14081407 var builder = new NpgsqlConnectionStringBuilder ( ConnectionString ) ;
14091408
14101409 var password = builder . Password ;
1411- var passFile = Path . GetTempFileName ( ) ;
1410+ builder . Password = null ;
14121411
1413- builder . Password = password ;
1412+ var passFile = Path . GetTempFileName ( ) ;
1413+ File . WriteAllText ( passFile , $ "*:*:*:{ builder . Username } :{ password } ") ;
14141414 builder . Passfile = passFile ;
14151415
1416- using var deletePassFile = Defer ( ( ) => File . Delete ( passFile ) ) ;
1416+ try
1417+ {
1418+ using var pool = CreateTempPool ( builder . ConnectionString , out var connectionString ) ;
1419+ using var conn = await OpenConnectionAsync ( connectionString ) ;
1420+ }
1421+ finally
1422+ {
1423+ File . Delete ( passFile ) ;
1424+ }
1425+ }
14171426
1418- File . WriteAllText ( passFile , $ "*:*:*:{ builder . Username } :{ password } ") ;
1427+ [ Test ]
1428+ [ NonParallelizable ]
1429+ public async Task Use_pgpass_from_environment_variable ( )
1430+ {
1431+ using var resetPassword = SetEnvironmentVariable ( "PGPASSWORD" , null ) ;
1432+ var builder = new NpgsqlConnectionStringBuilder ( ConnectionString ) ;
14191433
1434+ var password = builder . Password ;
1435+ builder . Password = null ;
1436+
1437+ var passFile = Path . GetTempFileName ( ) ;
1438+ File . WriteAllText ( passFile , $ "*:*:*:{ builder . Username } :{ password } ") ;
14201439 using var passFileVariable = SetEnvironmentVariable ( "PGPASSFILE" , passFile ) ;
1421- using var pool = CreateTempPool ( builder . ConnectionString , out var connectionString ) ;
1422- using var conn = await OpenConnectionAsync ( connectionString ) ;
1440+
1441+ try
1442+ {
1443+ using var pool = CreateTempPool ( builder . ConnectionString , out var connectionString ) ;
1444+ using var conn = await OpenConnectionAsync ( connectionString ) ;
1445+ }
1446+ finally
1447+ {
1448+ File . Delete ( passFile ) ;
1449+ }
1450+ }
1451+
1452+ [ Test ]
1453+ [ NonParallelizable ]
1454+ public async Task Use_pgpass_from_homedir ( )
1455+ {
1456+ using var resetPassword = SetEnvironmentVariable ( "PGPASSWORD" , null ) ;
1457+ var builder = new NpgsqlConnectionStringBuilder ( ConnectionString ) ;
1458+
1459+ var password = builder . Password ;
1460+ builder . Password = null ;
1461+
1462+ string ? dirToDelete = null ;
1463+ string passFile ;
1464+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
1465+ {
1466+ var dir = Path . Combine ( Environment . GetEnvironmentVariable ( "APPDATA" ) ! , "postgresql" ) ;
1467+ if ( ! Directory . Exists ( dir ) )
1468+ {
1469+ Directory . CreateDirectory ( dir ) ;
1470+ dirToDelete = dir ;
1471+
1472+ }
1473+ passFile = Path . Combine ( dir , "pgpass.conf" ) ;
1474+ }
1475+ else
1476+ {
1477+ passFile = Path . Combine ( Environment . GetEnvironmentVariable ( "HOME" ) ! , ".pgpass" ) ;
1478+ }
1479+
1480+ try
1481+ {
1482+ File . WriteAllText ( passFile , $ "*:*:*:{ builder . Username } :{ password } ") ;
1483+ using var pool = CreateTempPool ( builder . ConnectionString , out var connectionString ) ;
1484+ using var conn = await OpenConnectionAsync ( connectionString ) ;
1485+ }
1486+ finally
1487+ {
1488+ File . Delete ( passFile ) ;
1489+ if ( dirToDelete is not null )
1490+ Directory . Delete ( dirToDelete ) ;
1491+ }
14231492 }
14241493
14251494 [ Test ]
14261495 [ NonParallelizable ]
1427- public void PasswordSourcePrecendence ( )
1496+ public void PasswordSourcePrecedence ( )
14281497 {
14291498 using var resetPassword = SetEnvironmentVariable ( "PGPASSWORD" , null ) ;
14301499 var builder = new NpgsqlConnectionStringBuilder ( ConnectionString ) ;
@@ -1464,7 +1533,7 @@ Func<ValueTask> OpenConnection(string? password, string? passFile) => async () =
14641533 builder . Password = password ;
14651534 builder . Passfile = passFile ;
14661535 builder . IntegratedSecurity = false ;
1467- builder . ApplicationName = $ "{ nameof ( PasswordSourcePrecendence ) } :{ Guid . NewGuid ( ) } ";
1536+ builder . ApplicationName = $ "{ nameof ( PasswordSourcePrecedence ) } :{ Guid . NewGuid ( ) } ";
14681537
14691538 using var pool = CreateTempPool ( builder . ConnectionString , out var connectionString ) ;
14701539 using var connection = await OpenConnectionAsync ( connectionString ) ;
0 commit comments