Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New audit: using deprecated APIs (#1470)
- Loading branch information
1 parent
56c9bd3
commit 3c8f57c
Showing
7 changed files
with
294 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
/** | ||
* @fileoverview Audits a page to determine if it is calling deprecated APIs. | ||
* This is done by collecting console log messages and filtering them by ones | ||
* that contain deprecated API warnings sent by Chrome. | ||
*/ | ||
|
||
const Audit = require('./audit'); | ||
const Formatter = require('../formatters/formatter'); | ||
|
||
class Deprecations extends Audit { | ||
/** | ||
* @return {!AuditMeta} | ||
*/ | ||
static get meta() { | ||
return { | ||
category: 'Deprecations', | ||
name: 'deprecations', | ||
description: 'Site does not use deprecated APIs', | ||
helpText: 'We found some uses of deprecated APIs. Please consider migrating ' + | ||
'to a newer option. [Learn more](https://www.chromestatus.com/features#deprecated).', | ||
requiredArtifacts: ['ChromeConsoleMessages'] | ||
}; | ||
} | ||
|
||
/** | ||
* @param {!Artifacts} artifacts | ||
* @return {!AuditResult} | ||
*/ | ||
static audit(artifacts) { | ||
if (artifacts.ChromeConsoleMessages.rawValue === -1) { | ||
return Deprecations.generateAuditResult(artifacts.ChromeConsoleMessages); | ||
} | ||
|
||
const entries = artifacts.ChromeConsoleMessages; | ||
|
||
const deprecations = entries.filter(log => log.entry.source === 'deprecation') | ||
.map(log => { | ||
// CSS deprecations can have missing URLs and lineNumbers. See https://crbug.com/680832. | ||
const label = log.entry.lineNumber ? `line: ${log.entry.lineNumber}` : 'line: ???'; | ||
const url = log.entry.url || 'Unable to determine URL'; | ||
return Object.assign({ | ||
label, | ||
url, | ||
code: log.entry.text | ||
}, log.entry); | ||
}); | ||
|
||
let displayValue = ''; | ||
if (deprecations.length > 1) { | ||
displayValue = `${deprecations.length} warnings found`; | ||
} else if (deprecations.length === 1) { | ||
displayValue = `${deprecations.length} warning found`; | ||
} | ||
|
||
return Deprecations.generateAuditResult({ | ||
rawValue: deprecations.length === 0, | ||
displayValue, | ||
extendedInfo: { | ||
formatter: Formatter.SUPPORTED_FORMATS.URLLIST, | ||
value: deprecations | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports = Deprecations; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
lighthouse-core/gather/gatherers/chrome-console-messages.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @fileoverview Gathers console deprecation and intervention warnings logged by Chrome. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Gatherer = require('./gatherer'); | ||
|
||
class ChromeConsoleMessages extends Gatherer { | ||
|
||
constructor() { | ||
super(); | ||
this._logEntries = []; | ||
this._onConsoleEntryAdded = this.onConsoleEntry.bind(this); | ||
} | ||
|
||
onConsoleEntry(entry) { | ||
this._logEntries.push(entry); | ||
} | ||
|
||
beforePass(options) { | ||
options.driver.on('Log.entryAdded', this._onConsoleEntryAdded); | ||
return options.driver.sendCommand('Log.enable'); | ||
} | ||
|
||
afterPass(options) { | ||
options.driver.off('Log.entryAdded', this._onConsoleEntryAdded); | ||
return options.driver.sendCommand('Log.disable') | ||
.then(_ => this._logEntries) | ||
.catch(err => { | ||
return { | ||
rawValue: -1, | ||
debugString: err.message | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = ChromeConsoleMessages; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const DeprecationsAudit = require('../../audits/deprecations.js'); | ||
const assert = require('assert'); | ||
|
||
/* eslint-env mocha */ | ||
|
||
describe('Console deprecations audit', () => { | ||
it('fails when gather fails', () => { | ||
const debugString = 'the uniquest debug string'; | ||
const auditResult = DeprecationsAudit.audit({ | ||
ChromeConsoleMessages: { | ||
rawValue: -1, | ||
debugString | ||
} | ||
}); | ||
assert.equal(auditResult.rawValue, -1); | ||
assert.strictEqual(auditResult.debugString, debugString); | ||
}); | ||
|
||
it('passes when no console messages were found', () => { | ||
const auditResult = DeprecationsAudit.audit({ | ||
ChromeConsoleMessages: [] | ||
}); | ||
assert.equal(auditResult.rawValue, true); | ||
assert.ok(!auditResult.debugString); | ||
assert.equal(auditResult.extendedInfo.value.length, 0); | ||
}); | ||
|
||
it('handles deprecations that do not have url or line numbers', () => { | ||
const auditResult = DeprecationsAudit.audit({ | ||
ChromeConsoleMessages: [ | ||
{ | ||
entry: { | ||
source: 'deprecation', | ||
text: 'Deprecation message' | ||
} | ||
} | ||
] | ||
}); | ||
assert.equal(auditResult.rawValue, false); | ||
assert.equal(auditResult.displayValue, '1 warning found'); | ||
assert.equal(auditResult.extendedInfo.value.length, 1); | ||
assert.equal(auditResult.extendedInfo.value[0].url, 'Unable to determine URL'); | ||
assert.equal(auditResult.extendedInfo.value[0].label, 'line: ???'); | ||
}); | ||
|
||
it('fails when deprecation messages are found', () => { | ||
const URL = 'http://example.com'; | ||
|
||
const auditResult = DeprecationsAudit.audit({ | ||
ChromeConsoleMessages: [ | ||
{ | ||
entry: { | ||
source: 'deprecation', | ||
lineNumber: 123, | ||
url: URL, | ||
text: 'Deprecation message 123' | ||
} | ||
}, { | ||
entry: { | ||
source: 'deprecation', | ||
lineNumber: 456, | ||
url: 'http://example2.com', | ||
text: 'Deprecation message 456' | ||
} | ||
}, { | ||
entry: { | ||
source: 'somethingelse', | ||
lineNumber: 789, | ||
url: 'http://example3.com', | ||
text: 'Not a deprecation message 456' | ||
} | ||
} | ||
] | ||
}); | ||
assert.equal(auditResult.rawValue, false); | ||
assert.equal(auditResult.displayValue, '2 warnings found'); | ||
assert.equal(auditResult.extendedInfo.value.length, 2); | ||
assert.equal(auditResult.extendedInfo.value[0].url, URL); | ||
assert.equal(auditResult.extendedInfo.value[0].label, 'line: 123'); | ||
}); | ||
}); |