2828from iris .tests .stock .netcdf import ncgen_from_cdl
2929from iris .warnings import IrisCfLoadWarning , IrisCfSaveWarning
3030
31- encoding_options = [None ] + SUPPORTED_ENCODINGS
31+ # Note: for test options, include "no encoding" and an alias name
32+ ENCODING_NONE = None
33+ ENCODING_UTF8_ALIAS = "UTF8"
34+ encoding_options = [ENCODING_NONE , ENCODING_UTF8_ALIAS ] + SUPPORTED_ENCODINGS
3235
3336samples_3_ascii = np .array (
3437 ["one" , "" , "seven" ], # N.B. include empty!
@@ -45,9 +48,17 @@ def encoding(request):
4548 return request .param
4649
4750
51+ # TODO: remove (debug)
52+ PERSIST_TESTFILES : str | None = "~/chararray_testfiles"
53+ # PERSIST_TESTFILES: str | None = None
54+
55+
4856@pytest .fixture (scope = "module" )
4957def tempdir (tmp_path_factory ):
50- path = tmp_path_factory .mktemp ("netcdf" )
58+ if PERSIST_TESTFILES :
59+ path = Path (PERSIST_TESTFILES ).expanduser ()
60+ else :
61+ path = tmp_path_factory .mktemp ("netcdf" )
5162 return path
5263
5364
@@ -134,7 +145,7 @@ class TestWriteStrings:
134145
135146 def test_encodings (self , encoding , tempdir ):
136147 # Create a dataset with the variable
137- path = tempdir / f"test_writestrings_encoding_ { encoding !s} .nc"
148+ path = tempdir / f"test_bytecoded_writestrings_encoding_ { encoding !s} .nc"
138149
139150 if encoding in [None , "ascii" ]:
140151 writedata = samples_3_ascii
@@ -164,7 +175,7 @@ def test_encodings(self, encoding, tempdir):
164175
165176 def test_scalar (self , tempdir ):
166177 # Like 'test_write_strings', but the variable has *only* the string dimension.
167- path = tempdir / "test_writestrings_scalar .nc"
178+ path = tempdir / "test_bytecoded_writestrings_scalar .nc"
168179
169180 strlen = 5
170181 ds_encoded = make_encoded_dataset (path , strlen = strlen )
@@ -180,7 +191,7 @@ def test_scalar(self, tempdir):
180191
181192 def test_multidim (self , tempdir ):
182193 # Like 'test_write_strings', but the variable has additional dimensions.
183- path = tempdir / "test_writestrings_multidim .nc"
194+ path = tempdir / "test_bytecoded_writestrings_multidim .nc"
184195
185196 strlen = 5
186197 ds_encoded = make_encoded_dataset (path , strlen = strlen )
@@ -209,7 +220,7 @@ def test_multidim(self, tempdir):
209220
210221 @pytest .mark .parametrize ("encoding" , [None , "ascii" ])
211222 def test_write_encoding_failure (self , tempdir , encoding ):
212- path = tempdir / f"test_writestrings_encoding_ { encoding } _fail.nc"
223+ path = tempdir / f"test_bytecoded_writestrings_encoding_ { encoding } _fail.nc"
213224 ds = make_encoded_dataset (path , strlen = 5 , encoding = encoding )
214225 v = ds .variables ["vxs" ]
215226 encoding_name = encoding
@@ -223,7 +234,7 @@ def test_write_encoding_failure(self, tempdir, encoding):
223234 v [:] = samples_3_nonascii
224235
225236 def test_write_badencoding_ignore (self , tempdir ):
226- path = tempdir / "test_writestrings_badencoding_ignore .nc"
237+ path = tempdir / "test_bytecoded_writestrings_badencoding_ignore .nc"
227238 ds = make_encoded_dataset (path , strlen = 5 , encoding = "unknown" )
228239 v = ds .variables ["vxs" ]
229240 msg = (
@@ -235,7 +246,7 @@ def test_write_badencoding_ignore(self, tempdir):
235246
236247 def test_overlength (self , tempdir ):
237248 # Check expected behaviour with over-length data
238- path = tempdir / "test_writestrings_overlength .nc"
249+ path = tempdir / "test_bytecoded_writestrings_overlength .nc"
239250 strlen = 6
240251 ds = make_encoded_dataset (path , strlen = strlen , encoding = "utf8" )
241252 v = ds .variables ["vxs" ]
@@ -248,7 +259,7 @@ def test_overlength(self, tempdir):
248259
249260 def test_overlength_splitcoding (self , tempdir ):
250261 # Check expected behaviour when non-ascii multibyte coding gets truncated
251- path = tempdir / "test_writestrings_overlength_splitcoding .nc"
262+ path = tempdir / "test_bytecoded_writestrings_overlength_splitcoding .nc"
252263 strlen = 5
253264 ds = make_encoded_dataset (path , strlen = strlen , encoding = "utf-8" )
254265 v = ds .variables ["vxs" ]
@@ -291,7 +302,7 @@ def test_write_chars(self, tempdir, write_form):
291302 strlen = strings_maxbytes (write_strings , encoding )
292303 write_bytes = make_bytearray (write_strings , strlen , encoding = encoding )
293304 # NOTE: 'flexi' form util decides the width needs to be 7 !!
294- path = tempdir / f"test_writechars_ { write_form } .nc"
305+ path = tempdir / f"test_bytecoded_writechars_ { write_form } .nc"
295306 ds = make_encoded_dataset (path , encoding = encoding , strlen = strlen )
296307 v = ds .variables ["vxs" ]
297308
@@ -331,7 +342,7 @@ def undecoded_testvar(self, ds_encoded, varname: str):
331342
332343 def test_encodings (self , encoding , tempdir , readmode ):
333344 # Create a dataset with the variable
334- path = tempdir / f"test_read_encodings_ { encoding !s} _{ readmode } .nc"
345+ path = tempdir / f"test_bytecoded_read_encodings_ { encoding !s} _{ readmode } .nc"
335346
336347 if encoding in [None , "ascii" ]:
337348 write_strings = samples_3_ascii
@@ -352,10 +363,10 @@ def test_encodings(self, encoding, tempdir, readmode):
352363 # Test "normal" read --> string array
353364 result = v [:]
354365 expected = write_strings
355- if encoding in ("utf-8" , "utf-16" ):
366+ if encoding in ("utf-8" , ENCODING_UTF8_ALIAS , "utf-16" ):
356367 # In these cases, with the given non-ascii sample data, the
357368 # "default minimum string length" is overestimated.
358- if encoding == "utf-8" :
369+ if encoding in [ "utf-8" , ENCODING_UTF8_ALIAS ] :
359370 assert strlen == 7
360371 assert result .dtype == "U7"
361372 # correct the result dtype to pass the write_strings comparison below
@@ -378,7 +389,7 @@ def test_encodings(self, encoding, tempdir, readmode):
378389
379390 def test_scalar (self , tempdir , readmode ):
380391 # Like 'test_write_strings', but the variable has *only* the string dimension.
381- path = tempdir / f"test_read_scalar_ { readmode } .nc"
392+ path = tempdir / f"test_bytecoded_read_scalar_ { readmode } .nc"
382393
383394 strlen = 5
384395 ds_encoded = make_encoded_dataset (path , strlen = strlen )
@@ -404,7 +415,7 @@ def test_scalar(self, tempdir, readmode):
404415
405416 def test_multidim (self , tempdir , readmode ):
406417 # Like 'test_write_strings', but the variable has additional dimensions.
407- path = tempdir / f"test_read_multidim_ { readmode } .nc"
418+ path = tempdir / f"test_bytecoded_read_multidim_ { readmode } .nc"
408419
409420 strlen = 5
410421 ds_encoded = make_encoded_dataset (path , strlen = strlen )
@@ -440,7 +451,7 @@ def test_multidim(self, tempdir, readmode):
440451 check_array_matching (result , expected )
441452
442453 def test_read_encoding_failure (self , tempdir , readmode ):
443- path = tempdir / f"test_read_encoding_failure_ { readmode } .nc"
454+ path = tempdir / f"test_bytecoded_read_encoding_failure_ { readmode } .nc"
444455 strlen = 10
445456 ds_encoded = make_encoded_dataset (path , strlen = strlen , encoding = "ascii" )
446457 v = ds_encoded .variables ["vxs" ]
@@ -463,7 +474,7 @@ def test_read_encoding_failure(self, tempdir, readmode):
463474 assert np .all (result == test_utf8_bytes )
464475
465476 def test_read_badencoding_ignore (self , tempdir ):
466- path = tempdir / f"test_read_badencoding_ignore .nc"
477+ path = tempdir / f"test_bytecoded_read_badencoding_ignore .nc"
467478 strlen = 10
468479 ds = make_encoded_dataset (path , strlen = strlen , encoding = "unknown" )
469480 v = ds .variables ["vxs" ]
0 commit comments