How to use magic_wrap method in tavern

Best Python code snippet using tavern

test_utilities.py

Source:test_utilities.py Github

copy

Full Screen

...315 os.remove(f.name)316class TestLoadFile:317 @staticmethod318 @contextlib.contextmanager319 def magic_wrap(to_wrap, suffix):320 with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as wrapped_tmp:321 dumped = yaml.dump(to_wrap, default_flow_style=False)322 wrapped_tmp.write(dumped.encode("utf8"))323 wrapped_tmp.close()324 try:325 yield wrapped_tmp.name326 finally:327 os.remove(wrapped_tmp.name)328 @pytest.mark.parametrize("suffix", (".yaml", ".yml", ".json"))329 def test_load_extensions(self, suffix):330 example = {"a": "b"}331 with TestLoadFile.magic_wrap(example, suffix) as tmpfile:332 with patch("tavern.util.loader.os.path.join", return_value=tmpfile):333 assert example == construct_include(Mock(), Mock())334 def test_load_bad_extension(self):335 example = {"a": "b"}336 with TestLoadFile.magic_wrap(example, ".bllakjf") as tmpfile:337 with patch("tavern.util.loader.os.path.join", return_value=tmpfile):338 with pytest.raises(exceptions.BadSchemaError):339 construct_include(Mock(), Mock())340 def test_include_path(self):341 example = {"a": "b"}342 with TestLoadFile.magic_wrap(example, ".yaml") as tmpfile:343 tmppath, tmpfilename = os.path.split(tmpfile)344 with pytest.raises(exceptions.BadSchemaError):345 construct_include(346 Mock(347 _root="/does-not-exist", construct_scalar=lambda x: tmpfilename348 ),349 Mock(),350 )351 with patch("tavern.util.loader.IncludeLoader.env_path_list", None):352 assert example == construct_include(353 Mock(_root=tmppath, construct_scalar=lambda x: tmpfilename), Mock()354 )355 os.environ[IncludeLoader.env_var_name] = tmppath356 with patch("tavern.util.loader.IncludeLoader.env_path_list", None):...

Full Screen

Full Screen

selectors.py

Source:selectors.py Github

copy

Full Screen

1"""2Helpers to pick a selector based on its contents.3Typically used before injecting data to the bus, when the selector has not4already been determined by user-input or by the agent..5To use this module to pick a selector, import the following module:6from rebus.tools.selector import guess_selector7Then call guess_selector(fname, buf, label)8To register a new function that can guess selectors, use the following9construct:10from rebus.tools import selectors11@selectors.register12def my_function(fname, buf, label, magic_txt, default_selector):13 if is_foobar(value):14 return 10, "/foobar"15"""16import struct17from rebus.tools import magic_wrap18_guess_functions = []19def register(f):20 """21 The expected return value for such registered guessing function is either:22 * None, meaning no type could be guessed by this function23 * (score: int, selector: str) having score > 0. If several guessing24 functions return a selector, then one of the selectors having the highest25 confidence score is chosen.26 Expected signature for registered functions:27 :param magic_txt: string returned by the "magic" module28 :param label: descriptor's label29 :param default_selector: selector that has been determined by rebus'30 default method, _default_guess31 :param value: descriptor's value32 """33 _guess_functions.append(f)34 return f35def guess_selector(fname=None, buf=None, label=None):36 """37 Called by agents that want to pick a selector.38 :param fname: local file name39 :param buf: descriptor contents as str40 :param label: descriptor label41 """42 if fname is not None:43 magic_txt = magic_wrap.from_file(fname)44 elif buf is not None:45 magic_txt = magic_wrap.from_buffer(buf)46 else:47 raise Exception("Either fname or buffer must be set when calling "48 "guess_selector.")49 default_selector = selector = _default_guess(fname, buf, label, magic_txt)50 score = 051 for guess_fun in _guess_functions:52 res = guess_fun(fname, buf, label, magic_txt, default_selector)53 if res:54 new_score, new_selector = res55 if new_score > score:56 selector = new_selector57 score = new_score58 return selector59def _default_guess(fname=None, buf=None, label=None, magic_txt=""):60 """61 Contains common selector determination functions. Agents that want to pick62 a selector should call guess_selector instead.63 """64 # Executable files65 if ".Net" in magic_txt:66 return "/binary/net"67 if "ELF" in magic_txt and ("sharedobject" in magic_txt or68 "executable" in magic_txt):69 return "/binary/elf"70 if "executable" in magic_txt and "PE32" in magic_txt:71 return "/binary/pe"72 if "DOS" in magic_txt:73 # libmagic is a bit buggy74 # make sure it's not a PE75 try:76 if fname is not None:77 buf = open(fname).read()78 if buf is not None:79 # MZ.e_lfanew80 e_lfanew = struct.unpack('<I', buf[0x3C:0x3C+4])[0]81 if buf[e_lfanew:e_lfanew+4] == "PE\x00\x00":82 return "/binary/pe"83 except:84 return "/binary/dos"85 return "/binary/dos"86 if "Mach-O" in magic_txt:87 return "/binary/macho"88 # Compressed files89 if "gzip compressed data" in magic_txt:90 return "/compressed/gzip"91 if "bzip2 compressed data" in magic_txt:92 return "/compressed/bzip2"93 # Archive files94 if "POSIX tar archive" in magic_txt:95 return "/archive/tar"96 if "Zip archive data" in magic_txt:97 return "/archive/zip"98 if "Microsoft Cabinet archive data" in magic_txt:99 return "/archive/cab"100 if "Java Jar file data" in magic_txt:101 return "/archive/jar"102 if "RAR archive data" in magic_txt:103 return "/archive/rar"104 # E-mails105 if 'Composite Document File V2 Document' in magic_txt:106 if label and label.endswith('.msg'):107 return "/email/msg"108 if 'ASCII text' in magic_txt:109 if label and label.endswith('.eml'):110 return '/email/eml'111 # TODO peek at contents & grep headers to identify e-mail?112 if 'RFC 822 mail, ASCII text' in magic_txt:113 return '/email/eml'114 if 'SMTP mail, ASCII text' in magic_txt:115 return '/email/eml'116 # Documents117 if 'PDF document, version' in magic_txt:118 return "/document/pdf"119 if 'Rich Text Format' in magic_txt:120 return "/document/rtf"121 if 'Microsoft Word 2007+' in magic_txt:122 return "/document/msoffice/docx"123 if 'Microsoft Excel 2007+' in magic_txt:124 return "/document/msoffice/xlsx"125 if 'Microsoft PowerPoint 2007+' in magic_txt:126 return "/document/msoffice/pptx"127 if ('Composite Document File V2 Document' in magic_txt or128 'Microsoft OOXML' in magic_txt):129 if 'MSI Installer' in magic_txt:130 return "/binary/msi"131 if 'Microsoft Excel' in magic_txt:132 return "/document/msoffice/xls"133 if 'Microsoft PowerPoint' in magic_txt:134 return "/document/msoffice/ppt"135 if label:136 if label.endswith('.ppt'):137 return "/document/msoffice/ppt"138 if label.endswith('.pptx'):139 return "/document/msoffice/pptx"140 if label.endswith('.xls'):141 return "/document/msoffice/xls"142 if label.endswith('.xlsx'):143 return "/document/msoffice/xlsx"144 if label.endswith('.doc'):145 return "/document/msoffice/doc"146 if label.endswith('.docx'):147 return "/document/msoffice/docx"148 return "/document/msoffice/doc"149 # ASCII text150 if 'ASCII text' in magic_txt:151 return "/text/ascii"...

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 tavern 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