| OLD | NEW |
| 1 # Copyright (C) 2011 Google Inc. All rights reserved. | 1 # Copyright (C) 2011 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 class MockFileSystemTest(unittest.TestCase, filesystem_unittest.GenericFileSyste
mTests): | 37 class MockFileSystemTest(unittest.TestCase, filesystem_unittest.GenericFileSyste
mTests): |
| 38 | 38 |
| 39 def setUp(self): | 39 def setUp(self): |
| 40 self.fs = filesystem_mock.MockFileSystem() | 40 self.fs = filesystem_mock.MockFileSystem() |
| 41 self.setup_generic_test_dir() | 41 self.setup_generic_test_dir() |
| 42 | 42 |
| 43 def tearDown(self): | 43 def tearDown(self): |
| 44 self.teardown_generic_test_dir() | 44 self.teardown_generic_test_dir() |
| 45 self.fs = None | 45 self.fs = None |
| 46 | 46 |
| 47 def quick_check(self, test_fn, good_fn, *tests): | 47 def check_with_reference_function(self, test_function, good_function, tests)
: |
| 48 for test in tests: | 48 for test in tests: |
| 49 if hasattr(test, '__iter__'): | 49 if hasattr(test, '__iter__'): |
| 50 expected = good_fn(*test) | 50 expected = good_function(*test) |
| 51 actual = test_fn(*test) | 51 actual = test_function(*test) |
| 52 else: | 52 else: |
| 53 expected = good_fn(test) | 53 expected = good_function(test) |
| 54 actual = test_fn(test) | 54 actual = test_function(test) |
| 55 self.assertEqual(expected, actual, 'given %s, expected %s, got %s' %
(repr(test), repr(expected), repr(actual))) | 55 self.assertEqual(expected, actual, 'given %s, expected %s, got %s' %
(repr(test), repr(expected), repr(actual))) |
| 56 | 56 |
| 57 def test_join(self): | 57 def test_join(self): |
| 58 self.quick_check(self.fs.join, | 58 self.check_with_reference_function( |
| 59 self.fs._slow_but_correct_join, | 59 self.fs.join, |
| 60 ('',), | 60 self.fs._slow_but_correct_join, |
| 61 ('', 'bar'), | 61 [ |
| 62 ('foo',), | 62 ('',), |
| 63 ('foo/',), | 63 ('', 'bar'), |
| 64 ('foo', ''), | 64 ('foo',), |
| 65 ('foo/', ''), | 65 ('foo/',), |
| 66 ('foo', 'bar'), | 66 ('foo', ''), |
| 67 ('foo', '/bar'), | 67 ('foo/', ''), |
| 68 ) | 68 ('foo', 'bar'), |
| 69 ('foo', '/bar'), |
| 70 ]) |
| 69 | 71 |
| 70 def test_normpath(self): | 72 def test_normpath(self): |
| 71 self.quick_check(self.fs.normpath, | 73 self.check_with_reference_function( |
| 72 self.fs._slow_but_correct_normpath, | 74 self.fs.normpath, |
| 73 '', | 75 self.fs._slow_but_correct_normpath, |
| 74 '/', | 76 [ |
| 75 '.', | 77 '', |
| 76 '/.', | 78 '/', |
| 77 'foo', | 79 '.', |
| 78 'foo/', | 80 '/.', |
| 79 'foo/.', | 81 'foo', |
| 80 'foo/bar', | 82 'foo/', |
| 81 '/foo', | 83 'foo/.', |
| 82 'foo/../bar', | 84 'foo/bar', |
| 83 'foo/../bar/baz', | 85 '/foo', |
| 84 '../foo') | 86 'foo/../bar', |
| 87 'foo/../bar/baz', |
| 88 '../foo', |
| 89 ]) |
| 85 | 90 |
| 86 def test_relpath_win32(self): | 91 def test_abspath_given_abs_path(self): |
| 87 pass | 92 self.assertEqual(self.fs.abspath('/some/path'), '/some/path') |
| 93 |
| 94 def test_abspath_given_rel_path(self): |
| 95 self.fs.cwd = '/home/user' |
| 96 self.assertEqual(self.fs.abspath('docs/foo'), '/home/user/docs/foo') |
| 97 |
| 98 def test_abspath_given_rel_path_up_dir(self): |
| 99 self.fs.cwd = '/home/user' |
| 100 self.assertEqual(self.fs.abspath('../../etc'), '/etc') |
| 101 |
| 102 def test_relpath_down_one_dir(self): |
| 103 self.assertEqual(self.fs.relpath('/foo/bar/', '/foo/'), 'bar') |
| 104 |
| 105 def test_relpath_no_start_arg(self): |
| 106 self.fs.cwd = '/home/user' |
| 107 self.assertEqual(self.fs.relpath('/home/user/foo/bar'), 'foo/bar') |
| 88 | 108 |
| 89 def test_filesystem_walk(self): | 109 def test_filesystem_walk(self): |
| 90 mock_dir = 'foo' | 110 mock_dir = 'foo' |
| 91 mock_files = {'foo/bar/baz': '', | 111 mock_files = {'foo/bar/baz': '', |
| 92 'foo/a': '', | 112 'foo/a': '', |
| 93 'foo/b': '', | 113 'foo/b': '', |
| 94 'foo/c': ''} | 114 'foo/c': ''} |
| 95 host = MockHost() | 115 host = MockHost() |
| 96 host.filesystem = MockFileSystem(files=mock_files) | 116 host.filesystem = MockFileSystem(files=mock_files) |
| 97 self.assertEquals(host.filesystem.walk(mock_dir), [('foo', ['bar'], ['a'
, 'b', 'c']), ('foo/bar', [], ['baz'])]) | 117 self.assertEquals(host.filesystem.walk(mock_dir), [('foo', ['bar'], ['a'
, 'b', 'c']), ('foo/bar', [], ['baz'])]) |
| 98 | 118 |
| 99 def test_filesystem_walk_deeply_nested(self): | 119 def test_filesystem_walk_deeply_nested(self): |
| 100 mock_dir = 'foo' | 120 mock_dir = 'foo' |
| 101 mock_files = {'foo/bar/baz': '', | 121 mock_files = {'foo/bar/baz': '', |
| 102 'foo/bar/quux': '', | 122 'foo/bar/quux': '', |
| 103 'foo/a/x': '', | 123 'foo/a/x': '', |
| 104 'foo/a/y': '', | 124 'foo/a/y': '', |
| 105 'foo/a/z/lyrics': '', | 125 'foo/a/z/lyrics': '', |
| 106 'foo/b': '', | 126 'foo/b': '', |
| 107 'foo/c': ''} | 127 'foo/c': ''} |
| 108 host = MockHost() | 128 host = MockHost() |
| 109 host.filesystem = MockFileSystem(files=mock_files) | 129 host.filesystem = MockFileSystem(files=mock_files) |
| 110 self.assertEquals(host.filesystem.walk(mock_dir), [('foo', ['a', 'bar'],
['c', 'b']), | 130 self.assertEquals(host.filesystem.walk(mock_dir), [('foo', ['a', 'bar'],
['c', 'b']), |
| 111 ('foo/a', ['z'], ['x'
, 'y']), | 131 ('foo/a', ['z'], ['x'
, 'y']), |
| 112 ('foo/a/z', [], ['lyr
ics']), | 132 ('foo/a/z', [], ['lyr
ics']), |
| 113 ('foo/bar', [], ['quu
x', 'baz'])]) | 133 ('foo/bar', [], ['quu
x', 'baz'])]) |
| 114 | 134 |
| 135 def test_relpath_win32(self): |
| 136 # This unit test inherits tests from GenericFileSystemTests, but |
| 137 # test_relpath_win32 doesn't work with a mock filesystem since sep |
| 138 # is always '/' for MockFileSystem. |
| 139 # FIXME: Remove this. |
| 140 pass |
| 141 |
| 115 def test_executable(self): | 142 def test_executable(self): |
| 116 host = MockHost() | 143 host = MockHost() |
| 117 mock_files = {'foo': '', 'bar': ''} | 144 mock_files = {'foo': '', 'bar': ''} |
| 118 host.filesystem = MockFileSystem(files=mock_files) | 145 host.filesystem = MockFileSystem(files=mock_files) |
| 119 host.filesystem.make_executable('foo') | 146 host.filesystem.make_executable('foo') |
| 120 self.assertEquals(host.filesystem.is_executable('foo'), True) | 147 self.assertEquals(host.filesystem.is_executable('foo'), True) |
| 121 self.assertEquals(host.filesystem.is_executable('bar'), False) | 148 self.assertEquals(host.filesystem.is_executable('bar'), False) |
| OLD | NEW |