From 8ac6d98bcb29e2e1e8ca93090df88a116f0201d4 Mon Sep 17 00:00:00 2001 From: Ignacio Calderon Date: Mon, 19 Aug 2024 15:04:03 +0200 Subject: [PATCH] refactor: make tests agnostic from the running path (#358) --- .github/workflows/publish.yml | 2 +- Makefile | 10 +++++----- tests/pytest.ini => pytest.ini | 3 ++- tests/TestXCodeProject.py | 6 +++++- tests/pbxcli/TestPBXCLI.py | 8 ++++++-- tests/pbxcli/TestPBXProjFile.py | 8 +++++--- tests/pbxcli/TestPBXProjFlag.py | 6 +++++- tests/pbxcli/TestPBXProjFolder.py | 9 ++++++--- tests/pbxcli/TestPBXProjShow.py | 4 ++++ tests/pbxcli/__init__.py | 2 +- tests/pbxextensions/TestProjectFiles.py | 6 ++++++ 11 files changed, 46 insertions(+), 18 deletions(-) rename tests/pytest.ini => pytest.ini (50%) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 61c9854..734254c 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -27,5 +27,5 @@ jobs: python setup.py sdist bdist_wheel twine upload dist/* env: - TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} + TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} diff --git a/Makefile b/Makefile index cfd589b..d003eab 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,15 @@ .PHONY: coverage coverage-term test install-dependencies coverage: install-dependencies - cd tests; pytest --cov-report=xml --cov=../ --cov-branch - cd tests; rm -rf .coverage + pytest --cov-report=xml --cov=../ --cov-branch + rm -rf .coverage coverage-term: install-dependencies - cd tests; pytest --cov-report=term --cov=../ --cov-branch - cd tests; rm -rf .coverage + pytest --cov-report=term --cov=../ --cov-branch + rm -rf .coverage test: - cd tests; pytest + pytest install-dependencies: pip3 install -r dev-requirements.txt diff --git a/tests/pytest.ini b/pytest.ini similarity index 50% rename from tests/pytest.ini rename to pytest.ini index 95ec019..b513e88 100644 --- a/tests/pytest.ini +++ b/pytest.ini @@ -1,3 +1,4 @@ [pytest] python_files = Test*.py -pythonpath = .. \ No newline at end of file +pythonpath = . +testpaths = tests diff --git a/tests/TestXCodeProject.py b/tests/TestXCodeProject.py index 08effc4..1163842 100644 --- a/tests/TestXCodeProject.py +++ b/tests/TestXCodeProject.py @@ -3,6 +3,7 @@ import unittest from pbxproj import XcodeProject +from os.path import join import re @@ -37,6 +38,9 @@ def setUp(self): } } + self.pwd = os.getcwd() + os.chdir(os.path.dirname(os.path.abspath(__file__))) + # create tmp directory for results if not os.path.exists("results"): os.mkdir("results") @@ -44,6 +48,7 @@ def setUp(self): def tearDown(self): # remove tmp directory shutil.rmtree('results') + os.chdir(self.pwd) def testSaveOnGivenPath(self): XcodeProject().save("results/sample") @@ -112,4 +117,3 @@ def testConsistency(self): saved = project.__repr__() + '\n' assert saved == original - diff --git a/tests/pbxcli/TestPBXCLI.py b/tests/pbxcli/TestPBXCLI.py index 24498a9..d09895c 100644 --- a/tests/pbxcli/TestPBXCLI.py +++ b/tests/pbxcli/TestPBXCLI.py @@ -6,16 +6,20 @@ from pbxproj import XcodeProject from pbxproj.pbxcli import open_project, resolve_backup, backup_project, command_parser, PROJECT_PLACEHOLDER +from tests.pbxcli import BASE_PROJECT_PATH import pytest -BASE_PROJECT_PATH = 'samplescli/project.pbxproj' - class PBXCLITest(unittest.TestCase): + def setUp(self): + self.pwd = os.getcwd() + os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) + def tearDown(self): if hasattr(self, 'backup_file') and self.backup_file: os.remove(self.backup_file) sys.stdout = sys.__stdout__ + os.chdir(self.pwd) def testOpenProjectWithFullPath(self): project = open_project({PROJECT_PLACEHOLDER: BASE_PROJECT_PATH}) diff --git a/tests/pbxcli/TestPBXProjFile.py b/tests/pbxcli/TestPBXProjFile.py index cd57927..a230f6e 100644 --- a/tests/pbxcli/TestPBXProjFile.py +++ b/tests/pbxcli/TestPBXProjFile.py @@ -6,20 +6,22 @@ import pbxproj.pbxcli.pbxproj_file as pbxproj_file from pbxproj import PBXGenericObject from pbxproj.pbxcli import open_project, PROJECT_PLACEHOLDER, PATH_PLACEHOLDER +from tests.pbxcli import BASE_PROJECT_PATH, SAMPLE_PROJECT_PATH import pytest -SAMPLE_PROJECT_PATH = 'samplescli/test.pbxproj' -BASE_PROJECT_PATH = 'samplescli/project.pbxproj' - class PBXProjFileTest(unittest.TestCase): def setUp(self): # copy the project.pbxproj, into a file that can be used by the tests + self.pwd = os.getcwd() + os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) + shutil.copyfile(BASE_PROJECT_PATH, SAMPLE_PROJECT_PATH) def tearDown(self): os.remove(SAMPLE_PROJECT_PATH) sys.stdout = sys.__stdout__ + os.chdir(self.pwd) def testRemoveFileUnknown(self): args = { diff --git a/tests/pbxcli/TestPBXProjFlag.py b/tests/pbxcli/TestPBXProjFlag.py index 7e80f41..2229957 100644 --- a/tests/pbxcli/TestPBXProjFlag.py +++ b/tests/pbxcli/TestPBXProjFlag.py @@ -10,12 +10,16 @@ class TestPBXProjFlag(unittest.TestCase): def setUp(self): + self.pwd = os.getcwd() + os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) + # copy the project.pbxproj, into a file that can be used by the tests shutil.copyfile(BASE_PROJECT_PATH, SAMPLE_PROJECT_PATH) def tearDown(self): os.remove(SAMPLE_PROJECT_PATH) sys.stdout = sys.__stdout__ + os.chdir(self.pwd) def testAddFlags(self): args = { @@ -56,4 +60,4 @@ def testRemoveFlags(self): assert result == 'Flags removed successfully.' for configuration in project.objects.get_configurations_on_targets(args['--target'], args['--configuration']): - assert 'MYFLAG' not in configuration.buildSettings \ No newline at end of file + assert 'MYFLAG' not in configuration.buildSettings diff --git a/tests/pbxcli/TestPBXProjFolder.py b/tests/pbxcli/TestPBXProjFolder.py index f16c111..bddf710 100644 --- a/tests/pbxcli/TestPBXProjFolder.py +++ b/tests/pbxcli/TestPBXProjFolder.py @@ -7,20 +7,23 @@ from pbxproj import PBXGenericObject from pbxproj.pbxcli import open_project, PROJECT_PLACEHOLDER, PATH_PLACEHOLDER from pbxproj.pbxextensions.ProjectFiles import TreeType +from tests.pbxcli import BASE_PROJECT_PATH, SAMPLE_PROJECT_PATH import pytest -SAMPLE_PROJECT_PATH = 'samplescli/test.pbxproj' -BASE_PROJECT_PATH = 'samplescli/project.pbxproj' - class PBXProjFolderTest(unittest.TestCase): + def setUp(self): + self.pwd = os.getcwd() + os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) + # copy the project.pbxproj, into a file that can be used by the tests shutil.copyfile(BASE_PROJECT_PATH, SAMPLE_PROJECT_PATH) def tearDown(self): os.remove(SAMPLE_PROJECT_PATH) sys.stdout = sys.__stdout__ + os.chdir(self.pwd) def testRemoveFolderUnknown(self): args = { diff --git a/tests/pbxcli/TestPBXProjShow.py b/tests/pbxcli/TestPBXProjShow.py index b6c6cdf..557d2af 100644 --- a/tests/pbxcli/TestPBXProjShow.py +++ b/tests/pbxcli/TestPBXProjShow.py @@ -10,12 +10,16 @@ class PBXProjShowTest(unittest.TestCase): def setUp(self): + self.pwd = os.getcwd() + os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) + # copy the project.pbxproj, into a file that can be used by the tests shutil.copyfile(BASE_PROJECT_PATH, SAMPLE_PROJECT_PATH) def tearDown(self): os.remove(SAMPLE_PROJECT_PATH) sys.stdout = sys.__stdout__ + os.chdir(self.pwd) def testShowAllTargetsInfo(self): args = { diff --git a/tests/pbxcli/__init__.py b/tests/pbxcli/__init__.py index e59d134..e37fb7a 100644 --- a/tests/pbxcli/__init__.py +++ b/tests/pbxcli/__init__.py @@ -1,2 +1,2 @@ SAMPLE_PROJECT_PATH = 'samplescli/test.pbxproj' -BASE_PROJECT_PATH = 'samplescli/project.pbxproj' \ No newline at end of file +BASE_PROJECT_PATH = 'samplescli/project.pbxproj' diff --git a/tests/pbxextensions/TestProjectFiles.py b/tests/pbxextensions/TestProjectFiles.py index c082686..a2df95b 100644 --- a/tests/pbxextensions/TestProjectFiles.py +++ b/tests/pbxextensions/TestProjectFiles.py @@ -41,6 +41,12 @@ def setUp(self): } } + self.pwd = os.getcwd() + os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) + + def tearDown(self): + os.chdir(self.pwd) + def testInit(self): with pytest.raises(EnvironmentError, match='^This class cannot be instantiated directly'): ProjectFiles()