From b9090b86ce9a5336e28b09da880d8c69fef6a79c Mon Sep 17 00:00:00 2001 From: Willi Ballenthin Date: Thu, 13 Jul 2023 09:37:39 +0200 Subject: [PATCH 1/6] tests: make fixtures available via conftest.py closes #1592 --- .github/ruff.toml | 18 ------------------ tests/conftest.py | 16 ++++++++++++++++ tests/test_binja_features.py | 2 -- tests/test_dnfile_features.py | 2 -- tests/test_dotnet_features.py | 2 -- tests/test_dotnetfile_features.py | 10 +--------- tests/test_freeze.py | 1 - tests/test_function_id.py | 6 +++--- tests/test_main.py | 10 ---------- tests/test_pefile_features.py | 3 --- tests/test_proto.py | 1 - tests/test_result_document.py | 1 - tests/test_viv_features.py | 1 - 13 files changed, 20 insertions(+), 53 deletions(-) create mode 100644 tests/conftest.py 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/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..37f43396 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,16 @@ +# 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] 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( From ea3090a0666d404c19cea0323a7ab217fdf51138 Mon Sep 17 00:00:00 2001 From: Willi Ballenthin Date: Thu, 13 Jul 2023 09:39:04 +0200 Subject: [PATCH 2/6] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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) From fd36946c4be4528bfad546c189e5de9eb3816f61 Mon Sep 17 00:00:00 2001 From: Willi Ballenthin Date: Thu, 13 Jul 2023 14:32:24 +0200 Subject: [PATCH 3/6] conftest: import symbols prefixed with _ --- tests/conftest.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 37f43396..447d870e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,3 +14,7 @@ # 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 _039a6_dotnetfile_extractor # noqa: F403 [unable to detect undefined names] +from fixtures import _0953c_dotnetfile_extractor # noqa: F403 [unable to detect undefined names] +from fixtures import _692f_dotnetfile_extractor # noqa: F403 [unable to detect undefined names] +from fixtures import _1c444_dotnetfile_extractor # noqa: F403 [unable to detect undefined names] From c4da4bcfe78dbd9fb1c04e263883cbbf519488a9 Mon Sep 17 00:00:00 2001 From: Willi Ballenthin Date: Thu, 13 Jul 2023 14:35:09 +0200 Subject: [PATCH 4/6] conftest: update noqa ignores --- tests/conftest.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 447d870e..1da700a9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,7 +14,7 @@ # 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 _039a6_dotnetfile_extractor # noqa: F403 [unable to detect undefined names] -from fixtures import _0953c_dotnetfile_extractor # noqa: F403 [unable to detect undefined names] -from fixtures import _692f_dotnetfile_extractor # noqa: F403 [unable to detect undefined names] -from fixtures import _1c444_dotnetfile_extractor # noqa: F403 [unable to detect undefined names] +from fixtures import _039a6_dotnetfile_extractor # noqa: F401 [imported but unused] +from fixtures import _0953c_dotnetfile_extractor # noqa: F401 [imported but unused] +from fixtures import _692f_dotnetfile_extractor # noqa: F401 [imported but unused] +from fixtures import _1c444_dotnetfile_extractor # noqa: F401 [imported but unused] From 724f9e4b81ad4333f9f48999f71d686edd15e9f5 Mon Sep 17 00:00:00 2001 From: Willi Ballenthin Date: Thu, 13 Jul 2023 14:52:05 +0200 Subject: [PATCH 5/6] conftest: isort --- tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 1da700a9..cf99e8f7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,7 +14,7 @@ # 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 _039a6_dotnetfile_extractor # noqa: F401 [imported but unused] from fixtures import _0953c_dotnetfile_extractor # noqa: F401 [imported but unused] -from fixtures import _692f_dotnetfile_extractor # noqa: F401 [imported but unused] from fixtures import _1c444_dotnetfile_extractor # noqa: F401 [imported but unused] From 82223dcdc912fd895f9973c3040d4f729ee0f30d Mon Sep 17 00:00:00 2001 From: Willi Ballenthin Date: Thu, 13 Jul 2023 13:12:13 +0000 Subject: [PATCH 6/6] conftest: isort --- tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index cf99e8f7..efe603a7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,6 +15,6 @@ # 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] -from fixtures import _1c444_dotnetfile_extractor # noqa: F401 [imported but unused]