Skip to content

Commit 7460b7d

Browse files
ShahanaFarooquirustyrussell
authored andcommitted
tests: Add logger config with fixture
Pyln logger's configuration was unexpectedly being overwritten by the root logger during the autogenerated examples test, complicating error debugging. We resolved this by introducing a pytest fixture to reapply the logger configuration before the tests executes. Changelog-None. Fixes: #8023
1 parent 32a991a commit 7460b7d

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

doc/contribute-to-core-lightning/coding-style-guidelines/writing-json-schemas.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ preventing accidental overwrites from unrelated tests.
9494
where `n` can be any number of repetitions. OR by manually running the test multiple times with:
9595

9696
```bash
97-
rm -rf /tmp/ltests* && make -s && VALGRIND=0 TIMEOUT=40 TEST_DEBUG=1 GENERATE_EXAMPLES=1 pytest -vvv tests/autogenerate-rpc-examples.py
97+
rm -rf /tmp/ltests* && make -s && VALGRIND=0 TIMEOUT=40 TEST_DEBUG=1 GENERATE_EXAMPLES=1 pytest -vvv -s tests/autogenerate-rpc-examples.py
9898
```
9999

100100
- Identify changing values, and add them to `REPLACE_RESPONSE_VALUES`:
@@ -117,7 +117,7 @@ environment variable with a comma-separated list of method names. Eg. `REGENERAT
117117
only regenerate examples for the `getinfo` and `connect` RPCs.
118118
2. To regenerate specific examples, set the REGENERATE environment variable:
119119
```bash
120-
REGENERATE='getinfo,connect' VALGRIND=0 TIMEOUT=10 TEST_DEBUG=1 GENERATE_EXAMPLES=1 pytest -vvv tests/autogenerate-rpc-examples.py
120+
REGENERATE='getinfo,connect' VALGRIND=0 TIMEOUT=10 TEST_DEBUG=1 GENERATE_EXAMPLES=1 pytest -vvv -s tests/autogenerate-rpc-examples.py
121121
```
122122
3. Logs are saved in `tests/autogenerate-examples-status.log`, and JSON data is in `tests/autogenerate-examples.json`.
123123
4. Run `make` after the script completes to ensure schema updates are applied in other places too, such as `...msggen/schema.json`.
@@ -127,4 +127,4 @@ REGENERATE='getinfo,connect' VALGRIND=0 TIMEOUT=10 TEST_DEBUG=1 GENERATE_EXAMPLE
127127

128128
1. Sip whenever you have an additional comma at the end of a sequence.
129129
2. Sip whenever you omit a comma in a sequence because you cut & paste.
130-
3. Skull whenever you wish JSON had comments.
130+
3. Skull whenever you wish JSON had comments.

tests/autogenerate-rpc-examples.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,6 @@
297297

298298
if os.path.exists(LOG_FILE):
299299
open(LOG_FILE, 'w').close()
300-
301-
logging.basicConfig(level=logging.INFO,
302-
format='%(asctime)s - %(levelname)s - %(message)s',
303-
datefmt='%H:%M:%S',
304-
handlers=[
305-
logging.FileHandler(LOG_FILE),
306-
logging.StreamHandler()
307-
])
308-
309300
logger = logging.getLogger(__name__)
310301

311302

@@ -2035,6 +2026,19 @@ def generate_list_examples(l1, l2, l3, c12, c23_2, inv_l31, inv_l32, offer_l23,
20352026
raise
20362027

20372028

2029+
@pytest.fixture(autouse=True)
2030+
def setup_logging():
2031+
global logger
2032+
logger.setLevel(logging.DEBUG)
2033+
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s", "%H:%M:%S")
2034+
stream_handler = logging.StreamHandler()
2035+
stream_handler.setFormatter(formatter)
2036+
logger.addHandler(stream_handler)
2037+
file_handler = logging.FileHandler(LOG_FILE)
2038+
file_handler.setFormatter(formatter)
2039+
logger.addHandler(file_handler)
2040+
2041+
20382042
@unittest.skipIf(not GENERATE_EXAMPLES, 'Generates examples for doc/schema/lightning-*.json files.')
20392043
def test_generate_examples(node_factory, bitcoind, executor):
20402044
"""Re-generates examples for doc/schema/lightning-*.json files"""
@@ -2055,9 +2059,9 @@ def list_all_examples():
20552059
for node in ast.walk(tree):
20562060
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == 'update_example':
20572061
for keyword in node.keywords:
2058-
if (keyword.arg == 'method' and isinstance(keyword.value, ast.Str)):
2059-
if keyword.value.s not in methods:
2060-
methods.append(keyword.value.s)
2062+
if (keyword.arg == 'method' and isinstance(keyword.value, ast.Constant)):
2063+
if keyword.value.value not in methods:
2064+
methods.append(keyword.value.value)
20612065
return methods
20622066
except Exception as e:
20632067
logger.error(f'Error in listing all examples: {e}')
@@ -2105,5 +2109,5 @@ def list_missing_examples():
21052109
update_examples_in_schema_files()
21062110
logger.info('All Done!!!')
21072111
except Exception as e:
2108-
logger.error(e)
2112+
logger.error(e, exc_info=True)
21092113
sys.exit(1)

0 commit comments

Comments
 (0)