def test_stderr(self): cap = capture.FDCapture(2) cap.start() print_("hello", file=sys.stderr) s = cap.snap() cap.done() assert s == "hello\n"
def fnmatch_lines(self, lines2): if isinstance(lines2, str): lines2 = py.code.Source(lines2) if isinstance(lines2, py.code.Source): lines2 = lines2.strip().lines from fnmatch import fnmatch lines1 = self.lines[:] nextline = None extralines = [] __tracebackhide__ = True for line in lines2: nomatchprinted = False while lines1: nextline = lines1.pop(0) if line == nextline: print_("exact match:", repr(line)) break elif fnmatch(nextline, line): print_("fnmatch:", repr(line)) print_(" with:", repr(nextline)) break else: if not nomatchprinted: print_("nomatch:", repr(line)) nomatchprinted = True print_(" and:", repr(nextline)) extralines.append(nextline) else: assert line == nextline
def _run(self, *cmdargs): cmdargs = [str(x) for x in cmdargs] p1 = self.tmpdir.join("stdout") p2 = self.tmpdir.join("stderr") print_("running", cmdargs, "curdir=", py.path.local()) f1 = p1.open("wb") f2 = p2.open("wb") now = time.time() popen = self.popen(cmdargs, stdout=f1, stderr=f2, close_fds=(sys.platform != "win32")) ret = popen.wait() f1.close() f2.close() out = p1.read("rb") out = getdecoded(out).splitlines() err = p2.read("rb") err = getdecoded(err).splitlines() def dump_lines(lines, fp): try: for line in lines: py.builtin.print_(line, file=fp) except UnicodeEncodeError: print("couldn't print to %s because of encoding" % (fp,)) dump_lines(out, sys.stdout) dump_lines(err, sys.stderr) return RunResult(ret, out, err, time.time() - now)
def _run(self, *cmdargs): cmdargs = [str(x) for x in cmdargs] p1 = self.tmpdir.join("stdout") p2 = self.tmpdir.join("stderr") print_("running:", ' '.join(cmdargs)) print_(" in:", str(py.path.local())) f1 = codecs.open(str(p1), "w", encoding="utf8") f2 = codecs.open(str(p2), "w", encoding="utf8") try: now = time.time() popen = self.popen(cmdargs, stdout=f1, stderr=f2, close_fds=(sys.platform != "win32")) ret = popen.wait() finally: f1.close() f2.close() f1 = codecs.open(str(p1), "r", encoding="utf8") f2 = codecs.open(str(p2), "r", encoding="utf8") try: out = f1.read().splitlines() err = f2.read().splitlines() finally: f1.close() f2.close() self._dump_lines(out, sys.stdout) self._dump_lines(err, sys.stderr) return RunResult(ret, out, err, time.time()-now)
def fnmatch_lines(self, lines2): if isinstance(lines2, str): lines2 = py.code.Source(lines2) if isinstance(lines2, py.code.Source): lines2 = lines2.strip().lines from fnmatch import fnmatch __tracebackhide__ = True lines1 = self.lines[:] nextline = None extralines = [] for line in lines2: nomatchprinted = False while lines1: nextline = lines1.pop(0) if line == nextline: print_("exact match:", repr(line)) break elif fnmatch(nextline, line): print_("fnmatch:", repr(line)) print_(" with:", repr(nextline)) break else: if not nomatchprinted: print_("nomatch:", repr(line)) nomatchprinted = True print_(" and:", repr(nextline)) extralines.append(nextline) else: if line != nextline: #__tracebackhide__ = True raise AssertionError("expected line not found: %r" % line) extralines.extend(lines1) return extralines
def fnmatch_lines_random(self, lines2): lines2 = self._getlines(lines2) for line in lines2: for x in self.lines: if line == x or fnmatch(x, line): print_("matched: ", repr(line)) break else: raise ValueError("line %r not found in output" % line)
def fnmatch_lines_random(self, lines2): """Check lines exist in the output. The argument is a list of lines which have to occur in the output, in any order. Each line can contain glob whildcards. """ lines2 = self._getlines(lines2) for line in lines2: for x in self.lines: if line == x or fnmatch(x, line): print_("matched: ", repr(line)) break else: raise ValueError("line %r not found in output" % line)
def test_dupfile(tmpfile): flist = [] for i in range(5): nf = capture.safe_text_dupfile(tmpfile, "wb") assert nf != tmpfile assert nf.fileno() != tmpfile.fileno() assert nf not in flist print_(i, end="", file=nf) flist.append(nf) for i in range(5): f = flist[i] f.close() tmpfile.seek(0) s = tmpfile.read() assert "01234" in repr(s) tmpfile.close()
def test_dupfile(tmpfile): flist = [] for i in range(5): nf = py.io.dupfile(tmpfile, encoding="utf-8") assert nf != tmpfile assert nf.fileno() != tmpfile.fileno() assert nf not in flist print_(i, end="", file=nf) flist.append(nf) for i in range(5): f = flist[i] f.close() tmpfile.seek(0) s = tmpfile.read() assert "01234" in repr(s) tmpfile.close()
def getrepowc(tmpdir, reponame='basetestrepo', wcname='wc'): repo = tmpdir.mkdir(reponame) wcdir = tmpdir.mkdir(wcname) repo.ensure(dir=1) py.process.cmdexec('svnadmin create "%s"' % svncommon._escape_helper(repo)) py.process.cmdexec('svnadmin load -q "%s" <"%s"' % (svncommon._escape_helper(repo), repodump)) print_("created svn repository", repo) wcdir.ensure(dir=1) wc = py.path.svnwc(wcdir) if py.std.sys.platform == 'win32': repourl = "file://" + '/' + str(repo).replace('\\', '/') else: repourl = "file://%s" % repo wc.checkout(repourl) print_("checked out new repo into", wc) return (repo, repourl, wc)
def _run(self, *cmdargs): cmdargs = [str(x) for x in cmdargs] p1 = py.path.local("stdout") p2 = py.path.local("stderr") print_("running", cmdargs, "curdir=", py.path.local()) f1 = p1.open("w") f2 = p2.open("w") popen = self.popen(cmdargs, stdout=f1, stderr=f2, close_fds=(sys.platform != "win32")) ret = popen.wait() f1.close() f2.close() out, err = p1.readlines(cr=0), p2.readlines(cr=0) if err: for line in err: py.builtin.print_(line, file=sys.stderr) if out: for line in out: py.builtin.print_(line, file=sys.stdout) return RunResult(ret, out, err)
def assert_contains(self, entries): __tracebackhide__ = True i = 0 entries = list(entries) backlocals = sys._getframe(1).f_locals while entries: name, check = entries.pop(0) for ind, call in enumerate(self.calls[i:]): if call._name == name: print_("NAMEMATCH", name, call) if eval(check, backlocals, call.__dict__): print_("CHECKERMATCH", repr(check), "->", call) else: print_("NOCHECKERMATCH", repr(check), "-", call) continue i += ind + 1 break print_("NONAMEMATCH", name, "with", call) else: pytest.fail("could not find %r check %r" % (name, check))
def test_print_simple(): from py.builtin import print_ py.test.raises(TypeError, "print_(hello=3)") f = py.io.TextIO() print_("hello", "world", file=f) s = f.getvalue() assert s == "hello world\n" f = py.io.TextIO() print_("hello", end="", file=f) s = f.getvalue() assert s == "hello" f = py.io.TextIO() print_("xyz", "abc", sep="", end="", file=f) s = f.getvalue() assert s == "xyzabc" class X: def __repr__(self): return "rep" f = py.io.TextIO() print_(X(), file=f) assert f.getvalue() == "rep\n"
def write_log_entry(self, testpath, lettercode, longrepr): print_("%s %s" % (lettercode, testpath), file=self.logfile) for line in longrepr.splitlines(): print_(" %s" % line, file=self.logfile)
def test_stderr(self): cap = py.io.FDCapture(2, patchsys=True) print_("hello", file=sys.stderr) f = cap.done() s = f.read() assert s == "hello\n"
def write_log_entry(self, testpath, shortrepr, longrepr): print_("%s %s" % (shortrepr, testpath), file=self.logfile) for line in longrepr.splitlines(): print_(" %s" % line, file=self.logfile)