From 79045afada7f2a24221adf0788d4a40683a503ec Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Sun, 20 Aug 2017 00:48:43 -0700 Subject: Add tests for output redirection. --- lib/spack/spack/test/log.py | 95 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 lib/spack/spack/test/log.py (limited to 'lib') diff --git a/lib/spack/spack/test/log.py b/lib/spack/spack/test/log.py new file mode 100644 index 0000000000..9c05ed002d --- /dev/null +++ b/lib/spack/spack/test/log.py @@ -0,0 +1,95 @@ +############################################################################## +# Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC. +# Produced at the Lawrence Livermore National Laboratory. +# +# This file is part of Spack. +# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved. +# LLNL-CODE-647188 +# +# For details, see https://github.com/llnl/spack +# Please also see the NOTICE and LICENSE files for our notice and the LGPL. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License (as +# published by the Free Software Foundation) version 2.1, February 1999. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and +# conditions of the GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +############################################################################## +from __future__ import print_function +import pytest + +from llnl.util.tty.log import log_output +from spack.util.executable import which + + +def test_log_python_output_with_python_stream(capsys, tmpdir): + # pytest's DontReadFromInput object does not like what we do here, so + # disable capsys or things hang. + with capsys.disabled(): + with log_output('foo.txt'): + print('logged') + + with open('foo.txt') as f: + assert f.read() == 'logged\n' + + assert capsys.readouterr() == ('', '') + + +def test_log_python_output_with_fd_stream(capfd, tmpdir): + with log_output('foo.txt'): + print('logged') + + with open('foo.txt') as f: + assert f.read() == 'logged\n' + + assert capfd.readouterr() == ('', '') + + +def test_log_python_output_and_echo_output(capfd, tmpdir): + with log_output('foo.txt') as logger: + with logger.force_echo(): + print('echo') + print('logged') + + assert capfd.readouterr() == ('echo\n', '') + + with open('foo.txt') as f: + assert f.read() == 'echo\nlogged\n' + + +@pytest.mark.skipif(not which('echo'), reason="needs echo command") +def test_log_subproc_output(capsys, tmpdir): + echo = which('echo') + + # pytest seems to interfere here, so we need to use capsys.disabled() + # TODO: figure out why this is and whether it means we're doing + # sometihng wrong with OUR redirects. Seems like it should work even + # with capsys enabled. + with capsys.disabled(): + with log_output('foo.txt'): + echo('logged') + + with open('foo.txt') as f: + assert f.read() == 'logged\n' + + +@pytest.mark.skipif(not which('echo'), reason="needs echo command") +def test_log_subproc_and_echo_output(capfd, tmpdir): + echo = which('echo') + + with log_output('foo.txt') as logger: + with logger.force_echo(): + echo('echo') + print('logged') + + assert capfd.readouterr() == ('echo\n', '') + + with open('foo.txt') as f: + assert f.read() == 'logged\n' -- cgit v1.2.3-60-g2f50