Skip to content

Commit

Permalink
Ensure update uses default serialiser #113
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Apr 2, 2017
1 parent 8372ef8 commit 4a1428e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 143 deletions.
Binary file not shown.
141 changes: 19 additions & 122 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
from __future__ import print_function

from contextlib import contextmanager
import unittest
import os
import time

import pytest
import pytest_localserver

from util import WorkflowMock, create_info_plist, delete_info_plist
from workflow import Workflow, update
from workflow.background import is_running
from workflow import web

# Where test data is
Expand Down Expand Up @@ -162,6 +159,7 @@ def test_valid_releases(httpserver, info):
assert update.Version(releases[0]['version']) == \
update.Version(RELEASE_LATEST)


def test_valid_releases_with_prereleases(httpserver, info):
"""Valid releases with prereleases"""
with fakeresponse(httpserver, DATA_JSON, HTTP_HEADERS_JSON):
Expand All @@ -182,7 +180,6 @@ def test_valid_releases_with_prereleases(httpserver, info):

def test_version_formats(httpserver, info):
"""Version formats"""

falsey = (
# Up-to-date versions
'6.0', 'v6.0',
Expand All @@ -203,7 +200,6 @@ def test_version_formats(httpserver, info):

def test_prerelease_version_formats(httpserver, info):
"""Prerelease version formats"""

falsey = (
# Up-to-date versions
'7.1.0-beta', 'v7.1.0-beta',
Expand All @@ -217,9 +213,11 @@ def test_prerelease_version_formats(httpserver, info):

with fakeresponse(httpserver, DATA_JSON, HTTP_HEADERS_JSON):
for vstr in falsey:
assert update.check_update(TEST_REPO_SLUG, vstr, prereleases=True) is False
assert update.check_update(TEST_REPO_SLUG, vstr,
prereleases=True) is False
for vstr in truthy:
assert update.check_update(TEST_REPO_SLUG, vstr, prereleases=True) is True
assert update.check_update(TEST_REPO_SLUG, vstr,
prereleases=True) is True


def test_check_update(httpserver, info):
Expand Down Expand Up @@ -309,121 +307,20 @@ def test_no_auto_update(info):
assert wf.cached_data('__workflow_update_status') is None


# def test_workflow_update_methods(httpserver, info):
# """Workflow update methods"""

# def fake(wf):
# return

# with fakeresponse(httpserver, DATA_JSON, HTTP_HEADERS_JSON):
# Workflow().reset()
# # Initialise with outdated version
# wf = Workflow(update_settings={
# 'github_slug': 'deanishe/alfred-workflow-dummy',
# 'version': 'v1.0',
# 'frequency': 1,
# })

# wf.run(fake)

# # Check shouldn't have completed yet
# assert wf.update_available is False

# # Wait for background update check
# assert is_running('__workflow_update_check') is True
# while is_running('__workflow_update_check'):
# time.sleep(0.5)
# time.sleep(1)

# # There *is* a newer version in the repo
# print(repr(wf.cached_data('__workflow_update_status', max_age=0)))
# assert wf.update_available is True

# # Mock out subprocess and check the correct command is run
# c = WorkflowMock()
# with c:
# assert wf.start_update() is True
# # wf.logger.debug('start_update : {}'.format(c.cmd))
# assert c.cmd[0] == '/usr/bin/python'
# assert c.cmd[2] == '__workflow_update_install'

# # Grab the updated release data, then reset the cache
# update_info = wf.cached_data('__workflow_update_status')

# wf.reset()

# # Initialise with latest available release
# wf = Workflow(update_settings={
# 'github_slug': 'deanishe/alfred-workflow-dummy',
# 'version': update_info['version'],
# })

# wf.run(fake)

# # Wait for background update check
# assert is_running('__workflow_update_check') is True
# while is_running('__workflow_update_check'):
# time.sleep(0.05)

# # Remote version is same as the one we passed to Workflow
# assert wf.update_available is False
# assert wf.start_update() is False

# wf.reset()

# # Initialise with outdated version allowing pre-release updates
# wf = Workflow(update_settings={
# 'github_slug': 'deanishe/alfred-workflow-dummy',
# 'version': 'v1.0',
# 'frequency': 1,
# 'prereleases': True,
# })

# wf.run(fake)

# # Check shouldn't have completed yet
# assert wf.update_available is False

# # Wait for background update check
# assert is_running('__workflow_update_check') is True
# while is_running('__workflow_update_check'):
# time.sleep(0.5)
# time.sleep(1)

# # There *is* a newer version in the repo
# print(repr(wf.cached_data('__workflow_update_status', max_age=0)))
# assert wf.update_available is True

# # Mock out subprocess and check the correct command is run
# c = WorkflowMock()
# with c:
# assert wf.start_update() is True
# # wf.logger.debug('start_update : {}'.format(c.cmd))
# assert c.cmd[0] == '/usr/bin/python'
# assert c.cmd[2] == '__workflow_update_install'

# # Grab the updated release data, then reset the cache
# update_info = wf.cached_data('__workflow_update_status')

# wf.reset()

# # Initialise with latest available release allowing pre-release updates
# wf = Workflow(update_settings={
# 'github_slug': 'deanishe/alfred-workflow-dummy',
# 'version': update_info['version'],
# 'prereleases': True,
# })

# wf.run(fake)

# # Wait for background update check
# assert is_running('__workflow_update_check') is True
# while is_running('__workflow_update_check'):
# time.sleep(0.05)

# # Remote version is same as the one we passed to Workflow
# assert wf.update_available is False
# assert wf.start_update() is False
def test_update_nondefault_serialiser(httpserver, info):
"""Check update works when a custom serialiser is set on Workflow
https://github.com/deanishe/alfred-workflow/issues/113
"""
wf = Workflow()
wf.cache_serializer = 'json'
wf.reset()

with fakeresponse(httpserver, DATA_JSON, HTTP_HEADERS_JSON):
assert update.check_update(TEST_REPO_SLUG,
RELEASE_CURRENT) is True

assert wf.update_available is True


if __name__ == '__main__': # pragma: no cover
Expand Down
2 changes: 1 addition & 1 deletion workflow/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.25
1.25.1
48 changes: 28 additions & 20 deletions workflow/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1974,26 +1974,30 @@ def filter(self, query, items, key=lambda x: x, ascending=False,
By default, :meth:`filter` uses all of the following flags (i.e.
:const:`MATCH_ALL`). The tests are always run in the given order:
1. :const:`MATCH_STARTSWITH` : Item search key startswith
``query``(case-insensitive).
2. :const:`MATCH_CAPITALS` : The list of capital letters in item
search key starts with ``query`` (``query`` may be
lower-case). E.g., ``of`` would match ``OmniFocus``,
``gc`` would match ``Google Chrome``.
3. :const:`MATCH_ATOM` : Search key is split into "atoms" on
non-word characters (.,-,' etc.). Matches if ``query`` is
one of these atoms (case-insensitive).
4. :const:`MATCH_INITIALS_STARTSWITH` : Initials are the first
characters of the above-described "atoms" (case-insensitive).
5. :const:`MATCH_INITIALS_CONTAIN` : ``query`` is a substring of
the above-described initials.
6. :const:`MATCH_INITIALS` : Combination of (4) and (5).
7. :const:`MATCH_SUBSTRING` : Match if ``query`` is a substring
of item search key (case-insensitive).
8. :const:`MATCH_ALLCHARS` : Matches if all characters in
``query`` appear in item search key in the same order
1. :const:`MATCH_STARTSWITH`
Item search key starts with ``query`` (case-insensitive).
2. :const:`MATCH_CAPITALS`
The list of capital letters in item search key starts with
``query`` (``query`` may be lower-case). E.g., ``of``
would match ``OmniFocus``, ``gc`` would match ``Google Chrome``.
3. :const:`MATCH_ATOM`
Search key is split into "atoms" on non-word characters
(.,-,' etc.). Matches if ``query`` is one of these atoms
(case-insensitive).
9. :const:`MATCH_ALL` : Combination of all the above.
4. :const:`MATCH_INITIALS_STARTSWITH`
Initials are the first characters of the above-described
"atoms" (case-insensitive).
5. :const:`MATCH_INITIALS_CONTAIN`
``query`` is a substring of the above-described initials.
6. :const:`MATCH_INITIALS`
Combination of (4) and (5).
7. :const:`MATCH_SUBSTRING`
``query`` is a substring of item search key (case-insensitive).
8. :const:`MATCH_ALLCHARS`
All characters in ``query`` appear in item search key in
the same order (case-insensitive).
9. :const:`MATCH_ALL`
Combination of all the above.
:const:`MATCH_ALLCHARS` is considerably slower than the other
Expand Down Expand Up @@ -2432,7 +2436,11 @@ def update_available(self):
:returns: ``True`` if an update is available, else ``False``
"""
update_data = self.cached_data('__workflow_update_status', max_age=0)
# Create a new workflow object to ensure standard serialiser
# is used (update.py is called without the user's settings)
update_data = Workflow().cached_data('__workflow_update_status',
max_age=0)

self.logger.debug('update_data : {0}'.format(update_data))

if not update_data or not update_data.get('available'):
Expand Down

0 comments on commit 4a1428e

Please sign in to comment.