~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Matt Nordhoff
  • Date: 2009-04-04 02:50:01 UTC
  • mfrom: (4253 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4256.
  • Revision ID: mnordhoff@mattnordhoff.com-20090404025001-z1403k0tatmc8l91
Merge bzr.dev, fixing conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for the osutils wrapper."""
18
18
 
19
19
from cStringIO import StringIO
20
20
import errno
21
21
import os
 
22
import re
22
23
import socket
23
24
import stat
24
25
import sys
40
41
        canonical_relpath,
41
42
        )
42
43
from bzrlib.tests import (
43
 
        adapt_tests,
44
44
        Feature,
45
45
        probe_unicode_in_user_encoding,
46
 
        split_suite_by_re,
47
46
        StringIOWrapper,
48
47
        SymlinkFeature,
49
48
        CaseInsCasePresFilenameFeature,
50
49
        TestCase,
51
50
        TestCaseInTempDir,
52
 
        TestScenarioApplier,
53
51
        TestSkipped,
54
52
        )
55
53
from bzrlib.tests.file_utils import (
171
169
                         (['src'], 'src'),
172
170
                         ]:
173
171
            self.assert_(is_inside_or_parent_of_any(dirs, fn))
174
 
            
 
172
 
175
173
        for dirs, fn in [(['src'], 'srccontrol'),
176
174
                         (['srccontrol/foo.c'], 'src'),
177
175
                         (['src'], 'srccontrol/foo')]:
202
200
        if osutils.has_symlinks():
203
201
            os.symlink('symlink', 'symlink')
204
202
            self.assertEquals('symlink', osutils.file_kind('symlink'))
205
 
        
 
203
 
206
204
        # TODO: jam 20060529 Test a block device
207
205
        try:
208
206
            os.lstat('/dev/null')
310
308
        self.assertEqual(bar_path, osutils.realpath('./bar'))
311
309
        os.symlink('bar', 'foo')
312
310
        self.assertEqual(bar_path, osutils.realpath('./foo'))
313
 
        
 
311
 
314
312
        # Does not dereference terminal symlinks
315
313
        foo_path = osutils.pathjoin(cwd, 'foo')
316
314
        self.assertEqual(foo_path, osutils.dereference_path('./foo'))
379
377
class TestPumpFile(TestCase):
380
378
    """Test pumpfile method."""
381
379
    def setUp(self):
 
380
        TestCase.setUp(self)
382
381
        # create a test datablock
383
382
        self.block_size = 512
384
383
        pattern = '0123456789ABCDEF'
692
691
 
693
692
class TestWin32FuncsDirs(TestCaseInTempDir):
694
693
    """Test win32 functions that create files."""
695
 
    
 
694
 
696
695
    def test_getcwd(self):
697
696
        if win32utils.winver == 'Windows 98':
698
697
            raise TestSkipped('Windows 98 cannot handle unicode filenames')
882
881
        self.assertEqual(expected_dirblocks[1:],
883
882
            [(dirinfo, [line[0:3] for line in block]) for dirinfo, block in result])
884
883
 
 
884
    def test_walkdirs_os_error(self):
 
885
        # <https://bugs.edge.launchpad.net/bzr/+bug/338653>
 
886
        # Pyrex readdir didn't raise useful messages if it had an error
 
887
        # reading the directory
 
888
        if sys.platform == 'win32':
 
889
            raise tests.TestNotApplicable(
 
890
                "readdir IOError not tested on win32")
 
891
        os.mkdir("test-unreadable")
 
892
        os.chmod("test-unreadable", 0000)
 
893
        # must chmod it back so that it can be removed
 
894
        self.addCleanup(lambda: os.chmod("test-unreadable", 0700))
 
895
        # The error is not raised until the generator is actually evaluated.
 
896
        # (It would be ok if it happened earlier but at the moment it
 
897
        # doesn't.)
 
898
        e = self.assertRaises(OSError, list, osutils._walkdirs_utf8("."))
 
899
        self.assertEquals('./test-unreadable', e.filename)
 
900
        self.assertEquals(errno.EACCES, e.errno)
 
901
        # Ensure the message contains the file name
 
902
        self.assertContainsRe(str(e), "\./test-unreadable")
 
903
 
885
904
    def test__walkdirs_utf8(self):
886
905
        tree = [
887
906
            '.bzr',
1330
1349
 
1331
1350
 
1332
1351
class TestCopyTree(TestCaseInTempDir):
1333
 
    
 
1352
 
1334
1353
    def test_copy_basic_tree(self):
1335
1354
        self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
1336
1355
        osutils.copy_tree('source', 'target')
1415
1434
 
1416
1435
    def test_unicode(self):
1417
1436
        """Environment can only contain plain strings
1418
 
        
 
1437
 
1419
1438
        So Unicode strings must be encoded.
1420
1439
        """
1421
1440
        uni_val, env_val = probe_unicode_in_user_encoding()
1457
1476
        self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1458
1477
 
1459
1478
 
 
1479
class TestSizeShaFile(TestCaseInTempDir):
 
1480
 
 
1481
    def test_sha_empty(self):
 
1482
        self.build_tree_contents([('foo', '')])
 
1483
        expected_sha = osutils.sha_string('')
 
1484
        f = open('foo')
 
1485
        self.addCleanup(f.close)
 
1486
        size, sha = osutils.size_sha_file(f)
 
1487
        self.assertEqual(0, size)
 
1488
        self.assertEqual(expected_sha, sha)
 
1489
 
 
1490
    def test_sha_mixed_endings(self):
 
1491
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
 
1492
        self.build_tree_contents([('foo', text)])
 
1493
        expected_sha = osutils.sha_string(text)
 
1494
        f = open('foo')
 
1495
        self.addCleanup(f.close)
 
1496
        size, sha = osutils.size_sha_file(f)
 
1497
        self.assertEqual(38, size)
 
1498
        self.assertEqual(expected_sha, sha)
 
1499
 
 
1500
 
1460
1501
class TestShaFileByName(TestCaseInTempDir):
1461
1502
 
1462
1503
    def test_sha_empty(self):
1485
1526
            'yyy.xx')
1486
1527
        # test unknown resource
1487
1528
        self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
 
1529
 
 
1530
 
 
1531
class TestReCompile(TestCase):
 
1532
 
 
1533
    def test_re_compile_checked(self):
 
1534
        r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
 
1535
        self.assertTrue(r.match('aaaa'))
 
1536
        self.assertTrue(r.match('aAaA'))
 
1537
 
 
1538
    def test_re_compile_checked_error(self):
 
1539
        # like https://bugs.launchpad.net/bzr/+bug/251352
 
1540
        err = self.assertRaises(
 
1541
            errors.BzrCommandError,
 
1542
            osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
 
1543
        self.assertEqual(
 
1544
            "Invalid regular expression in test case: '*': "
 
1545
            "nothing to repeat",
 
1546
            str(err))