Commit ff5b3a36 authored by chrishenry's avatar chrishenry Committed by Commit bot

Move serving_dirs to UserStorySet, delete Page.archive_path (instead using...

Move serving_dirs to UserStorySet, delete Page.archive_path (instead using PageSet.WprFilePathForUserStory).
Update user_story_runner to no longer gate archive-specific logic with instanceof PageSet check.

BUG=439512

Review URL: https://codereview.chromium.org/725133005

Cr-Commit-Position: refs/heads/master@{#308134}
parent 3bce201c
...@@ -25,8 +25,12 @@ def _UpdateCredentials(credentials_path): ...@@ -25,8 +25,12 @@ def _UpdateCredentials(credentials_path):
class Page(user_story.UserStory): class Page(user_story.UserStory):
def __init__(self, url, page_set=None, base_dir=None, name='', def __init__(self, url, page_set=None, base_dir=None, name='',
credentials_path=None, labels=None, startup_url=''): credentials_path=None, labels=None, startup_url=''):
super(Page, self).__init__(shared_page_state.SharedPageState, name, labels)
self._url = url self._url = url
super(Page, self).__init__(
shared_page_state.SharedPageState, name=name, labels=labels,
is_local=self._scheme in ['file', 'chrome', 'about'])
self._page_set = page_set self._page_set = page_set
# Default value of base_dir is the directory of the file that defines the # Default value of base_dir is the directory of the file that defines the
# class of this page instance. # class of this page instance.
...@@ -153,11 +157,6 @@ class Page(user_story.UserStory): ...@@ -153,11 +157,6 @@ class Page(user_story.UserStory):
"""Returns True iff this URL points to a file.""" """Returns True iff this URL points to a file."""
return self._scheme == 'file' return self._scheme == 'file'
@property
def is_local(self):
"""Returns True iff this URL is local. This includes chrome:// URLs."""
return self._scheme in ['file', 'chrome', 'about']
@property @property
def file_path(self): def file_path(self):
"""Returns the path of the file, stripping the scheme and query string.""" """Returns the path of the file, stripping the scheme and query string."""
...@@ -200,7 +199,3 @@ class Page(user_story.UserStory): ...@@ -200,7 +199,3 @@ class Page(user_story.UserStory):
all_urls = [p.url.rstrip('/') for p in self.page_set if p.is_file] all_urls = [p.url.rstrip('/') for p in self.page_set if p.is_file]
common_prefix = os.path.dirname(os.path.commonprefix(all_urls)) common_prefix = os.path.dirname(os.path.commonprefix(all_urls))
return self.url[len(common_prefix):].strip('/') return self.url[len(common_prefix):].strip('/')
@property
def archive_path(self):
return self.page_set.WprFilePathForUserStory(self)
...@@ -23,8 +23,6 @@ class PageSet(user_story_set.UserStorySet): ...@@ -23,8 +23,6 @@ class PageSet(user_story_set.UserStorySet):
def __init__(self, file_path=None, archive_data_file='', user_agent_type=None, def __init__(self, file_path=None, archive_data_file='', user_agent_type=None,
make_javascript_deterministic=True, serving_dirs=None, make_javascript_deterministic=True, serving_dirs=None,
bucket=None): bucket=None):
super(PageSet, self).__init__(
archive_data_file=archive_data_file, cloud_storage_bucket=bucket)
# The default value of file_path is location of the file that define this # The default value of file_path is location of the file that define this
# page set instance's class. # page set instance's class.
# TODO(chrishenry): Move this logic to user_story_set. Consider passing # TODO(chrishenry): Move this logic to user_story_set. Consider passing
...@@ -35,14 +33,15 @@ class PageSet(user_story_set.UserStorySet): ...@@ -35,14 +33,15 @@ class PageSet(user_story_set.UserStorySet):
# Turn pyc file into py files if we can # Turn pyc file into py files if we can
if file_path.endswith('.pyc') and os.path.exists(file_path[:-1]): if file_path.endswith('.pyc') and os.path.exists(file_path[:-1]):
file_path = file_path[:-1] file_path = file_path[:-1]
self.file_path = file_path self.file_path = file_path
super(PageSet, self).__init__(
archive_data_file=archive_data_file, cloud_storage_bucket=bucket,
serving_dirs=serving_dirs)
# These attributes can be set dynamically by the page set. # These attributes can be set dynamically by the page set.
self.user_agent_type = user_agent_type self.user_agent_type = user_agent_type
self.make_javascript_deterministic = make_javascript_deterministic self.make_javascript_deterministic = make_javascript_deterministic
# Convert any relative serving_dirs to absolute paths.
self._serving_dirs = set(os.path.realpath(os.path.join(self.base_dir, d))
for d in serving_dirs or [])
@property @property
def pages(self): def pages(self):
...@@ -70,10 +69,6 @@ class PageSet(user_story_set.UserStorySet): ...@@ -70,10 +69,6 @@ class PageSet(user_story_set.UserStorySet):
else: else:
return self.file_path return self.file_path
@property
def serving_dirs(self):
return self._serving_dirs
def ReorderPageSet(self, results_file): def ReorderPageSet(self, results_file):
"""Reorders this page set based on the results of a past run.""" """Reorders this page set based on the results of a past run."""
page_set_dict = {} page_set_dict = {}
......
...@@ -153,3 +153,16 @@ class TestPage(unittest.TestCase): ...@@ -153,3 +153,16 @@ class TestPage(unittest.TestCase):
self.assertEqual([page_foo, page_baz], page_set_a.pages) self.assertEqual([page_foo, page_baz], page_set_a.pages)
self.assertEqual([page_bar], page_set_b.pages) self.assertEqual([page_bar], page_set_b.pages)
self.assertIs(page_set_b, page_bar.page_set) self.assertIs(page_set_b, page_bar.page_set)
def testIsLocal(self):
p = page.Page('file://foo.html')
self.assertTrue(p.is_local)
p = page.Page('chrome://extensions')
self.assertTrue(p.is_local)
p = page.Page('about:blank')
self.assertTrue(p.is_local)
p = page.Page('http://foo.com')
self.assertFalse(p.is_local)
...@@ -11,11 +11,11 @@ from telemetry.core import browser_finder_exceptions ...@@ -11,11 +11,11 @@ from telemetry.core import browser_finder_exceptions
from telemetry.core import browser_info from telemetry.core import browser_info
from telemetry.core import util from telemetry.core import util
from telemetry.core import wpr_modes from telemetry.core import wpr_modes
from telemetry.core.platform.profiler import profiler_finder
from telemetry.page import page_test from telemetry.page import page_test
from telemetry.user_story import shared_user_story_state from telemetry.user_story import shared_user_story_state
from telemetry.util import file_handle from telemetry.util import file_handle
from telemetry.value import skip from telemetry.value import skip
from telemetry.core.platform.profiler import profiler_finder
def _PrepareFinderOptions(finder_options, test, page_set): def _PrepareFinderOptions(finder_options, test, page_set):
...@@ -111,7 +111,8 @@ class SharedPageState(shared_user_story_state.SharedUserStoryState): ...@@ -111,7 +111,8 @@ class SharedPageState(shared_user_story_state.SharedUserStoryState):
if self._test.RestartBrowserBeforeEachPage() or page.startup_url: if self._test.RestartBrowserBeforeEachPage() or page.startup_url:
self._StopBrowser() self._StopBrowser()
started_browser = not self.browser started_browser = not self.browser
self._PrepareWpr(self.platform.network_controller, page.archive_path, self._PrepareWpr(self.platform.network_controller,
page_set.WprFilePathForUserStory(page),
page_set.make_javascript_deterministic) page_set.make_javascript_deterministic)
if self.browser: if self.browser:
# Set new credential path for browser. # Set new credential path for browser.
......
...@@ -18,14 +18,16 @@ class UserStory(object): ...@@ -18,14 +18,16 @@ class UserStory(object):
Args: Args:
shared_user_story_state_class: subclass of shared_user_story_state_class: subclass of
telemetry.user_story.shared_user_story_state.SharedUserStoryState. telemetry.user_story.shared_user_story_state.SharedUserStoryState.
name: string name of this user story that can be used for identifying user name: string name of this user story that can be used for identifying user
story in results output. story in results output.
labels: A list or set of string labels that are used for filtering. See labels: A list or set of string labels that are used for filtering. See
user_story.user_story_filter for more information. user_story.user_story_filter for more information.
is_local: If true, the user story does not require network.
""" """
def __init__(self, shared_user_story_state_class, name='', labels=None): def __init__(self, shared_user_story_state_class, name='', labels=None,
is_local=False):
assert issubclass(shared_user_story_state_class, assert issubclass(shared_user_story_state_class,
shared_user_story_state.SharedUserStoryState) shared_user_story_state.SharedUserStoryState)
self._shared_user_story_state_class = shared_user_story_state_class self._shared_user_story_state_class = shared_user_story_state_class
...@@ -40,6 +42,7 @@ class UserStory(object): ...@@ -40,6 +42,7 @@ class UserStory(object):
else: else:
assert isinstance(labels, set) assert isinstance(labels, set)
self._labels = labels self._labels = labels
self._is_local = is_local
@property @property
def labels(self): def labels(self):
...@@ -78,3 +81,8 @@ class UserStory(object): ...@@ -78,3 +81,8 @@ class UserStory(object):
return self.name return self.name
else: else:
return self.__class__.__name__ return self.__class__.__name__
@property
def is_local(self):
"""Returns True iff this user story does not require network."""
return self._is_local
...@@ -6,9 +6,10 @@ from telemetry import user_story ...@@ -6,9 +6,10 @@ from telemetry import user_story
from telemetry.user_story.android import shared_app_state from telemetry.user_story.android import shared_app_state
class AppStory(user_story.UserStory): class AppStory(user_story.UserStory):
def __init__(self, start_intent, name='', labels=None): def __init__(self, start_intent, name='', labels=None, is_local=False):
super(AppStory, self).__init__( super(AppStory, self).__init__(
shared_app_state.SharedAppState, name, labels) shared_app_state.SharedAppState, name=name, labels=labels,
is_local=is_local)
self.start_intent = start_intent self.start_intent = start_intent
def RunPageInteractions(self): def RunPageInteractions(self):
......
...@@ -10,12 +10,13 @@ import sys ...@@ -10,12 +10,13 @@ import sys
import time import time
from telemetry import decorators from telemetry import decorators
from telemetry import page as page_module
from telemetry.core import exceptions from telemetry.core import exceptions
from telemetry.core import util from telemetry.core import util
from telemetry.core import wpr_modes from telemetry.core import wpr_modes
from telemetry.page import shared_page_state
from telemetry.page import page_set as page_set_module from telemetry.page import page_set as page_set_module
from telemetry.page import page_test from telemetry.page import page_test
from telemetry.page import shared_page_state
from telemetry.page.actions import page_action from telemetry.page.actions import page_action
from telemetry.results import results_options from telemetry.results import results_options
from telemetry.user_story import user_story_filter from telemetry.user_story import user_story_filter
...@@ -120,14 +121,14 @@ def _RunUserStoryAndProcessErrorIfNeeded( ...@@ -120,14 +121,14 @@ def _RunUserStoryAndProcessErrorIfNeeded(
@decorators.Cache @decorators.Cache
def _UpdateUserStoryArchivesIfChanged(page_set): def _UpdateUserStoryArchivesIfChanged(user_story_set):
# Scan every serving directory for .sha1 files # Scan every serving directory for .sha1 files
# and download them from Cloud Storage. Assume all data is public. # and download them from Cloud Storage. Assume all data is public.
all_serving_dirs = page_set.serving_dirs.copy() all_serving_dirs = user_story_set.serving_dirs.copy()
# Add individual page dirs to all serving dirs. # Add individual page dirs to all serving dirs.
for page in page_set: for user_story in user_story_set:
if page.is_file: if isinstance(user_story, page_module.Page) and user_story.is_file:
all_serving_dirs.add(page.serving_dir) all_serving_dirs.add(user_story.serving_dir)
# Scan all serving dirs. # Scan all serving dirs.
for serving_dir in all_serving_dirs: for serving_dir in all_serving_dirs:
if os.path.splitdrive(serving_dir)[1] == '/': if os.path.splitdrive(serving_dir)[1] == '/':
...@@ -138,7 +139,7 @@ def _UpdateUserStoryArchivesIfChanged(page_set): ...@@ -138,7 +139,7 @@ def _UpdateUserStoryArchivesIfChanged(page_set):
os.path.join(dirpath, filename)) os.path.join(dirpath, filename))
if extension != '.sha1': if extension != '.sha1':
continue continue
cloud_storage.GetIfChanged(path, page_set.bucket) cloud_storage.GetIfChanged(path, user_story_set.bucket)
class UserStoryGroup(object): class UserStoryGroup(object):
...@@ -196,14 +197,11 @@ def Run(test, user_story_set, expectations, finder_options, results): ...@@ -196,14 +197,11 @@ def Run(test, user_story_set, expectations, finder_options, results):
user_stories = _ShuffleAndFilterUserStorySet(user_story_set, finder_options) user_stories = _ShuffleAndFilterUserStorySet(user_story_set, finder_options)
if (not finder_options.use_live_sites and if (not finder_options.use_live_sites and
finder_options.browser_options.wpr_mode != wpr_modes.WPR_RECORD and finder_options.browser_options.wpr_mode != wpr_modes.WPR_RECORD):
# TODO(nednguyen): also handle these logic for user_story_set in next
# patch.
isinstance(user_story_set, page_set_module.PageSet)):
_UpdateUserStoryArchivesIfChanged(user_story_set) _UpdateUserStoryArchivesIfChanged(user_story_set)
if not _CheckArchives( if not _CheckArchives(
user_story_set.archive_data_file, user_story_set.wpr_archive_info, user_story_set.archive_data_file, user_story_set.wpr_archive_info,
user_story_set.pages): user_stories):
return return
for user_story in list(user_stories): for user_story in list(user_stories):
...@@ -278,18 +276,18 @@ def _ShuffleAndFilterUserStorySet(user_story_set, finder_options): ...@@ -278,18 +276,18 @@ def _ShuffleAndFilterUserStorySet(user_story_set, finder_options):
return user_stories return user_stories
def _CheckArchives(archive_data_file, wpr_archive_info, pages): def _CheckArchives(archive_data_file, wpr_archive_info, filtered_user_stories):
"""Verifies that all pages are local or have WPR archives. """Verifies that all user stories are local or have WPR archives.
Logs warnings and returns False if any are missing. Logs warnings and returns False if any are missing.
""" """
# Report any problems with the entire page set. # Report any problems with the entire user story set.
if any(not p.is_local for p in pages): if any(not user_story.is_local for user_story in filtered_user_stories):
if not archive_data_file: if not archive_data_file:
logging.error('The page set is missing an "archive_data_file" ' logging.error('The user story set is missing an "archive_data_file" '
'property.\nTo run from live sites pass the flag ' 'property.\nTo run from live sites pass the flag '
'--use-live-sites.\nTo create an archive file add an ' '--use-live-sites.\nTo create an archive file add an '
'archive_data_file property to the page set and then ' 'archive_data_file property to the user story set and then '
'run record_wpr.') 'run record_wpr.')
return False return False
if not wpr_archive_info: if not wpr_archive_info:
...@@ -299,35 +297,42 @@ def _CheckArchives(archive_data_file, wpr_archive_info, pages): ...@@ -299,35 +297,42 @@ def _CheckArchives(archive_data_file, wpr_archive_info, pages):
'or create a new archive using record_wpr.') 'or create a new archive using record_wpr.')
return False return False
# Report any problems with individual pages. # Report any problems with individual user story.
pages_missing_archive_path = [] user_stories_missing_archive_path = []
pages_missing_archive_data = [] user_stories_missing_archive_data = []
for page in pages: for user_story in filtered_user_stories:
if not page.is_local and not page.archive_path: if not user_story.is_local:
pages_missing_archive_path.append(page) archive_path = wpr_archive_info.WprFilePathForUserStory(user_story)
elif not page.is_local and not os.path.isfile(page.archive_path): if not archive_path:
pages_missing_archive_data.append(page) user_stories_missing_archive_path.append(user_story)
if pages_missing_archive_path: elif not os.path.isfile(archive_path):
logging.error('The page set archives for some pages do not exist.\n' user_stories_missing_archive_data.append(user_story)
'To fix this, record those pages using record_wpr.\n' if user_stories_missing_archive_path:
'To ignore this warning and run against live sites, ' logging.error(
'pass the flag --use-live-sites.') 'The user story set archives for some user stories do not exist.\n'
'To fix this, record those user stories using record_wpr.\n'
'To ignore this warning and run against live sites, '
'pass the flag --use-live-sites.')
logging.error(
'User stories without archives: %s',
', '.join(user_story.display_name
for user_story in user_stories_missing_archive_path))
if user_stories_missing_archive_data:
logging.error( logging.error(
'Pages without archives: %s', 'The user story set archives for some user stories are missing.\n'
', '.join(page.display_name for page in pages_missing_archive_path)) 'Someone forgot to check them in, uploaded them to the '
if pages_missing_archive_data: 'wrong cloud storage bucket, or they were deleted.\n'
logging.error('The page set archives for some pages are missing.\n' 'To fix this, record those user stories using record_wpr.\n'
'Someone forgot to check them in, uploaded them to the ' 'To ignore this warning and run against live sites, '
'wrong cloud storage bucket, or they were deleted.\n' 'pass the flag --use-live-sites.')
'To fix this, record those pages using record_wpr.\n'
'To ignore this warning and run against live sites, '
'pass the flag --use-live-sites.')
logging.error( logging.error(
'Pages missing archives: %s', 'User stories missing archives: %s',
', '.join(page.display_name for page in pages_missing_archive_data)) ', '.join(user_story.display_name
if pages_missing_archive_path or pages_missing_archive_data: for user_story in user_stories_missing_archive_data))
if user_stories_missing_archive_path or user_stories_missing_archive_data:
return False return False
# Only run valid pages if no problems with the page set or individual pages. # Only run valid user stories if no problems with the user story set or
# individual user stories.
return True return True
......
...@@ -9,7 +9,7 @@ import sys ...@@ -9,7 +9,7 @@ import sys
from telemetry import benchmark from telemetry import benchmark
from telemetry import user_story from telemetry import user_story
from telemetry.core import exceptions from telemetry.core import exceptions
from telemetry.page import page_set from telemetry.page import page as page_module
from telemetry.page import page_test from telemetry.page import page_test
from telemetry.page import test_expectations from telemetry.page import test_expectations
from telemetry.results import results_options from telemetry.results import results_options
...@@ -19,6 +19,7 @@ from telemetry.unittest_util import system_stub ...@@ -19,6 +19,7 @@ from telemetry.unittest_util import system_stub
from telemetry.user_story import shared_user_story_state from telemetry.user_story import shared_user_story_state
from telemetry.user_story import user_story_runner from telemetry.user_story import user_story_runner
from telemetry.user_story import user_story_set from telemetry.user_story import user_story_set
from telemetry.util import cloud_storage
from telemetry.util import exception_formatter as exception_formatter_module from telemetry.util import exception_formatter as exception_formatter_module
from telemetry.value import scalar from telemetry.value import scalar
from telemetry.value import string from telemetry.value import string
...@@ -88,6 +89,15 @@ class EmptyMetadataForTest(benchmark.BenchmarkMetadata): ...@@ -88,6 +89,15 @@ class EmptyMetadataForTest(benchmark.BenchmarkMetadata):
super(EmptyMetadataForTest, self).__init__('') super(EmptyMetadataForTest, self).__init__('')
class DummyLocalUserStory(user_story.UserStory):
def __init__(self, shared_user_story_state_class, name=''):
super(DummyLocalUserStory, self).__init__(
shared_user_story_state_class, name=name)
@property
def is_local(self):
return True
def _GetOptionForUnittest(): def _GetOptionForUnittest():
options = options_for_unittests.GetCopy() options = options_for_unittests.GetCopy()
options.output_formats = ['none'] options.output_formats = ['none']
...@@ -136,10 +146,10 @@ class UserStoryRunnerTest(unittest.TestCase): ...@@ -136,10 +146,10 @@ class UserStoryRunnerTest(unittest.TestCase):
def testGetUserStoryGroupsWithSameSharedUserStoryClass(self): def testGetUserStoryGroupsWithSameSharedUserStoryClass(self):
us = user_story_set.UserStorySet() us = user_story_set.UserStorySet()
us.AddUserStory(user_story.UserStory(FooUserStoryState)) us.AddUserStory(DummyLocalUserStory(FooUserStoryState))
us.AddUserStory(user_story.UserStory(FooUserStoryState)) us.AddUserStory(DummyLocalUserStory(FooUserStoryState))
us.AddUserStory(user_story.UserStory(BarUserStoryState)) us.AddUserStory(DummyLocalUserStory(BarUserStoryState))
us.AddUserStory(user_story.UserStory(FooUserStoryState)) us.AddUserStory(DummyLocalUserStory(FooUserStoryState))
story_groups = ( story_groups = (
user_story_runner.GetUserStoryGroupsWithSameSharedUserStoryClass( user_story_runner.GetUserStoryGroupsWithSameSharedUserStoryClass(
us)) us))
...@@ -153,9 +163,9 @@ class UserStoryRunnerTest(unittest.TestCase): ...@@ -153,9 +163,9 @@ class UserStoryRunnerTest(unittest.TestCase):
def testSuccefulUserStoryTest(self): def testSuccefulUserStoryTest(self):
us = user_story_set.UserStorySet() us = user_story_set.UserStorySet()
us.AddUserStory(user_story.UserStory(FooUserStoryState)) us.AddUserStory(DummyLocalUserStory(FooUserStoryState))
us.AddUserStory(user_story.UserStory(FooUserStoryState)) us.AddUserStory(DummyLocalUserStory(FooUserStoryState))
us.AddUserStory(user_story.UserStory(BarUserStoryState)) us.AddUserStory(DummyLocalUserStory(BarUserStoryState))
user_story_runner.Run( user_story_runner.Run(
DummyTest(), us, self.expectations, self.options, self.results) DummyTest(), us, self.expectations, self.options, self.results)
self.assertEquals(0, len(self.results.failures)) self.assertEquals(0, len(self.results.failures))
...@@ -184,10 +194,10 @@ class UserStoryRunnerTest(unittest.TestCase): ...@@ -184,10 +194,10 @@ class UserStoryRunnerTest(unittest.TestCase):
def TearDownState(self, _results): def TearDownState(self, _results):
barz_tear_down_call_counter[0] += 1 barz_tear_down_call_counter[0] += 1
us.AddUserStory(user_story.UserStory(FoozUserStoryState)) us.AddUserStory(DummyLocalUserStory(FoozUserStoryState))
us.AddUserStory(user_story.UserStory(FoozUserStoryState)) us.AddUserStory(DummyLocalUserStory(FoozUserStoryState))
us.AddUserStory(user_story.UserStory(BarzUserStoryState)) us.AddUserStory(DummyLocalUserStory(BarzUserStoryState))
us.AddUserStory(user_story.UserStory(BarzUserStoryState)) us.AddUserStory(DummyLocalUserStory(BarzUserStoryState))
user_story_runner.Run( user_story_runner.Run(
DummyTest(), us, self.expectations, self.options, self.results) DummyTest(), us, self.expectations, self.options, self.results)
self.assertEquals(0, len(self.results.failures)) self.assertEquals(0, len(self.results.failures))
...@@ -204,7 +214,7 @@ class UserStoryRunnerTest(unittest.TestCase): ...@@ -204,7 +214,7 @@ class UserStoryRunnerTest(unittest.TestCase):
def WillRunUserStory(self, user_storyz): def WillRunUserStory(self, user_storyz):
raise exceptions.AppCrashException() raise exceptions.AppCrashException()
us.AddUserStory(user_story.UserStory(SharedUserStoryThatCausesAppCrash)) us.AddUserStory(DummyLocalUserStory(SharedUserStoryThatCausesAppCrash))
user_story_runner.Run( user_story_runner.Run(
DummyTest(), us, self.expectations, self.options, self.results) DummyTest(), us, self.expectations, self.options, self.results)
self.assertEquals(1, len(self.results.failures)) self.assertEquals(1, len(self.results.failures))
...@@ -231,8 +241,8 @@ class UserStoryRunnerTest(unittest.TestCase): ...@@ -231,8 +241,8 @@ class UserStoryRunnerTest(unittest.TestCase):
def ValidateAndMeasurePage(self, page, tab, results): def ValidateAndMeasurePage(self, page, tab, results):
pass pass
us.AddUserStory(user_story.UserStory(TestSharedUserStoryState)) us.AddUserStory(DummyLocalUserStory(TestSharedUserStoryState))
us.AddUserStory(user_story.UserStory(TestSharedUserStoryState)) us.AddUserStory(DummyLocalUserStory(TestSharedUserStoryState))
test = Test() test = Test()
user_story_runner.Run( user_story_runner.Run(
test, us, self.expectations, self.options, self.results) test, us, self.expectations, self.options, self.results)
...@@ -258,8 +268,8 @@ class UserStoryRunnerTest(unittest.TestCase): ...@@ -258,8 +268,8 @@ class UserStoryRunnerTest(unittest.TestCase):
def ValidateAndMeasurePage(self, page, tab, results): def ValidateAndMeasurePage(self, page, tab, results):
pass pass
us.AddUserStory(user_story.UserStory(TestSharedUserStoryState)) us.AddUserStory(DummyLocalUserStory(TestSharedUserStoryState))
us.AddUserStory(user_story.UserStory(TestSharedUserStoryState)) us.AddUserStory(DummyLocalUserStory(TestSharedUserStoryState))
test = Test() test = Test()
user_story_runner.Run( user_story_runner.Run(
test, us, self.expectations, self.options, self.results) test, us, self.expectations, self.options, self.results)
...@@ -269,8 +279,8 @@ class UserStoryRunnerTest(unittest.TestCase): ...@@ -269,8 +279,8 @@ class UserStoryRunnerTest(unittest.TestCase):
def testDiscardFirstResult(self): def testDiscardFirstResult(self):
us = user_story_set.UserStorySet() us = user_story_set.UserStorySet()
us.AddUserStory(user_story.UserStory(TestSharedUserStoryState)) us.AddUserStory(DummyLocalUserStory(TestSharedUserStoryState))
us.AddUserStory(user_story.UserStory(TestSharedUserStoryState)) us.AddUserStory(DummyLocalUserStory(TestSharedUserStoryState))
class Measurement(page_test.PageTest): class Measurement(page_test.PageTest):
@property @property
def discard_first_result(self): def discard_first_result(self):
...@@ -324,9 +334,9 @@ class UserStoryRunnerTest(unittest.TestCase): ...@@ -324,9 +334,9 @@ class UserStoryRunnerTest(unittest.TestCase):
def testPagesetRepeat(self): def testPagesetRepeat(self):
us = user_story_set.UserStorySet() us = user_story_set.UserStorySet()
us.AddUserStory(user_story.UserStory( us.AddUserStory(DummyLocalUserStory(
TestSharedUserStoryState, name='blank')) TestSharedUserStoryState, name='blank'))
us.AddUserStory(user_story.UserStory( us.AddUserStory(DummyLocalUserStory(
TestSharedUserStoryState, name='green')) TestSharedUserStoryState, name='green'))
class Measurement(page_test.PageTest): class Measurement(page_test.PageTest):
...@@ -361,35 +371,43 @@ class UserStoryRunnerTest(unittest.TestCase): ...@@ -361,35 +371,43 @@ class UserStoryRunnerTest(unittest.TestCase):
sys.stdout = real_stdout sys.stdout = real_stdout
def testCheckArchives(self): def testCheckArchives(self):
ps = page_set.PageSet() uss = user_story_set.UserStorySet()
ps.AddPageWithDefaultRunNavigate('http://www.testurl.com') uss.AddUserStory(page_module.Page(
'http://www.testurl.com', self, uss.base_dir))
# Page set missing archive_data_file. # Page set missing archive_data_file.
self.assertFalse(user_story_runner._CheckArchives( self.assertFalse(user_story_runner._CheckArchives(
ps.archive_data_file, ps.wpr_archive_info, ps.pages)) uss.archive_data_file, uss.wpr_archive_info, uss.user_stories))
ps = page_set.PageSet(archive_data_file='missing_archive_data_file.json') uss = user_story_set.UserStorySet(
ps.AddPageWithDefaultRunNavigate('http://www.testurl.com') archive_data_file='missing_archive_data_file.json')
uss.AddUserStory(page_module.Page(
'http://www.testurl.com', self, uss.base_dir))
# Page set missing json file specified in archive_data_file. # Page set missing json file specified in archive_data_file.
self.assertFalse(user_story_runner._CheckArchives( self.assertFalse(user_story_runner._CheckArchives(
ps.archive_data_file, ps.wpr_archive_info, ps.pages)) uss.archive_data_file, uss.wpr_archive_info, uss.user_stories))
ps = page_set.PageSet(archive_data_file='../../unittest_data/test.json', uss = user_story_set.UserStorySet(
bucket=page_set.PUBLIC_BUCKET) archive_data_file='../../unittest_data/test.json',
ps.AddPageWithDefaultRunNavigate('http://www.testurl.com') cloud_storage_bucket=cloud_storage.PUBLIC_BUCKET)
uss.AddUserStory(page_module.Page(
'http://www.testurl.com', self, uss.base_dir))
# Page set with valid archive_data_file. # Page set with valid archive_data_file.
self.assertTrue(user_story_runner._CheckArchives( self.assertTrue(user_story_runner._CheckArchives(
ps.archive_data_file, ps.wpr_archive_info, ps.pages)) uss.archive_data_file, uss.wpr_archive_info, uss.user_stories))
ps.AddPageWithDefaultRunNavigate('http://www.google.com') uss.AddUserStory(page_module.Page(
'http://www.google.com', self, uss.base_dir))
# Page set with an archive_data_file which exists but is missing a page. # Page set with an archive_data_file which exists but is missing a page.
self.assertFalse(user_story_runner._CheckArchives( self.assertFalse(user_story_runner._CheckArchives(
ps.archive_data_file, ps.wpr_archive_info, ps.pages)) uss.archive_data_file, uss.wpr_archive_info, uss.user_stories))
ps = page_set.PageSet( uss = user_story_set.UserStorySet(
archive_data_file='../../unittest_data/test_missing_wpr_file.json', archive_data_file='../../unittest_data/test_missing_wpr_file.json',
bucket=page_set.PUBLIC_BUCKET) cloud_storage_bucket=cloud_storage.PUBLIC_BUCKET)
ps.AddPageWithDefaultRunNavigate('http://www.testurl.com') uss.AddUserStory(page_module.Page(
ps.AddPageWithDefaultRunNavigate('http://www.google.com') 'http://www.testurl.com', self, uss.base_dir))
uss.AddUserStory(page_module.Page(
'http://www.google.com', self, uss.base_dir))
# Page set with an archive_data_file which exists and contains all pages # Page set with an archive_data_file which exists and contains all pages
# but fails to find a wpr file. # but fails to find a wpr file.
self.assertFalse(user_story_runner._CheckArchives( self.assertFalse(user_story_runner._CheckArchives(
ps.archive_data_file, ps.wpr_archive_info, ps.pages)) uss.archive_data_file, uss.wpr_archive_info, uss.user_stories))
...@@ -16,7 +16,8 @@ class UserStorySet(object): ...@@ -16,7 +16,8 @@ class UserStorySet(object):
AddUserStory for each UserStory.. AddUserStory for each UserStory..
""" """
def __init__(self, archive_data_file='', cloud_storage_bucket=None): def __init__(self, archive_data_file='', cloud_storage_bucket=None,
serving_dirs=None):
"""Creates a new UserStorySet. """Creates a new UserStorySet.
Args: Args:
...@@ -33,6 +34,9 @@ class UserStorySet(object): ...@@ -33,6 +34,9 @@ class UserStorySet(object):
archive_info.AssertValidCloudStorageBucket(cloud_storage_bucket) archive_info.AssertValidCloudStorageBucket(cloud_storage_bucket)
self._cloud_storage_bucket = cloud_storage_bucket self._cloud_storage_bucket = cloud_storage_bucket
self._base_dir = os.path.dirname(inspect.getfile(self.__class__)) self._base_dir = os.path.dirname(inspect.getfile(self.__class__))
# Convert any relative serving_dirs to absolute paths.
self._serving_dirs = set(os.path.realpath(os.path.join(self.base_dir, d))
for d in serving_dirs or [])
@property @property
def base_dir(self): def base_dir(self):
...@@ -42,6 +46,10 @@ class UserStorySet(object): ...@@ -42,6 +46,10 @@ class UserStorySet(object):
""" """
return self._base_dir return self._base_dir
@property
def serving_dirs(self):
return self._serving_dirs
@property @property
def archive_data_file(self): def archive_data_file(self):
return self._archive_data_file return self._archive_data_file
...@@ -87,6 +95,15 @@ class UserStorySet(object): ...@@ -87,6 +95,15 @@ class UserStorySet(object):
pass pass
def WprFilePathForUserStory(self, story): def WprFilePathForUserStory(self, story):
"""Convenient function to retrive WPR archive file path.
Args:
user_story: The UserStory to lookup.
Returns:
The WPR archive file path for the given UserStory, if found.
Otherwise, return None.
"""
if not self.wpr_archive_info: if not self.wpr_archive_info:
return None return None
return self.wpr_archive_info.WprFilePathForUserStory(story) return self.wpr_archive_info.WprFilePathForUserStory(story)
......
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