22# copyright 2009, James William Pye
33# http://python.projects.postgresql.org
44##
5+ import os
56import unittest
67from io import StringIO
78from .. import configfile
3233winning_cases = [
3334 (
3435 # Two top contenders; the first should be altered, second commented.
35- "foo = bar\n foo = bar" ,
36+ "foo = bar" + os . linesep + "foo = bar" ,
3637 {'foo' : 'newbar' },
37- "foo = 'newbar'\n #foo = bar"
38+ "foo = 'newbar'" + os . linesep + " #foo = bar"
3839 ),
3940 (
4041 # Two top contenders, first one stays commented
41- "#foo = bar\n foo = bar" ,
42+ "#foo = bar" + os . linesep + "foo = bar" ,
4243 {'foo' : 'newbar' },
43- "#foo = bar\n foo = 'newbar'"
44+ "#foo = bar" + os . linesep + "foo = 'newbar'"
4445 ),
4546 (
4647 # Two top contenders, second one stays commented
47- "foo = bar\n #foo = bar" ,
48+ "foo = bar" + os . linesep + " #foo = bar" ,
4849 {'foo' : 'newbar' },
49- "foo = 'newbar'\n #foo = bar"
50+ "foo = 'newbar'" + os . linesep + " #foo = bar"
5051 ),
5152 (
5253 # Two candidates
53- "foo = bar\n foo = none" ,
54+ "foo = bar" + os . linesep + "foo = none" ,
5455 {'foo' : 'bar' },
55- "foo = 'bar'\n #foo = none"
56+ "foo = 'bar'" + os . linesep + " #foo = none"
5657 ),
5758 (
5859 # Two candidates, winner should be the first, second gets comment
59- "#foo = none\n foo = bar" ,
60+ "#foo = none" + os . linesep + "foo = bar" ,
6061 {'foo' : 'none' },
61- "foo = 'none'\n #foo = bar"
62+ "foo = 'none'" + os . linesep + " #foo = bar"
6263 ),
6364 (
6465 # Two commented candidates
65- "#foo = none\n #foo = some" ,
66+ "#foo = none" + os . linesep + " #foo = some" ,
6667 {'foo' : 'bar' },
67- "foo = 'bar'\n #foo = some"
68+ "foo = 'bar'" + os . linesep + " #foo = some"
6869 ),
6970 (
7071 # Two commented candidates, the latter a top contender
71- "#foo = none\n #foo = bar" ,
72+ "#foo = none" + os . linesep + " #foo = bar" ,
7273 {'foo' : 'bar' },
73- "#foo = none\n foo = 'bar'"
74+ "#foo = none" + os . linesep + "foo = 'bar'"
7475 ),
7576 (
7677 # Replace empty value
77- "foo = \n " ,
78+ "foo = " + os . linesep ,
7879 {'foo' : 'feh' },
7980 "foo = 'feh'"
8081 ),
118119 # New setting
119120 "foo = 'bar'" ,
120121 {'bar' : 'newvar' },
121- "foo = 'bar'\n bar = 'newvar'" ,
122+ "foo = 'bar'" + os . linesep + "bar = 'newvar'" ,
122123 ),
123124 (
124125 # New setting with quote escape
125126 "foo = 'bar'" ,
126127 {'bar' : "new'var" },
127- "foo = 'bar'\n bar = 'new''var'" ,
128+ "foo = 'bar'" + os . linesep + "bar = 'new''var'" ,
128129 ),
129130]
130131
@@ -167,25 +168,25 @@ def parseExpect(self, line, key, val):
167168
168169 def testParser (self ):
169170 self .parseExpect ("#%s = %s" , 'foo' , 'none' )
170- self .parseExpect ("#%s=%s\n " , 'foo' , 'bar' )
171- self .parseExpect (" #%s=%s\n " , 'foo' , 'bar' )
172- self .parseExpect ('%s =%s\n ' , 'foo' , 'bar' )
173- self .parseExpect (' %s=%s \n ' , 'foo' , 'Bar' )
174- self .parseExpect (' %s = %s \n ' , 'foo' , 'Bar' )
175- self .parseExpect ('# %s = %s \n ' , 'foo' , 'Bar' )
176- self .parseExpect ('\t # %s = %s \n ' , 'foo' , 'Bar' )
177- self .parseExpect (' # %s = %s \n ' , 'foo' , 'Bar' )
178- self .parseExpect (" # %s = %s\n " , 'foo' , "' Bar '" )
179- self .parseExpect ("%s = %s# comment\n " , 'foo' , '' )
180- self .parseExpect (" # %s = %s # A # comment\n " , 'foo' , "' B''a#r '" )
171+ self .parseExpect ("#%s=%s" + os . linesep , 'foo' , 'bar' )
172+ self .parseExpect (" #%s=%s" + os . linesep , 'foo' , 'bar' )
173+ self .parseExpect ('%s =%s' + os . linesep , 'foo' , 'bar' )
174+ self .parseExpect (' %s=%s ' + os . linesep , 'foo' , 'Bar' )
175+ self .parseExpect (' %s = %s ' + os . linesep , 'foo' , 'Bar' )
176+ self .parseExpect ('# %s = %s ' + os . linesep , 'foo' , 'Bar' )
177+ self .parseExpect ('\t # %s = %s ' + os . linesep , 'foo' , 'Bar' )
178+ self .parseExpect (' # %s = %s ' + os . linesep , 'foo' , 'Bar' )
179+ self .parseExpect (" # %s = %s" + os . linesep , 'foo' , "' Bar '" )
180+ self .parseExpect ("%s = %s# comment" + os . linesep , 'foo' , '' )
181+ self .parseExpect (" # %s = %s # A # comment" + os . linesep , 'foo' , "' B''a#r '" )
181182 # No equality or equality in complex comment
182- self .parseNone (' #i # foo = Bar \n ' )
183+ self .parseNone (' #i # foo = Bar ' + os . linesep )
183184 self .parseNone ('#bar' )
184185 self .parseNone ('bar' )
185186
186187 def testConfigRead (self ):
187- sample = "foo = bar\n # A comment, yes.\n bar = foo # yet?\n "
188- d = configfile .read_config (sample .split (' \n ' ))
188+ sample = "foo = bar" + os . linesep + " # A comment, yes." + os . linesep + " bar = foo # yet?" + os . linesep
189+ d = configfile .read_config (sample .split (os . linesep ))
189190 self .failUnless (d ['foo' ] == 'bar' )
190191 self .failUnless (d ['bar' ] == 'foo' )
191192
@@ -200,7 +201,7 @@ def testConfigWriteRead(self):
200201 def testWinningCases (self ):
201202 i = 0
202203 for before , alters , after in winning_cases :
203- befg = (x + ' \n ' for x in before .split (' \n ' ))
204+ befg = (x + os . linesep for x in before .split (os . linesep ))
204205 became = '' .join (configfile .alter_config (alters , befg ))
205206 self .failUnless (
206207 became .strip () == after ,
@@ -213,7 +214,7 @@ def testWinningCases(self):
213214 def testSimpleConfigAlter (self ):
214215 # Simple set and uncomment and set test.
215216 strio = StringIO ()
216- strio .write ("foo = bar\n # bleh = unset\n # grr = 'oh yeah''s'" )
217+ strio .write ("foo = bar" + os . linesep + " # bleh = unset" + os . linesep + " # grr = 'oh yeah''s'" )
217218 strio .seek (0 )
218219 lines = configfile .alter_config ({'foo' : 'yes' , 'bleh' : 'feh' }, strio )
219220 d = configfile .read_config (lines )
@@ -225,7 +226,7 @@ def testAroma(self):
225226 lines = configfile .alter_config ({
226227 'shared_buffers' : '800' ,
227228 'port' : None
228- }, (x + ' \n ' for x in sample_config_Aroma .split ('\n ' ))
229+ }, (x + os . linesep for x in sample_config_Aroma .split ('\n ' ))
229230 )
230231 d = configfile .read_config (lines )
231232 self .failUnless (d ['shared_buffers' ] == '800' )
@@ -240,11 +241,11 @@ def testAroma(self):
240241
241242 def testSelection (self ):
242243 # Sanity
243- red = configfile .read_config (['foo = bar\n ' , 'bar = foo' ])
244+ red = configfile .read_config (['foo = bar' + os . linesep , 'bar = foo' ])
244245 self .failUnless (len (red .keys ()) == 2 )
245246
246247 # Test a simple selector
247- red = configfile .read_config (['foo = bar\n ' , 'bar = foo' ],
248+ red = configfile .read_config (['foo = bar' + os . linesep , 'bar = foo' ],
248249 selector = lambda x : x == 'bar' )
249250 rkeys = list (red .keys ())
250251 self .failUnless (len (rkeys ) == 1 )
0 commit comments