diff --git a/.github/ruff.toml b/.github/ruff.toml index 3a5254a9..306cc891 100644 --- a/.github/ruff.toml +++ b/.github/ruff.toml @@ -41,21 +41,3 @@ exclude = [ "*_pb2.py", "*_pb2.pyi" ] - -[per-file-ignores] -# until we address #1592 and move test fixtures into conftest.py -# then we need to ignore imports done to enable pytest fixtures. -# -# F401: `foo` imported but unused -# F811 Redefinition of unused `foo` -"tests/test_main.py" = ["F401", "F811"] -"tests/test_proto.py" = ["F401", "F811"] -"tests/test_freeze.py" = ["F401", "F811"] -"tests/test_function_id.py" = ["F401", "F811"] -"tests/test_viv_features.py" = ["F401", "F811"] -"tests/test_binja_features.py" = ["F401", "F811"] -"tests/test_pefile_features.py" = ["F401", "F811"] -"tests/test_dnfile_features.py" = ["F401", "F811"] -"tests/test_dotnet_features.py" = ["F401", "F811"] -"tests/test_result_document.py" = ["F401", "F811"] -"tests/test_dotnetfile_features.py" = ["F401", "F811"] diff --git a/CHANGELOG.md b/CHANGELOG.md index 49073491..9ccfcbeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,7 @@ ### Development - update ATT&CK/MBC data for linting #1568 @mr-tz - log time taken to analyze each function #1290 @williballenthin +- tests: make fixture available via conftest.py #1592 @williballenthin ### Raw diffs - [capa v5.1.0...master](https://github.com/mandiant/capa/compare/v5.1.0...master) diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..efe603a7 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,20 @@ +# Copyright (C) 2023 Mandiant, Inc. All Rights Reserved. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: [package root]/LICENSE.txt +# Unless required by applicable law or agreed to in writing, software distributed under the License +# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and limitations under the License. + +# import all the symbols from our fixtures +# and make available to test cases, implicitly. +# this is thanks to pytest magic. +# +# see the following for a discussion: +# https://www.revsys.com/tidbits/pytest-fixtures-are-magic/ +# https://lobste.rs/s/j8xgym/pytest_fixtures_are_magic +from fixtures import * # noqa: F403 [unable to detect undefined names] +from fixtures import _692f_dotnetfile_extractor # noqa: F401 [imported but unused] +from fixtures import _1c444_dotnetfile_extractor # noqa: F401 [imported but unused] +from fixtures import _039a6_dotnetfile_extractor # noqa: F401 [imported but unused] +from fixtures import _0953c_dotnetfile_extractor # noqa: F401 [imported but unused] diff --git a/tests/test_binja_features.py b/tests/test_binja_features.py index 769e85f3..b2256f80 100644 --- a/tests/test_binja_features.py +++ b/tests/test_binja_features.py @@ -5,13 +5,11 @@ # Unless required by applicable law or agreed to in writing, software distributed under the License # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. -import os import logging from pathlib import Path import pytest import fixtures -from fixtures import scope, sample import capa.main diff --git a/tests/test_dnfile_features.py b/tests/test_dnfile_features.py index d6325b5e..d5448766 100644 --- a/tests/test_dnfile_features.py +++ b/tests/test_dnfile_features.py @@ -5,9 +5,7 @@ # Unless required by applicable law or agreed to in writing, software distributed under the License # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. - import fixtures -from fixtures import scope, sample @fixtures.parametrize( diff --git a/tests/test_dotnet_features.py b/tests/test_dotnet_features.py index b8ab4b96..51ce66b0 100644 --- a/tests/test_dotnet_features.py +++ b/tests/test_dotnet_features.py @@ -5,9 +5,7 @@ # Unless required by applicable law or agreed to in writing, software distributed under the License # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. - import fixtures -from fixtures import scope, sample, b9f5b_dotnetfile_extractor, mixed_mode_64_dotnetfile_extractor @fixtures.parametrize( diff --git a/tests/test_dotnetfile_features.py b/tests/test_dotnetfile_features.py index 56d7a959..c1dfcf46 100644 --- a/tests/test_dotnetfile_features.py +++ b/tests/test_dotnetfile_features.py @@ -5,23 +5,15 @@ # Unless required by applicable law or agreed to in writing, software distributed under the License # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. - import pytest import fixtures -from fixtures import ( - FEATURE_PRESENCE_TESTS_DOTNET, - scope, - sample, - b9f5b_dotnetfile_extractor, - mixed_mode_64_dotnetfile_extractor, -) import capa.features.file @fixtures.parametrize( "sample,scope,feature,expected", - FEATURE_PRESENCE_TESTS_DOTNET, + fixtures.FEATURE_PRESENCE_TESTS_DOTNET, indirect=["sample", "scope"], ) def test_dotnetfile_features(sample, scope, feature, expected): diff --git a/tests/test_freeze.py b/tests/test_freeze.py index 7603b9fb..cbc60b48 100644 --- a/tests/test_freeze.py +++ b/tests/test_freeze.py @@ -10,7 +10,6 @@ from typing import List from pathlib import Path import pytest -from fixtures import z9324d_extractor import capa.main import capa.rules diff --git a/tests/test_function_id.py b/tests/test_function_id.py index 242b1949..bc177347 100644 --- a/tests/test_function_id.py +++ b/tests/test_function_id.py @@ -5,7 +5,7 @@ # Unless required by applicable law or agreed to in writing, software distributed under the License # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. -from fixtures import get_function, pma16_01_extractor, extract_function_features +import fixtures import capa.features.insn @@ -29,6 +29,6 @@ def test_function_id_complex_match(pma16_01_extractor): def test_function_id_api_feature(pma16_01_extractor): - f = get_function(pma16_01_extractor, 0x404548) - features = extract_function_features(pma16_01_extractor, f) + f = fixtures.get_function(pma16_01_extractor, 0x404548) + features = fixtures.extract_function_features(pma16_01_extractor, f) assert capa.features.insn.API("__aulldiv") in features diff --git a/tests/test_main.py b/tests/test_main.py index 8fc80deb..278bc729 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -10,16 +10,6 @@ import json import textwrap import fixtures -from fixtures import ( - z499c2_extractor, - z9324d_extractor, - pma16_01_extractor, - pingtaest_extractor, - _692f_dotnetfile_extractor, - _1c444_dotnetfile_extractor, - _039a6_dotnetfile_extractor, - _0953c_dotnetfile_extractor, -) import capa.main import capa.rules diff --git a/tests/test_pefile_features.py b/tests/test_pefile_features.py index c666357b..cb19b468 100644 --- a/tests/test_pefile_features.py +++ b/tests/test_pefile_features.py @@ -5,11 +5,8 @@ # Unless required by applicable law or agreed to in writing, software distributed under the License # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. -from pathlib import PosixPath - import pytest import fixtures -from fixtures import scope, sample import capa.features.file diff --git a/tests/test_proto.py b/tests/test_proto.py index 8a76ccfc..6f0137fe 100644 --- a/tests/test_proto.py +++ b/tests/test_proto.py @@ -9,7 +9,6 @@ import copy from typing import Any import pytest -from fixtures import a3f3bbc_rd, a076114_rd, pma0101_rd, al_khaserx64_rd, al_khaserx86_rd, dotnet_1c444e_rd import capa.rules import capa.render diff --git a/tests/test_result_document.py b/tests/test_result_document.py index f5496db1..27a1dbb2 100644 --- a/tests/test_result_document.py +++ b/tests/test_result_document.py @@ -9,7 +9,6 @@ import copy import pytest import fixtures -from fixtures import a3f3bbc_rd, a076114_rd, pma0101_rd, al_khaserx64_rd, al_khaserx86_rd, dotnet_1c444e_rd import capa import capa.engine as ceng diff --git a/tests/test_viv_features.py b/tests/test_viv_features.py index 683a7931..b63f0dc6 100644 --- a/tests/test_viv_features.py +++ b/tests/test_viv_features.py @@ -6,7 +6,6 @@ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. import fixtures -from fixtures import scope, sample @fixtures.parametrize(