def test_compare_same_files(): approver = FileApprover() writer = StringWriter("a") writer.write_received_file("a.txt") shutil.copy("a.txt", "a_same.txt") approver.verify_files("a.txt", "a_same.txt", None, Options().comparator)
def test_writes_file(tmpdir): contents = "foo" + str(randint(0, 100)) sw = StringWriter(contents) filename = os.path.join(str(tmpdir), 'stuff.txt') sw.write_received_file(filename) with open(filename, 'r') as received: assert contents == received.read()
def test_writes_file_to_missing_directory(tmpdir): contents = "foo" sw = StringWriter(contents) filename = os.path.join(str(tmpdir), 'non_existent_folder', './stuff.txt') sw.write_received_file(filename) with open(filename, 'r') as received: assert contents == received.read()
def test_writes_file(self): contents = "foo" + str(randint(0, 100)) sw = StringWriter(contents) filename = './stuff.txt' sw.write_received_file(filename) with open(filename, 'r') as received: self.assertEqual(contents, received.read()) os.remove(filename)
def test_returns_none_when_files_are_same_files(self): namer = Namer() writer = StringWriter("b") reporter = GenericDiffReporterFactory().get_first_working() approver = FileApprover() error = approver.verify(namer, writer, reporter) self.assertEqual(None, error)
def test_returns_error_when_files_are_different(self): namer = Namer() writer = StringWriter("b") reporter = TestingReporter() approver = FileApprover() error = approver.verify(namer, writer, reporter) self.assertEqual("Approval Mismatch", error)
def test_full(self): namer = Namer() writer = StringWriter("b") reporter = TestingReporter() approver = FileApprover() approver.verify(namer, writer, reporter) self.assertTrue(reporter.called)
def test_full(self): namer = get_default_namer() writer = StringWriter("b") reporter = ReporterForTesting() approver = FileApprover() approver.verify(namer, writer, reporter, Options().comparator) self.assertTrue(reporter.called)
def verify_with_namer(data, namer, reporter): reporter = get_reporter(reporter) approver = FileApprover() writer = StringWriter(data) error = approver.verify(namer, writer, reporter) if error is not None: raise ApprovalException(error)
def test_returns_error_when_files_are_different(self): namer = get_default_namer() writer = StringWriter("b") reporter = ReporterForTesting() approver = FileApprover() error = approver.verify(namer, writer, reporter) import re replaced = re.sub("ved: .*approved_files.", 'ved: <rootdir>/', error) verify(replaced)
def verify_with_namer( data: Any, namer: Namer, reporter: Optional[Reporter] = None, encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None, *, # enforce keyword arguments - https://www.python.org/dev/peps/pep-3102/ options: Optional[Options] = None ) -> None: """Verify string data against a previously approved version of the string. Args: data: A string containing the data to be compared with approved data from a previous run. On Python 2 this can be a bytes, str, or unicode object. On Python 3 this should be a str object. namer: A Namer instance used for naming approved and received data files. reporter: An optional Reporter. If None (the default), the default reporter will be used; see get_default_reporter(). encoding: An optional encoding to be used when serialising the data to a byte stream for comparison. If None (the default) a locale-dependent encoding will be used; see locale.getpreferredencoding(). errors: An optional string that specifies how encoding and decoding errors are to be handled If None (the default) or 'strict', raise a ValueError exception if there is an encoding error. Pass 'ignore' to ignore encoding errors. Pass 'replace' to use a replacement marker (such as '?') when there is malformed data. newline: An optional string that controls how universal newlines work when comparing data. It can be None, '', '\n', '\r', and '\r\n'. If None (the default) universal newlines are enabled and any '\n' characters are translated to the system default line separator given by os.linesep. If newline is '', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string. Raises: ApprovalException: If the verification fails because the given string does not match the approved string. ValueError: If data cannot be encoded using the specified encoding when errors is set to None or 'strict'. """ options = initialize_options(options, reporter) writer = StringWriter( options.scrub(str(data)), encoding=encoding, errors=errors, newline=newline ) verify_with_namer_and_writer(namer, writer, options=options)
def test_compare_same_files(): approver = FileApprover() writer = StringWriter("a") writer.write_received_file("a.txt") shutil.copy("a.txt", "a_same.txt") approver.verify_files("a.txt", "a_same.txt", None)
def test_new_lines_with_empty_string(): sw = StringWriter("") sw2 = StringWriter(None) assert sw.contents == sw2.contents