Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tests #43

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tempfile
import time
import unittest
from unittest.mock import patch

import git

Expand All @@ -12,8 +13,11 @@ class CommonCase(unittest.TestCase):

_settings = {
"branch1": "15.0",
"remote_branch1": "origin/15.0",
"branch2": "16.0",
"remote_branch2": "origin/16.0",
"branch3": "17.0",
"remote_branch3": "origin/17.0",
"addon": "my_module",
"from_org": None,
"from_remote": None, # We're testing locally without any remote
Expand All @@ -23,20 +27,38 @@ class CommonCase(unittest.TestCase):

def setUp(self):
# Create a temporary Git repository
self.repo_path = self._create_tmp_git_repository()
self.module_path = os.path.join(self.repo_path, self._settings["addon"])
self.repo_upstream_path = self._create_tmp_git_repository()
self.module_path = os.path.join(
self.repo_upstream_path, self._settings["addon"]
)
self.manifest_path = os.path.join(self.module_path, "__manifest__.py")
self._fill_git_repository()
self._fill_git_repository(self.repo_upstream_path)
# By cloning the first repository this will set an 'origin' remote
self.repo_path = self._clone_tmp_git_repository(self.repo_upstream_path)
self._add_fork_remote(self.repo_path)
# Patch GitHub class to prevent sending HTTP requests
self._patch_github_class()

def _patch_github_class(self):
self.patcher = patch("oca_port.app.GitHub.request")
github_request = self.patcher.start()
github_request.return_value = {}
self.addCleanup(self.patcher.stop)

def _create_tmp_git_repository(self):
"""Create a temporary Git repository to run tests."""
repo_path = tempfile.mkdtemp()
git.Repo.init(repo_path)
return repo_path

def _fill_git_repository(self):
def _clone_tmp_git_repository(self, upstream_path):
repo_path = tempfile.mkdtemp()
git.Repo.clone_from(upstream_path, repo_path)
return repo_path

def _fill_git_repository(self, repo_path):
"""Create branches with some content in the Git repository."""
repo = git.Repo(self.repo_path)
repo = git.Repo(repo_path)
tpl_manifest_path = os.path.join(
pathlib.Path(__file__).parent.resolve(),
"data",
Expand All @@ -63,9 +85,14 @@ def _fill_git_repository(self):
repo.git.reset("--hard")
repo.git.commit("-m", "Init", "--allow-empty")

def _commit_change_on_branch(self, branch):
def _add_fork_remote(self, repo_path):
repo = git.Repo(repo_path)
# We do not really care about the remote URL here, re-use origin one
repo.create_remote(self._settings["user_org"], repo.remotes.origin.url)

def _commit_change_on_branch(self, repo_path, branch):
"""Commit a change that can be ported to another branch."""
repo = git.Repo(self.repo_path)
repo = git.Repo(repo_path)
repo.git.checkout(branch)
# Do some changes and commit
with open(self.manifest_path, "r+") as manifest:
Expand All @@ -79,4 +106,5 @@ def _commit_change_on_branch(self, branch):

def tearDown(self):
# Clean up the Git repository
shutil.rmtree(self.repo_upstream_path)
shutil.rmtree(self.repo_path)
79 changes: 53 additions & 26 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,95 +19,118 @@ def _create_app(self, from_branch, to_branch, **kwargs):
"no_cache": self._settings["no_cache"],
}
params.update(kwargs)
# NOTE: app will run in non-interactive mode
return App(**params)

def test_app_nothing_to_port(self):
app = self._create_app(self._settings["branch1"], self._settings["branch2"])
app = self._create_app(
self._settings["remote_branch1"], self._settings["remote_branch2"]
)
try:
app.run()
except SystemExit as exc:
# exit code 0 means nothing needs to be migrated/ported
self.assertEqual(exc.args[0], 0)

def test_app_commit_to_port(self):
app = self._create_app(self._settings["branch1"], self._settings["branch2"])
self._commit_change_on_branch(self._settings["branch1"])
self._commit_change_on_branch(
self.repo_upstream_path, self._settings["branch1"]
)
app = self._create_app(
self._settings["remote_branch1"],
self._settings["remote_branch2"],
fetch=True,
)
try:
app.run()
except SystemExit as exc:
# exit code 110 means pull requests or commits could be ported
self.assertEqual(exc.args[0], 110)
# The other way around, no commit to backport (no exception)
# (with CLI, the returned exit code is then 0)
app = self._create_app(self._settings["branch2"], self._settings["branch1"])
app = self._create_app(
self._settings["remote_branch2"], self._settings["remote_branch1"]
)
res = app.run()
self.assertFalse(res)

def test_app_module_to_migrate(self):
app = self._create_app(self._settings["branch2"], self._settings["branch3"])
app = self._create_app(
self._settings["remote_branch2"], self._settings["remote_branch3"]
)
try:
app.run()
except SystemExit as exc:
# exit code 100 means the module could be migrated
self.assertEqual(exc.args[0], 100)
# The other way around, nothing to migrate as the module doesn't exist
# (with CLI, the returned exit code is then 1)
app = self._create_app(self._settings["branch3"], self._settings["branch2"])
error_msg = "my_module does not exist on 17.0"
app = self._create_app(
self._settings["remote_branch3"], self._settings["remote_branch2"]
)
error_msg = "my_module does not exist on origin/17.0"
with self.assertRaisesRegex(ValueError, error_msg):
app.run()

def test_app_commit_to_port_non_interactive(self):
self._commit_change_on_branch(
self.repo_upstream_path, self._settings["branch1"]
)
app = self._create_app(
self._settings["branch1"],
self._settings["branch2"],
self._settings["remote_branch1"],
self._settings["remote_branch2"],
non_interactive=True,
fetch=True,
)
self._commit_change_on_branch(self._settings["branch1"])
result = app.run()
self.assertTrue(result)
self.assertIsInstance(result, bool)
# The other way around, no commit to backport
app = self._create_app(
self._settings["branch2"], self._settings["branch1"], non_interactive=True
self._settings["remote_branch2"],
self._settings["remote_branch1"],
non_interactive=True,
)
result = app.run()
self.assertFalse(result)
self.assertIsInstance(result, bool)

def test_app_module_to_migrate_non_interactive(self):
app = self._create_app(
self._settings["branch2"],
self._settings["branch3"],
self._settings["remote_branch2"],
self._settings["remote_branch3"],
non_interactive=True,
)
result = app.run()
self.assertTrue(result)
self.assertIsInstance(result, bool)
# The other way around, nothing to migrate as the module doesn't exist
app = self._create_app(
self._settings["branch3"], self._settings["branch2"], non_interactive=True
self._settings["remote_branch3"],
self._settings["remote_branch2"],
non_interactive=True,
)
error_msg = "my_module does not exist on 17.0"
error_msg = "my_module does not exist on origin/17.0"
with self.assertRaisesRegex(ValueError, error_msg):
app.run()

def test_app_wrong_output(self):
with self.assertRaisesRegex(ValueError, "Supported outputs are"):
self._create_app(
self._settings["branch2"],
self._settings["branch3"],
self._settings["remote_branch2"],
self._settings["remote_branch3"],
output="wrong_format",
)

def test_app_commit_to_port_output_json(self):
commit_sha = self._commit_change_on_branch(
self.repo_upstream_path, self._settings["branch1"]
)
app = self._create_app(
self._settings["branch1"],
self._settings["branch2"],
self._settings["remote_branch1"],
self._settings["remote_branch2"],
output="json",
fetch=True,
)
commit_sha = self._commit_change_on_branch(self._settings["branch1"])
output = app.run()
self.assertTrue(output)
self.assertIsInstance(output, str)
Expand All @@ -128,7 +151,9 @@ def test_app_commit_to_port_output_json(self):
)
# The other way around, no commit to backport
app = self._create_app(
self._settings["branch2"], self._settings["branch1"], output="json"
self._settings["remote_branch2"],
self._settings["remote_branch1"],
output="json",
)
output = app.run()
self.assertTrue(output)
Expand All @@ -138,8 +163,8 @@ def test_app_commit_to_port_output_json(self):

def test_app_module_to_migrate_output_json(self):
app = self._create_app(
self._settings["branch2"],
self._settings["branch3"],
self._settings["remote_branch2"],
self._settings["remote_branch3"],
output="json",
)
output = app.run()
Expand All @@ -150,8 +175,10 @@ def test_app_module_to_migrate_output_json(self):
self.assertEqual(output["results"], {})
# The other way around, nothing to migrate as the module doesn't exist
app = self._create_app(
self._settings["branch3"], self._settings["branch2"], output="json"
self._settings["remote_branch3"],
self._settings["remote_branch2"],
output="json",
)
error_msg = "my_module does not exist on 17.0"
error_msg = "my_module does not exist on origin/17.0"
with self.assertRaisesRegex(ValueError, error_msg):
app.run()