1- import netrc , os , unittest , sys , textwrap , tempfile
1+ import netrc , os , unittest , sys , textwrap
22
33from test import support
44from contextlib import ExitStack
1111
1212class NetrcEnvironment :
1313 """
14- Context manager for setting up an isolated environment to test `.netrc` file handling.
14+ Context manager for setting up an isolated environment to test `.netrc` file
15+ handling.
1516
16- This class configures a temporary directory for the `.netrc` file and environment variables, providing
17- a controlled setup to simulate different scenarios.
17+ This class configures a temporary directory for the `.netrc` file and
18+ environment variables, providing a controlled setup to simulate different
19+ scenarios.
1820 """
1921
20- def __enter__ (self ) -> 'NetrcEnvironment' :
22+ def __enter__ (self ):
2123 """
22- Enters the managed environment.
24+ Enter the managed environment.
2325 """
2426 self .stack = ExitStack ()
25- self .environ = self .stack .enter_context (support .os_helper .EnvironmentVarGuard ())
26- self .tmpdir = self .stack .enter_context (tempfile .TemporaryDirectory ())
27+ self .environ = self .stack .enter_context (
28+ support .os_helper .EnvironmentVarGuard (),
29+ )
30+ self .tmpdir = self .stack .enter_context (support .os_helper .temp_dir ())
2731 return self
2832
29- def __exit__ (self , * ignore_exc ) -> None :
33+ def __exit__ (self , * ignore_exc ):
3034 """
31- Exits the managed environment and performs cleanup. This method closes the `ExitStack`,
32- which automatically cleans up the temporary directory and environment.
35+ Exit the managed environment and performs cleanup. This method closes
36+ the `ExitStack`, which automatically cleans up the temporary directory
37+ and environment.
3338 """
3439 self .stack .close ()
3540
36- def generate_netrc (self , content , filename = ".netrc" , mode = 0o600 , encoding = "utf-8" ) -> str :
37- """
38- Creates a `.netrc` file in the temporary directory with the given content and permissions.
39-
40- Args:
41- content (str): The content to write into the `.netrc` file.
42- filename (str, optional): The name of the file to write. Defaults to ".netrc".
43- mode (int, optional): File permission bits to set after writing. Defaults to `0o600`. Mode
44- is set only if the platform supports `chmod`.
45- encoding (str, optional): The encoding used to write the file. Defaults to "utf-8".
46-
47- Returns:
48- str: The full path to the generated `.netrc` file.
49- """
41+ def generate_netrc (
42+ self ,
43+ content ,
44+ filename = ".netrc" ,
45+ mode = 0o600 ,
46+ encoding = "utf-8" ,
47+ ):
48+ """Create and return the path to a temporary `.netrc` file."""
5049 write_mode = "w"
5150 if sys .platform != "cygwin" :
5251 write_mode += "t"
@@ -62,19 +61,14 @@ def generate_netrc(self, content, filename=".netrc", mode=0o600, encoding="utf-8
6261
6362
6463class NetrcBuilder :
65- """
66- Utility class to construct and load `netrc.netrc` instances using different configuration scenarios.
67-
68- This class provides static methods to simulate different ways the `netrc` module can locate and load
69- a `.netrc` file.
70-
71- These methods are useful for testing or mocking `.netrc` behavior in different system environments.
64+ """Utility class to construct and load `netrc.netrc` instances using
65+ different configuration scenarios.
7266 """
7367
7468 @staticmethod
75- def use_default_netrc_in_home (* args , ** kwargs ) -> netrc . netrc :
76- """
77- Loads an instance of netrc using the default `.netrc` file from the user's home directory.
69+ def use_default_netrc_in_home (* args , ** kwargs ):
70+ """Load an instance of netrc using the default `.netrc` file from the
71+ user's home directory.
7872 """
7973 with NetrcEnvironment () as helper :
8074 helper .environ .unset ("NETRC" )
@@ -84,9 +78,9 @@ def use_default_netrc_in_home(*args, **kwargs) -> netrc.netrc:
8478 return netrc .netrc ()
8579
8680 @staticmethod
87- def use_netrc_envvar (* args , ** kwargs ) -> netrc . netrc :
88- """
89- Loads an instance of the netrc using the `.netrc` file specified by the `NETRC` environment variable.
81+ def use_netrc_envvar (* args , ** kwargs ):
82+ """Load an instance of the netrc using the `.netrc` file specified by
83+ the `NETRC` environment variable.
9084 """
9185 with NetrcEnvironment () as helper :
9286 netrc_file = helper .generate_netrc (* args , ** kwargs )
@@ -95,24 +89,23 @@ def use_netrc_envvar(*args, **kwargs) -> netrc.netrc:
9589 return netrc .netrc ()
9690
9791 @staticmethod
98- def use_file_argument (* args , ** kwargs ) -> netrc .netrc :
99- """
100- Loads an instance of `.netrc` file using the file as argument.
92+ def use_file_argument (* args , ** kwargs ):
93+ """Load an instance of `.netrc` file using the file as argument.
10194 """
10295 with NetrcEnvironment () as helper :
103- # Just to stress a bit more the test scenario, the NETRC envvar will contain
104- # rubish information which shouldn't be used
96+ # Just to stress a bit more the test scenario, the NETRC envvar
97+ # will contain rubish information which shouldn't be used
10598 helper .environ .set ("NETRC" , "not-a-file.netrc" )
10699
107100 netrc_file = helper .generate_netrc (* args , ** kwargs )
108101 return netrc .netrc (netrc_file )
109102
110103 @staticmethod
111104 def get_all_scenarios ():
112- """
113- Returns all `.netrc` loading scenarios as callables.
105+ """Return all `.netrc` loading scenarios as callables.
114106
115- This method is useful for iterating through all supported ways the `.netrc` file can be located.
107+ This method is useful for iterating through all supported ways the
108+ `.netrc` file can be located.
116109 """
117110 return (NetrcBuilder .use_default_netrc_in_home ,
118111 NetrcBuilder .use_netrc_envvar ,
@@ -128,7 +121,8 @@ def test_toplevel_non_ordered_tokens(self, make_nrc):
128121 machine host.domain.com password pass1 login log1 account acct1
129122 default login log2 password pass2 account acct2
130123 """ )
131- self .assertEqual (nrc .hosts ['host.domain.com' ], ('log1' , 'acct1' , 'pass1' ))
124+ self .assertEqual (nrc .hosts ['host.domain.com' ],
125+ ('log1' , 'acct1' , 'pass1' ))
132126 self .assertEqual (nrc .hosts ['default' ], ('log2' , 'acct2' , 'pass2' ))
133127
134128 @support .subTests ('make_nrc' , ALL_NETRC_FILE_SCENARIOS )
@@ -137,7 +131,8 @@ def test_toplevel_tokens(self, make_nrc):
137131 machine host.domain.com login log1 password pass1 account acct1
138132 default login log2 password pass2 account acct2
139133 """ )
140- self .assertEqual (nrc .hosts ['host.domain.com' ], ('log1' , 'acct1' , 'pass1' ))
134+ self .assertEqual (nrc .hosts ['host.domain.com' ],
135+ ('log1' , 'acct1' , 'pass1' ))
141136 self .assertEqual (nrc .hosts ['default' ], ('log2' , 'acct2' , 'pass2' ))
142137
143138 @support .subTests ('make_nrc' , ALL_NETRC_FILE_SCENARIOS )
0 commit comments