# Handling auto_clean_logs() during Integration Tests

When running integration tests with pytest, i.e. tests that call cli_controller directly, certain exception handlers may trigger calls to auto_clean_logs():

```python
try:
    return main(cli_options, wox_steward.wox_data)
except (ConfigFileNotFoundError, MissingWoxSectionError) as e:
    print(e)
    auto_clean_logs()
    return ExitCode.CONFIG_ERROR.value
except KeyboardInterrupt:
    print('Wox session aborted by user.')
    return ExitCode.INTERRUPTED.value
except Exception as e:
    auto_clean_logs()
    print(traceback.format_exc())
    raise e
```

However, some degraded integration tests — tests that intentionally trigger errors — may enter these except blocks, which causes the log file to be deleted.
This behavior is undesirable when running under pytest.

To prevent it, we can detect whether Wox is running under a pytest session and disable auto_clean_logs() accordingly:

```python
try:
    return main(cli_options, wox_steward.wox_data)
except (ConfigFileNotFoundError, MissingWoxSectionError) as e:
    print(e)
    if 'pytest' not in sys.modules:
        auto_clean_logs()
    return ExitCode.CONFIG_ERROR.value
except KeyboardInterrupt:
    print('Wox session aborted by user.')
    return ExitCode.INTERRUPTED.value
except Exception as e:
    if 'pytest' not in sys.modules:
        auto_clean_logs()
    print(traceback.format_exc())
    raise e
```

## Design Consideration

It might seem questionable to introduce test-specific logic into production code.
However, this check is located exactly where it should be:

- The core logic resides in main().

- The CLI controller serves as a boundary between the user-facing interface and the internal execution flow.

- Therefore, it is the appropriate place to handle such environmental conditions.

This approach follows good design principles by isolating test-related behavior at the edge of the runtime, without polluting the application’s business logic or altering production reliability.
