def test_check_output_with_stderr(capsys: SysCapture, use_filter: bool, cmd_error: bool) -> None: cmd = r'>&2 echo "first error\nsecond error" && echo "the output"' stderr_filter = None if not use_filter else lambda line: line.startswith( "second") if cmd_error: cmd += r" && exit 1" with pytest.raises(subprocess.CalledProcessError) as err: check_output_with_stderr(cmd, shell=True, stderr_filter=stderr_filter) assert err.value.stderr == b"first error\nsecond error\n" else: output = check_output_with_stderr(cmd, shell=True, stderr_filter=stderr_filter) assert output == b"the output\n" captured = capsys.readouterr() assert captured.out == "" if use_filter: assert captured.err == "second error\n" else: assert captured.err == "first error\nsecond error\n"
def test_check_output_with_stderr(capsysbinary: SysCaptureBinary): output = check_output_with_stderr( r'>&2 echo "the error" && echo "the output"', shell=True) assert output == b"the output\n" captured = capsysbinary.readouterr() assert captured.out == b"" assert captured.err == b"the error\n"
def _vl2vg(self, spec: JSONDict) -> JSONDict: """Compile a Vega-Lite spec into a Vega spec.""" vl2vg = exec_path("vl2vg") vl_json = json.dumps(spec).encode() vg_json = check_output_with_stderr( [vl2vg], input=vl_json, stderr_filter=self._stderr_filter ) return json.loads(vg_json)
def _vg2svg(self, spec: JSONDict) -> str: """Generate an SVG image from a Vega spec.""" vg2svg = exec_path("vg2svg") vg_json = json.dumps(spec).encode() return check_output_with_stderr( [vg2svg, *(self._vega_cli_options or [])], input=vg_json, stderr_filter=self._stderr_filter, ).decode()
def _vg2pdf(self, spec: JSONDict) -> bytes: """Generate a PDF image from a Vega spec.""" vg2pdf = exec_path("vg2pdf") vg_json = json.dumps(spec).encode() return check_output_with_stderr( [vg2pdf, *(self._vega_cli_options or [])], input=vg_json, stderr_filter=self._stderr_filter, )
def npm_bin(global_: bool) -> str: """Locate the npm binary directory.""" npm = shutil.which("npm") if not npm: raise ExecutableNotFound("npm") cmd = [npm, "bin"] if global_: cmd.append("--global") return check_output_with_stderr(cmd).decode().strip()
def test_check_output_with_stderr_exit_1(capsysbinary: SysCaptureBinary): with pytest.raises(subprocess.CalledProcessError) as err: output = check_output_with_stderr( r'>&2 echo "the error" && echo "the output" && exit 1', shell=True) assert output == b"the output\n" assert err.value.stderr == b"the error\n" captured = capsysbinary.readouterr() assert captured.out == b"" assert captured.err == b"the error\n"
def vg2svg(spec: JSONDict) -> str: """Generate an SVG image from a Vega spec.""" vg2svg = exec_path("vg2svg") vg_json = json.dumps(spec).encode() return check_output_with_stderr([vg2svg], input=vg_json).decode()
def vg2pdf(spec: JSONDict) -> bytes: """Generate a PDF image from a Vega spec.""" vg2pdf = exec_path("vg2pdf") vg_json = json.dumps(spec).encode() return check_output_with_stderr([vg2pdf], input=vg_json)