[gjs/gnome-3-26] coverage: Fix coverage prefixes



commit 6ce9e420d053151198fb8c94be8bc0889b49088a
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Sep 30 00:25:15 2017 -0700

    coverage: Fix coverage prefixes
    
    Filename prefixes passed to gjs_coverage_new() were ignored. This
    implements the expected functionality. Coverage is only recorded for JS
    files which are prefixed with one of the specified prefixes.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=788166

 installed-tests/js/testCoverage.js | 64 ++++++++++++++++++++++++--------------
 modules/coverage.js                |  5 +++
 2 files changed, 45 insertions(+), 24 deletions(-)
---
diff --git a/installed-tests/js/testCoverage.js b/installed-tests/js/testCoverage.js
index 8fa4e9f..54deb3b 100644
--- a/installed-tests/js/testCoverage.js
+++ b/installed-tests/js/testCoverage.js
@@ -1044,21 +1044,30 @@ describe('Coverage.incrementFunctionCounters', function () {
 
 describe('Coverage statistics container', function () {
     const MockFiles = {
-        'filename': "function f() {\n" +
-                    "    return 1;\n" +
-                    "}\n" +
-                    "if (f())\n" +
-                    "    f = 0;\n" +
-                    "\n",
-        'uncached': "function f() {\n" +
-                    "    return 1;\n" +
-                    '}\n',
-        'shebang': `#!/usr/bin/env gjs
+        'prefix/filename':
+            `function f() {
+                return 1;
+            }
+            if (f())
+                f = 0;
+            `,
+        'prefix/uncached':
+            `function f() {
+                return 1;
+            }
+            `,
+        'unprefixed':
+            `function f() {
+                return 1;
+            }
+            `,
+        'prefix/shebang':
+            `#!/usr/bin/env gjs
             function f() {}
             `,
     };
 
-    const MockFilenames = Object.keys(MockFiles).concat(['nonexistent']);
+    const MockFilenames = Object.keys(MockFiles).concat(['prefix/nonexistent']);
 
     let container;
 
@@ -1069,31 +1078,38 @@ describe('Coverage statistics container', function () {
             jasmine.createSpy('getFileChecksum').and.returnValue('abcd');
         Coverage.getFileModificationTime =
             jasmine.createSpy('getFileModificationTime').and.returnValue([1, 2]);
-        container = new Coverage.CoverageStatisticsContainer(MockFilenames);
+        container = new Coverage.CoverageStatisticsContainer(['prefix/']);
     });
 
     it('fetches valid statistics for file', function () {
-        let statistics = container.fetchStatistics('filename');
+        let statistics = container.fetchStatistics('prefix/filename');
         expect(statistics).toBeDefined();
 
         let files = container.getCoveredFiles();
-        expect(files).toEqual(['filename']);
+        expect(files).toEqual(['prefix/filename']);
     });
 
     it('throws for nonexisting file', function () {
-        expect(() => container.fetchStatistics('nonexistent')).toThrow();
+        expect(() => container.fetchStatistics('prefix/nonexistent')).toThrow();
     });
 
     it('handles a shebang on line 1', function () {
-        expect(() => container.fetchStatistics('shebang')).not.toThrow();
+        let statistics = container.fetchStatistics('prefix/shebang');
+        expect(statistics).toBeDefined();
     });
 
     it('ignores a file in angle brackets (our convention for programmatic scripts)', function () {
-        expect(() => container.fetchStatistics('<script>')).not.toThrow();
+        let statistics = container.fetchStatistics('<script>');
+        expect(statistics).not.toBeDefined();
+    });
+
+    it('ignores a file without the specified prefix', function () {
+        let statistics = container.fetchStatistics('unprefixed');
+        expect(statistics).not.toBeDefined();
     });
 
     const MockCache = '{ \
-        "filename": { \
+        "prefix/filename": { \
             "mtime": [1, 2], \
             "checksum": null, \
             "lines": [2, 4, 5], \
@@ -1121,22 +1137,22 @@ describe('Coverage statistics container', function () {
         });
 
         it('fetches counters from cache', function () {
-            container.fetchStatistics('filename');
+            container.fetchStatistics('prefix/filename');
             expect(Coverage._fetchCountersFromReflection).not.toHaveBeenCalled();
         });
 
         it('fetches counters from reflection if missed', function () {
-            container.fetchStatistics('uncached');
+            container.fetchStatistics('prefix/uncached');
             expect(Coverage._fetchCountersFromReflection).toHaveBeenCalled();
         });
 
         it('cache is not stale if all hit', function () {
-            container.fetchStatistics('filename');
+            container.fetchStatistics('prefix/filename');
             expect(container.staleCache()).toBeFalsy();
         });
 
         it('cache is stale if missed', function () {
-            container.fetchStatistics('uncached');
+            container.fetchStatistics('prefix/uncached');
             expect(container.staleCache()).toBeTruthy();
         });
     });
@@ -1147,10 +1163,10 @@ describe('Coverage statistics container', function () {
         beforeEach(function () {
             container = new Coverage.CoverageStatisticsContainer(MockFilenames,
                                                                  MockCache);
-            statistics = container.fetchStatistics('filename');
+            statistics = container.fetchStatistics('prefix/filename');
 
             containerWithNoCaching = new Coverage.CoverageStatisticsContainer(MockFilenames);
-            statisticsWithNoCaching = containerWithNoCaching.fetchStatistics('filename');
+            statisticsWithNoCaching = containerWithNoCaching.fetchStatistics('prefix/filename');
         });
 
         it('have same executable lines as reflection', function () {
diff --git a/modules/coverage.js b/modules/coverage.js
index 4365899..5c985f7 100644
--- a/modules/coverage.js
+++ b/modules/coverage.js
@@ -793,6 +793,7 @@ function _fetchCountersFromReflection(filename, contents, nLines) {
 function CoverageStatisticsContainer(prefixes, cache) {
     /* Copy the files array, so that it can be re-used in the tests */
     let cachedASTs = cache ? JSON.parse(cache) : null;
+    let _prefixes = prefixes;
     let coveredFiles = {};
     let cacheMisses = 0;
 
@@ -819,6 +820,10 @@ function CoverageStatisticsContainer(prefixes, cache) {
         // Skip scripts fed to JS engine programmatically.
         if (filename.startsWith('<') && filename.endsWith('>'))
             return undefined;
+
+        if (!_prefixes.some(prefix => filename.startsWith(prefix)))
+            return undefined;
+
         if (!coveredFiles[filename])
             coveredFiles[filename] = createStatisticsFor(filename);
         return coveredFiles[filename];


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]