-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #888 from CMSgov/QPPA-9863-update-logic-for-update…
…-program-names QPPA-9863: Move updateProgramNames logic to util directory
- Loading branch information
Showing
4 changed files
with
108 additions
and
90 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import fs from 'fs'; | ||
import fse from 'fs-extra'; | ||
import { vol } from 'memfs'; | ||
import * as updateProgramNames from './update-program-names'; | ||
import { mockMvpJson } from '../index.spec'; | ||
|
||
describe("updateProgramNames", () => { | ||
it("updates the program-names json with any newly-found programs.", () => { | ||
const writeSpy = jest | ||
.spyOn(fse, "writeFileSync") | ||
.mockImplementation(jest.fn()); | ||
vol.fromNestedJSON({ | ||
"util/program-names": { | ||
"program-names.json": JSON.stringify({ | ||
mips: "mips", | ||
cpcPlus: "cpcPlus", | ||
pcf: "pcf", | ||
app1: "app1", | ||
DEFAULT: "mips", | ||
G0053: "G0053", | ||
}), | ||
}, | ||
"mvp/2024": { | ||
"mvp.json": JSON.stringify(mockMvpJson), | ||
}, | ||
}); | ||
|
||
updateProgramNames.updateProgramNames(2024); | ||
|
||
expect(writeSpy).toBeCalledWith( | ||
expect.any(String), | ||
JSON.stringify( | ||
{ | ||
mips: "mips", | ||
cpcPlus: "cpcPlus", | ||
pcf: "pcf", | ||
app1: "app1", | ||
DEFAULT: "mips", | ||
G0053: "G0053", | ||
G0054: "G0054", | ||
}, | ||
null, | ||
2 | ||
) | ||
); | ||
}); | ||
|
||
it("gracefully logs an error if json parsing fails.", () => { | ||
const writeSpy = jest | ||
.spyOn(fs, "writeFileSync") | ||
.mockImplementation(jest.fn()); | ||
const logSpy = jest.spyOn(console, "log").mockImplementationOnce(jest.fn()); | ||
vol.fromNestedJSON({ | ||
"util/program-names": { | ||
"program-names.json": "{'badformat':: 'mips','cpcPlus': 'cpcPlus'}", | ||
}, | ||
"mvp/2024": { | ||
"mvp.json": JSON.stringify(mockMvpJson), | ||
}, | ||
}); | ||
|
||
updateProgramNames.updateProgramNames(2024); | ||
|
||
expect(writeSpy).not.toBeCalled(); | ||
expect(logSpy).toBeCalled(); | ||
}); | ||
}); |
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,33 @@ | ||
import * as fse from 'fs-extra'; | ||
import * as path from 'path'; | ||
|
||
/** | ||
* | ||
* @return {void} | ||
* Adds any new program name fields from the mvp.json file for the given performance year | ||
*/ | ||
export function updateProgramNames(performanceYear: number): void { | ||
let programNames: any; | ||
const programNamesFilePath = path.join(__dirname, '../util/program-names', 'program-names.json'); | ||
const mvpFilePath = path.join(__dirname, '../mvp', performanceYear.toString(), 'mvp.json'); | ||
|
||
console.log('programNamesFilePath', programNamesFilePath); | ||
console.log('mvpFilePath', mvpFilePath); | ||
|
||
let mvpData: any[] = []; | ||
|
||
try { | ||
programNames = JSON.parse(fse.readFileSync(programNamesFilePath, 'utf8')); | ||
mvpData = JSON.parse(fse.readFileSync(mvpFilePath, 'utf8')); | ||
|
||
mvpData.forEach(mvp => { | ||
if (!programNames[mvp.mvpId]) { | ||
programNames[mvp.mvpId] = mvp.mvpId; | ||
} | ||
}); | ||
|
||
fse.writeFileSync(programNamesFilePath, JSON.stringify(programNames, null, 2)); | ||
} catch (e) { | ||
console.log('Error parsing the program-names.json or mvp.json file: ' + ' --> ' + e); | ||
} | ||
} |