def test_log_summary_writer_as_context_manager(ex, tf):
    """
    Check that Tensorflow log directory is captured by LogFileWriter context manager.
    """
    TEST_LOG_DIR = "/tmp/sacred_test1"
    TEST_LOG_DIR2 = "/tmp/sacred_test2"

    @ex.main
    def run_experiment(_run):
        assert _run.info.get("tensorflow", None) is None
        with tf.Session() as s:
            # Without using the LogFileWriter context manager, nothing should change
            swr = tf.summary.FileWriter(logdir=TEST_LOG_DIR, graph=s.graph)
            assert swr is not None
            assert _run.info.get("tensorflow", None) is None

            # Capturing the log directory should be done only in scope of the context manager
            with LogFileWriter(ex):
                swr = tf.summary.FileWriter(logdir=TEST_LOG_DIR, graph=s.graph)
                assert swr is not None
                assert _run.info["tensorflow"]["logdirs"] == [TEST_LOG_DIR]
                tf.summary.FileWriter(TEST_LOG_DIR2, s.graph)
                assert _run.info["tensorflow"]["logdirs"] == [
                    TEST_LOG_DIR,
                    TEST_LOG_DIR2,
                ]

            # This should not be captured:
            tf.summary.FileWriter("/tmp/whatever", s.graph)
            assert _run.info["tensorflow"]["logdirs"] == [TEST_LOG_DIR, TEST_LOG_DIR2]

    ex.run()

    Check that Tensorflow log directory is captured by LogFileWriter context manager.
    
tests/test_stflow/test_method_interception.py