@@ -44,61 +44,55 @@ def generate_netrc(
4444 return netrc_file
4545
4646
47- class NetrcBuilder :
48- """Utility class to construct and load `netrc .netrc` instances using
49- different configuration scenarios .
47+ def use_default_netrc_in_home ( * args , ** kwargs ) :
48+ """Load an instance of netrc using the default ` .netrc` file from the
49+ user's home directory .
5050 """
51-
52- @staticmethod
53- def use_default_netrc_in_home (* args , ** kwargs ):
54- """Load an instance of netrc using the default `.netrc` file from the
55- user's home directory.
56- """
57- with NetrcEnvironment () as helper :
58- helper .generate_netrc (* args , ** kwargs )
59- helper .environ .unset ("NETRC" )
60- helper .environ .set ("HOME" , helper .tmpdir )
61- real_expanduser = os .path .expanduser
62- with mock .patch ("os.path.expanduser" ) as mock_expanduser :
63- mock_expanduser .side_effect = lambda arg : helper .tmpdir \
64- if arg == "~" else real_expanduser (arg )
65- return netrc .netrc ()
66-
67- @staticmethod
68- def use_netrc_envvar (* args , ** kwargs ):
69- """Load an instance of the netrc using the `.netrc` file specified by
70- the `NETRC` environment variable.
71- """
72- with NetrcEnvironment () as helper :
73- netrc_file = helper .generate_netrc (* args , ** kwargs )
74- helper .environ .set ("NETRC" , netrc_file )
51+ with NetrcEnvironment () as helper :
52+ helper .generate_netrc (* args , ** kwargs )
53+ helper .environ .unset ("NETRC" )
54+ helper .environ .set ("HOME" , helper .tmpdir )
55+ with mock .patch ("os.path.expanduser" ) as mock_expanduser :
56+ def fake_expanduser (path ):
57+ return helper .tmpdir if path == "~" else os .path .expanduser (path )
58+ mock_expanduser .side_effect = fake_expanduser
7559 return netrc .netrc ()
7660
77- @staticmethod
78- def use_file_argument (* args , ** kwargs ):
79- """Load an instance of `.netrc` file using the file as argument.
80- """
81- with NetrcEnvironment () as helper :
82- # Just to stress a bit more the test scenario, the NETRC envvar
83- # will contain rubish information which shouldn't be used
84- helper .environ .set ("NETRC" , "not-a-file.netrc" )
85- netrc_file = helper .generate_netrc (* args , ** kwargs )
86- return netrc .netrc (netrc_file )
87-
88- @staticmethod
89- def get_all_scenarios ():
90- """Return all `.netrc` loading scenarios as callables.
91-
92- This method is useful for iterating through all d ways the
93- `.netrc` file can be located.
94- """
95- return (NetrcBuilder .use_default_netrc_in_home ,
96- NetrcBuilder .use_netrc_envvar ,
97- NetrcBuilder .use_file_argument )
61+
62+ def use_netrc_envvar (* args , ** kwargs ):
63+ """Load an instance of the netrc using the `.netrc` file specified by
64+ the `NETRC` environment variable.
65+ """
66+ with NetrcEnvironment () as helper :
67+ netrc_file = helper .generate_netrc (* args , ** kwargs )
68+ helper .environ .set ("NETRC" , netrc_file )
69+ return netrc .netrc ()
70+
71+
72+ def use_file_argument (* args , ** kwargs ):
73+ """Load an instance of `.netrc` file using the file as argument.
74+ """
75+ with NetrcEnvironment () as helper :
76+ # Just to stress a bit more the test scenario, the NETRC envvar
77+ # will contain rubish information which shouldn't be used
78+ helper .environ .set ("NETRC" , "not-a-file.netrc" )
79+ netrc_file = helper .generate_netrc (* args , ** kwargs )
80+ return netrc .netrc (netrc_file )
81+
82+
83+ def get_all_scenarios ():
84+ """Return all `.netrc` loading scenarios as callables.
85+
86+ This method is useful for iterating through all ways the
87+ `.netrc` file can be located.
88+ """
89+ return (use_default_netrc_in_home ,
90+ use_netrc_envvar ,
91+ use_file_argument )
9892
9993
10094class NetrcTestCase (unittest .TestCase ):
101- ALL_NETRC_FILE_SCENARIOS = NetrcBuilder . get_all_scenarios ()
95+ ALL_NETRC_FILE_SCENARIOS = get_all_scenarios ()
10296
10397 @subTests ('make_nrc' , ALL_NETRC_FILE_SCENARIOS )
10498 def test_toplevel_non_ordered_tokens (self , make_nrc ):
0 commit comments