How to use setup_dirs method in autotest

Best Python code snippet using autotest_python

test_organize.py

Source:test_organize.py Github

copy

Full Screen

1# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-2#3# Copyright 2015-2021 Canonical Ltd.4#5# This program is free software; you can redistribute it and/or6# modify it under the terms of the GNU Lesser General Public7# License version 3 as published by the Free Software Foundation.8#9# This program is distributed in the hope that it will be useful,10# but WITHOUT ANY WARRANTY; without even the implied warranty of11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12# Lesser General Public License for more details.13#14# You should have received a copy of the GNU Lesser General Public License15# along with this program. If not, see <http://www.gnu.org/licenses/>.16import os17import re18from pathlib import Path19from typing import Any, List20import pytest21from craft_parts import errors22from craft_parts.executor.organize import organize_files23@pytest.mark.parametrize(24 "data",25 [26 # simple_file27 dict(28 setup_dirs=[],29 setup_files=["foo"],30 organize_map={"foo": "bar"},31 expected=[(["bar"], "")],32 expected_message=None,33 expected_overwrite=None,34 ),35 # simple_dir_with_file36 dict(37 setup_dirs=["foodir"],38 setup_files=[os.path.join("foodir", "foo")],39 organize_map={"foodir": "bardir"},40 expected=[(["bardir"], ""), (["foo"], "bardir")],41 expected_message=None,42 expected_overwrite=None,43 ),44 # organize_to_the_same_directory45 dict(46 setup_dirs=["bardir", "foodir"],47 setup_files=[48 os.path.join("foodir", "foo"),49 os.path.join("bardir", "bar"),50 "basefoo",51 ],52 organize_map={53 "foodir": "bin",54 "bardir": "bin",55 "basefoo": "bin/basefoo",56 },57 expected=[(["bin"], ""), (["bar", "basefoo", "foo"], "bin")],58 expected_message=None,59 expected_overwrite=None,60 ),61 # leading_slash_in_value62 dict(63 setup_dirs=[],64 setup_files=["foo"],65 organize_map={"foo": "/bar"},66 expected=[(["bar"], "")],67 expected_message=None,68 expected_overwrite=None,69 ),70 # overwrite_existing_file71 dict(72 setup_dirs=[],73 setup_files=["foo", "bar"],74 organize_map={"foo": "bar"},75 expected=errors.FileOrganizeError,76 expected_message=(77 r".*trying to organize file 'foo' to 'bar', but 'bar' already exists.*"78 ),79 expected_overwrite=[(["bar"], "")],80 ),81 # *_for_files82 dict(83 setup_dirs=[],84 setup_files=["foo.conf", "bar.conf"],85 organize_map={"*.conf": "dir/"},86 expected=[(["dir"], ""), (["bar.conf", "foo.conf"], "dir")],87 expected_message=None,88 expected_overwrite=None,89 ),90 # *_for_files_with_non_dir_dst91 dict(92 setup_dirs=[],93 setup_files=["foo.conf", "bar.conf"],94 organize_map={"*.conf": "dir"},95 expected=errors.FileOrganizeError,96 expected_message=r".*multiple files to be organized into 'dir'.*",97 expected_overwrite=None,98 ),99 # *_for_directories100 dict(101 setup_dirs=["dir1", "dir2"],102 setup_files=[103 os.path.join("dir1", "foo"),104 os.path.join("dir2", "bar"),105 ],106 organize_map={"dir*": "dir/"},107 expected=[108 (["dir"], ""),109 (["dir1", "dir2"], "dir"),110 (["foo"], os.path.join("dir", "dir1")),111 (["bar"], os.path.join("dir", "dir2")),112 ],113 expected_message=None,114 expected_overwrite=None,115 ),116 # combined_*_with_file117 dict(118 setup_dirs=["dir1", "dir2"],119 setup_files=[120 os.path.join("dir1", "foo"),121 os.path.join("dir1", "bar"),122 os.path.join("dir2", "bar"),123 ],124 organize_map={"dir*": "dir/", "dir1/bar": "."},125 expected=[126 (["bar", "dir"], ""),127 (["dir1", "dir2"], "dir"),128 (["foo"], os.path.join("dir", "dir1")),129 (["bar"], os.path.join("dir", "dir2")),130 ],131 expected_message=None,132 expected_overwrite=None,133 ),134 # *_into_dir135 dict(136 setup_dirs=["dir"],137 setup_files=[138 os.path.join("dir", "foo"),139 os.path.join("dir", "bar"),140 ],141 organize_map={"dir/f*": "nested/dir/"},142 expected=[143 (["dir", "nested"], ""),144 (["bar"], "dir"),145 (["dir"], "nested"),146 (["foo"], os.path.join("nested", "dir")),147 ],148 expected_message=None,149 expected_overwrite=None,150 ),151 ],152)153def test_organize(new_dir, data):154 _organize_and_assert(155 tmp_path=new_dir,156 setup_dirs=data["setup_dirs"],157 setup_files=data["setup_files"],158 organize_map=data["organize_map"],159 expected=data["expected"],160 expected_message=data["expected_message"],161 expected_overwrite=data["expected_overwrite"],162 overwrite=False,163 )164 # Verify that it can be organized again by overwriting165 _organize_and_assert(166 tmp_path=new_dir,167 setup_dirs=data["setup_dirs"],168 setup_files=data["setup_files"],169 organize_map=data["organize_map"],170 expected=data["expected"],171 expected_message=data["expected_message"],172 expected_overwrite=data["expected_overwrite"],173 overwrite=True,174 )175def _organize_and_assert(176 *,177 tmp_path: Path,178 setup_dirs,179 setup_files,180 organize_map,181 expected: List[Any],182 expected_message,183 expected_overwrite,184 overwrite,185):186 base_dir = Path(tmp_path / "install")187 base_dir.mkdir(parents=True, exist_ok=True)188 for directory in setup_dirs:189 (base_dir / directory).mkdir(exist_ok=True)190 for file_entry in setup_files:191 (base_dir / file_entry).touch()192 if overwrite and expected_overwrite is not None:193 expected = expected_overwrite194 if isinstance(expected, type) and issubclass(expected, Exception):195 with pytest.raises(expected) as raised: # type: ignore196 organize_files(197 part_name="part-name",198 mapping=organize_map,199 base_dir=base_dir,200 overwrite=overwrite,201 )202 assert re.match(expected_message, str(raised.value)) is not None203 else:204 organize_files(205 part_name="part-name",206 mapping=organize_map,207 base_dir=base_dir,208 overwrite=overwrite,209 )210 for expect in expected:211 dir_path = (base_dir / expect[1]).as_posix()212 dir_contents = os.listdir(dir_path)213 dir_contents.sort()...

