How to use _create_test_file method in autotest

Best Python code snippet using autotest_python

fake_filesystem_vs_real_test.py

Source:fake_filesystem_vs_real_test.py Github

copy

Full Screen

...53 return None, None54 return (os.path.join(self.real_base, path),55 os.path.join(self.fake_base, path))5657 def _create_test_file(self, file_type, path, contents=None):58 """Create a dir, file, or link in both the real fs and the fake."""59 path = sep(path)60 self._created_files.append([file_type, path, contents])61 real_path, fake_path = self._paths(path)62 if file_type == 'd':63 os.mkdir(real_path)64 self.fake_os.mkdir(fake_path)65 if file_type == 'f':66 fh = open(real_path, 'w')67 fh.write(contents or '')68 fh.close()69 fh = self.fake_open(fake_path, 'w')70 fh.write(contents or '')71 fh.close()72 # b for binary file73 if file_type == 'b':74 fh = open(real_path, 'wb')75 fh.write(contents or '')76 fh.close()77 fh = self.fake_open(fake_path, 'wb')78 fh.write(contents or '')79 fh.close()80 # l for symlink, h for hard link81 if file_type in ('l', 'h'):82 real_target, fake_target = (contents, contents)83 # If it begins with '/', make it relative to the base. You can't go84 # creating files in / for the real file system.85 if contents.startswith(os.sep):86 real_target, fake_target = self._paths(contents[1:])87 if file_type == 'l':88 os.symlink(real_target, real_path)89 self.fake_os.symlink(fake_target, fake_path)90 elif file_type == 'h':91 os.link(real_target, real_path)92 self.fake_os.link(fake_target, fake_path)9394 def setUp(self):95 # Base paths in the real and test file systems. We keep them different96 # so that missing features in the fake don't fall through to the base97 # operations and magically succeed.98 tsname = 'fakefs.%s' % time.time()99 self.cwd = os.getcwd()100 # Fully expand the base_path - required on OS X.101 self.real_base = os.path.realpath(102 os.path.join(tempfile.gettempdir(), tsname))103 os.chdir(tempfile.gettempdir())104 if os.path.isdir(self.real_base):105 shutil.rmtree(self.real_base)106 os.mkdir(self.real_base)107 self.fake_base = self._FAKE_FS_BASE108109 # Make sure we can write to the physical testing temp directory.110 self.assertTrue(os.access(self.real_base, os.W_OK))111112 self.fake_filesystem = fake_filesystem.FakeFilesystem()113 self.fake_filesystem.create_dir(self.fake_base)114 self.fake_os = fake_filesystem.FakeOsModule(self.fake_filesystem)115 self.fake_open = fake_filesystem.FakeFileOpen(self.fake_filesystem)116 self._created_files = []117118 os.chdir(self.real_base)119 self.fake_os.chdir(self.fake_base)120121 def tearDown(self):122 # We have to remove all the files from the real FS. Doing the same for123 # the fake FS is optional, but doing it is an extra sanity check.124 os.chdir(tempfile.gettempdir())125 try:126 rev_files = self._created_files[:]127 rev_files.reverse()128 for info in rev_files:129 real_path, fake_path = self._paths(info[1])130 if info[0] == 'd':131 try:132 os.rmdir(real_path)133 except OSError as e:134 if 'Directory not empty' in e:135 self.fail('Real path %s not empty: %s : %s' % (136 real_path, e, os.listdir(real_path)))137 else:138 raise139 self.fake_os.rmdir(fake_path)140 if info[0] == 'f' or info[0] == 'l':141 os.remove(real_path)142 self.fake_os.remove(fake_path)143 finally:144 shutil.rmtree(self.real_base)145 os.chdir(self.cwd)146147 def _compare_behaviors(self, method_name, path, real, fake,148 method_returns_path=False):149 """Invoke an os method in both real and fake contexts and compare150 results.151152 Invoke a real filesystem method with a path to a real file and invoke153 a fake filesystem method with a path to a fake file and compare the154 results. We expect some calls to throw Exceptions, so we catch those155 and compare them.156157 Args:158 method_name: Name of method being tested, for use in159 error messages.160 path: potential path to a file in the real and fake file systems,161 passing an empty tuple indicates that no arguments to pass162 to method.163 real: built-in system library or method from the built-in system164 library which takes a path as an arg and returns some value.165 fake: fake_filsystem object or method from a fake_filesystem class166 which takes a path as an arg and returns some value.167 method_returns_path: True if the method returns a path, and thus we168 must compensate for expected difference between real and fake.169170 Returns:171 A description of the difference in behavior, or None.172 """173 # pylint: disable=C6403174175 def _error_class(exc):176 if exc:177 if hasattr(exc, 'errno'):178 return '{}({})'.format(exc.__class__.__name__, exc.errno)179 return exc.__class__.__name__180 return 'None'181182 real_err, real_value = self._get_real_value(method_name, path, real)183 fake_err, fake_value = self._get_fake_value(method_name, path, fake)184185 method_call = '%s' % method_name186 method_call += '()' if path == () else '(%s)' % path187 # We only compare on the error class because the acutal error contents188 # is almost always different because of the file paths.189 if _error_class(real_err) != _error_class(fake_err):190 if real_err is None:191 return '%s: real version returned %s, fake raised %s' % (192 method_call, real_value, _error_class(fake_err))193 if fake_err is None:194 return '%s: real version raised %s, fake returned %s' % (195 method_call, _error_class(real_err), fake_value)196 return '%s: real version raised %s, fake raised %s' % (197 method_call, _error_class(real_err), _error_class(fake_err))198 real_errno = _get_errno(real_err)199 fake_errno = _get_errno(fake_err)200 if real_errno != fake_errno:201 return '%s(%s): both raised %s, real errno %s, fake errno %s' % (202 method_name, path, _error_class(real_err),203 real_errno, fake_errno)204 # If the method is supposed to return a full path AND both values205 # begin with the expected full path, then trim it off.206 if method_returns_path:207 if (real_value and fake_value208 and real_value.startswith(self.real_base)209 and fake_value.startswith(self.fake_base)):210 real_value = real_value[len(self.real_base):]211 fake_value = fake_value[len(self.fake_base):]212 if real_value != fake_value:213 return '%s: real return %s, fake returned %s' % (214 method_call, real_value, fake_value)215 return None216217 @staticmethod218 def _get_fake_value(method_name, path, fake):219 fake_value = None220 fake_err = None221 try:222 fake_method = fake223 if not callable(fake):224 fake_method = getattr(fake, method_name)225 args = [] if path == () else [path]226 fake_value = str(fake_method(*args))227 except Exception as e: # pylint: disable-msg=W0703228 fake_err = e229 return fake_err, fake_value230231 @staticmethod232 def _get_real_value(method_name, path, real):233 real_value = None234 real_err = None235 # Catching Exception below gives a lint warning, but it's what we need.236 try:237 args = [] if path == () else [path]238 real_method = real239 if not callable(real):240 real_method = getattr(real, method_name)241 real_value = str(real_method(*args))242 except Exception as e: # pylint: disable-msg=W0703243 real_err = e244 return real_err, real_value245246 def assertOsMethodBehaviorMatches(self, method_name, path,247 method_returns_path=False):248 """Invoke an os method in both real and fake contexts and compare.249250 For a given method name (from the os module) and a path, compare the251 behavior of the system provided module against the fake_filesystem252 module.253 We expect results and/or Exceptions raised to be identical.254255 Args:256 method_name: Name of method being tested.257 path: potential path to a file in the real and fake file systems.258 method_returns_path: True if the method returns a path, and thus we259 must compensate for expected difference between real and fake.260261 Returns:262 A description of the difference in behavior, or None.263 """264 path = sep(path)265 return self._compare_behaviors(method_name, path, os, self.fake_os,266 method_returns_path)267268 def diff_open_method_behavior(self, method_name, path, mode, data,269 method_returns_data=True):270 """Invoke an open method in both real and fkae contexts and compare.271272 Args:273 method_name: Name of method being tested.274 path: potential path to a file in the real and fake file systems.275 mode: how to open the file.276 data: any data to pass to the method.277 method_returns_data: True if a method returns some sort of data.278279 For a given method name (from builtin open) and a path, compare the280 behavior of the system provided module against the fake_filesystem281 module.282 We expect results and/or Exceptions raised to be identical.283284 Returns:285 A description of the difference in behavior, or None.286 """287 with open(path, mode) as real_fh:288 with self.fake_open(path, mode) as fake_fh:289 return self._compare_behaviors(290 method_name, data, real_fh, fake_fh, method_returns_data)291292 def diff_os_path_method_behavior(self, method_name, path,293 method_returns_path=False):294 """Invoke an os.path method in both real and fake contexts and compare.295296 For a given method name (from the os.path module) and a path, compare297 the behavior of the system provided module against the298 fake_filesytem module.299 We expect results and/or Exceptions raised to be identical.300301 Args:302 method_name: Name of method being tested.303 path: potential path to a file in the real and fake file systems.304 method_returns_path: True if the method returns a path, and thus we305 must compensate for expected difference between real and fake.306307 Returns:308 A description of the difference in behavior, or None.309 """310 return self._compare_behaviors(method_name, path, os.path,311 self.fake_os.path,312 method_returns_path)313314 def assertOsPathMethodBehaviorMatches(self, method_name, path,315 method_returns_path=False):316 """Assert that an os.path behaves the same in both real and317 fake contexts.318319 Wraps DiffOsPathMethodBehavior, raising AssertionError if any320 differences are reported.321322 Args:323 method_name: Name of method being tested.324 path: potential path to a file in the real and fake file systems.325 method_returns_path: True if the method returns a path, and thus we326 must compensate for expected difference between real and fake.327328 Raises:329 AssertionError if there is any difference in behavior.330 """331 path = sep(path)332 diff = self.diff_os_path_method_behavior(333 method_name, path, method_returns_path)334 if diff:335 self.fail(diff)336337 def assertAllOsBehaviorsMatch(self, path):338 path = sep(path)339 os_method_names = [] if self.is_windows else ['readlink']340 os_method_names_no_args = ['getcwd']341 os_path_method_names = ['isabs', 'isdir']342 if not self.is_windows:343 os_path_method_names += ['islink', 'lexists']344 if not self.is_windows or not IS_PYPY:345 os_path_method_names += ['isfile', 'exists']346347 wrapped_methods = [348 ['access', self._access_real, self._access_fake],349 ['stat.size', self._stat_size_real, self._stat_size_fake],350 ['lstat.size', self._lstat_size_real, self._lstat_size_fake]351 ]352353 differences = []354 for method_name in os_method_names:355 diff = self.assertOsMethodBehaviorMatches(method_name, path)356 if diff:357 differences.append(diff)358 for method_name in os_method_names_no_args:359 diff = self.assertOsMethodBehaviorMatches(method_name, (),360 method_returns_path=True)361 if diff:362 differences.append(diff)363 for method_name in os_path_method_names:364 diff = self.diff_os_path_method_behavior(method_name, path)365 if diff:366 differences.append(diff)367 for m in wrapped_methods:368 diff = self._compare_behaviors(m[0], path, m[1], m[2])369 if diff:370 differences.append(diff)371 if differences:372 self.fail('Behaviors do not match for %s:\n %s' %373 (path, '\n '.join(differences)))374375 def assertFileHandleBehaviorsMatch(self, path, mode, data):376 path = sep(path)377 write_method_names = ['write', 'writelines']378 read_method_names = ['read', 'readlines']379 other_method_names = ['truncate', 'flush', 'close']380 differences = []381 for method_name in write_method_names:382 diff = self.diff_open_method_behavior(383 method_name, path, mode, data)384 if diff:385 differences.append(diff)386 for method_name in read_method_names + other_method_names:387 diff = self.diff_open_method_behavior(method_name, path, mode, ())388 if diff:389 differences.append(diff)390 if differences:391 self.fail('Behaviors do not match for %s:\n %s' %392 (path, '\n '.join(differences)))393394 # Helpers for checks which are not straight method calls.395 @staticmethod396 def _access_real(path):397 return os.access(path, os.R_OK)398399 def _access_fake(self, path):400 return self.fake_os.access(path, os.R_OK)401402 def _stat_size_real(self, path):403 real_path, unused_fake_path = self._paths(path)404 # fake_filesystem.py does not implement stat().st_size for directories405 if os.path.isdir(real_path):406 return None407 return os.stat(real_path).st_size408409 def _stat_size_fake(self, path):410 unused_real_path, fake_path = self._paths(path)411 # fake_filesystem.py does not implement stat().st_size for directories412 if self.fake_os.path.isdir(fake_path):413 return None414 return self.fake_os.stat(fake_path).st_size415416 def _lstat_size_real(self, path):417 real_path, unused_fake_path = self._paths(path)418 if os.path.isdir(real_path):419 return None420 size = os.lstat(real_path).st_size421 # Account for the difference in the lengths of the absolute paths.422 if os.path.islink(real_path):423 if os.readlink(real_path).startswith(os.sep):424 size -= len(self.real_base)425 return size426427 def _lstat_size_fake(self, path):428 unused_real_path, fake_path = self._paths(path)429 # size = 0430 if self.fake_os.path.isdir(fake_path):431 return None432 size = self.fake_os.lstat(fake_path).st_size433 # Account for the difference in the lengths of the absolute paths.434 if self.fake_os.path.islink(fake_path):435 if self.fake_os.readlink(fake_path).startswith(os.sep):436 size -= len(self.fake_base)437 return size438439 def test_isabs(self):440 # We do not have to create any files for isabs.441 self.assertOsPathMethodBehaviorMatches('isabs', None)442 self.assertOsPathMethodBehaviorMatches('isabs', '')443 self.assertOsPathMethodBehaviorMatches('isabs', '/')444 self.assertOsPathMethodBehaviorMatches('isabs', '/a')445 self.assertOsPathMethodBehaviorMatches('isabs', 'a')446447 def test_none_path(self):448 self.assertAllOsBehaviorsMatch(None)449450 def test_empty_path(self):451 self.assertAllOsBehaviorsMatch('')452453 def test_root_path(self):454 self.assertAllOsBehaviorsMatch('/')455456 def test_non_existant_file(self):457 self.assertAllOsBehaviorsMatch('foo')458459 def test_empty_file(self):460 self._create_test_file('f', 'aFile')461 self.assertAllOsBehaviorsMatch('aFile')462463 def test_file_with_contents(self):464 self._create_test_file('f', 'aFile', 'some contents')465 self.assertAllOsBehaviorsMatch('aFile')466467 def test_file_with_binary_contents(self):468 self._create_test_file('b', 'aFile', b'some contents')469 self.assertAllOsBehaviorsMatch('aFile')470471 @unittest.skipIf(TestCase.is_windows, 'no symlink in Windows')472 def test_sym_link_to_empty_file(self):473 self._create_test_file('f', 'aFile')474 self._create_test_file('l', 'link_to_empty', 'aFile')475 self.assertAllOsBehaviorsMatch('link_to_empty')476477 @unittest.skipIf(TestCase.is_windows, 'no symlink in Windows')478 def test_hard_link_to_empty_file(self):479 self._create_test_file('f', 'aFile')480 self._create_test_file('h', 'link_to_empty', 'aFile')481 self.assertAllOsBehaviorsMatch('link_to_empty')482483 @unittest.skipIf(TestCase.is_windows, 'no symlink in Windows')484 def test_sym_link_to_real_file(self):485 self._create_test_file('f', 'aFile', 'some contents')486 self._create_test_file('l', 'link_to_file', 'aFile')487 self.assertAllOsBehaviorsMatch('link_to_file')488489 @unittest.skipIf(TestCase.is_windows, 'no symlink in Windows')490 def test_hard_link_to_real_file(self):491 self._create_test_file('f', 'aFile', 'some contents')492 self._create_test_file('h', 'link_to_file', 'aFile')493 self.assertAllOsBehaviorsMatch('link_to_file')494495 @unittest.skipIf(TestCase.is_windows, 'no symlink in Windows')496 def test_broken_sym_link(self):497 self._create_test_file('l', 'broken_link', 'broken')498 self._create_test_file('l', 'loop', '/a/loop')499 self.assertAllOsBehaviorsMatch('broken_link')500501 def test_file_in_a_folder(self):502 self._create_test_file('d', 'a')503 self._create_test_file('d', 'a/b')504 self._create_test_file('f', 'a/b/file', 'contents')505 self.assertAllOsBehaviorsMatch('a/b/file')506507 @unittest.skipIf(TestCase.is_windows, 'no symlink in Windows')508 def test_absolute_sym_link_to_folder(self):509 self._create_test_file('d', 'a')510 self._create_test_file('d', 'a/b')511 self._create_test_file('f', 'a/b/file', 'contents')512 self._create_test_file('l', 'a/link', '/a/b')513 self.assertAllOsBehaviorsMatch('a/link/file')514515 @unittest.skipIf(TestCase.is_windows, 'no symlink in Windows')516 def test_link_to_folder_after_chdir(self):517 self._create_test_file('d', 'a')518 self._create_test_file('d', 'a/b')519 self._create_test_file('f', 'a/b/file', 'contents')520 self._create_test_file('l', 'a/link', '/a/b')521522 real_dir, fake_dir = self._paths('a/b')523 os.chdir(real_dir)524 self.fake_os.chdir(fake_dir)525 self.assertAllOsBehaviorsMatch('file')526527 @unittest.skipIf(TestCase.is_windows, 'no symlink in Windows')528 def test_relative_sym_link_to_folder(self):529 self._create_test_file('d', 'a')530 self._create_test_file('d', 'a/b')531 self._create_test_file('f', 'a/b/file', 'contents')532 self._create_test_file('l', 'a/link', 'b')533 self.assertAllOsBehaviorsMatch('a/link/file')534535 @unittest.skipIf(TestCase.is_windows, 'no symlink in Windows')536 def test_sym_link_to_parent(self):537 # Soft links on HFS+ / OS X behave differently.538 if os.uname()[0] != 'Darwin':539 self._create_test_file('d', 'a')540 self._create_test_file('d', 'a/b')541 self._create_test_file('l', 'a/b/c', '..')542 self.assertAllOsBehaviorsMatch('a/b/c')543544 @unittest.skipIf(TestCase.is_windows, 'no symlink in Windows')545 def test_path_through_sym_link_to_parent(self):546 self._create_test_file('d', 'a')547 self._create_test_file('f', 'a/target', 'contents')548 self._create_test_file('d', 'a/b')549 self._create_test_file('l', 'a/b/c', '..')550 self.assertAllOsBehaviorsMatch('a/b/c/target')551552 @unittest.skipIf(TestCase.is_windows, 'no symlink in Windows')553 def test_sym_link_to_sibling_directory(self):554 self._create_test_file('d', 'a')555 self._create_test_file('d', 'a/b')556 self._create_test_file('d', 'a/sibling_of_b')557 self._create_test_file('f', 'a/sibling_of_b/target', 'contents')558 self._create_test_file('l', 'a/b/c', '../sibling_of_b')559 self.assertAllOsBehaviorsMatch('a/b/c/target')560561 @unittest.skipIf(TestCase.is_windows, 'no symlink in Windows')562 def test_sym_link_to_sibling_directory_non_existant_file(self):563 self._create_test_file('d', 'a')564 self._create_test_file('d', 'a/b')565 self._create_test_file('d', 'a/sibling_of_b')566 self._create_test_file('f', 'a/sibling_of_b/target', 'contents')567 self._create_test_file('l', 'a/b/c', '../sibling_of_b')568 self.assertAllOsBehaviorsMatch('a/b/c/file_does_not_exist')569570 @unittest.skipIf(TestCase.is_windows, 'no symlink in Windows')571 def test_broken_sym_link_to_sibling_directory(self):572 self._create_test_file('d', 'a')573 self._create_test_file('d', 'a/b')574 self._create_test_file('d', 'a/sibling_of_b')575 self._create_test_file('f', 'a/sibling_of_b/target', 'contents')576 self._create_test_file('l', 'a/b/c', '../broken_sibling_of_b')577 self.assertAllOsBehaviorsMatch('a/b/c/target')578579 def test_relative_path(self):580 self._create_test_file('d', 'a')581 self._create_test_file('d', 'a/b')582 self._create_test_file('d', 'a/sibling_of_b')583 self._create_test_file('f', 'a/sibling_of_b/target', 'contents')584 self.assertAllOsBehaviorsMatch('a/b/../sibling_of_b/target')585586 def test_broken_relative_path(self):587 self._create_test_file('d', 'a')588 self._create_test_file('d', 'a/b')589 self._create_test_file('d', 'a/sibling_of_b')590 self._create_test_file('f', 'a/sibling_of_b/target', 'contents')591 self.assertAllOsBehaviorsMatch('a/b/../broken/target')592593 def test_bad_relative_path(self):594 self._create_test_file('d', 'a')595 self._create_test_file('f', 'a/target', 'contents')596 self._create_test_file('d', 'a/b')597 self._create_test_file('d', 'a/sibling_of_b')598 self._create_test_file('f', 'a/sibling_of_b/target', 'contents')599 self.assertAllOsBehaviorsMatch('a/b/../broken/../target')600601 def test_getmtime_nonexistant_path(self):602 self.assertOsPathMethodBehaviorMatches('getmtime', 'no/such/path')603604 def test_builtin_open_modes(self):605 self._create_test_file('f', 'read', 'some contents')606 self._create_test_file('f', 'write', 'some contents')607 self._create_test_file('f', 'append', 'some contents')608 self.assertFileHandleBehaviorsMatch('read', 'r', 'other contents')609 self.assertFileHandleBehaviorsMatch('write', 'w', 'other contents')610 self.assertFileHandleBehaviorsMatch('append', 'a', 'other contents')611 self._create_test_file('f', 'readplus', 'some contents')612 self._create_test_file('f', 'writeplus', 'some contents')613 self.assertFileHandleBehaviorsMatch(614 'readplus', 'r+', 'other contents')615 self.assertFileHandleBehaviorsMatch(616 'writeplus', 'w+', 'other contents')617 self._create_test_file('b', 'binaryread', b'some contents')618 self._create_test_file('b', 'binarywrite', b'some contents')619 self._create_test_file('b', 'binaryappend', b'some contents')620 self.assertFileHandleBehaviorsMatch(621 'binaryread', 'rb', b'other contents')622 self.assertFileHandleBehaviorsMatch(623 'binarywrite', 'wb', b'other contents')624 self.assertFileHandleBehaviorsMatch(625 'binaryappend', 'ab', b'other contents')626 self.assertFileHandleBehaviorsMatch('read', 'rb', 'other contents')627 self.assertFileHandleBehaviorsMatch('write', 'wb', 'other contents')628 self.assertFileHandleBehaviorsMatch('append', 'ab', 'other contents')629630631def main(_):632 unittest.main()633 ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful