Skip to content

Commit

Permalink
Merge pull request #95 from w3c/test-sourcefile
Browse files Browse the repository at this point in the history
r=gsnedders
  • Loading branch information
gsnedders authored Aug 4, 2016
2 parents 494b6a8 + f6183c0 commit 0b15820
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 17 deletions.
33 changes: 25 additions & 8 deletions manifest/sourcefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from . import vcs
from .item import Stub, ManualTest, WebdriverSpecTest, RefTest, TestharnessTest
from .utils import rel_path_to_url, is_blacklisted, ContextManagerStringIO, cached_property
from .utils import rel_path_to_url, is_blacklisted, ContextManagerBytesIO, cached_property

wd_pattern = "*.py"

Expand All @@ -32,20 +32,25 @@ class SourceFile(object):
"xhtml":ElementTree.parse,
"svg":ElementTree.parse}

def __init__(self, tests_root, rel_path, url_base, use_committed=False):
def __init__(self, tests_root, rel_path, url_base, use_committed=False,
contents=None):
"""Object representing a file in a source tree.
:param tests_root: Path to the root of the source tree
:param rel_path: File path relative to tests_root
:param url_base: Base URL used when converting file paths to urls
:param use_committed: Work with the last committed version of the file
rather than the on-disk version.
:param contents: Byte array of the contents of the file or ``None``.
"""

assert not (use_committed and contents)

self.tests_root = tests_root
self.rel_path = rel_path
self.url_base = url_base
self.use_committed = use_committed
self.contents = contents

self.url = rel_path_to_url(rel_path, url_base)
self.path = os.path.join(tests_root, rel_path)
Expand Down Expand Up @@ -77,15 +82,27 @@ def name_prefix(self, prefix):
:param prefix: The prefix to check"""
return self.name.startswith(prefix)

def is_dir(self):
"""Return whether this file represents a directory."""
if self.contents:
return False

return os.path.isdir(self.rel_path)

def open(self):
"""Return a File object opened for reading the file contents,
or the contents of the file when last committed, if
use_comitted is true."""
"""
Return either
* the contents specified in the constructor, if any;
* the contents of the file when last committed, if use_committed is true; or
* a File object opened for reading the file contents.
"""

if self.use_committed:
if self.contents:
file_obj = ContextManagerBytesIO(self.contents)
elif self.use_committed:
git = vcs.get_git_func(os.path.dirname(__file__))
blob = git("show", "HEAD:%s" % self.rel_path)
file_obj = ContextManagerStringIO(blob)
file_obj = ContextManagerBytesIO(blob)
else:
file_obj = open(self.path, 'rb')
return file_obj
Expand All @@ -94,7 +111,7 @@ def open(self):
def name_is_non_test(self):
"""Check if the file name matches the conditions for the file to
be a non-test file"""
return (os.path.isdir(self.rel_path) or
return (self.is_dir() or
self.name_prefix("MANIFEST") or
self.filename.startswith(".") or
is_blacklisted(self.url))
Expand Down
7 changes: 0 additions & 7 deletions manifest/tests/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,3 @@ def test_manifest_to_json():
assert list(loaded) == list(m)

assert loaded.to_json() == json_str

def test_multi_global():
s = sourcefile.SourceFile("/", "test.any.js", "/")
assert s.name_is_multi_global
assert not s.name_is_manual
assert not s.name_is_reference
assert not s.name_is_worker
94 changes: 94 additions & 0 deletions manifest/tests/test_sourcefile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from ..sourcefile import SourceFile

def create(filename, contents=b""):
return SourceFile("/", filename, "/", contents=contents)


def items(s):
return [
(item.item_type, item.url)
for item in s.manifest_items()
]


def test_name_is_non_test():
non_tests = [
".gitignore",
".travis.yml",
"MANIFEST.json",
"tools/test.html",
"resources/test.html",
"common/test.html",
"conformance-checkers/test.html",
]

for rel_path in non_tests:
s = create(rel_path)
assert s.name_is_non_test

assert items(s) == []


def test_name_is_manual():
manual_tests = [
"html/test-manual.html",
"html/test-manual.xhtml",
]

for rel_path in manual_tests:
s = create(rel_path)
assert not s.name_is_non_test
assert s.name_is_manual

assert items(s) == [("manual", "/" + rel_path)]


def test_worker():
s = create("html/test.worker.js")
assert not s.name_is_non_test
assert not s.name_is_manual
assert not s.name_is_multi_global
assert s.name_is_worker
assert not s.name_is_reference

assert items(s) == [("testharness", "/html/test.worker")]


def test_multi_global():
s = create("html/test.any.js")
assert not s.name_is_non_test
assert not s.name_is_manual
assert s.name_is_multi_global
assert not s.name_is_worker
assert not s.name_is_reference

assert items(s) == [
("testharness", "/html/test.any.html"),
("testharness", "/html/test.any.worker"),
]


def test_testharness():
s = create("html/test.html", b"<script src=/resources/testharness.js></script>")
assert not s.name_is_non_test
assert not s.name_is_manual
assert not s.name_is_multi_global
assert not s.name_is_worker
assert not s.name_is_reference

assert s.content_is_testharness

assert items(s) == [("testharness", "/html/test.html")]


def test_relative_testharness():
s = create("html/test.html", b"<script src=../resources/testharness.js></script>")
assert not s.name_is_non_test
assert not s.name_is_manual
assert not s.name_is_multi_global
assert not s.name_is_worker
assert not s.name_is_reference

assert not s.content_is_testharness

assert items(s) == []
4 changes: 2 additions & 2 deletions manifest/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from six import StringIO
from six import BytesIO

blacklist = ["/", "/tools/", "/resources/", "/common/", "/conformance-checkers/", "_certs"]

Expand All @@ -26,7 +26,7 @@ def from_os_path(path):
def to_os_path(path):
return path.replace("/", os.path.sep)

class ContextManagerStringIO(StringIO):
class ContextManagerBytesIO(BytesIO):
def __enter__(self):
return self

Expand Down

0 comments on commit 0b15820

Please sign in to comment.