From e290abc29cc399a0ed2ca9470c3f2e5eb1170c34 Mon Sep 17 00:00:00 2001 From: Jimmy Reichley Date: Wed, 24 Oct 2018 17:17:02 -0400 Subject: [PATCH] fix(schematics): dont let bad .nx-results file prevent test run If the .nx-results file got into a bad state, then it caused CLI commands to fail in a cryptic way due to yargs failing JSON parsing. This included having an empty .nx-results file. Now we treat this case as if there was no results file in the first place. --- .../command-line/workspace-results.spec.ts | 11 ++++++++++ .../src/command-line/workspace-results.ts | 20 +++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/schematics/src/command-line/workspace-results.spec.ts b/packages/schematics/src/command-line/workspace-results.spec.ts index a063c0b15b78c..5481728667f02 100644 --- a/packages/schematics/src/command-line/workspace-results.spec.ts +++ b/packages/schematics/src/command-line/workspace-results.spec.ts @@ -127,6 +127,17 @@ describe('WorkspacesResults', () => { expect(results.getResult('proj')).toBe(false); }); + it('should handle a corrupted results file', () => { + spyOn(fs, 'readFileSync').and.returnValue('invalid json'); + + const runTests = () => { + results = new WorkspaceResults('test'); + }; + + expect(runTests).not.toThrow(); + expect((results).startedWithFailedProjects).toBeFalsy(); + }); + it('should not read the existing results when the previous command was different', () => { spyOn(fs, 'readFileSync').and.returnValue( serializeJson({ diff --git a/packages/schematics/src/command-line/workspace-results.ts b/packages/schematics/src/command-line/workspace-results.ts index 0ad0956da5750..b8806619369d8 100644 --- a/packages/schematics/src/command-line/workspace-results.ts +++ b/packages/schematics/src/command-line/workspace-results.ts @@ -29,14 +29,18 @@ export class WorkspaceResults { constructor(private command: string) { const resultsExists = fs.existsSync(RESULTS_FILE); - if (!resultsExists) { - this.startedWithFailedProjects = false; - } else { - const commandResults = readJsonFile(RESULTS_FILE); - this.startedWithFailedProjects = commandResults.command === command; - - if (this.startedWithFailedProjects) { - this.commandResults = commandResults; + this.startedWithFailedProjects = false; + if (resultsExists) { + try { + const commandResults = readJsonFile(RESULTS_FILE); + this.startedWithFailedProjects = commandResults.command === command; + if (this.startedWithFailedProjects) { + this.commandResults = commandResults; + } + } catch (e) { + // RESULTS_FILE is likely not valid JSON + console.error('Error: .nx-results file is corrupted.'); + console.error(e); } } }