def test_encoding(): """ Test encoding """ p = util.tmppath() def test(enc): f = codecs.open(p, "w", enc) f.write(given) f.close() eq_(sy.path.slurp(p, binary=True), given.encode(enc)) eq_(sy.path.slurp(p, encoding=enc), expected) eq_(sy.path.lines(p, encoding=enc), expected_lines_retain) eq_(sy.path.lines(p, encoding=enc, newline=False), expected_lines) expected_nohang = expected + u"\n" sy.path.dump(p, expected_nohang, encoding=enc) sy.path.append(p, expected_nohang, encoding=enc) # utf-16 writes BOM to the middle of the file on appending # dont test further if enc == "utf-16": return expected_bytes = 2 * expected_nohang.replace("\n", os.linesep).encode(enc) expected_lines_retain_nohang = expected_lines_retain[:] expected_lines_retain_nohang[-1] += "\n" eq_(sy.path.slurp(p, binary=True), expected_bytes) eq_(sy.path.slurp(p, encoding=enc), 2 * expected_nohang) eq_(sy.path.lines(p, encoding=enc), 2 * expected_lines_retain_nohang) eq_(sy.path.lines(p, encoding=enc, newline=False), 2 * expected_lines) test("utf8") test("utf-16be") test("utf-16le") test("utf-16")
def test_http_download(): mock = util.MockHTTPHandler() util.setup_urllib_mock(mock) p = util.tmppath() sy.net.download('http://www', p) eq_(sy.path.slurp(p), 'mock file')
def test_nonexistant_url(): mock = util.MockHTTPHandler(mock_url = 'http://xxx') util.setup_urllib_mock(mock) p = util.tmppath() try: sy.net.download('http://www', p) assert False, 'Download bad url should fail' except urllib2.HTTPError: pass
def test_bad_url(): def urlerror(req): raise urllib2.URLError('no host given') mock = util.MockHTTPHandler(resp_func=urlerror) util.setup_urllib_mock(mock) p = util.tmppath() try: sy.net.download('http:/www', p) assert False, 'Bad url should fail' except urllib2.URLError: pass
def test_dump_newline(): p = util.tmppath() sy.path.dump(p, "\r") eq_(sy.path.slurp(p), "\n") sy.path.dump(p, "\r\n", newline=None) eq_(sy.path.slurp(p), "\n") sy.path.dump(p, "\r", newline=None) eq_(sy.path.slurp(p, binary=True), "\r") sy.path.dump(p, "\r\n", newline=None) eq_(sy.path.slurp(p, binary=True), "\r\n")
def test_md5sum(): p = util.tmppath("basic") eq_(sy.path.md5sum(p), "5a0b7eaaa1b487776afd6e68eb9fbd29") eq_(sy.path.md5sum(p, hex=False), "Z\x0b~\xaa\xa1\xb4\x87wj\xfdnh\xeb\x9f\xbd)") # compare with os command try: md5sum = sy.cmd.find("md5sum") except sy.CommandError: # not comparing to os md5sum print "Not comparing to os md5sum" return out, _ = sy.cmd.do(md5sum + " {}", p) ossum, _ = out.split() eq_(sy.path.md5sum(p), ossum)
def test_txt_attachment(self): p = util.tmppath(suffix='.txt') sy.path.dump(p, 'attachment') msg = {'attachments': [p]} msg.update(self.msg_dict) mimeattach = ('Content-Type: text/plain; charset="us-ascii"\n' 'MIME-Version: 1.0\n' 'Content-Transfer-Encoding: 7bit\n' 'Content-Disposition: attachment;' 'filename="SY_TEST_Ozmxzu.txt"\n\n' 'attachment\n' '--===============1919994729==--') sy.net.sendmail(**msg) eq_(len(self.inbox), 1) self._assert_message_eq(self.msg_dict, self.inbox[0], mimeattach=mimeattach)
def test_unknown_attachment(self): p = util.tmppath() sy.path.dump(p, 'attachment') msg = {'attachments': [p]} msg.update(self.msg_dict) mimeattach = ('Content-Type: application/octet-stream\n' 'MIME-Version: 1.0\n' 'Content-Transfer-Encoding: base64\n' 'Content-Disposition: attachment;' 'filename="SY_TEST_Ozmxzu"\n\n' 'YXR0YWNobWVudA==\n' '--===============1919994729==--') sy.net.sendmail(**msg) eq_(len(self.inbox), 1) self._assert_message_eq(self.msg_dict, self.inbox[0], mimeattach=mimeattach)
def test_replace_remove_keywords(): p = util.tmppath("replace") nr = sy.path.remove_lines(p, matching="irst_line") eq_(nr, 1)
def setup_basic(): p = util.tmppath("basic") sy.path.dump(p, basic_given)
def test_contains_found(): p = util.tmppath("basic") assert sy.path.contains(p, "letters") assert sy.path.contains(p, "\d") assert sy.path.contains(p, "num.er\n") assert not sy.path.contains(p, "people")
def test_replace_count_replace(): p = util.tmppath("replace") nr = sy.path.replace(p, match="column2", replacement="c") eq_(nr, 2)
def test_replace_count_replace_lines(): p = util.tmppath("replace") nr = sy.path.replace_lines(p, matching="column", replacement="") eq_(nr, 2)
def test_replace_sub_M(): p = util.tmppath("replace") rx = re.compile("^(\w+)_line", re.MULTILINE) sy.path.replace(p, rx, r"line_\1") eq_(sy.path.slurp(p), replace_expect_sub)
def test_replace_sub(): p = util.tmppath("replace") sy.path.replace(p, r"(\w+)_line", r"line_\1") eq_(sy.path.slurp(p), replace_expect_sub)
def test_replace_multiline(): p = util.tmppath("replace") rx = re.compile("column2.*", re.DOTALL) sy.path.replace(p, rx, "x\nx\n") eq_(sy.path.slurp(p), replace_expect_multiline)
def test_replace_basic(): p = util.tmppath("replace") nr = sy.path.replace(p, match="column", replacement="col") eq_(sy.path.slurp(p), replace_expect) eq_(nr, 3)
def test_replace_remove(): p = util.tmppath("replace") sy.path.remove_lines(p, "irst_line") eq_(len(sy.path.slurp(p).splitlines()), 2, msg="re.search should be used") sy.path.remove_lines(p, ".*column2$") eq_(len(sy.path.slurp(p).splitlines()), 1)
def test_replace_line(): p = util.tmppath("replace") sy.path.replace_lines(p, "first_line", "first_line_replaced") sy.path.replace_lines(p, "second_line", "second_line_replaced") sy.path.replace_lines(p, "column3", "third_line_replaced") eq_(sy.path.slurp(p), replace_expect_line)
def test_replace_line_keywords(): p = util.tmppath("replace") nr = sy.path.replace_lines(p, matching="first_line", replacement="first_line_replaced") eq_(nr, 1)
def setup_replace(): p = util.tmppath("replace") sy.path.dump(p, replace_given)