diff options
author | 2022-09-24 21:42:58 +0300 | |
---|---|---|
committer | 2022-09-24 21:42:58 +0300 | |
commit | 19e9e33ee43437e7bf77a3a2b71341354f18cd30 (patch) | |
tree | 05901dea877cb792c1c9198887caa7956ccb5143 | |
parent | Revert "dist.distutils_extensions: fix _verinfo.py file" (diff) | |
download | snakeoil-19e9e33ee43437e7bf77a3a2b71341354f18cd30.tar.gz snakeoil-19e9e33ee43437e7bf77a3a2b71341354f18cd30.tar.bz2 snakeoil-19e9e33ee43437e7bf77a3a2b71341354f18cd30.zip |
test/mixins: remove `mk_named_tempfile`
Last usage was removed, so we can remove the function as well. For tests
just use normal `tmp_path / [filename]` instead.
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
-rw-r--r-- | src/snakeoil/test/mixins.py | 10 | ||||
-rw-r--r-- | tests/test_bash.py | 85 |
2 files changed, 35 insertions, 60 deletions
diff --git a/src/snakeoil/test/mixins.py b/src/snakeoil/test/mixins.py index 03360ab9..aa66839c 100644 --- a/src/snakeoil/test/mixins.py +++ b/src/snakeoil/test/mixins.py @@ -1,19 +1,12 @@ import errno import inspect -import io import os import stat import sys -import tempfile from ..compatibility import IGNORED_EXCEPTIONS -def mk_named_tempfile(*args, **kwds): - tmp_f = tempfile.NamedTemporaryFile(*args, **kwds) - return io.TextIOWrapper(tmp_f) - - class PythonNamespaceWalker: ignore_all_import_failures = False @@ -91,8 +84,7 @@ class PythonNamespaceWalker: seen = set(['__init__']) for x, st in stats: if not (x.startswith(".") or x.endswith("~")) and stat.S_ISREG(st): - if (x.endswith(".py") or x.endswith(".pyc") - or x.endswith(".pyo") or x.endswith(".so")): + if x.endswith((".py", ".pyc", ".pyo", ".so")): y = x.rsplit(".", 1)[0] # Ensure we're not looking at a >=py3k .so which injects # the version name in... diff --git a/tests/test_bash.py b/tests/test_bash.py index bd737f59..ec9df537 100644 --- a/tests/test_bash.py +++ b/tests/test_bash.py @@ -1,11 +1,8 @@ -import os from io import StringIO import pytest from snakeoil.bash import (BashParseError, iter_read_bash, read_bash, read_bash_dict, read_dict) -from snakeoil.fileutils import write_file -from snakeoil.test.mixins import mk_named_tempfile class TestBashCommentStripping: @@ -109,16 +106,17 @@ class TestReadDictConfig: read_dict(['invalid'], source_isiter=True) bash_dict = read_dict(StringIO("foo bar\nfoo2 bar\nfoo3\tbar\n"), splitter=None) - assert bash_dict == {}.fromkeys(('foo', 'foo2', 'foo3'), 'bar') + assert bash_dict == dict.fromkeys(('foo', 'foo2', 'foo3'), 'bar') bash_dict = read_dict(['foo = blah', 'foo2= blah ', 'foo3=blah'], strip=True) - assert bash_dict == {}.fromkeys(('foo', 'foo2', 'foo3'), 'blah') + assert bash_dict == dict.fromkeys(('foo', 'foo2', 'foo3'), 'blah') class TestReadBashDict: - def setup_method(self, method): - self.valid_file = mk_named_tempfile() - self.valid_file.write( + @pytest.fixture(autouse=True) + def _setup(self, tmp_path): + self.valid_file = tmp_path / "valid" + self.valid_file.write_text( '# hi I am a comment\n' 'foo1=bar\n' "foo2='bar'\n" @@ -127,42 +125,27 @@ class TestReadBashDict: 'foo5=\n' 'export foo6="bar"\n' ) - self.valid_file.flush() - self.sourcing_file = mk_named_tempfile() - self.sourcing_file.write('source "%s"\n' % self.valid_file.name) - self.sourcing_file.flush() - self.sourcing_file2 = mk_named_tempfile() - self.sourcing_file2.write('source "%s"\n' % os.path.basename(self.valid_file.name)) - self.sourcing_file2.flush() - self.advanced_file = mk_named_tempfile() - self.advanced_file.write( + self.sourcing_file = tmp_path / "sourcing" + self.sourcing_file.write_text(f'source "{self.valid_file}"\n') + self.sourcing_file2 = tmp_path / "sourcing2" + self.sourcing_file2.write_text(f'source "{self.valid_file}"\n') + self.advanced_file = tmp_path / "advanced" + self.advanced_file.write_text( 'one1=1\n' 'one_=$one1\n' 'two1=2\n' 'two_=${two1}\n' - ) - self.advanced_file.flush() - self.env_file = mk_named_tempfile() - self.env_file.write( - 'imported=${external}\n' - ) - self.env_file.flush() - self.escaped_file = mk_named_tempfile() - self.escaped_file.write( + ) + self.env_file = tmp_path / "env" + self.env_file.write_text('imported=${external}\n') + self.escaped_file = tmp_path / "escaped" + self.escaped_file.write_text( 'end=bye\n' 'quoteddollar="\\${dollar}"\n' 'quotedexpansion="\\${${end}}"\n' - ) - self.escaped_file.flush() - self.unclosed_file = mk_named_tempfile() - self.unclosed_file.write('foo="bar') - self.unclosed_file.flush() - - def teardown_method(self, method): - for x in ("valid", "sourcing", "advanced", "env", "escaped", "unclosed"): - x = getattr(self, '%s_file' % x, None) - if x is not None: - x.close() + ) + self.unclosed_file = tmp_path / "unclosed" + self.unclosed_file.write_text('foo="bar') def invoke_and_close(self, handle, *args, **kwds): try: @@ -173,7 +156,7 @@ class TestReadBashDict: def test_read_bash_dict(self): # TODO this is not even close to complete - bash_dict = self.invoke_and_close(self.valid_file.name) + bash_dict = self.invoke_and_close(str(self.valid_file)) d = { 'foo1': 'bar', 'foo2': 'bar', @@ -194,12 +177,12 @@ class TestReadBashDict: assert self.invoke_and_close(StringIO('x="\nasdf\nfdsa"')) == {'x': '\nasdf\nfdsa'} def test_empty_assign(self): - write_file(self.valid_file.name, 'w', "foo=\ndar=blah\n") - assert self.invoke_and_close(self.valid_file.name) == {'foo': '', 'dar': 'blah'} - write_file(self.valid_file.name, 'w', "foo=\ndar=\n") - assert self.invoke_and_close(self.valid_file.name) == {'foo': '', 'dar': ''} - write_file(self.valid_file.name, 'w', "foo=blah\ndar=\n") - assert self.invoke_and_close(self.valid_file.name) == {'foo': 'blah', 'dar': ''} + self.valid_file.write_text("foo=\ndar=blah\n") + assert self.invoke_and_close(str(self.valid_file)) == {'foo': '', 'dar': 'blah'} + self.valid_file.write_text("foo=\ndar=\n") + assert self.invoke_and_close(str(self.valid_file)) == {'foo': '', 'dar': ''} + self.valid_file.write_text("foo=blah\ndar=\n") + assert self.invoke_and_close(str(self.valid_file)) == {'foo': 'blah', 'dar': ''} def test_quoting(self): assert self.invoke_and_close(StringIO("x='y \\\na'")) == {'x': 'y \\\na'} @@ -211,15 +194,15 @@ class TestReadBashDict: assert self.invoke_and_close(StringIO("x='y'a")) == {'x': 'ya'} def test_sourcing(self): - output = self.invoke_and_close(self.sourcing_file.name, sourcing_command='source') + output = self.invoke_and_close(str(self.sourcing_file), sourcing_command='source') expected = {'foo1': 'bar', 'foo2': 'bar', 'foo3': 'bar', 'foo4': '-/:j4', 'foo5': '', 'foo6': 'bar'} assert output == expected - output = self.invoke_and_close(self.sourcing_file2.name, sourcing_command='source') + output = self.invoke_and_close(str(self.sourcing_file2), sourcing_command='source') expected = {'foo1': 'bar', 'foo2': 'bar', 'foo3': 'bar', 'foo4': '-/:j4', 'foo5': '', 'foo6': 'bar'} assert output == expected def test_read_advanced(self): - output = self.invoke_and_close(self.advanced_file.name) + output = self.invoke_and_close(str(self.advanced_file)) expected = { 'one1': '1', 'one_': '1', @@ -229,14 +212,14 @@ class TestReadBashDict: assert output == expected def test_env(self): - assert self.invoke_and_close(self.env_file.name) == {'imported': ''} + assert self.invoke_and_close(str(self.env_file)) == {'imported': ''} env = {'external': 'imported foo'} env_backup = env.copy() - assert self.invoke_and_close(self.env_file.name, env) == {'imported': 'imported foo'} + assert self.invoke_and_close(str(self.env_file), env) == {'imported': 'imported foo'} assert env_backup == env def test_escaping(self): - output = self.invoke_and_close(self.escaped_file.name) + output = self.invoke_and_close(str(self.escaped_file)) expected = { 'end': 'bye', 'quoteddollar': '${dollar}', @@ -246,7 +229,7 @@ class TestReadBashDict: def test_unclosed(self): with pytest.raises(BashParseError): - self.invoke_and_close(self.unclosed_file.name) + self.invoke_and_close(str(self.unclosed_file)) def test_wordchards(self): assert self.invoke_and_close(StringIO("x=-*")) == {"x": "-*"} |