Skip to content

Commit

Permalink
fix(schematics): dont let bad .nx-results file prevent test run
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Jimmy Reichley authored and vsavkin committed Dec 3, 2018
1 parent 973d33b commit e290abc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
11 changes: 11 additions & 0 deletions packages/schematics/src/command-line/workspace-results.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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((<any>results).startedWithFailedProjects).toBeFalsy();
});

it('should not read the existing results when the previous command was different', () => {
spyOn(fs, 'readFileSync').and.returnValue(
serializeJson({
Expand Down
20 changes: 12 additions & 8 deletions packages/schematics/src/command-line/workspace-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit e290abc

Please sign in to comment.