diff --git a/cpp/examples/ExampleFunctions/CMakeLists.txt b/cpp/examples/ExampleFunctions/CMakeLists.txt index 9a4e746..df1a82b 100644 --- a/cpp/examples/ExampleFunctions/CMakeLists.txt +++ b/cpp/examples/ExampleFunctions/CMakeLists.txt @@ -10,3 +10,4 @@ add_test(ExampleFunctions call_with_optional_string) add_test(ExampleFunctions pass_by_reference_dict) add_test(ExampleFunctions pass_by_reference_vector) add_test(ExampleFunctions throws_error) +add_test(ExampleFunctions returns_noisy_int) diff --git a/cpp/examples/ExampleFunctions/returns_noisy_int.cpp b/cpp/examples/ExampleFunctions/returns_noisy_int.cpp new file mode 100644 index 0000000..27b3847 --- /dev/null +++ b/cpp/examples/ExampleFunctions/returns_noisy_int.cpp @@ -0,0 +1,20 @@ +// Copyright 2020 Lawrence Livermore National Security, LLC and other CLIPPy +// Project Developers. See the top-level COPYRIGHT file for details. +// +// SPDX-License-Identifier: MIT + +#include + +int main(int argc, char **argv) { + clippy::clippy clip("returns_noisy_int", + "Tests returning a int with some extraneous stdout"); + clip.returns("The Int"); + if (clip.parse(argc, argv)) { + return 0; + } + + std::cout << "Here's some noise\n"; + std::cout << "More noise!\n"; + clip.to_return(size_t(42)); + return 0; +} diff --git a/py/src/clippy/backends/fs/execution.py b/py/src/clippy/backends/fs/execution.py index 0a1b248..023935f 100644 --- a/py/src/clippy/backends/fs/execution.py +++ b/py/src/clippy/backends/fs/execution.py @@ -10,6 +10,7 @@ import os import select import subprocess +import sys from ... import cfg from ...clippy_types import AnyDict @@ -99,7 +100,12 @@ def _stream_exec( stdout_buffer += text while "\n" in stdout_buffer: line, stdout_buffer = stdout_buffer.split("\n", 1) - d = json.loads(line, object_hook=decode_clippy_json) + try: + d = json.loads(line, object_hook=decode_clippy_json) + except json.JSONDecodeError: + warning = f"Warning: invalid JSON on stdout: {line!r}" + stderr_lines.append(warning + "\n") + print(warning, file=sys.stderr, flush=True) elif fd == stderr_fd: stderr_buffer += text while "\n" in stderr_buffer: diff --git a/test/test_clippy.py b/test/test_clippy.py index cbd2660..a274032 100644 --- a/test/test_clippy.py +++ b/test/test_clippy.py @@ -12,6 +12,9 @@ clippy.logger.setLevel(logging.WARN) logging.getLogger().setLevel(logging.WARN) +@pytest.fixture() +def examplefn(): + return clippy.ExampleFunctions() @pytest.fixture() def examplebag(): @@ -206,3 +209,9 @@ def test_graph(examplegraph): examplegraph.degree(examplegraph.node.degree) c_e_only = examplegraph.dump2(examplegraph.node.degree, where=examplegraph.node.degree > 2) assert "c" in c_e_only and "e" in c_e_only and len(c_e_only) == 2 + + +def test_noise(examplefn, capsys): + assert examplefn.returns_noisy_int() == 42 + captured = capsys.readouterr() + assert "Warning: invalid JSON on stdout" in captured.err