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 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 4 | |
| 5 | TEST_NETRC = """ |
| 6 | machine foo login log1 password pass1 account acct1 |
| 7 | |
| 8 | macdef macro1 |
| 9 | line1 |
| 10 | line2 |
| 11 | |
| 12 | macdef macro2 |
| 13 | line3 |
| 14 | line4 |
| 15 | |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 16 | default login log2 password pass2 |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 17 | |
| 18 | """ |
| 19 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 20 | temp_filename = support.TESTFN |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 21 | |
| 22 | class NetrcTestCase(unittest.TestCase): |
| 23 | |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 24 | def setUp(self): |
Jason Tishler | 0fd54d8 | 2003-08-11 12:13:14 +0000 | [diff] [blame] | 25 | mode = 'w' |
| 26 | if sys.platform not in ['cygwin']: |
| 27 | mode += 't' |
| 28 | fp = open(temp_filename, mode) |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 29 | fp.write(TEST_NETRC) |
| 30 | fp.close() |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 32 | def tearDown(self): |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 33 | os.unlink(temp_filename) |
| 34 | |
| 35 | def test_case_1(self): |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 36 | nrc = netrc.netrc(temp_filename) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 37 | self.assertTrue(nrc.macros == {'macro1':['line1\n', 'line2\n'], |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 38 | 'macro2':['line3\n', 'line4\n']} |
| 39 | ) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 40 | self.assertTrue(nrc.hosts['foo'] == ('log1', 'acct1', 'pass1')) |
| 41 | self.assertTrue(nrc.hosts['default'] == ('log2', None, 'pass2')) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 42 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 43 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 44 | support.run_unittest(NetrcTestCase) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 45 | |
Andrew M. Kuchling | 76fffd8 | 2002-03-22 02:48:57 +0000 | [diff] [blame] | 46 | if __name__ == "__main__": |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 47 | test_main() |