Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 1 | |
Jason Tishler | 0fd54d8 | 2003-08-11 12:13:14 +0000 | [diff] [blame] | 2 | import netrc, os, unittest, sys |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 3 | from test import test_support |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 4 | |
| 5 | TEST_NETRC = """ |
R. David Murray | d75cc91 | 2010-12-02 03:16:23 +0000 | [diff] [blame] | 6 | |
| 7 | #this is a comment |
| 8 | #this is a comment |
| 9 | # this is a comment |
| 10 | |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 11 | machine foo login log1 password pass1 account acct1 |
R. David Murray | d75cc91 | 2010-12-02 03:16:23 +0000 | [diff] [blame] | 12 | machine bar login log1 password pass# account acct1 |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 13 | |
| 14 | macdef macro1 |
| 15 | line1 |
| 16 | line2 |
| 17 | |
| 18 | macdef macro2 |
| 19 | line3 |
| 20 | line4 |
| 21 | |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 22 | default login log2 password pass2 |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 23 | |
| 24 | """ |
| 25 | |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 26 | temp_filename = test_support.TESTFN |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 27 | |
| 28 | class NetrcTestCase(unittest.TestCase): |
| 29 | |
| 30 | def setUp (self): |
Jason Tishler | 0fd54d8 | 2003-08-11 12:13:14 +0000 | [diff] [blame] | 31 | mode = 'w' |
| 32 | if sys.platform not in ['cygwin']: |
| 33 | mode += 't' |
| 34 | fp = open(temp_filename, mode) |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 35 | fp.write(TEST_NETRC) |
| 36 | fp.close() |
R. David Murray | d75cc91 | 2010-12-02 03:16:23 +0000 | [diff] [blame] | 37 | self.nrc = netrc.netrc(temp_filename) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 38 | |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 39 | def tearDown (self): |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 40 | os.unlink(temp_filename) |
| 41 | |
| 42 | def test_case_1(self): |
R. David Murray | d75cc91 | 2010-12-02 03:16:23 +0000 | [diff] [blame] | 43 | 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 Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 52 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 53 | def test_main(): |
| 54 | test_support.run_unittest(NetrcTestCase) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 55 | |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 56 | if __name__ == "__main__": |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 57 | test_main() |