Full Screen

Full Screen

test_datasetpublisher.py

Source:test_datasetpublisher.py Github

copy

Full Screen

...15 None,16 in_dir,17 None,18 )19def setup_dirs():20 tmp_dir = tempfile.mkdtemp()21 in_dir = os.path.join(tmp_dir, "in")22 os.mkdir(in_dir)23 cp = MagicMock()24 return in_dir, cp25def test_process_only_data():26 in_dir, cp = setup_dirs()27 dsp = make_bogus_datasetpublisher(cp, in_dir)28 data_name = os.path.join(in_dir, "foo")29 with open(data_name, 'wb') as data:30 data.write(b'test\n')31 dsp.process_file(data_name)32def test_process_only_md5():33 in_dir, cp = setup_dirs()34 dsp = make_bogus_datasetpublisher(cp, in_dir)35 data_name = os.path.join(in_dir, "foo")36 md5_name = data_name + '.md5'37 with open(md5_name, 'wb') as md5:38 md5.write(b'd8e8fca2dc0f896fd7cb4cb0031ba249') # md5sum of 'test\n'39 dsp.process_file(md5_name)40@raises(ecreceive.exceptions.InvalidFilenameException)41def test_process_bad_fileformat():42 in_dir, cp = setup_dirs()43 dsp = make_bogus_datasetpublisher(cp, in_dir)44 data_name = os.path.join(in_dir, "foo")45 md5_name = data_name + '.md5'46 with open(data_name, 'wb') as data:47 data.write(b'test\n')48 with open(md5_name, 'wb') as md5:49 md5.write(b'd8e8fca2dc0f896fd7cb4cb0031ba249') # md5sum of 'test\n'50 dsp.process_file(md5_name)51def test_process_data():52 in_dir, cp = setup_dirs()53 mock_datainstance = MagicMock()54 mock_productstatus_api = MagicMock()55 mock_productstatus_api.datainstance.find_or_create = Mock(return_value=mock_datainstance)56 productstatus_service_backend = '1234-1234-4321-4321'57 productstatus_source = '0303-9090-6060-4040'58 base_url = "http://hei.ho/"59 dsp = ecreceive.dataset.DatasetPublisher(60 cp,61 base_url,62 120,63 productstatus_service_backend,64 productstatus_source,65 in_dir,66 mock_productstatus_api,...

Full Screen

Full Screen

data_setup.py

Source:data_setup.py Github

copy

Full Screen

...10 try:11 os.stat(dir)12 except:13 os.mkdir(dir)14def setup_dirs(dest_dir, train_dir, val_dir, test_dir):15 make_dir(dest_dir) # data16 for folder in [train_dir, val_dir, test_dir]:17 make_dir(Path(dest_dir + folder)) # data/test18 _ = [make_dir(Path(dest_dir + folder + x)) for x in ["cat", "dog"]] # creates cat and dog directories under folder19def copy_data(train_range, val_range, test_range, source_dir, dest_dir, train_dir, val_dir, test_dir):20 for a in ["cat", "dog"]:21 ranges = [ train_range, val_range, test_range]22 folders = [train_dir, val_dir, test_dir]23 for r, f in zip(ranges, folders):24 for i in r:25 src = Path(source_dir + a + "." + str(i) + ".jpg")26 dst = Path(dest_dir + f + a + "/" + a + "." + str(i) + ".jpg")27 shutil.copyfile(src, dst)28def main():29 dest_dir = "data/"30 source_dir = "train/"31 # append these directories to /data32 test_dir = "test/"33 val_dir = "val/"34 train_dir = "train/"35 # test and val will be 10%36 test_range = range(11249, 12500)37 val_range = range(10000, 11250)38 train_range = range(0, 10000)39 setup_dirs(dest_dir, train_dir, val_dir, test_dir)40 copy_data(train_range, val_range, test_range, source_dir, dest_dir, train_dir, val_dir, test_dir)41if __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