Read about our upcoming Code of Conduct on this issue

Commit b67f6135 authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

Leveraging new Context Managers in test modules

Less pollution, even though try…finally wasn't so bad.
parent 06e855749696
......@@ -58,16 +58,15 @@ def test_import_basic(public_project, tmpdir):
print("Graph of source project after push:")
print(src_repo.graphlog())
project = None
project_name = unique_name('test_import')
try:
status, err, project = Project.api_import_url(
heptapod,
user_name='test_basic',
project_name=project_name,
url=src_url,
)
status, err, project = Project.api_import_url(
heptapod,
user_name='test_basic',
project_name=project_name,
url=src_url,
)
with project:
assert status == 'finished'
clone = LocalRepo.clone(project.owner_basic_auth_url,
tmpdir.join('repo'))
......@@ -75,14 +74,6 @@ def test_import_basic(public_project, tmpdir):
phase=False)
assert project.api_default_branch() == 'branch/default'
finally:
if project is not None:
project.api_destroy()
# In some cases, destroying the project is not enough,
# an orphaned route may linger, preventing next test run
# we may want to run something like this in the docker container:
# echo 'Route.where(path: "test_basic/test_import_small").destroy_all()' | gitlab-rails console # noqa
def test_import_non_topic_draft(public_project, tmpdir):
"""Draft non topic changesets should not been published by import."""
......@@ -92,16 +83,15 @@ def test_import_non_topic_draft(public_project, tmpdir):
print("Graph of source project after push:")
print(src_repo.graphlog())
project = None
project_name = unique_name('test_import')
try:
status, err, project = Project.api_import_url(
heptapod,
user_name='test_basic',
project_name=project_name,
url=src_url,
)
status, err, project = Project.api_import_url(
heptapod,
user_name='test_basic',
project_name=project_name,
url=src_url,
)
with project:
assert status == 'finished'
clone = LocalRepo.clone(project.owner_basic_auth_url,
tmpdir.join('repo'))
......@@ -111,14 +101,6 @@ def test_import_non_topic_draft(public_project, tmpdir):
assert hg_log_set(clone) == hg_log_set(src_repo)
assert project.api_default_branch() == 'branch/default'
finally:
if project is not None:
project.api_destroy()
# In some cases, destroying the project is not enough,
# an orphaned route may linger, preventing next test run
# we may want to run something like this in the docker container:
# echo 'Route.where(path: "test_basic/test_import_small").destroy_all()' | gitlab-rails console # noqa
def test_import_multiple_heads(public_project, tmpdir):
"""Test import, using Heptapod instance itself as source."""
......@@ -134,23 +116,19 @@ def test_import_multiple_heads(public_project, tmpdir):
print("Graph of source project after push:")
print(src_repo.graphlog())
project = None
project_name = unique_name('test_import')
try:
status, err, project = Project.api_import_url(
heptapod,
user_name='test_basic',
project_name=project_name,
url=src_url,
)
status, err, project = Project.api_import_url(
heptapod,
user_name='test_basic',
project_name=project_name,
url=src_url,
)
with project:
assert status == 'finished'
clone = LocalRepo.clone(project.owner_basic_auth_url,
tmpdir.join('repo'))
assert hg_log_set(clone) == hg_log_set(src_repo)
finally:
if project is not None:
project.api_destroy()
def test_import_bookmarks(public_project, tmpdir):
......@@ -166,21 +144,17 @@ def test_import_bookmarks(public_project, tmpdir):
print("Graph of source project after push:")
print(src_repo.graphlog())
project = None
project_name = unique_name('test_import')
try:
status, err, project = Project.api_import_url(
heptapod,
user_name='test_basic',
project_name=project_name,
url=src_url,
)
status, err, project = Project.api_import_url(
heptapod,
user_name='test_basic',
project_name=project_name,
url=src_url,
)
with project:
assert status == 'finished'
clone = LocalRepo.clone(project.owner_basic_auth_url,
tmpdir.join('repo'))
assert (hg_log_set(clone, bookmarks=True)
== hg_log_set(src_repo, bookmarks=True))
finally:
if project is not None:
project.api_destroy()
......@@ -42,10 +42,8 @@ def test_webdriver_destroy(test_project, hashed_storage):
def test_webdriver_create(heptapod, tmpdir):
"""Create a project through the Web UI and check basic operation."""
project = None
try:
project = Project.webdriver_create(heptapod, 'test_basic',
unique_name('test_create'))
with Project.webdriver_create(heptapod, 'test_basic',
unique_name('test_create')) as project:
# Empty page (instructions)
webdriver = project.owner_webdriver
webdriver.get(project.url)
......@@ -61,14 +59,6 @@ def test_webdriver_create(heptapod, tmpdir):
repo.hg("push")
assert project.api_branch_titles() == {'branch/default': 'Commit 0'}
finally:
if project is not None:
# TODO there are cases where the project is created, but
# our retrieval failed nevertheless, we should try and find
# it through the API to make sure we can clean it up in that
# case (spotted in first runs over Heptapod source, because
# of session cookie name being different)
project.api_destroy()
def test_transfer(public_project, tmpdir):
......@@ -94,11 +84,9 @@ def inner_test_transfer(public_project, tmpdir):
repo.hg('commit', '-Am', "Commit 0")
repo.hg('phase', '-p', ".")
repo.hg('push', proj.owner_basic_auth_url)
group = None
try:
group = Group.api_create(proj.heptapod,
unique_name('test_group'),
user_name='test_basic')
with Group.api_create(proj.heptapod,
unique_name('test_group'),
user_name='test_basic') as group:
resp = proj.api_transfer(group)
assert resp.status_code < 400
......@@ -119,9 +107,6 @@ def inner_test_transfer(public_project, tmpdir):
# let's check what GitLab sees
assert proj.api_branch_titles() == {'branch/default': 'Commit 1'}
finally:
if group is not None:
group.api_delete()
def test_rename(test_project, tmpdir):
......@@ -273,9 +258,7 @@ def test_hashed_storage_migration(heptapod, tmpdir):
# the same scenario should work identically otherwise with personal
# namespaces
group_name = unique_name('test_hash_migr')
group = None
try:
group = Group.api_create(heptapod, group_name, user_name=user_name)
with Group.api_create(heptapod, group_name, user_name=user_name) as group:
group.put_hgrc(("[experimental]\n",
"groupconf=test\n"))
project = Project.api_create(heptapod, user_name, 'proj', group=group)
......@@ -327,10 +310,6 @@ def test_hashed_storage_migration(heptapod, tmpdir):
log = clone.hg('log', '-T', '{desc}:{phase}:{topic}\n')
assert log.splitlines() == ['Commit 3:draft:zetop', 'Commit 0:public:']
finally:
if group is not None:
group.api_delete()
@needs.services
def test_housekeeping(test_project):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment