How to use readonly_dir method in autotest

Best Python code snippet using autotest_python

test_cleanup_dirs.py

Source:test_cleanup_dirs.py Github

copy

Full Screen

1# -*- coding: UTF-8 -*-2"""3Unit tests for :func:`invoke_cleanup.cleanup_files()`.4"""5from __future__ import absolute_import, print_function6import stat7from invoke_cleanup import cleanup_dirs8from invoke.util import cd9import pytest10# ---------------------------------------------------------------------------11# TEST SUPPORT12# ---------------------------------------------------------------------------13from tests.workdir_util import setup_workdir14from tests.path_util import \15 path_is_readable, path_is_writable, path_is_executable, \16 make_path_readonly, path_is_readonly17# ---------------------------------------------------------------------------18# TEST SUITE19# ---------------------------------------------------------------------------20# NOT NEEDED: not_implemented = pytest.mark.skip(reason="Not implemented yet")21class TestCleanupDirs(object):22 """Test :func:`invoke_cleanup.cleanup_files()` function."""23 def test_without_workdir_param_uses_current_directory(self, tmp_path):24 # -- SETUP:25 setup_workdir(tmp_path, [26 "one.xxx/.ignored",27 "two.xxx/.ignored",28 "more/ignored.xxx/.ignored"29 ])30 my_dir1 = tmp_path/"one.xxx"31 my_dir2 = tmp_path/"two.xxx"32 my_dir3 = tmp_path/"more/ignored.xxx"33 assert my_dir1.exists() and my_dir1.is_dir()34 assert my_dir2.exists() and my_dir2.is_dir()35 assert my_dir3.exists() and my_dir3.is_dir()36 # -- EXECUTE AND VERIFY:37 cwd = tmp_path38 with cd(str(cwd)): # -- STRING-CONVERSION (needed for: python2.7)39 cleanup_dirs(["*.xxx"])40 assert not my_dir1.exists(), "OOPS: my_dir1 was NOT_REMOVED"41 assert not my_dir2.exists(), "OOPS: my_dir2 was NOT_REMOVED"42 assert my_dir3.exists(), "OOPS: my_dir3 was REMOVED"43 def test_with_one_pattern_in_current_workdir(self, tmp_path):44 # -- SETUP:45 setup_workdir(tmp_path, [46 "one.xxx/.ignored",47 "two.xxx/.ignored",48 "more/ignored.xxx/.ignored"49 ])50 my_dir1 = tmp_path/"one.xxx"51 my_dir2 = tmp_path/"two.xxx"52 my_dir3 = tmp_path/"more/ignored.xxx"53 assert my_dir1.exists() and my_dir1.is_dir()54 assert my_dir2.exists() and my_dir2.is_dir()55 assert my_dir3.exists() and my_dir3.is_dir()56 # -- EXECUTE AND VERIFY:57 cwd = tmp_path58 cleanup_dirs(["*.xxx"], cwd)59 assert not my_dir1.exists(), "OOPS: my_dir1 was NOT_REMOVED"60 assert not my_dir2.exists(), "OOPS: my_dir2 was NOT_REMOVED"61 assert my_dir3.exists(), "OOPS: my_dir3 was REMOVED"62 def test_with_one_pattern_in_current_workdir_subtree(self, tmp_path):63 # -- SETUP:64 setup_workdir(tmp_path, [65 "one.xxx/.ignored",66 "more/two.xxx/.ignored"67 ])68 my_dir1 = tmp_path/"one.xxx"69 my_dir2 = tmp_path/"more/two.xxx"70 assert my_dir1.exists() and my_dir1.is_dir()71 assert my_dir2.exists() and my_dir2.is_dir()72 # -- EXECUTE AND VERIFY:73 cwd = tmp_path74 cleanup_dirs(["**/*.xxx"], cwd)75 assert not my_dir1.exists(), "OOPS: my_dir1 was NOT_REMOVED"76 assert not my_dir2.exists(), "OOPS: my_dir2 was NOT_REMOVED"77 def test_with_one_pattern_in_other_workdir_subtree(self, tmp_path):78 # -- SETUP:79 setup_workdir(tmp_path, [80 "one.xxx/.ignored",81 "other/two.xxx/.ignored"82 ])83 my_dir1 = tmp_path/"one.xxx"84 my_dir2 = tmp_path/"other/two.xxx"85 assert my_dir1.exists() and my_dir1.is_dir()86 assert my_dir2.exists() and my_dir2.is_dir()87 # -- EXECUTE AND VERIFY:88 cwd = tmp_path89 cleanup_dirs(["**/*.xxx"], workdir=cwd/"other")90 assert my_dir1.exists(), "OOPS: my_dir1 was REMOVED"91 assert not my_dir2.exists(), "OOPS: my_dir2 was NOT_REMOVED"92 def test_with_two_patterns(self, tmp_path):93 # -- SETUP:94 setup_workdir(tmp_path, [95 "one.xxx/.ignored",96 "more/two.zzz/.ignored"97 ])98 my_dir1 = tmp_path/"one.xxx"99 my_dir2 = tmp_path/"more/two.zzz"100 assert my_dir1.exists() and my_dir1.is_dir()101 assert my_dir2.exists() and my_dir2.is_dir()102 # -- EXECUTE AND VERIFY:103 cwd = tmp_path104 cleanup_dirs(["**/*.xxx/", "**/*.zzz/"], cwd)105 assert not my_dir1.exists(), "OOPS: my_dir1 was NOT_REMOVED"106 assert not my_dir2.exists(), "OOPS: my_dir2 was NOT_REMOVED"107 def test_dry_run__should_removes_no_dirs(self, tmp_path):108 # -- SETUP:109 setup_workdir(tmp_path, [110 "one.xxx/.ignored",111 "more/two.xxx/.ignored"112 ])113 my_dir1 = tmp_path/"one.xxx"114 my_dir2 = tmp_path/"more/two.xxx"115 assert my_dir1.exists() and my_dir1.is_dir()116 assert my_dir2.exists() and my_dir2.is_dir()117 # -- EXECUTE AND VERIFY:118 cwd = tmp_path119 cleanup_dirs(["**/*.xxx"], cwd, dry_run=True)120 assert my_dir1.exists(), "OOPS: my_dir1 was REMOVED"121 assert my_dir2.exists(), "OOPS: my_dir2 was REMOVED"122 def test_with_readonly_dir__is_not_removed(self, tmp_path, capsys):123 # -- SETUP:124 # readonly_mode = 0o500 # d.r-x.---.--- (disabled: write-mode)125 readonly_mode = (stat.S_IRUSR | stat.S_IXUSR)126 setup_workdir(tmp_path, [127 "one.xxx/.ignored",128 "readonly.xxx/.ignored"129 ])130 my_dir1 = tmp_path/"one.xxx/"131 my_dir2 = tmp_path/"readonly.xxx/"132 assert my_dir1.exists() and my_dir1.is_dir()133 assert my_dir2.exists() and my_dir2.is_dir()134 readonly_dir = my_dir2135 readonly_dir_initial_mode = stat.S_IMODE(readonly_dir.stat().st_mode)136 readonly_dir.chmod(readonly_mode)137 readonly_dir_mode = stat.S_IMODE(readonly_dir.stat().st_mode)138 print("DIAG: readonly_dir.mode= 0o%o" % readonly_dir_mode)139 assert not path_is_writable(readonly_dir)140 assert path_is_readonly(readonly_dir)141 # -- EXECUTE AND VERIFY:142 cwd = tmp_path143 cleanup_dirs(["**/*.xxx"], cwd)144 captured = capsys.readouterr()145 assert not my_dir1.exists(), "OOPS: my_dir1 was NOT_REMOVED"146 assert my_dir2.exists(), "OOPS: my_dir2 was REMOVED"147 expected = "RMTREE: {0}".format(my_dir2)148 assert expected in captured.out149 # -- CLEANUP:150 readonly_dir.chmod(readonly_dir_initial_mode)151 def test_with_no_permissions_dir__is_not_removed(self, tmp_path, capsys):152 # -- SETUP:153 no_permission_mode = 0o000 # d---.---.--- (NO_PERMISSIONS)154 setup_workdir(tmp_path, [155 "one.xxx/.ignored",156 "no_permission.xxx/.ignored"157 ])158 my_dir1 = tmp_path/"one.xxx/"159 my_dir2 = tmp_path/"no_permission.xxx/"160 assert my_dir1.exists() and my_dir1.is_dir()161 assert my_dir2.exists() and my_dir2.is_dir()162 no_permission_dir = my_dir2163 no_permission_initial_mode = stat.S_IMODE(no_permission_dir.stat().st_mode)164 no_permission_dir.chmod(no_permission_mode)165 no_permission_mode = stat.S_IMODE(no_permission_dir.stat().st_mode)166 print("DIAG: no_permission_dir.mode= 0o%o" % no_permission_mode)167 assert not path_is_readable(no_permission_dir)168 assert not path_is_writable(no_permission_dir)169 assert not path_is_executable(no_permission_dir)170 # -- EXECUTE AND VERIFY:171 cwd = tmp_path172 cleanup_dirs(["**/*.xxx"], cwd)173 captured = capsys.readouterr()174 assert not my_dir1.exists(), "OOPS: my_dir1 was NOT_REMOVED"175 assert my_dir2.exists(), "OOPS: my_dir2 was REMOVED"176 expected = "RMTREE: {0}".format(my_dir2)177 assert expected in captured.out178 # -- CLEANUP:179 no_permission_dir.chmod(no_permission_initial_mode)180 def test_within_readonly_dir__is_not_removed(self, tmp_path, capsys):181 # -- SETUP:182 readonly_mode = 0o555 # dr-xr-xr-x (disabled: write-mode)183 setup_workdir(tmp_path, [184 "one.xxx/.ignored",185 "readonly.dir/two.xxx/.ignored"186 ])187 my_dir1 = tmp_path/"one.xxx/"188 readonly_dir = tmp_path/"readonly.dir/"189 my_dir2 = readonly_dir/"two.xxx/"190 assert my_dir1.exists() and my_dir1.is_dir()191 assert my_dir2.exists() and my_dir2.is_dir()192 readonly_dir_initial_mode = stat.S_IMODE(readonly_dir.stat().st_mode)193 readonly_dir.chmod(readonly_mode)194 readonly_dir_mode = stat.S_IMODE(readonly_dir.stat().st_mode)195 print("DIAG: readonly_dir.mode= 0o%o" % readonly_dir_mode)196 assert path_is_readonly(readonly_dir)197 # -- EXECUTE AND VERIFY:198 cwd = tmp_path199 cleanup_dirs(["**/*.xxx"], cwd)200 captured = capsys.readouterr()201 assert not my_dir1.exists(), "OOPS: my_dir1 was NOT_REMOVED"202 assert my_dir2.exists(), "OOPS: my_dir2 was REMOVED"203 expected = "RMTREE: {0}".format(my_dir2)204 assert expected in captured.out205 # -- CLEANUP:206 readonly_dir.chmod(readonly_dir_initial_mode)207 def test_within_no_permissions_dir__is_not_removed(self, tmp_path, capsys):208 # -- SETUP:209 no_permission_mode = 0o000 # d---.---.--- (NO_PERMISSIONS)210 setup_workdir(tmp_path, [211 "one.xxx/.ignored",212 "no_permission/two.xxx/.ignored"213 ])214 my_dir1 = tmp_path/"one.xxx/"215 no_permission_dir = tmp_path/"no_permission/"216 my_dir2 = no_permission_dir/"two.xxx/"217 assert my_dir1.exists() and my_dir1.is_dir()218 assert my_dir2.exists() and my_dir2.is_dir()219 no_permission_initial_mode = stat.S_IMODE(no_permission_dir.stat().st_mode)220 no_permission_dir.chmod(no_permission_mode)221 no_permission_mode = stat.S_IMODE(no_permission_dir.stat().st_mode)222 print("DIAG: no_permission_dir.mode= 0o%o" % no_permission_mode)223 assert not path_is_readable(no_permission_dir)224 assert not path_is_writable(no_permission_dir)225 assert not path_is_executable(no_permission_dir)226 # -- EXECUTE AND CLEANUP:227 cwd = tmp_path228 cleanup_dirs(["**/*.xxx"], cwd)229 captured = capsys.readouterr()230 no_permission_dir.chmod(no_permission_initial_mode)231 # -- VERIFY:232 assert not my_dir1.exists(), "OOPS: my_dir1 was NOT_REMOVED"233 assert my_dir2.exists(), "OOPS: my_dir2 was REMOVED"234 expected = "RMTREE: {0}".format(my_dir2)235 assert expected not in captured.out, "OOPS: Traversal into NO_PERMISSION.dir"236 def test_without_any_matching_dirs(self, tmp_path):237 # -- SETUP:238 setup_workdir(tmp_path, [239 "one.xxx/.ignored",240 "more/two.xxx/.ignored"241 ])242 my_dir1 = tmp_path/"one.xxx"243 my_dir2 = tmp_path/"more/two.xxx"244 assert my_dir1.exists() and my_dir1.is_dir()245 assert my_dir2.exists() and my_dir2.is_dir()246 # -- EXECUTE AND VERIFY:247 cwd = tmp_path248 cleanup_dirs(["**/*.OTHER"], cwd)249 assert my_dir1.exists(), "OOPS: my_dir1 was REMOVED"250 assert my_dir2.exists(), "OOPS: my_dir2 was REMOVED"251 def test_with_exact_name(self, tmp_path):252 # -- SETUP:253 setup_workdir(tmp_path, [254 "one.xxx/.ignored",255 "more/two.xxx/.ignored"256 ])257 my_dir1 = tmp_path/"one.xxx"258 my_dir2 = tmp_path/"more/two.xxx"259 assert my_dir1.exists() and my_dir1.is_dir()260 assert my_dir2.exists() and my_dir2.is_dir()261 # -- EXECUTE AND VERIFY:262 cwd = tmp_path263 cleanup_dirs(["more/two.xxx"], cwd)264 assert my_dir1.exists(), "OOPS: my_dir1 was REMOVED"265 assert not my_dir2.exists(), "OOPS: my_dir2 was NOT REMOVED"266 def test_with_matching_file__should_skip_remove(self, tmp_path, capsys):267 # -- SETUP:268 setup_workdir(tmp_path, [269 "one.xxx/.ignored",270 "more/two.xxx"271 ])272 my_dir1 = tmp_path/"one.xxx"273 my_file2 = tmp_path/"more/two.xxx"274 assert my_dir1.exists() and my_dir1.is_dir()275 assert my_file2.exists() and my_file2.is_file()276 # -- EXECUTE AND VERIFY:277 cwd = tmp_path278 cleanup_dirs(["**/*.xxx"], cwd)279 captured = capsys.readouterr()280 assert not my_dir1.exists(), "OOPS: my_dir1 was NOT_REMOVED"281 assert my_file2.exists(), "OOPS: my_file2 was REMOVED"282 # -- ONLY IN NON-VERBOSE MODE:283 expected1 = "RMTREE: {0}".format(my_file2)284 expected2 = "REMOVE: {0}".format(my_file2)285 assert expected1 not in captured.out286 assert expected2 not in captured.out287 def test_with_matching_file__should_skip_remove_and_showit_in_verbose_mode(self, tmp_path, capsys):288 # -- SETUP:289 setup_workdir(tmp_path, [290 "one.xxx/.ignored",291 "more/two.xxx"292 ])293 my_dir1 = tmp_path/"one.xxx"294 my_file2 = tmp_path/"more/two.xxx"295 assert my_dir1.exists() and my_dir1.is_dir()296 assert my_file2.exists() and my_file2.is_file()297 # -- EXECUTE AND VERIFY:298 cwd = tmp_path299 cleanup_dirs(["**/*.xxx"], cwd, verbose=True)300 captured = capsys.readouterr()301 assert not my_dir1.exists(), "OOPS: my_dir1 was NOT_REMOVED"302 assert my_file2.exists(), "OOPS: my_file2 was REMOVED"303 # -- ONLY IN VERBOSE MODE:304 expected = "RMTREE: {0} (SKIPPED: Not a directory)".format(my_file2)...

