Skip to content

Commit

Permalink
Add task.py
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLorenzo committed Mar 12, 2018
1 parent 8e1ff30 commit b7a79a1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
5 changes: 3 additions & 2 deletions pushsnapscript/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
"""
from scriptworker import client

from pushsnapscript import artifacts
from pushsnapscript import artifacts, task


async def async_main(context):
context.task = client.get_task(context.config)

# TODO Sanity checks on the file
snap_file_path = artifacts.get_snap_file_path(context)
print(snap_file_path)
channel = task.pluck_channel(context.task)
print(snap_file_path, channel)


__name__ == '__main__' and client.sync_main(async_main)
24 changes: 24 additions & 0 deletions pushsnapscript/task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from scriptworker.exceptions import TaskVerificationError
from scriptworker.utils import get_single_item_from_sequence

SNAP_SCOPES_PREFIX = 'project:releng:snapcraft:firefox:'
ALLOWED_CHANNELS = ('edge', 'candidate')


def pluck_channel(task):
scope = get_single_item_from_sequence(
task['scopes'],
lambda scope: scope.startswith(SNAP_SCOPES_PREFIX),
ErrorClass=TaskVerificationError,
no_item_error_message='No scope starts with {}'.format(SNAP_SCOPES_PREFIX),
too_many_item_error_message='Too many scopes start with {}'.format(SNAP_SCOPES_PREFIX),
)

channel = scope[len(SNAP_SCOPES_PREFIX):]

if channel not in ALLOWED_CHANNELS:
raise TaskVerificationError(
'Channel "{}" is not allowed. Allowed ones are: {}'.format(channel, ALLOWED_CHANNELS)
)

return channel
5 changes: 3 additions & 2 deletions pushsnapscript/test/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
from scriptworker import client
from unittest.mock import MagicMock

from pushsnapscript import artifacts
from pushsnapscript import artifacts, task
from pushsnapscript.script import async_main


@pytest.mark.asyncio
async def test_hello_world(capsys, monkeypatch):
monkeypatch.setattr(client, 'get_task', lambda _: {})
monkeypatch.setattr(artifacts, 'get_snap_file_path', lambda _: '/some/file.snap')
monkeypatch.setattr(task, 'pluck_channel', lambda _: 'edge')
context = MagicMock()

await async_main(context)

captured = capsys.readouterr()
assert captured.out == '/some/file.snap\n'
assert captured.out == '/some/file.snap edge\n'
assert captured.err == ''
24 changes: 24 additions & 0 deletions pushsnapscript/test/test_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest

from scriptworker.exceptions import TaskVerificationError

from pushsnapscript.task import pluck_channel


@pytest.mark.parametrize('raises, scopes, expected', (
(False, ['project:releng:snapcraft:firefox:candidate'], 'candidate'),
(False, ['project:releng:snapcraft:firefox:edge'], 'edge'),
(False, ['project:releng:snapcraft:firefox:edge', 'some:other:scope'], 'edge'),
(True, ['project:releng:snapcraft:firefox:edge', 'project:releng:snapcraft:firefox:edge'], None),
(True, ['project:releng:snapcraft:firefox:edge', 'project:releng:snapcraft:firefox:candidate'], None),
(True, ['project:releng:snapcraft:firefox:stable'], None),
(True, ['project:releng:snapcraft:firefox:beta'], None),
))
def test_pluck_channel(raises, scopes, expected):
task = {'scopes': scopes}
if raises:
with pytest.raises(TaskVerificationError):
pluck_channel(task)
else:
assert pluck_channel(task) == expected

0 comments on commit b7a79a1

Please sign in to comment.