Commit 65ff7d22 authored by aiolos's avatar aiolos Committed by Commit bot

Move base_dir to user_story_set, remove file_path.

BUG=454531

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

Cr-Commit-Position: refs/heads/master@{#317632}
parent 95b89c26
...@@ -24,19 +24,15 @@ class PageSet(user_story_set.UserStorySet): ...@@ -24,19 +24,15 @@ class PageSet(user_story_set.UserStorySet):
serving_dirs=None, bucket=None): serving_dirs=None, bucket=None):
# 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(aiolos): When migrating page_set's over to user_story_set's, make
# a base_dir directly. Alternatively, kill this and rely on the default # sure that we are passing a directory, not a file.
# behavior of using the instance's class file location. dir_name = file_path
if file_path is None: if file_path and os.path.isfile(file_path):
file_path = inspect.getfile(self.__class__) dir_name = os.path.dirname(file_path)
# Turn pyc file into py files if we can
if file_path.endswith('.pyc') and os.path.exists(file_path[:-1]):
file_path = file_path[:-1]
self.file_path = file_path
super(PageSet, self).__init__( super(PageSet, self).__init__(
archive_data_file=archive_data_file, cloud_storage_bucket=bucket, archive_data_file=archive_data_file, cloud_storage_bucket=bucket,
serving_dirs=serving_dirs) base_dir=dir_name, 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
...@@ -50,13 +46,6 @@ class PageSet(user_story_set.UserStorySet): ...@@ -50,13 +46,6 @@ class PageSet(user_story_set.UserStorySet):
assert user_story.page_set is self assert user_story.page_set is self
super(PageSet, self).AddUserStory(user_story) super(PageSet, self).AddUserStory(user_story)
@property
def base_dir(self):
if os.path.isfile(self.file_path):
return os.path.dirname(self.file_path)
else:
return self.file_path
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 = {}
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
import inspect
import os import os
import unittest import unittest
...@@ -52,9 +53,11 @@ class TestPage(unittest.TestCase): ...@@ -52,9 +53,11 @@ class TestPage(unittest.TestCase):
pages) pages)
def testGetUrlBaseDirAndFileForUrlBaseDir(self): def testGetUrlBaseDirAndFileForUrlBaseDir(self):
ps = page_set.PageSet(file_path='basedir/', serving_dirs=['../somedir/']) base_dir = os.path.dirname(__file__)
file_path = os.path.dirname(base_dir) + '/otherdir/file.html'
ps = page_set.PageSet(file_path=base_dir, serving_dirs=['../somedir/'])
ps.AddUserStory(page.Page('file://../otherdir/file.html', ps, ps.base_dir)) ps.AddUserStory(page.Page('file://../otherdir/file.html', ps, ps.base_dir))
self.assertPathEqual(ps[0].file_path, 'otherdir/file.html') self.assertPathEqual(ps[0].file_path, file_path)
def testDisplayUrlForHttp(self): def testDisplayUrlForHttp(self):
ps = page_set.PageSet(file_path=os.path.dirname(__file__)) ps = page_set.PageSet(file_path=os.path.dirname(__file__))
......
...@@ -86,10 +86,10 @@ class PageSetSmokeTest(unittest.TestCase): ...@@ -86,10 +86,10 @@ class PageSetSmokeTest(unittest.TestCase):
page, page_set.file_path))) page, page_set.file_path)))
def CheckAttributesOfPageSetBasicAttributes(self, page_set): def CheckAttributesOfPageSetBasicAttributes(self, page_set):
if page_set.file_path is not None: if page_set.base_dir is not None:
self.assertTrue( self.assertTrue(
isinstance(page_set.file_path, str), isinstance(page_set.base_dir, str),
msg='page_set %\'s file_path must have type string') msg='page_set %\'s base_dir must have type string')
self.assertTrue( self.assertTrue(
isinstance(page_set.archive_data_file, str), isinstance(page_set.archive_data_file, str),
......
...@@ -17,7 +17,7 @@ class UserStorySet(object): ...@@ -17,7 +17,7 @@ class UserStorySet(object):
""" """
def __init__(self, archive_data_file='', cloud_storage_bucket=None, def __init__(self, archive_data_file='', cloud_storage_bucket=None,
serving_dirs=None): base_dir=None, serving_dirs=None):
"""Creates a new UserStorySet. """Creates a new UserStorySet.
Args: Args:
...@@ -33,11 +33,21 @@ class UserStorySet(object): ...@@ -33,11 +33,21 @@ class UserStorySet(object):
self._wpr_archive_info = None self._wpr_archive_info = None
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__)) if base_dir:
if not os.path.isdir(base_dir):
raise ValueError('Must provide valid directory path for base_dir.')
self._base_dir = base_dir
else:
self._base_dir = os.path.dirname(inspect.getfile(self.__class__))
# Convert any relative serving_dirs to absolute paths. # Convert any relative serving_dirs to absolute paths.
self._serving_dirs = set(os.path.realpath(os.path.join(self.base_dir, d)) self._serving_dirs = set(os.path.realpath(os.path.join(self.base_dir, d))
for d in serving_dirs or []) for d in serving_dirs or [])
@property
def file_path(self):
return inspect.getfile(self.__class__).replace('.pyc', '.py')
@property @property
def base_dir(self): def base_dir(self):
"""The base directory to resolve archive_data_file. """The base directory to resolve archive_data_file.
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
import inspect
import os import os
import unittest import unittest
...@@ -14,6 +15,11 @@ class UserStorySetFoo(user_story_set.UserStorySet): ...@@ -14,6 +15,11 @@ class UserStorySetFoo(user_story_set.UserStorySet):
pass pass
class MockUserStorySet(user_story_set.UserStorySet):
def __init__(self):
super(MockUserStorySet, self).__init__()
class UserStorySetTest(unittest.TestCase): class UserStorySetTest(unittest.TestCase):
def testUserStoryTestName(self): def testUserStoryTestName(self):
...@@ -30,6 +36,11 @@ class UserStorySetTest(unittest.TestCase): ...@@ -30,6 +36,11 @@ class UserStorySetTest(unittest.TestCase):
self.assertTrue(os.path.isdir(base_dir)) self.assertTrue(os.path.isdir(base_dir))
self.assertEqual(base_dir, os.path.dirname(__file__)) self.assertEqual(base_dir, os.path.dirname(__file__))
def testFilePath(self):
uss = MockUserStorySet()
self.assertEqual(os.path.abspath(__file__),
uss.file_path)
def testCloudBucket(self): def testCloudBucket(self):
blank_uss = user_story_set.UserStorySet() blank_uss = user_story_set.UserStorySet()
self.assertEqual(blank_uss.bucket, None) self.assertEqual(blank_uss.bucket, None)
......
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