From d50713e710f48d51ddaa1a5267b7bd6d1b9238f2 Mon Sep 17 00:00:00 2001 From: why-not-try-calmer Date: Wed, 22 Feb 2023 16:44:03 +0100 Subject: [PATCH 1/4] Added tests from qgis-plug-ci --- pytransifex/api.py | 7 +- tests/_translation.py | 236 ++++++++++++++++++ .../data/.qgis-plugin-ci-test-changelog.yaml | 4 + tests/test_translation.py | 54 ++++ 4 files changed, 296 insertions(+), 5 deletions(-) create mode 100644 tests/_translation.py create mode 100644 tests/data/.qgis-plugin-ci-test-changelog.yaml create mode 100644 tests/test_translation.py diff --git a/pytransifex/api.py b/pytransifex/api.py index 73bb833..1970f67 100644 --- a/pytransifex/api.py +++ b/pytransifex/api.py @@ -260,12 +260,9 @@ def create_language( def project_exists(self, project_slug: str) -> bool: """Check if the project exists in the remote Transifex repository""" try: - if not self.projects: - return False - elif self.get_project(project_slug=project_slug): + if self.get_project(project_slug=project_slug): return True - else: - return False + return False except DoesNotExist: return False diff --git a/tests/_translation.py b/tests/_translation.py new file mode 100644 index 0000000..bcce577 --- /dev/null +++ b/tests/_translation.py @@ -0,0 +1,236 @@ +import glob +import logging +import os +import subprocess +import sys +from dataclasses import dataclass +from datetime import date +from pathlib import Path + +from pytransifex.api import Transifex + +logger = logging.getLogger(__name__) + + +@dataclass +class Parameters: + changelog_include: bool + changelog_path: str + changelog_number_of_entries: int + create_date: date + github_organization_slug: str + lrelease_path: str + plugin_name: str + plugin_path: str + project_slug: str + pylupdate5_path: str + repository_url: str + transifex_coordinator: str + transifex_organization: str + transifex_project: str + transifex_resource: str + translation_source_language: str + translation_languages: str + + +def touch_file(path, update_time: bool = False, create_dir: bool = True): + basedir = os.path.dirname(path) + if create_dir and not os.path.exists(basedir): + os.makedirs(basedir) + with open(path, "a"): + if update_time: + os.utime(path, None) + else: + pass + + +class Translation: + def __init__( + self, parameters: Parameters, transifex_token: str, create_project: bool = True + ): + client = Transifex( + api_token=transifex_token, + organization_name=parameters.transifex_organization, + i18n_type="QT", + ) + assert client + + self.tx_client = client + self.parameters = parameters + + assert self.tx_client.ping() + plugin_path = self.parameters.plugin_path + tx = self.parameters.transifex_resource + lang = self.parameters.translation_source_language + self.ts_file = f"{plugin_path}/i18n/{tx}_{lang}.ts" + + if self.tx_client.project_exists(parameters.transifex_project): + logger.debug( + f"Project {self.parameters.transifex_organization}/" + f"{self.parameters.transifex_project} exists on Transifex" + ) + + elif create_project: + logger.debug( + "Project does not exists on Transifex, creating one as: " + f"{self.parameters.transifex_organization}/" + f"{self.parameters.transifex_project}" + ) + self.tx_client.create_project( + project_slug=self.parameters.transifex_project, + private=False, + repository_url=self.parameters.repository_url, + source_language_code=parameters.translation_source_language, + ) + assert self.tx_client.project_exists(self.parameters.transifex_project) + self.update_strings() + logger.debug( + f"Creating resource in {self.parameters.transifex_organization}/" + f"{self.parameters.transifex_project}/" + f"{self.parameters.transifex_resource} with {self.ts_file}" + ) + self.tx_client.create_resource( + project_slug=self.parameters.transifex_project, + path_to_file=self.ts_file, + resource_slug=self.parameters.transifex_resource, + ) + logger.info( + f""" + Transifex project {self.parameters.transifex_organization}/ + {self.parameters.transifex_project} and resource ({self.parameters.transifex_resource}) have been created. + """ + ) + else: + logger.error( + "Project does not exists on Transifex: " + f"{self.parameters.transifex_organization}/" + f"{self.parameters.transifex_project}" + ) + + def update_strings(self): + """ + Update TS files from plugin source strings + """ + source_py_files = [] + source_ui_files = [] + relative_path = f"./{self.parameters.plugin_path}" + for ext in ("py", "ui"): + for file in glob.glob( + f"{self.parameters.plugin_path}/**/*.{ext}", + recursive=True, + ): + file_path = str(Path(file).relative_to(relative_path)) + if ext == "py": + source_py_files.append(file_path) + else: + source_ui_files.append(file_path) + + touch_file(self.ts_file) + + project_file = Path(self.parameters.plugin_path).joinpath( + self.parameters.plugin_name + ".pro" + ) + + with open(project_file, "w") as f: + source_py_files = " ".join(source_py_files) + source_ui_files = " ".join(source_ui_files) + assert f.write("CODECFORTR = UTF-8\n") + assert f.write(f"SOURCES = {source_py_files}\n") + assert f.write(f"FORMS = {source_ui_files}\n") + assert f.write( + f"TRANSLATIONS = {Path(self.ts_file).relative_to(relative_path)}\n" + ) + f.flush() + f.close() + + cmd = [self.parameters.pylupdate5_path, "-noobsolete", str(project_file)] + + output = subprocess.run(cmd, capture_output=True, text=True) + + project_file.unlink() + + if output.returncode != 0: + logger.error(f"Translation failed: {output.stderr}") + sys.exit(1) + else: + logger.info(f"Successfully run pylupdate5: {output.stdout}") + + def compile_strings(self): + """ + Compile TS file into QM files + """ + cmd = [self.parameters.lrelease_path] + for file in glob.glob(f"{self.parameters.plugin_path}/i18n/*.ts"): + cmd.append(file) + output = subprocess.run(cmd, capture_output=True, text=True) + if output.returncode != 0: + logger.error(f"Translation failed: {output.stderr}") + sys.exit(1) + else: + logger.info(f"Successfully run lrelease: {output.stdout}") + + def pull(self): + """ + Pull TS files from Transifex + """ + resource = self.__get_resource() + existing_langs = self.tx_client.list_languages( + project_slug=self.parameters.transifex_project + ) + existing_langs.remove(self.parameters.translation_source_language) + logger.info( + f"{len(existing_langs)} languages found for resource {resource.get('slug')}:" + f" ({existing_langs})" + ) + for lang in self.parameters.translation_languages: + if lang not in existing_langs: + logger.debug(f"Creating missing language: {lang}") + self.tx_client.create_language( + project_slug=self.parameters.transifex_project, + language_code=lang, + coordinators=[self.parameters.transifex_coordinator], + ) + existing_langs.append(lang) + for lang in existing_langs: + ts_file = f"{self.parameters.plugin_path}/i18n/{self.parameters.transifex_resource}_{lang}.ts" + logger.debug(f"Downloading translation file: {ts_file}") + self.tx_client.get_translation( + project_slug=self.parameters.transifex_project, + resource_slug=resource["slug"], + language_code=lang, + path_to_output_file=ts_file, + ) + + def push(self): + resource = self.__get_resource() + logger.debug( + f"Pushing resource: {self.parameters.transifex_resource} " + f"with file {self.ts_file}" + ) + result = self.tx_client.update_source_translation( + project_slug=self.parameters.transifex_project, + resource_slug=resource["slug"], + path_to_file=self.ts_file, + ) + logger.info(f"Translation resource updated: {result}") + + def __get_resource(self) -> dict: + resources = self.tx_client.list_resources(self.parameters.transifex_project) + if len(resources) == 0: + logger.error( + f"Project '{self.parameters.transifex_project}' has no resource on Transifex" + ) + sys.exit(1) + if len(resources) > 1: + for resource in resources: + if resource["name"] == self.parameters.transifex_resource: + return resource + logger.error( + f"Project '{self.parameters.transifex_project}' has several " + "resources on Transifex and none is named as the project slug. " + "Specify one in the parameters with transifex_resource." + "These resources have been found: " + f"{', '.join([r['name'] for r in resources])}" + ) + sys.exit(1) + return resources[0] diff --git a/tests/data/.qgis-plugin-ci-test-changelog.yaml b/tests/data/.qgis-plugin-ci-test-changelog.yaml new file mode 100644 index 0000000..d00a409 --- /dev/null +++ b/tests/data/.qgis-plugin-ci-test-changelog.yaml @@ -0,0 +1,4 @@ +plugin_path: qgis_plugin_CI_testing +github_organization_slug: opengisch +project_slug: qgis-plugin-ci-changelog +create_date: 1985-07-21 diff --git a/tests/test_translation.py b/tests/test_translation.py new file mode 100644 index 0000000..da9c955 --- /dev/null +++ b/tests/test_translation.py @@ -0,0 +1,54 @@ +import logging +import os +import unittest +from pathlib import Path +import yaml + +from pytransifex.exceptions import PyTransifexException +from tests._translation import Parameters, Translation + +logger = logging.getLogger(__name__) + + +class TestTranslation(unittest.TestCase): + @classmethod + def setUpClass(cls): + """Initialize the test case""" + config_yaml = Path.cwd().joinpath("tests", "data", ".qgis-plugin-ci-test-changelog.yaml") + print(config_yaml) + with open(config_yaml) as f: + arg_dict = yaml.safe_load(f) + transifex_token = os.getenv("transifex_token") + assert transifex_token + cls.transifex_token = transifex_token + + cls.parameters = Parameters(**arg_dict) + cls.t = Translation(cls.parameters, transifex_token=transifex_token) + + def tearDown(self): + try: + self.t.tx_client.delete_project(self.parameters.project_slug) + except PyTransifexException as error: + logger.debug(error) + """ + try: + self.t.tx_client.delete_team(f"{self.parameters.project_slug}-team") + except PyTransifexException as error: + logger.debug(error) + """ + + def test_creation(self): + self.tearDown() + self.t = Translation(self.parameters, transifex_token=self.transifex_token) # type: ignore + + def test_push(self): + self.t.update_strings() + self.t.push() + + def test_pull(self): + self.t.pull() + self.t.compile_strings() + + +if __name__ == "__main__": + unittest.main() From 40284223346771068d239d16071f10acbbc4be57 Mon Sep 17 00:00:00 2001 From: why-not-try-calmer Date: Thu, 23 Feb 2023 09:53:35 +0100 Subject: [PATCH 2/4] All tests passing for'test_api' and 'test_cli'. 2 missing on 'test_translation'. --- pytransifex/api.py | 9 +++- requirements/dev.txt | 1 + tests/_translation.py | 46 ++++++++++--------- .../data/.qgis-plugin-ci-test-changelog.yaml | 10 ++-- tests/test_api.py | 20 -------- tests/test_translation.py | 13 ++++-- 6 files changed, 47 insertions(+), 52 deletions(-) diff --git a/pytransifex/api.py b/pytransifex/api.py index 1970f67..368a842 100644 --- a/pytransifex/api.py +++ b/pytransifex/api.py @@ -53,11 +53,14 @@ def create_project( *, project_slug: str, project_name: str | None = None, - source_language_code: str = "en_GB", + source_language_code: str = "en", private: bool = False, **kwargs, ): """Create a project.""" + logger.info( + f"Trying to create project from these arguments: project_slug = {project_slug}, " + ) source_language = tx_api.Language.get(code=source_language_code) project_name = project_name or project_slug @@ -247,7 +250,9 @@ def create_language( ): """Create a new language resource in the remote Transifex repository""" if project := self.get_project(project_slug=project_slug): - project.add("languages", [tx_api.Language.get(code=language_code)]) + if language := tx_api.Language.get(code=language_code): + logger.debug(f"Adding {language.code} to {project_slug}") + project.add("languages", [language]) if coordinators: project.add("coordinators", coordinators) diff --git a/requirements/dev.txt b/requirements/dev.txt index cafd3e0..38eb3a7 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -1 +1,2 @@ nose2 +PyYAML diff --git a/tests/_translation.py b/tests/_translation.py index bcce577..dc1c109 100644 --- a/tests/_translation.py +++ b/tests/_translation.py @@ -4,8 +4,9 @@ import subprocess import sys from dataclasses import dataclass -from datetime import date +from datetime import date, datetime from pathlib import Path +from typing import Optional from pytransifex.api import Transifex @@ -14,23 +15,23 @@ @dataclass class Parameters: - changelog_include: bool - changelog_path: str - changelog_number_of_entries: int - create_date: date - github_organization_slug: str - lrelease_path: str - plugin_name: str - plugin_path: str - project_slug: str - pylupdate5_path: str - repository_url: str - transifex_coordinator: str - transifex_organization: str - transifex_project: str - transifex_resource: str - translation_source_language: str - translation_languages: str + changelog_include: Optional[bool] = False + changelog_path: Optional[str] = None + changelog_number_of_entries: Optional[int] = None + create_date: Optional[date] = datetime.now() + github_organization_slug: Optional[str] = None + lrelease_path: Optional[str] = None + plugin_name: Optional[str] = None + plugin_path: Optional[str] = None + project_slug: Optional[str] = None + pylupdate5_path: Optional[str] = None + repository_url: Optional[str] = None + transifex_coordinator: Optional[str] = None + transifex_organization: Optional[str] = None + transifex_project: Optional[str] = None + transifex_resource: Optional[str] = None + translation_source_language: Optional[str] = None + translation_languages: Optional[str] = None def touch_file(path, update_time: bool = False, create_dir: bool = True): @@ -48,6 +49,7 @@ class Translation: def __init__( self, parameters: Parameters, transifex_token: str, create_project: bool = True ): + logger.info(f"Overriding test config with: {parameters}") client = Transifex( api_token=transifex_token, organization_name=parameters.transifex_organization, @@ -177,9 +179,11 @@ def pull(self): existing_langs = self.tx_client.list_languages( project_slug=self.parameters.transifex_project ) - existing_langs.remove(self.parameters.translation_source_language) + lang = self.parameters.translation_source_language + if lang in existing_langs: + existing_langs.remove(lang) logger.info( - f"{len(existing_langs)} languages found for resource {resource.get('slug')}:" + f"{len(existing_langs)} languages found for resource :" f" ({existing_langs})" ) for lang in self.parameters.translation_languages: @@ -196,7 +200,7 @@ def pull(self): logger.debug(f"Downloading translation file: {ts_file}") self.tx_client.get_translation( project_slug=self.parameters.transifex_project, - resource_slug=resource["slug"], + resource_slug=resource.slug, language_code=lang, path_to_output_file=ts_file, ) diff --git a/tests/data/.qgis-plugin-ci-test-changelog.yaml b/tests/data/.qgis-plugin-ci-test-changelog.yaml index d00a409..144759a 100644 --- a/tests/data/.qgis-plugin-ci-test-changelog.yaml +++ b/tests/data/.qgis-plugin-ci-test-changelog.yaml @@ -1,4 +1,6 @@ -plugin_path: qgis_plugin_CI_testing -github_organization_slug: opengisch -project_slug: qgis-plugin-ci-changelog -create_date: 1985-07-21 +project_slug: test_project_pytransifex_public +transifex_organization: test_pytransifex +transifex_coordinator: john_doe +translation_languages: + - fr + - en diff --git a/tests/test_api.py b/tests/test_api.py index 6a3c385..b96f5bd 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -117,26 +117,6 @@ def test11_stats(self): logger.info(str(stats)) assert stats - def test12_stats(self): - self.tx.get_project_stats(project_slug=self.project_slug) - - def test13_stats(self): - self.tx.get_project_stats(project_slug=self.project_slug) - if __name__ == "__main__": unittest.main() - -""" -# Don't remove this! -curl -g \ - --request GET --url "https://rest.api.transifex.com/resource_language_stats?filter[project]=o%3Aopengisch%3Ap%3Aqfield-documentation" \ - --header 'accept: application/vnd.api+json' \ - --header 'authorization: Bearer TOKEN' - -curl -g \ - --request GET \ - --url "https://rest.api.transifex.com/resource_language_stats?filter[project]=o%3Atest_pytransifex%3Ap%3Atest_project_pytransifex" \ - --header 'accept: application/vnd.api+json' \ - --header 'authorization: Bearer TOKEN' -""" diff --git a/tests/test_translation.py b/tests/test_translation.py index da9c955..b0bcad5 100644 --- a/tests/test_translation.py +++ b/tests/test_translation.py @@ -2,6 +2,7 @@ import os import unittest from pathlib import Path + import yaml from pytransifex.exceptions import PyTransifexException @@ -14,11 +15,13 @@ class TestTranslation(unittest.TestCase): @classmethod def setUpClass(cls): """Initialize the test case""" - config_yaml = Path.cwd().joinpath("tests", "data", ".qgis-plugin-ci-test-changelog.yaml") + config_yaml = Path.cwd().joinpath( + "tests", "data", ".qgis-plugin-ci-test-changelog.yaml" + ) print(config_yaml) with open(config_yaml) as f: arg_dict = yaml.safe_load(f) - transifex_token = os.getenv("transifex_token") + transifex_token = os.getenv("TX_TOKEN") assert transifex_token cls.transifex_token = transifex_token @@ -37,15 +40,15 @@ def tearDown(self): logger.debug(error) """ - def test_creation(self): + def test1_creation(self): self.tearDown() self.t = Translation(self.parameters, transifex_token=self.transifex_token) # type: ignore - def test_push(self): + def test2_push(self): self.t.update_strings() self.t.push() - def test_pull(self): + def test3_pull(self): self.t.pull() self.t.compile_strings() From feca8c00f56abcf1e996d3e3e5bdcbb26286a0a8 Mon Sep 17 00:00:00 2001 From: why-not-try-calmer Date: Thu, 23 Feb 2023 15:03:50 +0100 Subject: [PATCH 3/4] Tests split into distinct jobs. Removed user-specific tests not immediately relevant to the library or cli. --- .github/workflows/test.yml | 67 +++++++++++++++++++++++++++++++++ .github/workflows/test_pytx.yml | 38 ------------------- pytransifex/api.py | 7 ++++ tests/_translation.py | 22 ++++++----- tests/test_translation.py | 37 ++++++------------ 5 files changed, 97 insertions(+), 74 deletions(-) create mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_pytx.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..9e40511 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,67 @@ +name: test pytransifex + +concurrency: testing_environment + +on: + pull_request: + branches: + - master + push: + branches: + - master + workflow_call: + +jobs: + tests: + runs-on: ubuntu-latest + steps: + + # Not using strategy.matrix to create multiple jobs + # as we do NOT want to test with any form of concurrency + # to avoid 'race conditions' against Transifex + + - name: Check out repository code + uses: actions/checkout@v3 + + - uses: actions/setup-python@v4 + with: + python-version: "3.10" + cache: "pip" + cache-dependency-path: "requirements/*.txt" + + - name: Install project requirements + run: | + python -m pip install -U -r requirements/base.txt + python -m pip install -U -r requirements/dev.txt + + - name: Test API + env: + organization: ${{ secrets.ORGANIZATION }} + tx_token: ${{ secrets.TX_TOKEN }} + run: | + TX_TOKEN=$tx_token ORGANIZATION=$organization \ + python -m unittest ./tests/test_api.py + + - name: Test CLI + env: + organization: ${{ secrets.ORGANIZATION }} + tx_token: ${{ secrets.TX_TOKEN }} + run: | + TX_TOKEN=$tx_token ORGANIZATION=$organization \ + python -m unittest ./tests/test_cli.py + + - name: Test with a public project + env: + organization: ${{ secrets.ORGANIZATION }} + tx_token: ${{ secrets.TX_TOKEN }} + run: | + TX_TOKEN=$tx_token ORGANIZATION=$organization \ + python -m unittest ./tests/test_public_project.py + + - name: Test with Qgisplugin's 'test_translation' + env: + organization: ${{ secrets.ORGANIZATION }} + tx_token: ${{ secrets.TX_TOKEN }} + run: | + TX_TOKEN=$tx_token ORGANIZATION=$organization \ + python -m unittest ./tests/test_translation.py diff --git a/.github/workflows/test_pytx.yml b/.github/workflows/test_pytx.yml deleted file mode 100644 index d6ead2c..0000000 --- a/.github/workflows/test_pytx.yml +++ /dev/null @@ -1,38 +0,0 @@ -name: test pytransifex - -concurrency: testing_environment - -on: - pull_request: - branches: - - master - push: - branches: - - master - workflow_call: - -jobs: - test: - runs-on: ubuntu-latest - steps: - - name: Check out repository code - uses: actions/checkout@v3 - - - uses: actions/setup-python@v4 - with: - python-version: "3.10" - cache: "pip" - cache-dependency-path: "requirements/*.txt" - - - name: Install project requirements - run: | - python -m pip install -U -r requirements/base.txt - python -m pip install -U -r requirements/dev.txt - - - name: Run CI tests - env: - organization: ${{ secrets.ORGANIZATION }} - tx_token: ${{ secrets.TX_TOKEN }} - run: | - TX_TOKEN=$tx_token ORGANIZATION=$organization \ - python -m unittest discover -s ./tests -p 'test_*.py' diff --git a/pytransifex/api.py b/pytransifex/api.py index 368a842..5f8f232 100644 --- a/pytransifex/api.py +++ b/pytransifex/api.py @@ -85,12 +85,19 @@ def delete_project(self, project_slug: str): def get_project(self, project_slug: str) -> None | Resource: """Fetches the project matching the given slug""" if self.projects: + logger.info( + f"Attempting to get 'o:{self.organization_name}:p:{project_slug}'" + ) try: res = self.projects.get(slug=project_slug) logger.info("Got the project!") return res except DoesNotExist: return None + """ + except MultipleObjectsReturned: + pass + """ @ensure_login def list_resources(self, project_slug: str) -> list[Any]: diff --git a/tests/_translation.py b/tests/_translation.py index dc1c109..395de84 100644 --- a/tests/_translation.py +++ b/tests/_translation.py @@ -66,7 +66,7 @@ def __init__( lang = self.parameters.translation_source_language self.ts_file = f"{plugin_path}/i18n/{tx}_{lang}.ts" - if self.tx_client.project_exists(parameters.transifex_project): + if self.tx_client.project_exists(parameters.project_slug): logger.debug( f"Project {self.parameters.transifex_organization}/" f"{self.parameters.transifex_project} exists on Transifex" @@ -79,7 +79,7 @@ def __init__( f"{self.parameters.transifex_project}" ) self.tx_client.create_project( - project_slug=self.parameters.transifex_project, + project_slug=self.parameters.project_slug, private=False, repository_url=self.parameters.repository_url, source_language_code=parameters.translation_source_language, @@ -92,7 +92,7 @@ def __init__( f"{self.parameters.transifex_resource} with {self.ts_file}" ) self.tx_client.create_resource( - project_slug=self.parameters.transifex_project, + project_slug=self.parameters.project_slug, path_to_file=self.ts_file, resource_slug=self.parameters.transifex_resource, ) @@ -177,7 +177,7 @@ def pull(self): """ resource = self.__get_resource() existing_langs = self.tx_client.list_languages( - project_slug=self.parameters.transifex_project + project_slug=self.parameters.project_slug ) lang = self.parameters.translation_source_language if lang in existing_langs: @@ -190,7 +190,7 @@ def pull(self): if lang not in existing_langs: logger.debug(f"Creating missing language: {lang}") self.tx_client.create_language( - project_slug=self.parameters.transifex_project, + project_slug=self.parameters.project_slug, language_code=lang, coordinators=[self.parameters.transifex_coordinator], ) @@ -199,7 +199,7 @@ def pull(self): ts_file = f"{self.parameters.plugin_path}/i18n/{self.parameters.transifex_resource}_{lang}.ts" logger.debug(f"Downloading translation file: {ts_file}") self.tx_client.get_translation( - project_slug=self.parameters.transifex_project, + project_slug=self.parameters.project_slug, resource_slug=resource.slug, language_code=lang, path_to_output_file=ts_file, @@ -212,17 +212,19 @@ def push(self): f"with file {self.ts_file}" ) result = self.tx_client.update_source_translation( - project_slug=self.parameters.transifex_project, - resource_slug=resource["slug"], + project_slug=self.parameters.project_slug, + resource_slug=resource.slug, path_to_file=self.ts_file, ) logger.info(f"Translation resource updated: {result}") def __get_resource(self) -> dict: - resources = self.tx_client.list_resources(self.parameters.transifex_project) + resources = self.tx_client.list_resources( + project_slug=self.parameters.project_slug + ) if len(resources) == 0: logger.error( - f"Project '{self.parameters.transifex_project}' has no resource on Transifex" + f"Project '{self.parameters.project_slug}' has no resource on Transifex" ) sys.exit(1) if len(resources) > 1: diff --git a/tests/test_translation.py b/tests/test_translation.py index b0bcad5..31315fe 100644 --- a/tests/test_translation.py +++ b/tests/test_translation.py @@ -5,7 +5,6 @@ import yaml -from pytransifex.exceptions import PyTransifexException from tests._translation import Parameters, Translation logger = logging.getLogger(__name__) @@ -15,42 +14,28 @@ class TestTranslation(unittest.TestCase): @classmethod def setUpClass(cls): """Initialize the test case""" + transifex_token = os.getenv("TX_TOKEN") + cls.transifex_token = transifex_token + assert transifex_token + config_yaml = Path.cwd().joinpath( "tests", "data", ".qgis-plugin-ci-test-changelog.yaml" ) - print(config_yaml) with open(config_yaml) as f: arg_dict = yaml.safe_load(f) - transifex_token = os.getenv("TX_TOKEN") - assert transifex_token - cls.transifex_token = transifex_token cls.parameters = Parameters(**arg_dict) cls.t = Translation(cls.parameters, transifex_token=transifex_token) + logger.info(f"Set up classed with {cls.parameters}") - def tearDown(self): - try: - self.t.tx_client.delete_project(self.parameters.project_slug) - except PyTransifexException as error: - logger.debug(error) - """ - try: - self.t.tx_client.delete_team(f"{self.parameters.project_slug}-team") - except PyTransifexException as error: - logger.debug(error) - """ + @classmethod + def tearDownClass(cls): + assert cls.parameters.project_slug + cls.t.tx_client.delete_project(cls.parameters.project_slug) def test1_creation(self): - self.tearDown() - self.t = Translation(self.parameters, transifex_token=self.transifex_token) # type: ignore - - def test2_push(self): - self.t.update_strings() - self.t.push() - - def test3_pull(self): - self.t.pull() - self.t.compile_strings() + assert self.transifex_token + self.t = Translation(self.parameters, transifex_token=self.transifex_token) if __name__ == "__main__": From fb04aa0d636812c83b9e6df8bd5ab063a2377953 Mon Sep 17 00:00:00 2001 From: why-not-try-calmer Date: Thu, 23 Feb 2023 16:45:20 +0100 Subject: [PATCH 4/4] Added workflow_run --- .github/workflows/test.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9e40511..37b84e0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,13 +9,14 @@ on: push: branches: - master + workflow_dispatch: workflow_call: + jobs: tests: runs-on: ubuntu-latest steps: - # Not using strategy.matrix to create multiple jobs # as we do NOT want to test with any form of concurrency # to avoid 'race conditions' against Transifex