Skip to content

Commit

Permalink
Fix exception parsing invalid source map file.
Browse files Browse the repository at this point in the history
Fix exception dumping mappings when there's unmapped files.
print() exception information to indicate what files are invalid.

Tests for same.

Pause tests to enable attaching debugger. To do this do
    export PAUSETESTS=1
  • Loading branch information
danmoseley committed Aug 18, 2015
1 parent 6da58fe commit 2f01f0e
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 13 deletions.
20 changes: 11 additions & 9 deletions projectsystem/DocumentMapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def get_mapping(file_name):
@staticmethod
def get_all_source_file_mappings():
result = {}
for val in MappingsManager.source_file_mappings.values():
result[val.generated_file] = val.authored_sources
for key, val in MappingsManager.source_file_mappings.items():
result[key] = val.authored_sources

return result

Expand All @@ -92,14 +92,16 @@ class MappingInfo:

def __init__(self, generated_file):
source_map_file = Sourcemap.get_sourcemap_file(generated_file)
self.parsed_source_map = None
if len(source_map_file):
self.parsed_source_map = Sourcemap.ParsedSourceMap(source_map_file)
self.generated_file = generated_file
if not len(source_map_file):
return

if self.parsed_source_map:
self.authored_sources = self.parsed_source_map.get_authored_sources_path()
self.line_mappings = self.parsed_source_map.line_mappings
self.parsed_source_map = Sourcemap.ParsedSourceMap(source_map_file)
if not self.parsed_source_map.is_valid():
return

self.generated_file = generated_file
self.authored_sources = self.parsed_source_map.get_authored_sources_path()
self.line_mappings = self.parsed_source_map.line_mappings

def is_valid(self):
return len(self.line_mappings) > 0
Expand Down
11 changes: 8 additions & 3 deletions projectsystem/Sourcemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def get_sourcemap_file(file_name):
map_file = sourcemap_info[len(sourcemap_prefix):].strip()
map_file = os.path.dirname(file_name) + os.path.sep + map_file
f.close()
except:
except Exception as e:
print('Could not read %s: %s' % (file_name, str(e)))
pass

return map_file
Expand All @@ -32,11 +33,15 @@ def __init__(self, file_name):
self.version = self.content["version"]
self.authored_sources = self.content["sources"]
self.line_mappings = SourceMapParser.calculate_line_mappings(self.content)
except:
except Exception as e:
print('Could not read %s: %s' % (file_name, str(e)))
pass

def is_valid(self):
return not self.content is None

def get_authored_sources_path(self):
return [os.path.abspath(self.root_path + os.path.sep + x).lower() for x in self.authored_sources] if self.content else []
return [os.path.abspath(self.root_path + os.path.sep + x).lower() for x in self.authored_sources] if self.is_valid() else []


class LineMapping:
Expand Down
11 changes: 11 additions & 0 deletions tests/DocumentMappingTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def test_invalid_generated_file_does_not_throw(self):
mapping = DocumentMapping.MappingInfo("foobar.js")
self.assertFalse(mapping.is_valid())

def test_invalid_map_file_does_not_throw(self):
mapping = DocumentMapping.MappingInfo(assets_path + "garbage.js")
self.assertFalse(mapping.is_valid())

def test_valid_generated_file(self):
mapping = DocumentMapping.MappingInfo(assets_path + "app.js")
self.assertTrue(mapping.is_valid())
Expand Down Expand Up @@ -115,6 +119,13 @@ def test_create_invalid_mapping(self):

self.assertFalse(mapping.is_valid())

def test_get_all_source_file_mappings_invalid(self):
file_name = assets_path + "garbage.js"
DocumentMapping.MappingsManager.create_mapping(file_name)
result = DocumentMapping.MappingsManager.get_all_source_file_mappings()

self.assertEqual(len(result[file_name]), 0)

def test_create_valid_mapping(self):
file_name = assets_path + "app.js"
DocumentMapping.MappingsManager.create_mapping(file_name)
Expand Down
6 changes: 5 additions & 1 deletion tests/SourcemapTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ class SourceMapParserTests(unittest.TestCase):

map_version_3 = '{"version":3,"file":"HelloWorld.js","sourceRoot":"","sources":["HelloWorld.ts"],"names":["Printer","printer","Printer.printer"],"mappings":"AAAA,IAAO,OAAO;AAGb,CAHD,UAAO,OAAO;IACHA,QAAIA,OAAOA,GAAGA,OAAOA,CAACA,GAAGA;IAChCA,QAASA;AACbA,CAACA,6BAAA","x_ms_mediaTypes":["application/x.typescript;version=1.0.3.0"],"x_ms_compilerFlags":"--target ES3 --module commonjs","x_ms_scopes":"CU>GT<","x_ms_locals":"CC"}'

def test_invalid_file_does_not_throw(self):
def test_nonexistent_file_does_not_throw(self):
file_name = Sourcemap.get_sourcemap_file("NotExistentFile")
self.assertFalse(file_name)

def test_invalid_file_does_not_throw(self):
file_name = Sourcemap.get_sourcemap_file("app.js") # exists, but not valid map file
self.assertFalse(file_name)

def test_version_check(self):
line_mappings = Sourcemap.SourceMapParser.calculate_line_mappings(json.loads(self.map_version_2))
self.assertFalse(line_mappings)
Expand Down
4 changes: 4 additions & 0 deletions tests/TestRunner.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import SourcemapTests
import DocumentMappingTests
import unittest
import os

def registerAndRunTests(className):
print("Running tests for", className.__name__)
suite = unittest.TestLoader().loadTestsFromTestCase(className)
unittest.TextTestRunner(verbosity=2).run(suite)

if 'PAUSETESTS' in os.environ:
input("Press Enter to continue...")

# Source map parser tests
registerAndRunTests(SourcemapTests.SourceMapParserTests)
registerAndRunTests(SourcemapTests.ParsedSourceMapTests)
Expand Down
3 changes: 3 additions & 0 deletions tests/assets/garbage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/assets/garbage.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2f01f0e

Please sign in to comment.