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..aca3f5b 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,14 +212,14 @@ def push(self): f"with file {self.ts_file}" ) result = self.tx_client.update_source_translation( - project_slug=self.parameters.transifex_project, + 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" diff --git a/tests/test_translation.py b/tests/test_translation.py index b0bcad5..b258dd2 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,34 +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 + assert self.transifex_token + self.t = Translation(self.parameters, transifex_token=self.transifex_token) def test2_push(self): self.t.update_strings()