Commit f389b3c7 authored by Ken Zangelin's avatar Ken Zangelin
Browse files

fix(logUtils): honour the documented `console` argument; ETSI_QUIET_IO=1 keeps...

fix(logUtils): honour the documented `console` argument; ETSI_QUIET_IO=1 keeps requests and responses out of the terminal
parent c7d5bdff
Loading
Loading
Loading
Loading
+10 −0
Changes for README.md: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -185,6 +185,16 @@ execute the following command:
It will generate a specific `errors.log` file into the `results` folder with the description of the different steps 
developed and the mismatched observed on them.

### Keep the requests and responses out of the terminal

Every request and response is written to the terminal as well as to `log.html`. On a full run that is thousands of
them. With `ETSI_QUIET_IO=1` they go to `log.html` and `output.xml` only, and the terminal shows one line per Test
Case:

```ETSI_QUIET_IO=1 robot --outputdir ./results ./TP/NGSI-LD```

The `Output` and `Output Notification` keywords also take `console=${False}` for a single call.

### Measure the API coverage of a run

[scripts/api_coverage](./scripts/api_coverage) counts, from a run's `output.xml`, which operations of the NGSI-LD
+33 −8
Changes for libraries/logUtils.py: 33 added lines, 8 removed lines.
Original line number Diff line number Diff line
from __future__ import unicode_literals
from __future__ import division
from json import dumps, JSONDecodeError, loads
from os import environ
from robot.api import logger
from robot.api.deco import keyword


def _to_console(console):
    """Should this be echoed to the terminal?

    The `console` argument of the keywords below is documented ("If false, the
    JSON is not written to terminal") and was ignored - every request and every
    response went to the terminal. On a full run that is thousands of
    request/response pairs, which bury the one line per test that tells what
    happened.

    ETSI_QUIET_IO=1 switches the echo off for the whole run, without touching a
    test. Nothing is lost: logger.info always writes to log.html and output.xml.
    `console` may come from Robot as a string ("False", "no", "0", "off").
    """
    if environ.get("ETSI_QUIET_IO") == "1":
        return False
    if isinstance(console, str):
        return console.strip().lower() not in ("false", "no", "0", "off", "")
    return bool(console)


@keyword(name="Output", tags=("I/O",))
def output(response, description, console=True):
    """*Request and response are output to terminal and file (in JSON).*
@@ -34,11 +55,13 @@ def output(response, description, console=True):
    pretty_request_json = dumps(request_json, indent=4, sort_keys=False, separators=(",", ": "))
    pretty_response_json = dumps(response_json, indent=4, sort_keys=False, separators=(",", ": "))

    logger.info("\n" + description, also_console=True)
    logger.info("Request ->", also_console=True)
    logger.info(pretty_request_json, also_console=True)
    logger.info("Response ->", also_console=True)
    logger.info(pretty_response_json, also_console=True)
    echo = _to_console(console)

    logger.info("\n" + description, also_console=echo)
    logger.info("Request ->", also_console=echo)
    logger.info(pretty_request_json, also_console=echo)
    logger.info("Response ->", also_console=echo)
    logger.info(pretty_response_json, also_console=echo)


@keyword(name="Output Notification", tags=("I/O",))
@@ -54,6 +77,8 @@ def output_notification(body, headers, description, console=True):

    pretty_request_json = dumps(request_json, indent=4, sort_keys=False, separators=(",", ": "))

    logger.info("\n" + description, also_console=True)
    logger.info("Notification ->", also_console=True)
    logger.info(pretty_request_json, also_console=True)
    echo = _to_console(console)

    logger.info("\n" + description, also_console=echo)
    logger.info("Notification ->", also_console=echo)
    logger.info(pretty_request_json, also_console=echo)