-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
extract the reporter out of integrity checker #3248
Merged
bestander
merged 8 commits into
yarnpkg:master
from
voxsim:extract-reporter-from-integrity-checker
Apr 26, 2017
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b2e7cd6
extract the reporter out of integrity checker
voxsim 0815eb0
use warning for messages from integrity-checker in check command
voxsim 543dc0d
add tests for integrity when integrity file is missing or is not a json
voxsim 0b06e74
add assert on pre-existent tests
voxsim 10abb6f
add test for integrity failed for linked modules
voxsim 59f0aee
delete warning for integrity failed for patterns because we return be…
voxsim 731e204
lint the code correctly
voxsim 02b46c6
use flow enum, and remove every Promise.resolve in integrity-checker
voxsim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
import type Config from './config.js'; | ||
import type {LockManifest} from './lockfile/wrapper.js'; | ||
import type {RegistryNames} from './registries/index.js'; | ||
import type {Reporter} from './reporters/index.js'; | ||
import * as constants from './constants.js'; | ||
import {registryNames} from './registries/index.js'; | ||
import * as fs from './util/fs.js'; | ||
|
@@ -47,15 +46,11 @@ type IntegrityFlags = { | |
export default class InstallationIntegrityChecker { | ||
constructor( | ||
config: Config, | ||
reporter: Reporter, | ||
) { | ||
this.config = config; | ||
this.reporter = reporter; | ||
} | ||
|
||
config: Config; | ||
reporter: Reporter; | ||
|
||
|
||
/** | ||
* Get the location of an existing integrity hash. If none exists then return the location where we should | ||
|
@@ -167,32 +162,58 @@ export default class InstallationIntegrityChecker { | |
return result; | ||
} | ||
|
||
_compareIntegrityFiles(actual: IntegrityFile, expected: IntegrityFile): boolean { | ||
if (!compareSortedArrays(actual.linkedModules, expected.linkedModules)) { | ||
this.reporter.warn(this.reporter.lang('integrityCheckLinkedModulesDontMatch')); | ||
return false; | ||
async _getIntegrityFile(locationPath: string): Promise<?IntegrityFile> { | ||
const expectedRaw = await fs.readFile(locationPath); | ||
try { | ||
return JSON.parse(expectedRaw); | ||
} catch (e) { | ||
// ignore JSON parsing for legacy text integrity files compatibility | ||
} | ||
if (!compareSortedArrays(actual.topLevelPatters, expected.topLevelPatters)) { | ||
this.reporter.warn(this.reporter.lang('integrityPatternsDontMatch')); | ||
return false; | ||
return null; | ||
} | ||
|
||
async _compareIntegrityFiles( | ||
actual: IntegrityFile, | ||
expected: ?IntegrityFile, | ||
checkFiles: boolean, | ||
locationFolder: string): Promise<string> { | ||
if (!expected) { | ||
return Promise.resolve('EXPECTED_IS_NOT_A_JSON'); | ||
} | ||
if (!compareSortedArrays(actual.linkedModules, expected.linkedModules)) { | ||
return Promise.resolve('LINKED_MODULES_DONT_MATCH'); | ||
} | ||
if (!compareSortedArrays(actual.flags, expected.flags)) { | ||
this.reporter.warn(this.reporter.lang('integrityFlagsDontMatch')); | ||
return false; | ||
return Promise.resolve('FLAGS_DONT_MATCH'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should all these errors be a reject rather than resolve? |
||
} | ||
for (const key of Object.keys(actual.lockfileEntries)) { | ||
if (actual.lockfileEntries[key] !== expected.lockfileEntries[key]) { | ||
this.reporter.warn(this.reporter.lang('integrityLockfilesDontMatch')); | ||
return false; | ||
return Promise.resolve('LOCKFILE_DONT_MATCH'); | ||
} | ||
} | ||
for (const key of Object.keys(expected.lockfileEntries)) { | ||
if (actual.lockfileEntries[key] !== expected.lockfileEntries[key]) { | ||
this.reporter.warn(this.reporter.lang('integrityLockfilesDontMatch')); | ||
return false; | ||
return Promise.resolve('LOCKFILE_DONT_MATCH'); | ||
} | ||
} | ||
if (checkFiles) { | ||
if (expected.files.length === 0) { | ||
// edge case handling - --check-fies is passed but .yarn-integrity does not contain any files | ||
// check and fail if there are file in node_modules after all. | ||
const actualFiles = await this._getFilesDeep(locationFolder); | ||
if (actualFiles.length > 0) { | ||
return Promise.resolve('FILES_MISSING'); | ||
} | ||
} else { | ||
// TODO we may want to optimise this check by checking only for package.json files on very large trees | ||
for (const file of expected.files) { | ||
if (!await fs.exists(path.join(locationFolder, file))) { | ||
return Promise.resolve('FILES_MISSING'); | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
return Promise.resolve('OK'); | ||
} | ||
|
||
async check( | ||
|
@@ -214,41 +235,13 @@ export default class InstallationIntegrityChecker { | |
patterns, | ||
Object.assign({}, {checkFiles: false}, flags), // don't generate files when checking, we check the files below | ||
loc.locationFolder); | ||
const expectedRaw = await fs.readFile(loc.locationPath); | ||
let expected: ?IntegrityFile; | ||
try { | ||
expected = JSON.parse(expectedRaw); | ||
} catch (e) { | ||
// ignore JSON parsing for legacy text integrity files compatibility | ||
} | ||
let integrityMatches; | ||
if (expected) { | ||
integrityMatches = this._compareIntegrityFiles(actual, expected); | ||
if (flags.checkFiles && expected.files.length === 0) { | ||
// edge case handling - --check-fies is passed but .yarn-integrity does not contain any files | ||
// check and fail if there are file in node_modules after all. | ||
const actualFiles = await this._getFilesDeep(loc.locationFolder); | ||
if (actualFiles.length > 0) { | ||
this.reporter.warn(this.reporter.lang('integrityFailedFilesMissing')); | ||
integrityMatches = false; | ||
} | ||
} else if (flags.checkFiles && expected.files.length > 0) { | ||
// TODO we may want to optimise this check by checking only for package.json files on very large trees | ||
for (const file of expected.files) { | ||
if (!await fs.exists(path.join(loc.locationFolder, file))) { | ||
this.reporter.warn(this.reporter.lang('integrityFailedFilesMissing')); | ||
integrityMatches = false; | ||
break; | ||
} | ||
} | ||
} | ||
} else { | ||
integrityMatches = false; | ||
} | ||
const expected = await this._getIntegrityFile(loc.locationPath); | ||
const integrityMatches = await this._compareIntegrityFiles(actual, expected, flags.checkFiles, loc.locationFolder); | ||
|
||
return { | ||
integrityFileMissing: false, | ||
integrityMatches, | ||
integrityMatches: integrityMatches === 'OK', | ||
whyIntegrityMatchesFailed: integrityMatches, | ||
missingPatterns, | ||
}; | ||
} | ||
|
@@ -295,5 +288,4 @@ export default class InstallationIntegrityChecker { | |
await fs.unlink(loc.locationPath); | ||
} | ||
} | ||
|
||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think flow enums would be a better fit here