From 822b2895f2afd596c235121e5fe5339e3cc211c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Chaves?= Date: Thu, 8 Feb 2024 14:44:45 +0100 Subject: [PATCH] Make tests pass (#446) --- setup.py | 9 +---- tests/image/test_build.py | 11 +++--- tests/image/test_deploy.py | 39 ++++++++++--------- tests/image/test_push.py | 79 +++++++++++++++++++------------------- tests/image/test_utils.py | 10 +++-- tests/test_deploy_egg.py | 5 ++- 6 files changed, 77 insertions(+), 76 deletions(-) diff --git a/setup.py b/setup.py index b5e2b8d1..6f5c7492 100644 --- a/setup.py +++ b/setup.py @@ -1,18 +1,11 @@ -import os from setuptools import setup, find_packages -about = {} -here = os.path.abspath(os.path.dirname(__file__)) -with open(os.path.join(here, 'shub', '__init__.py'), encoding='utf-8') as f: - exec(f.read(), about) - - setup( name='shub', version='2.15.3', packages=find_packages(exclude=('tests', 'tests.*')), - url=about['DOCS_LINK'], + url="https://shub.readthedocs.io/en/stable/", description='Scrapinghub Command Line Client', long_description=open('README.rst').read(), author='Scrapinghub', diff --git a/tests/image/test_build.py b/tests/image/test_build.py index 1427921c..9088a88f 100644 --- a/tests/image/test_build.py +++ b/tests/image/test_build.py @@ -1,4 +1,5 @@ import os +import re from unittest import mock import pytest @@ -91,12 +92,12 @@ def test_cli_with_progress(docker_client_mock, project_dir, test_mock): result = runner.invoke(cli, ["dev"]) assert result.exit_code == 0 expected = format_expected_progress( - 'Building registry.io/user/project:1.0.' - 'Steps: 0%| | 0/1' - 'Steps: 100%|██████████| 3/3' - 'The image registry.io/user/project:1.0 build is completed.' + r'Building registry\.io/user/project:1\.0\.' + r'Steps: 0%\| +\| 0/1' + r'Steps: 100%\|█+\| 3/3' + r'The image registry\.io/user/project:1\.0 build is completed\.' ) - assert expected in clean_progress_output(result.output) + assert re.search(clean_progress_output(result.output), expected) def test_cli_custom_version(docker_client_mock, project_dir, test_mock): diff --git a/tests/image/test_deploy.py b/tests/image/test_deploy.py index 4f01e734..637c8f19 100644 --- a/tests/image/test_deploy.py +++ b/tests/image/test_deploy.py @@ -1,4 +1,5 @@ import json +import re import time from unittest import mock @@ -165,19 +166,19 @@ def test_progress_bar_logic(): result = CliRunner().invoke(progress_cmd) assert result.exit_code == 0 expected = format_expected_progress( - 'Progress: 0%| | 0/100' - 'Progress: 25%|██▌ | 25/100' - 'Progress: 30%|███ | 30/100' - 'Progress: 35%|███▌ | 35/100' - 'Progress: 50%|█████ | 50/100' - 'Progress: 55%|█████▌ | 55/100' - 'Progress: 65%|██████▌ | 65/100' - 'Progress: 75%|███████▌ | 75/100' - 'Progress: 100%|██████████| 100/100' - 'Progress: 100%|██████████| 100/100' - 'Deploy results:' + r'Progress: 0%\|[^|]+\| 0/100' + r'Progress: 25%\|[^|]+\| 25/100' + r'Progress: 30%\|[^|]+\| 30/100' + r'Progress: 35%\|[^|]+\| 35/100' + r'Progress: 50%\|[^|]+\| 50/100' + r'Progress: 55%\|[^|]+\| 55/100' + r'Progress: 65%\|[^|]+\| 65/100' + r'Progress: 75%\|[^|]+\| 75/100' + r'Progress: 100%\|[^|]+\| 100/100' + r'Progress: 100%\|[^|]+\| 100/100' + r'Deploy results:' ) - assert expected in clean_progress_output(result.output) + assert re.search(expected, clean_progress_output(result.output)) lines = result.output.split('\n') # test that output ends with a newline symbol assert lines[-1] == '' @@ -192,14 +193,14 @@ def test_progress_bar_logic_incomplete(): result = CliRunner().invoke(progress_cmd) assert result.exit_code == 0 expected = format_expected_progress( - 'Progress: 0%| | 0/100' - 'Progress: 25%|██▌ | 25/100' - 'Progress: 30%|███ | 30/100' - 'Progress: 100%|██████████| 100/100' - 'Progress: 100%|██████████| 100/100' - 'Deploy results:' + r'Progress: 0%|[^|]+| 0/100' + r'Progress: 25%|[^|]+| 25/100' + r'Progress: 30%|[^|]+| 30/100' + r'Progress: 100%|[^|]+| 100/100' + r'Progress: 100%|[^|]+| 100/100' + r'Deploy results:' ) - assert expected in clean_progress_output(result.output) + assert re.search(expected, clean_progress_output(result.output)) # test that the command succeeded lines = result.output.split('\n') # test that output ends with a newline symbol diff --git a/tests/image/test_push.py b/tests/image/test_push.py index 6b0e1c1e..02339443 100644 --- a/tests/image/test_push.py +++ b/tests/image/test_push.py @@ -1,3 +1,4 @@ +import re from unittest import mock import pytest @@ -53,25 +54,25 @@ def test_cli_with_progress(docker_client_mock): result = runner.invoke(cli, ["dev", "--version", "test"]) assert result.exit_code == 0 expected = format_expected_progress( - 'Login to registry.io succeeded.' - 'Pushing registry.io/user/project:test to the registry.' - 'Layers: 0%| | 0/1' - 'Layers: 0%| | 0/1' - 'Layers: 0%| | 0/2' - 'Layers: 0%| | 0/3' - 'Layers: 0%| | 0/3' - 'Layers: 0%| | 0/3' - 'abc: 2%|▏ | 512/24.8k [1.00MB/s]' - 'Layers: 0%| | 0/3' - 'egh: 100%|██████████| 57.3k/57.3k [1.00MB/s]' - 'Layers: 33%|███▎ | 1/3' - 'Layers: 67%|██████▋ | 2/3' - 'Layers: 100%|██████████| 3/3' - 'abc: 100%|██████████| 24.8k/24.8k [1.00MB/s]' - 'egh: 100%|██████████| 57.3k/57.3k [1.00MB/s]' - 'The image registry.io/user/project:test pushed successfully.' + r'Login to registry\.io succeeded\.' + r'Pushing registry\.io/user/project:test to the registry\.' + r'Layers: 0%\|[^|]+\| 0/1' + r'Layers: 0%\|[^|]+\| 0/1' + r'Layers: 0%\|[^|]+\| 0/2' + r'Layers: 0%\|[^|]+\| 0/3' + r'Layers: 0%\|[^|]+\| 0/3' + r'Layers: 0%\|[^|]+\| 0/3' + r'abc: 2%\|[^|]+\| 512/24\.8k \[1\.00MB/s\]' + r'Layers: 0%\|[^|]+\| 0/3' + r'egh: 100%\|[^|]+\| 57\.3k/57\.3k \[1\.00MB/s\]' + r'Layers: 33%\|[^|]+\| 1/3' + r'Layers: 67%\|[^|]+\| 2/3' + r'Layers: 100%\|[^|]+\| 3/3' + r'abc: 100%\|[^|]+\| 24\.8k/24\.8k \[1\.00MB/s\]' + r'egh: 100%\|[^|]+\| 57\.3k/57\.3k \[1\.00MB/s\]' + r'The image registry\.io/user/project:test pushed successfully\.' ) - assert expected in clean_progress_output(result.output) + assert re.search(expected, clean_progress_output(result.output)) @pytest.mark.usefixtures('project_dir', 'test_mock', 'monkeypatch_bar_rate') @@ -100,28 +101,28 @@ def test_progress_no_total(docker_client_mock): result = runner.invoke(cli, ["dev", "--version", "test"]) assert result.exit_code == 0 expected = format_expected_progress( - 'Login to registry.io succeeded.' - 'Pushing registry.io/user/project:test to the registry.' - 'Layers: 0%| | 0/1' - 'Layers: 0%| | 0/1' - 'Layers: 0%| | 0/2' - 'Layers: 0%| | 0/3' - 'Layers: 0%| | 0/4' - 'Layers: 0%| | 0/4' - 'Layers: 0%| | 0/4' - 'Layers: 0%| | 0/4' - 'abc: 100%|██████████| 512/512 [1.00MB/s]' - 'Layers: 0%| | 0/4' - 'egh: 100%|██████████| 57.3k/57.3k [1.00MB/s]' - 'Layers: 25%|██▌ | 1/4' - 'Layers: 50%|█████ | 2/4' - 'Layers: 75%|███████▌ | 3/4' - 'Layers: 100%|██████████| 4/4' - 'abc: 100%|██████████| 24.8k/24.8k [1.00MB/s]' - 'egh: 100%|██████████| 57.3k/57.3k [1.00MB/s]' - 'The image registry.io/user/project:test pushed successfully.' + r'Login to registry\.io succeeded\.' + r'Pushing registry\.io/user/project:test to the registry\.' + r'Layers: 0%\|[^|]+\| 0/1' + r'Layers: 0%\|[^|]+\| 0/1' + r'Layers: 0%\|[^|]+\| 0/2' + r'Layers: 0%\|[^|]+\| 0/3' + r'Layers: 0%\|[^|]+\| 0/4' + r'Layers: 0%\|[^|]+\| 0/4' + r'Layers: 0%\|[^|]+\| 0/4' + r'Layers: 0%\|[^|]+\| 0/4' + r'abc: 100%\|[^|]+\| 512/512 \[1\.00MB/s\]' + r'Layers: 0%\|[^|]+\| 0/4' + r'egh: 100%\|[^|]+\| 57\.3k/57\.3k \[1\.00MB/s\]' + r'Layers: 25%\|[^|]+\| 1/4' + r'Layers: 50%\|[^|]+\| 2/4' + r'Layers: 75%\|[^|]+\| 3/4' + r'Layers: 100%\|[^|]+\| 4/4' + r'abc: 100%\|[^|]+\| 24\.8k/24\.8k \[1\.00MB/s\]' + r'egh: 100%\|[^|]+\| 57\.3k/57\.3k \[1\.00MB/s\]' + r'The image registry\.io/user/project:test pushed successfully\.' ) - assert expected in clean_progress_output(result.output) + assert re.search(expected, clean_progress_output(result.output)) @pytest.mark.usefixtures('project_dir') diff --git a/tests/image/test_utils.py b/tests/image/test_utils.py index f0be357b..be54e46d 100644 --- a/tests/image/test_utils.py +++ b/tests/image/test_utils.py @@ -110,12 +110,16 @@ def test_get_image_registry(self): class StatusUrlsTest(TestCase): def setUp(self): - tmpdir = tempfile.gettempdir() - os.chdir(tmpdir) - self.status_file = os.path.join(tmpdir, STATUS_FILE_LOCATION) + self.curdir = os.getcwd() + self.tmp_dir = tempfile.gettempdir() + os.chdir(self.tmp_dir) + self.status_file = os.path.join(self.tmp_dir, STATUS_FILE_LOCATION) if os.path.exists(self.status_file): os.remove(self.status_file) + def tearDown(self): + os.chdir(self.curdir) + def test_load_status_url(self): self.assertRaises(NotFoundException, load_status_url, 0) # try with void file diff --git a/tests/test_deploy_egg.py b/tests/test_deploy_egg.py index 802786f1..38c8efbf 100644 --- a/tests/test_deploy_egg.py +++ b/tests/test_deploy_egg.py @@ -25,7 +25,7 @@ def setUp(self): self.curdir = os.getcwd() self.fake_requester = FakeRequester() deploy_egg.utils.make_deploy_request = self.fake_requester.fake_request - self.tmp_dir = tempfile.mktemp(prefix="shub-test-deploy-eggs") + self.tmp_dir = tempfile.mkdtemp(prefix="shub-test-deploy-eggs") def tearDown(self): os.chdir(self.curdir) @@ -35,7 +35,8 @@ def tearDown(self): def test_parses_project_information_correctly(self): # this test's assertions are based on the values # defined on this folder's setup.py file - shutil.copytree('tests/samples/deploy_egg_sample_project/', self.tmp_dir) + shutil.rmtree(self.tmp_dir) + shutil.copytree('tests/samples/deploy_egg_sample_project', self.tmp_dir) os.chdir(self.tmp_dir) data = self.call_main_and_check_request_data()