blob: da7ec050d25ee7482833963c1fb8e67dace4dfc9 [file] [log] [blame]
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +00001
Jason Tishler0fd54d82003-08-11 12:13:14 +00002import netrc, os, unittest, sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +00004
5TEST_NETRC = """
R. David Murrayd2bb8302010-12-02 02:58:07 +00006
7 #this is a comment
8#this is a comment
9# this is a comment
10
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +000011machine foo login log1 password pass1 account acct1
R. David Murrayd2bb8302010-12-02 02:58:07 +000012machine bar login log1 password pass# account acct1
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +000013
14macdef macro1
15line1
16line2
17
18macdef macro2
19line3
20line4
21
Tim Peters863ac442002-04-16 01:38:40 +000022default login log2 password pass2
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +000023
24"""
25
Benjamin Petersonee8712c2008-05-20 21:35:26 +000026temp_filename = support.TESTFN
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +000027
28class NetrcTestCase(unittest.TestCase):
29
Guido van Rossumc12a8132007-10-26 04:29:23 +000030 def setUp(self):
Jason Tishler0fd54d82003-08-11 12:13:14 +000031 mode = 'w'
32 if sys.platform not in ['cygwin']:
33 mode += 't'
34 fp = open(temp_filename, mode)
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +000035 fp.write(TEST_NETRC)
36 fp.close()
R. David Murrayd2bb8302010-12-02 02:58:07 +000037 self.nrc = netrc.netrc(temp_filename)
Tim Peters863ac442002-04-16 01:38:40 +000038
Guido van Rossumc12a8132007-10-26 04:29:23 +000039 def tearDown(self):
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +000040 os.unlink(temp_filename)
41
42 def test_case_1(self):
R. David Murrayd2bb8302010-12-02 02:58:07 +000043 self.assertEqual(self.nrc.hosts['foo'], ('log1', 'acct1', 'pass1'))
44 self.assertEqual(self.nrc.hosts['default'], ('log2', None, 'pass2'))
45
46 def test_macros(self):
47 self.assertEqual(self.nrc.macros, {'macro1':['line1\n', 'line2\n'],
48 'macro2':['line3\n', 'line4\n']})
49
50 def test_parses_passwords_with_hash_character(self):
51 self.assertEqual(self.nrc.hosts['bar'], ('log1', 'acct1', 'pass#'))
Tim Peters863ac442002-04-16 01:38:40 +000052
Neal Norwitz996acf12003-02-17 14:51:41 +000053def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000054 support.run_unittest(NetrcTestCase)
Tim Peters863ac442002-04-16 01:38:40 +000055
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +000056if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +000057 test_main()