]>
crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/yapftests/utils.py
1 # -*- coding: utf-8 -*-
2 # Copyright 2017 Google Inc. All Rights Reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 """Utilities for tests."""
24 @contextlib.contextmanager
25 def stdout_redirector(stream
): # pylint: disable=invalid-name
26 old_stdout
= sys
.stdout
31 sys
.stdout
= old_stdout
34 # NamedTemporaryFile is useless because on Windows the temporary file would be
35 # created with O_TEMPORARY, which would not allow the file to be opened a
36 # second time, even by the same process, unless the same flag is used.
37 # Thus we provide a simplified version ourselves.
39 # Note: returns a tuple of (io.file_obj, file_path), instead of a file_obj with
42 # Note: `buffering` is set to -1 despite documentation of NamedTemporaryFile
43 # says None. This is probably a problem with the python documentation.
44 @contextlib.contextmanager
45 def NamedTempFile(mode
='w+b',
54 """Context manager creating a new temporary file in text mode."""
55 (fd
, fname
) = tempfile
.mkstemp(
56 suffix
=suffix
, prefix
=prefix
, dir=dirname
, text
=text
)
69 @contextlib.contextmanager
70 def TempFileContents(dirname
,
75 # Note: NamedTempFile properly handles unicode encoding when using mode='w'
81 suffix
=suffix
) as (f
, fname
):