Skip to content

Commit

Permalink
Handle broken resource mapping files (by throwing exceptions).
Browse files Browse the repository at this point in the history
Testing Done:
Ran new tests locally.

CI is green: https://travis-ci.org/pantsbuild/pants/builds/57559852

Reviewed at https://rbcommons.com/s/twitter/r/2038/
  • Loading branch information
dturner-tw committed Apr 7, 2015
1 parent 14886c5 commit 35069b8
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/python/pants/backend/jvm/tasks/jvm_compile/resource_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@
class ResourceMapping(object):
RESOURCES_BY_CLASS_NAME_RE = re.compile(r'^(?P<classname>[\w+.\$]+) -> (?P<path>.+)$')

class ResourceMappingFormatException(Exception):
pass

class MissingItemsLineException(ResourceMappingFormatException):
pass

class TooLongFileException(ResourceMappingFormatException):
pass

class TruncatedFileException(ResourceMappingFormatException):
pass

class UnparseableLineException(ResourceMappingFormatException):
pass

def __init__(self, classes_dir):
self._classes_dir = classes_dir
self._resource_mappings = None
Expand All @@ -24,7 +39,7 @@ def parse_items(line):
n, items = line.split(" ")
return int(n)
except ValueError as error:
raise ValueError(dedent('''
raise self.MissingItemsLineException(dedent('''
Unable to parse resource mappings.
Expected "N items", got "{line}: {error}"'''.format(line=line, error=error)))

Expand Down Expand Up @@ -52,11 +67,19 @@ def parse_items(line):
items_left -= 1
match = ResourceMapping.RESOURCES_BY_CLASS_NAME_RE.match(line)
if not match:
raise ValueError(dedent('''
raise self.UnparseableLineException(dedent('''
Unable to parse resource mappings.
Expected classname -> path, got "{line}"'''.format(line=line)))
classname, path = match.group('classname'), match.group('path')
mappings[classname].append(path)
else:
raise self.TooLongFileException('Unexpected line "{line}" in section {section}.'.format(
line=line, section=section))

if items_left:
raise self.TruncatedFileException(dedent('''
Unable to parse resource mappings.
Found EOF while still missing {items_left} lines'''.format(items_left=items_left)))

@property
def mappings(self):
Expand Down
11 changes: 11 additions & 0 deletions tests/python/pants_test/backend/jvm/tasks/jvm_compile/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ target(
name='jvm_compile',
dependencies=[
':jvm_fingerprint_strategy',
':resource_mapping',
':zinc_utils',
'tests/python/pants_test/backend/jvm/tasks/jvm_compile/java',
'tests/python/pants_test/backend/jvm/tasks/jvm_compile/scala',
Expand All @@ -21,6 +22,16 @@ python_tests(
]
)

python_tests(
name = 'resource_mapping',
sources = ['test_resource_mapping.py'],
dependencies = [
'src/python/pants/backend/jvm/tasks/jvm_compile:resource_mapping',
'tests/python/pants_test:base_test',
],
resources=rglobs('test-data/*'),
)

python_tests(
name = 'zinc_utils',
sources = ['test_zinc_utils.py'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resources by class name:
1 items
org.pantsbuild.example.Morx -> /home/user/project/.pants.d/compile/jvm/java/classes/org/pantsbuild/example/resource1.txt
#comment
org.pantsbuild.example.Fleem -> /home/user/project/.pants.d/compile/jvm/java/classes/org/pantsbuild/example/resource1.txt
#comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resources by class name:
2 items
org.pantsbuild.example.Morx -> /home/user/project/.pants.d/compile/jvm/java/classes/org/pantsbuild/example/resource1.txt
org.pantsbuild.example.Fleem NO CARRIER
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resources by class name:
nonsense
org.pantsbuild.example.Morx -> /home/user/project/.pants.d/compile/jvm/java/classes/org/pantsbuild/example/resource1.txt
#comment
org.pantsbuild.example.Fleem -> /home/user/project/.pants.d/compile/jvm/java/classes/org/pantsbuild/example/resource1.txt
#comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resources by class name:
2 items
org.pantsbuild.example.Morx -> /home/user/project/.pants.d/compile/jvm/java/classes/org/pantsbuild/example/resource1.txt
#comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resources by class name:
2 items
org.pantsbuild.example.Morx -> /home/user/project/.pants.d/compile/jvm/java/classes/org/pantsbuild/example/resource1.txt
#comment
org.pantsbuild.example.Fleem -> /home/user/project/.pants.d/compile/jvm/java/classes/org/pantsbuild/example/resource1.txt
#comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# coding=utf-8
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

import os

from pants.backend.jvm.tasks.jvm_compile.resource_mapping import ResourceMapping
from pants_test.base_test import BaseTest


class ResourceMappingTest(BaseTest):
def test_resource_mapping_ok(self):
rel_dir = 'tests/python/pants_test/backend/jvm/tasks/jvm_compile/test-data/resource_mapping'
resource_mapping = ResourceMapping(rel_dir)

self.assertEquals(2, len(resource_mapping.mappings))

def test_resource_mapping_short(self):
rel_dir = 'tests/python/pants_test/backend/jvm/tasks/jvm_compile/test-data/resource_mapping-broken-short'
resource_mapping = ResourceMapping(rel_dir)

with self.assertRaises(ResourceMapping.TruncatedFileException):
resource_mapping.mappings

def test_resource_mapping_long(self):
rel_dir = 'tests/python/pants_test/backend/jvm/tasks/jvm_compile/test-data/resource_mapping-broken-long'
resource_mapping = ResourceMapping(rel_dir)

with self.assertRaises(ResourceMapping.TooLongFileException):
resource_mapping.mappings

def test_resource_mapping_mangled(self):
rel_dir = 'tests/python/pants_test/backend/jvm/tasks/jvm_compile/test-data/resource_mapping-broken-mangled'
resource_mapping = ResourceMapping(rel_dir)

with self.assertRaises(ResourceMapping.UnparseableLineException):
resource_mapping.mappings


def test_resource_mapping_noitems(self):
rel_dir = 'tests/python/pants_test/backend/jvm/tasks/jvm_compile/test-data/resource_mapping-broken-missing-items'
resource_mapping = ResourceMapping(rel_dir)

with self.assertRaises(ResourceMapping.MissingItemsLineException):
resource_mapping.mappings

0 comments on commit 35069b8

Please sign in to comment.