blob: a988430ae0197d0e8c2e3f71416404caab85eea5 [file] [log] [blame]
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +00001
Jason Tishler0fd54d82003-08-11 12:13:14 +00002import netrc, os, unittest, sys
Barry Warsaw04f357c2002-07-23 19:04:11 +00003from test import test_support
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +00004
5TEST_NETRC = """
R. David Murrayd75cc912010-12-02 03:16:23 +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 Murrayd75cc912010-12-02 03:16:23 +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
Guido van Rossum3b0a3292002-08-09 16:38:32 +000026temp_filename = test_support.TESTFN
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +000027
28class NetrcTestCase(unittest.TestCase):
29
30 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 Murrayd75cc912010-12-02 03:16:23 +000037 self.nrc = netrc.netrc(temp_filename)
Tim Peters863ac442002-04-16 01:38:40 +000038
Andrew M. Kuchling76fffd82002-03-22 02:48:57 +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 Murrayd75cc912010-12-02 03:16:23 +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():
54 test_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()