Full Screen

Full Screen

test_local.py

Source:test_local.py Github

copy

Full Screen

...31 readonly_file = self.tmpdir.join('readonly').ensure()32 readonly_file.chmod(0)33 readonly_file.remove()34 assert not readonly_file.check(exists=1)35 def test_remove_removes_readonly_dir(self):36 readonly_dir = self.tmpdir.join('readonlydir').ensure(dir=1)37 readonly_dir.chmod(0500)38 readonly_dir.remove()39 assert not readonly_dir.check(exists=1)40 def test_remove_removes_dir_and_readonly_file(self):41 readonly_dir = self.tmpdir.join('readonlydir').ensure(dir=1)42 readonly_file = readonly_dir.join('readonlyfile').ensure()43 readonly_file.chmod(0)44 readonly_dir.remove()45 assert not readonly_dir.check(exists=1)46 def test_initialize_curdir(self):47 assert str(local()) == py.std.os.getcwd()48 def test_initialize_reldir(self):49 old = self.root.chdir()...

Full Screen

Full Screen

test_builder.py

Source:test_builder.py Github

copy

Full Screen

1import unittest2import shutil3import os4from codebuild_builder import CodebuildBuilder5from os.path import join6import threading7import time8class TestBuilder(unittest.TestCase):9 def _prepare_test(self, input_dir='good'):10 this_dir = os.path.dirname(os.path.realpath(__file__))11 output_dir = join(this_dir, 'tmp', 'output')12 readonly_dir = join(this_dir, 'data', 'input', input_dir)13 shutil.rmtree(output_dir, ignore_errors=True)14 os.makedirs(output_dir)15 return output_dir, readonly_dir16 def test_prepare_output(self):17 print 'test_prepare_output'18 output_dir, readonly_dir = self._prepare_test()19 builder = CodebuildBuilder(input_dir=readonly_dir,20 output_dir=output_dir,21 debug=False)22 builder._prepare_output()23 output_src = join(output_dir, 'src123456789')24 self.assertTrue(os.path.exists(join(output_src, 'source.foo')))25 pwd_txt = join(output_dir, 'tmp', 'pwd.txt')26 self.assertTrue(os.path.exists(pwd_txt))27 with open(pwd_txt, 'r') as pwd_txt_file:28 lines = pwd_txt_file.readlines()29 self.assertEqual(lines, [str(output_src)])30 def test_parse_buildspec(self):31 print 'test_parse_buildspec'32 output_dir, readonly_dir = self._prepare_test()33 builder = CodebuildBuilder(input_dir=readonly_dir,34 output_dir=output_dir,35 debug=False)36 builder._parse_buildspec()37 self.assertEqual(builder._version, 0.2)38 self.assertDictEqual(builder._envs, {'install': 'install', 'build': 'build'})39 self._verify_phase('install', builder._phases)40 self._verify_phase('pre_build', builder._phases)41 self._verify_phase('build', builder._phases)42 self._verify_phase('post_build', builder._phases)43 self.assertEqual(builder._artifacts['files'], ['**/*'])44 def _verify_phase(self, name, phases):45 self.assertTrue(name in phases)46 self.assertEqual(phases[name]['commands'], ['ls', "echo $%s > %s" % (name, name)])47 def test_run_phases(self):48 print 'test_run_phases'49 output_dir, readonly_dir = self._prepare_test()50 builder = CodebuildBuilder(input_dir=readonly_dir,51 output_dir=output_dir,52 debug=False)53 builder._prepare_output()54 builder._parse_buildspec()55 builder._run_phases()56 output_src = join(output_dir, 'src123456789')57 for expected_file_name in ['install', 'pre_build',58 'build', 'post_build']:59 expected_file_path = join(output_src, expected_file_name)60 print expected_file_path61 self.assertTrue(os.path.exists(expected_file_path))62 with open(expected_file_path, 'r') as expected_file:63 lines = expected_file.readlines()64 self.assertEqual(lines, [str(expected_file_name) + '\n'])65 def test_upload_artifacts(self):66 print 'test_upload_artifacts'67 output_dir, readonly_dir = self._prepare_test()68 builder = CodebuildBuilder(input_dir=readonly_dir,69 output_dir=output_dir,70 debug=False)71 builder._prepare_output()72 builder._parse_buildspec()73 builder._run_phases()74 builder._upload_artifacts()75 artifacts_dir = join(output_dir, 'artifacts')76 for expected_file_name in ['source.foo', 'install', 'pre_build',77 'build', 'post_build']:78 expected_file_path = join(artifacts_dir, expected_file_name)79 print expected_file_path80 self.assertTrue(os.path.exists(expected_file_path))81 def test_successful_run(self):82 print 'test_successful_run'83 output_dir, readonly_dir = self._prepare_test()84 builder = CodebuildBuilder(input_dir=readonly_dir,85 output_dir=output_dir,86 debug=False)87 builder.run()88 self.assertTrue(builder._succeeded)89 def test_failing_run(self):90 print 'test_failing_run'91 output_dir, readonly_dir = self._prepare_test('bad')92 builder = CodebuildBuilder(input_dir=readonly_dir,93 output_dir=output_dir,94 debug=False)95 expected_exception_massage = ''96 try:97 builder.run()98 except Exception as e:99 expected_exception_massage = str(e)100 self.assertEqual(expected_exception_massage, 'Build failed')101 artifacts_dir = join(output_dir, 'artifacts')102 # needs to upload the files because build failed103 for expected_file_name in ['source.foo', 'install',104 'pre_build', 'post_build']:105 expected_file_path = join(artifacts_dir, expected_file_name)106 print expected_file_path107 self.assertTrue(os.path.exists(expected_file_path))108 def test_debug_run(self):109 print 'test_debug_run'110 output_dir, readonly_dir = self._prepare_test()111 open(join(output_dir, 'debug'), 'a').close()112 builder = CodebuildBuilder(input_dir=readonly_dir,113 output_dir=output_dir,114 debug=False)115 run_thread = threading.Thread(target=builder.run)116 run_thread.start()117 time.sleep(1)118 # continue 'ls' command in install phase119 os.unlink(join(output_dir, 'debug'))120 time.sleep(1)121 # skip 'touch' command in install phase122 open(join(output_dir, 'skip'), 'a').close()123 os.unlink(join(output_dir, 'debug'))124 time.sleep(1)125 # skip 'ls' command in pre_build phase126 open(join(output_dir, 'skip'), 'a').close()127 os.unlink(join(output_dir, 'debug'))128 time.sleep(1)129 # continue 'touch' command in install phase130 os.unlink(join(output_dir, 'debug'))131 time.sleep(1)132 # stop run133 open(join(output_dir, 'exit'), 'a').close()134 os.unlink(join(output_dir, 'debug'))135 run_thread.join(timeout=10)136 self.assertTrue(not run_thread.is_alive())137 artifacts_dir = join(output_dir, 'src123456789')138 # needs to upload the files because build failed139 for expected_file_name in ['source.foo', 'pre_build']:140 expected_file_path = join(artifacts_dir, expected_file_name)141 print expected_file_path142 self.assertTrue(os.path.exists(expected_file_path))143 self.assertEquals(len(os.listdir(artifacts_dir)), 2)144if __name__ == '__main__':...

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