Best Python code snippet using lisa_python
test_unit_utils.py
Source:test_unit_utils.py
1#!/usr/bin/env python2"""Unit test driver for checkout_externals3Note: this script assume the path to the checkout_externals.py module is4already in the python path.5"""6from __future__ import absolute_import7from __future__ import unicode_literals8from __future__ import print_function9import os10import unittest11from manic.utils import last_n_lines, indent_string12from manic.utils import str_to_bool, execute_subprocess13from manic.utils import is_remote_url, split_remote_url, expand_local_url14class TestExecuteSubprocess(unittest.TestCase):15 """Test the application logic of execute_subprocess wrapper16 """17 def test_exesub_return_stat_err(self):18 """Test that execute_subprocess returns a status code when caller19 requests and the executed subprocess fails.20 """21 cmd = ['false']22 status = execute_subprocess(cmd, status_to_caller=True)23 self.assertEqual(status, 1)24 def test_exesub_return_stat_ok(self):25 """Test that execute_subprocess returns a status code when caller26 requests and the executed subprocess succeeds.27 """28 cmd = ['true']29 status = execute_subprocess(cmd, status_to_caller=True)30 self.assertEqual(status, 0)31 def test_exesub_except_stat_err(self):32 """Test that execute_subprocess raises an exception on error when33 caller doesn't request return code34 """35 cmd = ['false']36 with self.assertRaises(RuntimeError):37 execute_subprocess(cmd, status_to_caller=False)38class TestLastNLines(unittest.TestCase):39 """Test the last_n_lines function.40 """41 def test_last_n_lines_short(self):42 """With a message with <= n lines, result of last_n_lines should43 just be the original message.44 """45 mystr = """three46line47string48"""49 mystr_truncated = last_n_lines(50 mystr, 3, truncation_message='[truncated]')51 self.assertEqual(mystr, mystr_truncated)52 def test_last_n_lines_long(self):53 """With a message with > n lines, result of last_n_lines should54 be a truncated string.55 """56 mystr = """a57big58five59line60string61"""62 expected = """[truncated]63five64line65string66"""67 mystr_truncated = last_n_lines(68 mystr, 3, truncation_message='[truncated]')69 self.assertEqual(expected, mystr_truncated)70class TestIndentStr(unittest.TestCase):71 """Test the indent_string function.72 """73 def test_indent_string_singleline(self):74 """Test the indent_string function with a single-line string75 """76 mystr = 'foo'77 result = indent_string(mystr, 4)78 expected = ' foo'79 self.assertEqual(expected, result)80 def test_indent_string_multiline(self):81 """Test the indent_string function with a multi-line string82 """83 mystr = """hello84hi85goodbye86"""87 result = indent_string(mystr, 2)88 expected = """ hello89 hi90 goodbye91"""92 self.assertEqual(expected, result)93class TestStrToBool(unittest.TestCase):94 """Test the string to boolean conversion routine.95 """96 def test_case_insensitive_true(self):97 """Verify that case insensitive variants of 'true' returns the True98 boolean.99 """100 values = ['true', 'TRUE', 'True', 'tRuE', 't', 'T', ]101 for value in values:102 received = str_to_bool(value)103 self.assertTrue(received)104 def test_case_insensitive_false(self):105 """Verify that case insensitive variants of 'false' returns the False106 boolean.107 """108 values = ['false', 'FALSE', 'False', 'fAlSe', 'f', 'F', ]109 for value in values:110 received = str_to_bool(value)111 self.assertFalse(received)112 def test_invalid_str_error(self):113 """Verify that a non-true/false string generates a runtime error.114 """115 values = ['not_true_or_false', 'A', '1', '0',116 'false_is_not_true', 'true_is_not_false']117 for value in values:118 with self.assertRaises(RuntimeError):119 str_to_bool(value)120class TestIsRemoteURL(unittest.TestCase):121 """Crude url checking to determine if a url is local or remote.122 """123 def test_url_remote_git(self):124 """verify that a remote git url is identified.125 """126 url = 'git@somewhere'127 is_remote = is_remote_url(url)128 self.assertTrue(is_remote)129 def test_url_remote_ssh(self):130 """verify that a remote ssh url is identified.131 """132 url = 'ssh://user@somewhere'133 is_remote = is_remote_url(url)134 self.assertTrue(is_remote)135 def test_url_remote_http(self):136 """verify that a remote http url is identified.137 """138 url = 'http://somewhere'139 is_remote = is_remote_url(url)140 self.assertTrue(is_remote)141 def test_url_remote_https(self):142 """verify that a remote https url is identified.143 """144 url = 'https://somewhere'145 is_remote = is_remote_url(url)146 self.assertTrue(is_remote)147 def test_url_local_user(self):148 """verify that a local path with '~/path/to/repo' gets rejected149 """150 url = '~/path/to/repo'151 is_remote = is_remote_url(url)152 self.assertFalse(is_remote)153 def test_url_local_var_curly(self):154 """verify that a local path with env var '${HOME}' gets rejected155 """156 url = '${HOME}/path/to/repo'157 is_remote = is_remote_url(url)158 self.assertFalse(is_remote)159 def test_url_local_var(self):160 """verify that a local path with an env var '$HOME' gets rejected161 """162 url = '$HOME/path/to/repo'163 is_remote = is_remote_url(url)164 self.assertFalse(is_remote)165 def test_url_local_abs(self):166 """verify that a local abs path gets rejected167 """168 url = '/path/to/repo'169 is_remote = is_remote_url(url)170 self.assertFalse(is_remote)171 def test_url_local_rel(self):172 """verify that a local relative path gets rejected173 """174 url = '../../path/to/repo'175 is_remote = is_remote_url(url)176 self.assertFalse(is_remote)177class TestSplitRemoteURL(unittest.TestCase):178 """Crude url checking to determine if a url is local or remote.179 """180 def test_url_remote_git(self):181 """verify that a remote git url is identified.182 """183 url = 'git@somewhere.com:org/repo'184 received = split_remote_url(url)185 self.assertEqual(received, "org/repo")186 def test_url_remote_ssh(self):187 """verify that a remote ssh url is identified.188 """189 url = 'ssh://user@somewhere.com/path/to/repo'190 received = split_remote_url(url)191 self.assertEqual(received, 'somewhere.com/path/to/repo')192 def test_url_remote_http(self):193 """verify that a remote http url is identified.194 """195 url = 'http://somewhere.org/path/to/repo'196 received = split_remote_url(url)197 self.assertEqual(received, 'somewhere.org/path/to/repo')198 def test_url_remote_https(self):199 """verify that a remote http url is identified.200 """201 url = 'http://somewhere.gov/path/to/repo'202 received = split_remote_url(url)203 self.assertEqual(received, 'somewhere.gov/path/to/repo')204 def test_url_local_url_unchanged(self):205 """verify that a local path is unchanged206 """207 url = '/path/to/repo'208 received = split_remote_url(url)209 self.assertEqual(received, url)210class TestExpandLocalURL(unittest.TestCase):211 """Crude url checking to determine if a url is local or remote.212 Remote should be unmodified.213 Local, should perform user and variable expansion.214 """215 def test_url_local_user1(self):216 """verify that a local path with '~/path/to/repo' gets expanded to an217 absolute path.218 NOTE(bja, 2017-11) we can't test for something like:219 '~user/path/to/repo' because the user has to be in the local220 machine password directory and we don't know a user name that221 is valid on every system....?222 """223 field = 'test'224 url = '~/path/to/repo'225 received = expand_local_url(url, field)226 print(received)227 self.assertTrue(os.path.isabs(received))228 def test_url_local_expand_curly(self):229 """verify that a local path with '${HOME}' gets expanded to an absolute path.230 """231 field = 'test'232 url = '${HOME}/path/to/repo'233 received = expand_local_url(url, field)234 self.assertTrue(os.path.isabs(received))235 def test_url_local_expand_var(self):236 """verify that a local path with '$HOME' gets expanded to an absolute path.237 """238 field = 'test'239 url = '$HOME/path/to/repo'240 received = expand_local_url(url, field)241 self.assertTrue(os.path.isabs(received))242 def test_url_local_env_missing(self):243 """verify that a local path with env var that is missing gets left as-is244 """245 field = 'test'246 url = '$TMP_VAR/path/to/repo'247 received = expand_local_url(url, field)248 print(received)249 self.assertEqual(received, url)250 def test_url_local_expand_env(self):251 """verify that a local path with another env var gets expanded to an252 absolute path.253 """254 field = 'test'255 os.environ['TMP_VAR'] = '/some/absolute'256 url = '$TMP_VAR/path/to/repo'257 received = expand_local_url(url, field)258 del os.environ['TMP_VAR']259 print(received)260 self.assertTrue(os.path.isabs(received))261 self.assertEqual(received, '/some/absolute/path/to/repo')262 def test_url_local_normalize_rel(self):263 """verify that a local path with another env var gets expanded to an264 absolute path.265 """266 field = 'test'267 url = '/this/is/a/long/../path/to/a/repo'268 received = expand_local_url(url, field)269 print(received)270 self.assertEqual(received, '/this/is/a/path/to/a/repo')271if __name__ == '__main__':...
test_archives.py
Source:test_archives.py
...41 'location': ml.location,42 'alias': ml.alias,43 'compressed_dir': ml.compressed_dir,44 'is_local': ml.is_local(),45 'is_remote': ml.is_remote(),46 }47 self.check_single_dict(expected, result)48 def test_mailing_list_1(self):49 '''Remote URL with trailing slash'''50 ml = MailingList('http://mlstats.org/pipermail/mlstats-list/')51 target = 'mlstats.org/pipermail/mlstats-list'52 expected = {53 'location': 'http://mlstats.org/pipermail/mlstats-list',54 'alias': 'mlstats-list',55 'compressed_dir': os.path.join(COMPRESSED_DIR, target),56 'is_local': False,57 'is_remote': True,58 }59 result = {60 'location': ml.location,61 'alias': ml.alias,62 'compressed_dir': ml.compressed_dir,63 'is_local': ml.is_local(),64 'is_remote': ml.is_remote(),65 }66 self.check_single_dict(expected, result)67 def test_mailing_list_2(self):68 '''Remote short URL with no trailing slash'''69 ml = MailingList('http://mlstats.org')70 target = 'mlstats.org'71 expected = {72 'location': 'http://mlstats.org',73 'alias': 'mlstats.org',74 'compressed_dir': os.path.join(COMPRESSED_DIR, target),75 'is_local': False,76 'is_remote': True,77 }78 result = {79 'location': ml.location,80 'alias': ml.alias,81 'compressed_dir': ml.compressed_dir,82 'is_local': ml.is_local(),83 'is_remote': ml.is_remote(),84 }85 self.check_single_dict(expected, result)86 def test_mailing_list_3(self):87 '''Remote short URL with trailing slash'''88 ml = MailingList('http://mlstats.org/')89 target = 'mlstats.org'90 expected = {91 'location': 'http://mlstats.org',92 'alias': 'mlstats.org',93 'compressed_dir': os.path.join(COMPRESSED_DIR, target),94 'is_local': False,95 'is_remote': True,96 }97 result = {98 'location': ml.location,99 'alias': ml.alias,100 'compressed_dir': ml.compressed_dir,101 'is_local': ml.is_local(),102 'is_remote': ml.is_remote(),103 }104 self.check_single_dict(expected, result)105 def test_mailing_list_4(self):106 '''Local directory with no trailing slash'''107 ml = MailingList('/mlstats.org/pipermail/mlstats-list')108 target = 'mlstats.org/pipermail/mlstats-list'109 expected = {110 'location': '/mlstats.org/pipermail/mlstats-list',111 'alias': 'mlstats-list',112 'compressed_dir': os.path.join(COMPRESSED_DIR, target),113 'is_local': True,114 'is_remote': False,115 }116 result = {117 'location': ml.location,118 'alias': ml.alias,119 'compressed_dir': ml.compressed_dir,120 'is_local': ml.is_local(),121 'is_remote': ml.is_remote(),122 }123 self.check_single_dict(expected, result)124 def test_mailing_list_5(self):125 '''Local directory with trailing slash'''126 ml = MailingList('/mlstats.org/pipermail/mlstats-list/')127 target = 'mlstats.org/pipermail/mlstats-list'128 expected = {129 'location': '/mlstats.org/pipermail/mlstats-list',130 'alias': 'mlstats-list',131 'compressed_dir': os.path.join(COMPRESSED_DIR, target),132 'is_local': True,133 'is_remote': False,134 }135 result = {136 'location': ml.location,137 'alias': ml.alias,138 'compressed_dir': ml.compressed_dir,139 'is_local': ml.is_local(),140 'is_remote': ml.is_remote(),141 }142 self.check_single_dict(expected, result)143 def test_mailing_list_6(self):144 '''Local directory in .mlstats with no trailing slash'''145 base = os.path.sep.join([COMPRESSED_DIR,146 'mlstats.org/pipermail/mlstats-list'])147 target = base.lstrip(os.path.sep)148 ml = MailingList(base)149 expected = {150 'location': base.rstrip(os.path.sep),151 'alias': 'mlstats-list',152 'compressed_dir': os.path.join(COMPRESSED_DIR, target),153 'is_local': True,154 'is_remote': False,155 }156 result = {157 'location': ml.location,158 'alias': ml.alias,159 'compressed_dir': ml.compressed_dir,160 'is_local': ml.is_local(),161 'is_remote': ml.is_remote(),162 }163 self.check_single_dict(expected, result)164 def test_mailing_list_7(self):165 '''Local directory in .mlstats with trailing slash'''166 base = os.path.sep.join([COMPRESSED_DIR,167 'mlstats.org/pipermail/mlstats-list/'])168 ml = MailingList(base)169 target = base.strip(os.path.sep)170 expected = {171 'location': base.rstrip(os.path.sep),172 'alias': 'mlstats-list',173 'compressed_dir': os.path.join(COMPRESSED_DIR, target),174 'is_local': True,175 'is_remote': False,176 }177 result = {178 'location': ml.location,179 'alias': ml.alias,180 'compressed_dir': ml.compressed_dir,181 'is_local': ml.is_local(),182 'is_remote': ml.is_remote(),183 }184 self.check_single_dict(expected, result)185 def test_mailing_list_8(self):186 '''Local URL (file://) with no trailing slash'''187 base = 'file:///mlstats.org/pipermail/mlstats-list'188 target = '/mlstats.org/pipermail/mlstats-list'.lstrip(os.path.sep)189 ml = MailingList(base)190 expected = {191 'location': '/mlstats.org/pipermail/mlstats-list',192 'alias': 'mlstats-list',193 'compressed_dir': os.path.join(COMPRESSED_DIR, target),194 'is_local': True,195 'is_remote': False,196 }197 result = {198 'location': ml.location,199 'alias': ml.alias,200 'compressed_dir': ml.compressed_dir,201 'is_local': ml.is_local(),202 'is_remote': ml.is_remote(),203 }204 self.check_single_dict(expected, result)205 def test_mailing_list_9(self):206 '''Local URL (file://) with trailing slash'''207 base = 'file:///mlstats.org/pipermail/mlstats-list/'208 ml = MailingList(base)209 target = '/mlstats.org/pipermail/mlstats-list'.lstrip(os.path.sep)210 expected = {211 'location': '/mlstats.org/pipermail/mlstats-list',212 'alias': 'mlstats-list',213 'compressed_dir': os.path.join(COMPRESSED_DIR, target),214 'is_local': True,215 'is_remote': False,216 }217 result = {218 'location': ml.location,219 'alias': ml.alias,220 'compressed_dir': ml.compressed_dir,221 'is_local': ml.is_local(),222 'is_remote': ml.is_remote(),223 }...
file_transfer.py
Source:file_transfer.py
1from typing import Optional, Union2from .enums import FileTransferStrategyTypes, FtCallDepReturnValue, Order3from .file import File4from .strategies.http_strategy import HTTP5from .strategies.rsync_strategy import Rsync6from .strategies.transfer_strategy_base import FileTransferStrategy7class FileTransfer:8 """9 FileTransfer object class that takes two File objects or filepaths (from, to) and a File Transfer Strategy to perform remote or local file transfer operations.10 Attributes:11 from_file: Filepath or File object corresponding to the source file.12 to_file: Filepath or File object corresponding to the destination file.13 order: Order (enum) to execute the file transfer before (Order.BEFORE) or after (Order.AFTER) electron execution.14 strategy: Optional File Transfer Strategy to perform file operations - default will be resolved from provided file schemes.15 """16 def __init__(17 self,18 from_file: Optional[Union[File, str]] = None,19 to_file: Optional[Union[File, str]] = None,20 order: Order = Order.BEFORE,21 strategy: Optional[FileTransferStrategy] = None,22 ) -> None:23 if isinstance(from_file, str) or from_file is None:24 from_file = File(from_file)25 elif not isinstance(from_file, File):26 raise AttributeError(27 "FileTransfer() requires files to be either of type File, string, or None."28 )29 if isinstance(to_file, str) or to_file is None:30 to_file = File(to_file)31 elif not isinstance(to_file, File):32 raise AttributeError(33 "FileTransfer requires files to be either of type File, string, or None."34 )35 # assign explicit strategy or default to strategy based on from_file & to_file schemes36 if strategy:37 self.strategy = strategy38 elif (39 from_file.mapped_strategy_type == FileTransferStrategyTypes.Rsync40 and to_file.mapped_strategy_type == FileTransferStrategyTypes.Rsync41 ):42 self.strategy = Rsync()43 elif from_file.mapped_strategy_type == FileTransferStrategyTypes.HTTP:44 self.strategy = HTTP()45 else:46 raise AttributeError("FileTransfer requires a file transfer strategy to be specified")47 self.to_file = to_file48 self.from_file = from_file49 self.order = order50 # this is currently the case but as we further develop file transfer strategies we may support this51 # for example we may support streaming files between buckets in S352 if self.from_file.is_remote and self.to_file.is_remote:53 raise ValueError(54 "FileTransfer currently does not support remote->remote file transfers, please update from_filepath or to_filepath to correspond to a local filepath."55 )56 def cp(self):57 file_transfer_call_dep = None58 return_value_type = FtCallDepReturnValue.FROM_TO59 # local -> local or remote -> remote60 if (not self.from_file.is_remote and not self.to_file.is_remote) or (61 self.from_file.is_remote and self.to_file.is_remote62 ):63 file_transfer_call_dep = self.strategy.cp(self.from_file, self.to_file)64 # local -> remote65 if not self.from_file.is_remote and self.to_file.is_remote:66 file_transfer_call_dep = self.strategy.upload(self.from_file, self.to_file)67 # remote -> local68 if self.from_file.is_remote and not self.to_file.is_remote:69 file_transfer_call_dep = self.strategy.download(self.from_file, self.to_file)70 pre_transfer_hook_call_dep = self.strategy.pre_transfer_hook(71 self.from_file, self.to_file, return_value_type=return_value_type72 )73 return (pre_transfer_hook_call_dep, file_transfer_call_dep)74# Factories75def TransferFromRemote(76 from_filepath: str,77 to_filepath: Union[str, None] = None,78 strategy: Optional[FileTransferStrategy] = None,79 order: Order = Order.BEFORE,80) -> FileTransfer:81 """82 Factory for creating a FileTransfer instance where from_filepath is implicitly created as a remote File Object, and the order (Order.BEFORE) is set so that this file transfer will occur prior to electron execution.83 Args:84 from_filepath: File path corresponding to remote file (source).85 to_filepath: File path corresponding to local file (destination)86 strategy: Optional File Transfer Strategy to perform file operations - default will be resolved from provided file schemes.87 order: Order (enum) to execute the file transfer before (Order.BEFORE) or after (Order.AFTER) electron execution - default is BEFORE88 Returns:89 FileTransfer instance with implicit Order.BEFORE enum set and from (source) file marked as remote90 """91 # override is_remote for the case where from_filepath is of a file:// scheme where the file is remote (rsync ssh)92 from_file = File(from_filepath, is_remote=True)93 return FileTransfer(94 from_file=from_file, to_file=to_filepath, order=Order.BEFORE, strategy=strategy95 )96def TransferToRemote(97 to_filepath: str,98 from_filepath: Union[str, None] = None,99 strategy: Optional[FileTransferStrategy] = None,100 order: Order = Order.AFTER,101) -> FileTransfer:102 """103 Factory for creating a FileTransfer instance where to_filepath is implicitly created as a remote File Object, and the order (Order.AFTER) is set so that this file transfer will occur post electron execution.104 Args:105 to_filepath: File path corresponding to remote file (destination)106 from_filepath: File path corresponding to local file (source).107 strategy: Optional File Transfer Strategy to perform file operations - default will be resolved from provided file schemes.108 order: Order (enum) to execute the file transfer before (Order.BEFORE) or after (Order.AFTER) electron execution - default is AFTER109 Returns:110 FileTransfer instance with implicit Order.AFTER enum set and to (destination) file marked as remote111 """112 # override is_remote for the case where to_filepath is of a file:// scheme where the file is remote (rsync ssh)113 to_file = File(to_filepath, is_remote=True)114 return FileTransfer(115 from_file=from_filepath, to_file=to_file, order=Order.AFTER, strategy=strategy...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!