-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
660 lines (513 loc) · 26.4 KB
/
test_cli.py
File metadata and controls
660 lines (513 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
'''CLI Tests for fremor subcommands
Tests the command-line-interface calls for the fremor CLI (fremorizer package).
Each tool generally gets 3 tests:
- fremor $tool, checking for exit code 0 or 2 (fails if cli isn't configured right)
- fremor $tool --help, checking for exit code 0 (fails if the code doesn't run)
- fremor $tool --optionDNE, checking for exit code 2 (fails if cli isn't configured
right and thinks the tool has a --optionDNE option)
We also have a set of more complicated tests for testing the full set of
command-line args for fremor yaml and fremor run.
Migrated from NOAA-GFDL/fre-cli fre/tests/test_fre_cmor_cli.py.
'''
import json
import os
from pathlib import Path
import shutil
import tempfile
from unittest.mock import patch
from click.testing import CliRunner
from fremorizer.cli import fremor
from .conftest import (
ROOTDIR, INDIR, VARLIST, VARLIST_DIFF,
EXP_CONFIG, EXP_CONFIG_CMIP7,
CMIP6_TABLE_CONFIG, CMIP7_TABLE_CONFIG,
)
runner = CliRunner()
# log format strings produced by the fremor group callback (cli.py / fremor)
LOG_INFO_LINE = '[ INFO: cli.py: fremor] ' + \
'fre_file_handler added to base_fre_logger\n'
LOG_DEBUG_LINE = '[DEBUG: cli.py: fremor] ' + \
'click entry-point function call done.\n'
# ── setup ──────────────────────────────────────────────────────────────────
def test_setup_test_files(cli_sos_nc_file, cli_sosv2_nc_file): # pylint: disable=redefined-outer-name
"""Verify all required NetCDF test files exist via session-scoped fixtures."""
assert Path(cli_sos_nc_file).exists()
assert Path(cli_sosv2_nc_file).exists()
# ── fremor (top-level group) ──────────────────────────────────────────────
def test_cli_fremor():
''' fremor (no subcommand) '''
result = runner.invoke(fremor, args=[])
assert result.exit_code == 2
def test_cli_fremor_help():
''' fremor --help '''
result = runner.invoke(fremor, args=["--help"])
assert result.exit_code == 0
def test_cli_fremor_help_and_debuglog(tmp_path):
''' fremor -vv -l LOG yaml --help (logs created by group callback) '''
log_file = tmp_path / 'TEST_FOO_LOG.log'
result = runner.invoke(fremor, args=["-vv", "-l", str(log_file), "yaml", "--help"])
assert result.exit_code == 0
assert log_file.exists()
line_list = log_file.read_text(encoding='utf-8').splitlines(keepends=True)
assert LOG_INFO_LINE in line_list[0]
assert LOG_DEBUG_LINE in line_list[1]
def test_cli_fremor_help_and_infolog(tmp_path):
''' fremor -v -l LOG yaml --help '''
log_file = tmp_path / 'TEST_FOO_LOG.log'
result = runner.invoke(fremor, args=["-v", "-l", str(log_file), "yaml", "--help"])
assert result.exit_code == 0
assert log_file.exists()
line_list = log_file.read_text(encoding='utf-8').splitlines(keepends=True)
assert LOG_INFO_LINE in line_list[0]
def test_cli_fremor_help_and_quietlog(tmp_path):
''' fremor -q -l LOG yaml --help '''
log_file = tmp_path / 'TEST_FOO_LOG.log'
result = runner.invoke(fremor, args=["-q", "-l", str(log_file), "yaml", "--help"])
assert result.exit_code == 0
assert log_file.exists()
line_list = log_file.read_text(encoding='utf-8').splitlines(keepends=True)
assert line_list == []
def test_cli_fremor_opt_dne():
''' fremor optionDNE '''
result = runner.invoke(fremor, args=["optionDNE"])
assert result.exit_code == 2
# ── fremor yaml ───────────────────────────────────────────────────────────
def test_cli_fremor_yaml():
''' fremor yaml (no args) '''
result = runner.invoke(fremor, args=["yaml"])
assert result.exit_code == 2
def test_cli_fremor_yaml_help():
''' fremor yaml --help '''
result = runner.invoke(fremor, args=["yaml", "--help"])
assert result.exit_code == 0
def test_cli_fremor_yaml_opt_dne():
''' fremor yaml optionDNE '''
result = runner.invoke(fremor, args=["yaml", "optionDNE"])
assert result.exit_code == 2
@patch('fremorizer.cli.cmor_yaml_subtool')
def test_cli_fremor_yaml_case1(mock_subtool, tmp_path):
''' fremor yaml --dry_run -y YAMLFILE ... --output FOO_cmor.yaml '''
# use a temporary yaml placeholder file as the model yaml input
dummy_yaml = tmp_path / 'model.yaml'
dummy_yaml.write_text('placeholder', encoding='utf-8')
output_yaml = tmp_path / 'FOO_cmor.yaml'
mock_subtool.return_value = None
result = runner.invoke(fremor, args=["-v", "-v", "yaml", "--dry_run",
"-y", str(dummy_yaml),
"-e", "test_experiment",
"-p", "test_platform",
"-t", "test_target",
"--output", str(output_yaml) ])
assert result.exit_code == 0
mock_subtool.assert_called_once_with(
yamlfile=str(dummy_yaml),
exp_name="test_experiment",
target="test_target",
platform="test_platform",
output=str(output_yaml),
run_one_mode=False,
dry_run_mode=True,
start=None,
stop=None,
print_cli_call=True,
)
# ── fremor run ────────────────────────────────────────────────────────────
def test_cli_fremor_run():
''' fremor run (no args) '''
result = runner.invoke(fremor, args=["run"])
assert result.exit_code == 2
def test_cli_fremor_run_help():
''' fremor run --help '''
result = runner.invoke(fremor, args=["run", "--help"])
assert result.exit_code == 0
def test_cli_fremor_run_opt_dne():
''' fremor run optionDNE '''
result = runner.invoke(fremor, args=["run", "optionDNE"])
assert result.exit_code == 2
def test_cli_fremor_run_case1(cli_sos_nc_file, tmp_path):
'''fremor run, test-use case: sos → sos (CMIP6)'''
outdir = str(tmp_path / 'outdir')
result = runner.invoke(fremor, args = [ "-v", "-v",
"run", "--run_one",
"--indir", str(INDIR),
"--varlist", str(VARLIST),
"--table_config", str(CMIP6_TABLE_CONFIG),
"--exp_config", str(EXP_CONFIG),
"--outdir", outdir,
"--calendar", "julian",
"--grid_label", "gr",
"--grid_desc", "FOO_BAR_PLACEHOLD",
"--nom_res", "10000 km" ] )
assert result.exit_code == 0, f'case1 failed: {result.output}'
output_ncs = list(Path(outdir).rglob('sos_Omon_*.nc'))
assert len(output_ncs) > 0, 'no output sos file found'
assert Path(cli_sos_nc_file).exists(), 'input file should still exist'
def test_cli_fremor_run_case2(cli_sosv2_nc_file, tmp_path): # pylint: disable=redefined-outer-name
'''fremor run, test-use case 2: sosV2 varlist_diff (CMIP6)'''
outdir = str(tmp_path / 'outdir')
result = runner.invoke(fremor, args = ["-v", "-v",
"run", "--run_one",
"--indir", str(INDIR),
"--varlist", str(VARLIST_DIFF),
"--table_config", str(CMIP6_TABLE_CONFIG),
"--exp_config", str(EXP_CONFIG),
"--outdir", outdir,
"--calendar", "julian",
"--grid_label", "gr",
"--grid_desc", "FOO_BAR_PLACEHOLD",
"--nom_res", "10000 km" ] )
assert result.exit_code == 0, f'case2 failed: {result.output}'
output_ncs = list(Path(outdir).rglob('sos_Omon_*.nc'))
assert len(output_ncs) > 0, 'no output sos file found'
assert Path(cli_sosv2_nc_file).exists(), 'input file should still exist'
def test_cli_fremor_run_cmip7_case1(cli_sos_nc_file, tmp_path): # pylint: disable=redefined-outer-name
'''fremor run, test-use case for cmip7: sos → sos'''
outdir = str(tmp_path / 'outdir')
result = runner.invoke(fremor, args = [ "-v", "-v",
"run", "--run_one",
"--indir", str(INDIR),
"--varlist", str(VARLIST),
"--table_config", str(CMIP7_TABLE_CONFIG),
"--exp_config", str(EXP_CONFIG_CMIP7),
"--outdir", outdir,
"--calendar", "julian",
"--grid_label", "g999",
"--grid_desc", "FOO_BAR_PLACEHOLD",
"--nom_res", "10000 km" ] )
assert result.exit_code == 0, f'cmip7 case1 failed: {result.output}'
output_ncs = list(Path(outdir).rglob('sos_*.nc'))
assert len(output_ncs) > 0, 'no output sos file found'
assert Path(cli_sos_nc_file).exists(), 'input file should still exist'
def test_cli_fremor_run_cmip7_case2(cli_sosv2_nc_file, tmp_path): # pylint: disable=redefined-outer-name
'''fremor run, test-use case 2 for cmip7: sosV2 varlist_diff'''
outdir = str(tmp_path / 'outdir')
result = runner.invoke(fremor, args = [ "-v", "-v",
"run", "--run_one",
"--indir", str(INDIR),
"--varlist", str(VARLIST_DIFF),
"--table_config", str(CMIP7_TABLE_CONFIG),
"--exp_config", str(EXP_CONFIG_CMIP7),
"--outdir", outdir,
"--calendar", "julian",
"--grid_label", "g999",
"--grid_desc", "FOO_BAR_PLACEHOLD",
"--nom_res", "10000 km" ] )
assert result.exit_code == 0, f'cmip7 case2 failed: {result.output}'
output_ncs = list(Path(outdir).rglob('sos_*.nc'))
assert len(output_ncs) > 0, 'no output sos file found'
assert Path(cli_sosv2_nc_file).exists(), 'input file should still exist'
# ── fremor find ───────────────────────────────────────────────────────────
def test_cli_fremor_find():
''' fremor find (no args) '''
result = runner.invoke(fremor, args=["find"])
assert result.exit_code == 2
def test_cli_fremor_find_help():
''' fremor find --help '''
result = runner.invoke(fremor, args=["find", "--help"])
assert result.exit_code == 0
def test_cli_fremor_find_opt_dne():
''' fremor find optionDNE '''
result = runner.invoke(fremor, args=["find", "optionDNE"])
assert result.exit_code == 2
def test_cli_fremor_find_cmip6_case1():
''' fremor find, test-use case searching for variables in cmip6 tables '''
result = runner.invoke(fremor, args=["-v", "find",
"--varlist", str(VARLIST),
"--table_config_dir", str(CMIP6_TABLE_CONFIG.parent)] )
assert result.exit_code == 0
def test_cli_fremor_find_cmip6_case2():
''' fremor find, test-use case searching for variables in cmip6 tables '''
result = runner.invoke(fremor, args=["-v", "find",
"--opt_var_name", "sos",
"--table_config_dir", str(CMIP6_TABLE_CONFIG.parent)] )
assert result.exit_code == 0
# ── fremor config ─────────────────────────────────────────────────────────
def test_cli_fremor_config():
''' fremor config (no args) '''
result = runner.invoke(fremor, args=["config"])
assert result.exit_code == 2
def test_cli_fremor_config_help():
''' fremor config --help '''
result = runner.invoke(fremor, args=["config", "--help"])
assert result.exit_code == 0
def test_cli_fremor_config_opt_dne():
''' fremor config optionDNE '''
result = runner.invoke(fremor, args=["config", "optionDNE"])
assert result.exit_code == 2
def test_cli_fremor_config_case1(cli_sos_nc_file): # pylint: disable=redefined-outer-name
'''
fremor config -- generate a CMOR YAML config from a mock pp directory tree.
Uses the ocean_sos_var_file test data with a mock pp layout.
'''
# set up a mock pp directory tree that the writer can scan
mock_pp_dir = ROOTDIR / 'mock_pp_writer'
comp_ts_dir = mock_pp_dir / 'ocean' / 'ts' / 'monthly' / '5yr'
comp_ts_dir.mkdir(parents=True, exist_ok=True)
# make an ice component dir with no chunk-dir to skip accordingly
(mock_pp_dir / 'ice' / 'ts' / 'monthly' ).mkdir(parents=True, exist_ok=True)
# make a land component dir with no ts dir to skip accordingly
(mock_pp_dir / 'land' / 'av').mkdir(parents=True, exist_ok=True)
# make an empty atmos component dir with no netcdf files to make sure we skip a dir with no nc files
(mock_pp_dir / 'atmos' / 'ts' / 'monthly' / '5yr').mkdir(parents=True, exist_ok=True)
# create random file that's not a directory in the pp_dir that we should skip over gracefully
(mock_pp_dir / 'foo.json').touch()
# put an av directory in to make sure we're not targeting that at the moment
(mock_pp_dir / 'ocean' / 'av').mkdir(parents=True, exist_ok=True)
# put an annual directory in to make sure we're not targeting that at the moment
(mock_pp_dir / 'ocean' / 'ts' / 'annual').mkdir(parents=True, exist_ok=True)
# symlink the test nc file into the mock tree
src_nc = INDIR / 'reduced_ocean_monthly_1x1deg.199301-199302.sos.nc'
dst_nc = comp_ts_dir / src_nc.name
if dst_nc.exists() or dst_nc.is_symlink():
dst_nc.unlink()
dst_nc.symlink_to(src_nc.resolve())
varlist_out_dir = ROOTDIR / 'mock_writer_varlists'
varlist_out_dir.mkdir(exist_ok=True)
# create an empty variable list of one we want to create. it should be remade.
(varlist_out_dir / 'CMIP6_CMIP6_Omon_ocean.list').touch()
assert (varlist_out_dir / 'CMIP6_CMIP6_Omon_ocean.list').exists(), \
'pre-existing variable list failed to be created for tests'
output_yaml = ROOTDIR / 'mock_writer_output.yaml'
output_data_dir = ROOTDIR / 'mock_writer_outdir'
# clean up previous runs
for p in [output_yaml]:
if p.exists():
p.unlink()
# recreate the yaml to make sure it's recreated
output_yaml.touch()
result = runner.invoke(fremor, args=[
"-v", "-v",
"config",
"--pp_dir", str(mock_pp_dir),
"--mip_tables_dir", str(CMIP6_TABLE_CONFIG.parent),
"--mip_era", "cmip6",
"--exp_config", str(EXP_CONFIG),
"--output_yaml", str(output_yaml),
"--output_dir", str(output_data_dir),
"--varlist_dir", str(varlist_out_dir),
"--freq", "monthly",
"--chunk", "5yr",
"--grid", "gn",
"--overwrite"
])
assert result.exit_code == 0, f'config failed: {result.output}'
assert output_yaml.exists(), 'output YAML was not created'
assert (varlist_out_dir / 'CMIP6_CMIP6_Omon_ocean.list').exists(), \
'CMIP6_CMIP6_Omon_ocean.list was not created for some reason'
# basic sanity: the written file should contain "cmor:" and "table_targets:"
yaml_text = output_yaml.read_text(encoding='utf-8')
assert 'cmor:' in yaml_text
assert 'table_targets:' in yaml_text
# clean up
if dst_nc.is_symlink():
dst_nc.unlink()
shutil.rmtree(mock_pp_dir, ignore_errors=True)
shutil.rmtree(varlist_out_dir, ignore_errors=True)
if output_yaml.exists():
output_yaml.unlink()
# ── fremor varlist ────────────────────────────────────────────────────────
def test_cli_fremor_varlist():
''' fremor varlist (no args) '''
result = runner.invoke(fremor, args=["varlist"])
assert result.exit_code == 2
def test_cli_fremor_varlist_help():
''' fremor varlist --help '''
result = runner.invoke(fremor, args=["varlist", "--help"])
assert result.exit_code == 0
def test_cli_fremor_varlist_opt_dne():
''' fremor varlist optionDNE '''
result = runner.invoke(fremor, args=["varlist", "optionDNE"])
assert result.exit_code == 2
def test_cli_fremor_varlist_no_table_filter(cli_sos_nc_file, cli_sosv2_nc_file, tmp_path): # pylint: disable=redefined-outer-name
'''fremor varlist — no MIP table filter.
Creates a variable list from the ocean_sos_var_file test data without a MIP table,
so both sos and sosV2 should appear.'''
output_varlist = tmp_path / 'test_varlist_no_filter.json'
result = runner.invoke(fremor, args=[
"-v", "-v",
"varlist",
"--dir_targ", str(INDIR),
"--output_variable_list", str(output_varlist)
])
assert result.exit_code == 0, f'varlist failed: {result.output}'
assert output_varlist.exists(), 'output variable list was not created'
with open(output_varlist, 'r', encoding='utf-8') as f:
var_list = json.load(f)
assert 'sos' in var_list
assert 'sosV2' in var_list
assert len(var_list) == 2
def test_cli_fremor_varlist_cmip6_table_filter(cli_sos_nc_file, cli_sosv2_nc_file, tmp_path): # pylint: disable=redefined-outer-name
'''fremor varlist — with CMIP6 Omon MIP table filter.
Only sos should survive; sosV2 is not in the CMIP6 Omon table.'''
output_varlist = tmp_path / 'test_varlist_cmip6_filter.json'
result = runner.invoke(fremor, args=[
"-v", "-v",
"varlist",
"--dir_targ", str(INDIR),
"--output_variable_list", str(output_varlist),
"--mip_table", str(CMIP6_TABLE_CONFIG)
])
assert result.exit_code == 0, f'varlist failed: {result.output}'
assert output_varlist.exists(), 'output variable list was not created'
with open(output_varlist, 'r', encoding='utf-8') as f:
var_list = json.load(f)
assert 'sos' in var_list, 'sos should be in the CMIP6-filtered list'
assert 'sosV2' not in var_list, 'sosV2 should NOT be in the CMIP6-filtered list'
def test_cli_fremor_varlist_cmip7_table_filter(cli_sos_nc_file, cli_sosv2_nc_file, tmp_path): # pylint: disable=redefined-outer-name
'''fremor varlist — with CMIP7 ocean MIP table filter.
sos should survive (sos_tavg-u-hxy-sea splits to sos); sosV2 should not.'''
output_varlist = tmp_path / 'test_varlist_cmip7_filter.json'
result = runner.invoke(fremor, args=[
"-v", "-v",
"varlist",
"--dir_targ", str(INDIR),
"--output_variable_list", str(output_varlist),
"--mip_table", str(CMIP7_TABLE_CONFIG)
])
assert result.exit_code == 0, f'varlist failed: {result.output}'
assert output_varlist.exists(), 'output variable list was not created'
with open(output_varlist, 'r', encoding='utf-8') as f:
var_list = json.load(f)
assert 'sos' in var_list, 'sos should be in the CMIP7-filtered list'
assert 'sosV2' not in var_list, 'sosV2 should NOT be in the CMIP7-filtered list'
# ── fremor init ───────────────────────────────────────────────────────────
def test_cli_fremor_init():
''' fremor init (no args) '''
result = runner.invoke(fremor, args=["init"])
assert result.exit_code == 2
def test_cli_fremor_init_help():
''' fremor init --help '''
result = runner.invoke(fremor, args=["init", "--help"])
assert result.exit_code == 0
def test_cli_fremor_init_opt_dne():
''' fremor init optionDNE '''
result = runner.invoke(fremor, args=["init", "optionDNE"])
assert result.exit_code == 2
def test_cli_fremor_init_cmip6_exp_config(tmp_path):
'''
fremor init -- generate a CMIP6 experiment config template.
'''
output_path = tmp_path / 'test_cmip6_init_template.json'
result = runner.invoke(fremor, args=[
"init",
"--mip_era", "cmip6",
"--exp_config", str(output_path)
])
assert result.exit_code == 0, f'init failed: {result.output}'
assert output_path.exists(), 'output config was not created'
with open(output_path, 'r', encoding='utf-8') as f:
config = json.load(f)
assert config['mip_era'] == 'CMIP6'
assert config['_cmip6_option'] == 'CMIP6'
assert 'experiment_id' in config
assert 'output_path_template' in config
def test_cli_fremor_init_cmip7_exp_config(tmp_path):
'''
fremor init -- generate a CMIP7 experiment config template.
'''
output_path = tmp_path / 'test_cmip7_init_template.json'
result = runner.invoke(fremor, args=[
"init",
"--mip_era", "cmip7",
"--exp_config", str(output_path)
])
assert result.exit_code == 0, f'init failed: {result.output}'
assert output_path.exists(), 'output config was not created'
with open(output_path, 'r', encoding='utf-8') as f:
config = json.load(f)
assert config['mip_era'] == 'CMIP7'
assert config['_cmip7_option'] == 1
assert 'experiment_id' in config
assert 'output_path_template' in config
def test_cli_fremor_init_default_name(tmp_path):
'''
fremor init -- when no --exp_config is given and no --tables_dir,
a default-named file should be created in the current directory.
'''
# Use CliRunner's isolated_filesystem to avoid polluting the actual working directory
with runner.isolated_filesystem(temp_dir=tmp_path):
result = runner.invoke(fremor, args=[
"init",
"--mip_era", "cmip6"
])
assert result.exit_code == 0, f'init failed: {result.output}'
default_path = Path('CMOR_cmip6_template.json')
assert default_path.exists(), 'default output config was not created'
with open(default_path, 'r', encoding='utf-8') as f:
config = json.load(f)
assert config['mip_era'] == 'CMIP6'
# ── fremor run: logfile + omission tracking ───────────────────────────────
def test_cli_fremor_run_with_logfile(cli_sos_nc_file, tmp_path): # pylint: disable=redefined-outer-name
'''
fremor -vv -l LOGFILE run ...
Runs a real CMOR workflow with the -l flag and verifies that the resulting
log file contains log lines from both cli.py (the CLI entry point) and
cmor_mixer (the CMOR processing module).
'''
log_path = tmp_path / 'TEST_CMOR_RUN.log'
outdir = str(tmp_path / 'outdir')
result = runner.invoke(fremor, args=[
'-vv', '-l', str(log_path),
'run', '--run_one',
'--indir', str(INDIR),
'--varlist', str(VARLIST),
'--table_config', str(CMIP6_TABLE_CONFIG),
'--exp_config', str(EXP_CONFIG),
'--outdir', outdir,
'--calendar', 'julian',
'--grid_label', 'gr',
'--grid_desc', 'FOO_BAR_PLACEHOLD',
'--nom_res', '10000 km',
])
assert result.exit_code == 0, f'run failed: {result.output}'
assert log_path.exists(), 'log file was not created'
log_text = log_path.read_text(encoding='utf-8')
# cli.py entry-point must have written this line when setting up the file handler
assert 'fre_file_handler added to base_fre_logger' in log_text, \
'expected cli.py log line not found in log file'
# cmor_mixer must have emitted at least one line carrying its module filename
assert 'cmor_mixer.py' in log_text, \
'expected cmor_mixer.py log line not found in log file'
def test_cli_fremor_run_with_logfile_omission_case(cli_sos_nc_file, cli_sosv2_nc_file, tmp_path): # pylint: disable=redefined-outer-name
'''
fremor -vv -l LOGFILE run ...
Uses a varlist where sos->sos succeeds and sosV2->tob fails (tob is a valid
CMIP6_Omon variable but the sosV2 file contains sos data, not tob).
Verifies the OMISSION LOG appears in the log file with the failed variable info.
'''
log_path = tmp_path / 'TEST_CMOR_RUN_OMISSION.log'
outdir = str(tmp_path / 'outdir')
# create a temporary varlist: sos->sos will succeed, sosV2->tob will fail
varlist_data = {'sos': 'sos', 'sosV2': 'tob'}
varlist_fd, varlist_path = tempfile.mkstemp(suffix='.json')
with os.fdopen(varlist_fd, 'w') as f:
json.dump(varlist_data, f)
result = runner.invoke(fremor, args=[
'-vv', '-l', str(log_path),
'run',
'--indir', str(INDIR),
'--varlist', varlist_path,
'--table_config', str(CMIP6_TABLE_CONFIG),
'--exp_config', str(EXP_CONFIG),
'--outdir', outdir,
'--calendar', 'julian',
'--grid_label', 'gr',
'--grid_desc', 'FOO_BAR_PLACEHOLD',
'--nom_res', '10000 km',
])
assert result.exit_code == 0, f'run failed: {result.output}'
assert log_path.exists(), 'log file was not created'
log_text = log_path.read_text(encoding='utf-8')
# the omission log summary must appear
assert 'OMISSION LOG' in log_text, \
'expected OMISSION LOG not found in log file'
# the failed variable (sosV2/tob) must be mentioned in the omission
assert 'OMITTED local_var=sosV2 / target_var=tob' in log_text, \
'expected omission entry for sosV2/tob not found in log file'
# the expected file path for the omitted variable must appear
assert '.sosV2.nc' in log_text, \
'expected file path for omitted variable not found in log file'
# the CMOR module must have emitted log lines
assert 'cmor_mixer.py' in log_text, \
'expected cmor_mixer.py log line not found in log file'
Path(varlist_path).unlink(missing_ok=True)