From 86c4bcb1406786a8fe6354a53f7131e59aab95bc Mon Sep 17 00:00:00 2001 From: Chris Moesel Date: Thu, 26 Mar 2020 20:51:25 -0400 Subject: [PATCH] Fix handling of local templates in ig.ini Local templates start with # in the ig.ini file, but we were expecting name#version. This fixes it so it works when # is the first character. Fixes #299. --- src/ig/IGExporter.ts | 4 +-- test/ig/IGExporter.test.ts | 27 +++++++++++++++++++ .../ig-data/ig.ini | 3 +++ .../package.json | 20 ++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 test/ig/fixtures/customized-ig-with-local-template/ig-data/ig.ini create mode 100644 test/ig/fixtures/customized-ig-with-local-template/package.json diff --git a/src/ig/IGExporter.ts b/src/ig/IGExporter.ts index b7731f088..332591f3e 100644 --- a/src/ig/IGExporter.ts +++ b/src/ig/IGExporter.ts @@ -561,7 +561,7 @@ export class IGExporter { // FHIR allows templates to have versions identified using #. E.g., // template = hl7.fhir.template#0.1.0 // The ini library, however, treats # as a comment unless it is escaped. So if it exists, we need to escape it. - inputIniContents = inputIniContents.replace(/^\s*template\s*=\s*[^#]+(#.+)?$/m, ($0, $1) => + inputIniContents = inputIniContents.replace(/^\s*template\s*=\s*[^#]*(#.+)?$/m, ($0, $1) => $1 ? $0.replace($1, `\\${$1}`) : $0 ); const inputIni = ini.parse(inputIniContents); @@ -600,7 +600,7 @@ export class IGExporter { // Now we need to do the reverse of what we did before. If `#` is escaped, then unescape it. let outputIniContents = ini.encode(iniObj, { section: 'IG', whitespace: true }); - outputIniContents = outputIniContents.replace(/^template\s*=\s*.+?(\\#.+)?$/m, ($0, $1) => + outputIniContents = outputIniContents.replace(/^template\s*=\s*.*?(\\#.+)?$/m, ($0, $1) => $1 ? $0.replace($1, $1.slice(1)) : $0 ); diff --git a/test/ig/IGExporter.test.ts b/test/ig/IGExporter.test.ts index 9b5dd7080..4b5a50ac1 100644 --- a/test/ig/IGExporter.test.ts +++ b/test/ig/IGExporter.test.ts @@ -485,6 +485,33 @@ describe('IGExporter', () => { }); }); + describe('#customized-ig', () => { + let pkg: Package; + let exporter: IGExporter; + let tempOut: string; + + beforeAll(() => { + const fixtures = path.join(__dirname, 'fixtures', 'customized-ig-with-local-template'); + const config: Config = fs.readJSONSync(path.join(fixtures, 'package.json')); + pkg = new Package(config); + exporter = new IGExporter(pkg, new FHIRDefinitions(), path.resolve(fixtures, 'ig-data')); + tempOut = temp.mkdirSync('sushi-test'); + exporter.export(tempOut); + }); + + afterAll(() => { + temp.cleanupSync(); + }); + + it('should generate an ig.ini with working local-template value', () => { + const iniPath = path.join(tempOut, 'ig.ini'); + expect(fs.existsSync(iniPath)).toBeTruthy(); + const content = fs.readFileSync(iniPath, 'utf8'); + const lines = content.split(os.EOL); // Windows: /r/n; Mac: /n + expect(lines[2]).toEqual('template = #local-template'); + }); + }); + describe('#customized-ig-with-index-xml', () => { let pkg: Package; let exporter: IGExporter; diff --git a/test/ig/fixtures/customized-ig-with-local-template/ig-data/ig.ini b/test/ig/fixtures/customized-ig-with-local-template/ig-data/ig.ini new file mode 100644 index 000000000..ee642fe72 --- /dev/null +++ b/test/ig/fixtures/customized-ig-with-local-template/ig-data/ig.ini @@ -0,0 +1,3 @@ +[IG] +ig = input/ImplementationGuide-sushi-test.json +template = #local-template diff --git a/test/ig/fixtures/customized-ig-with-local-template/package.json b/test/ig/fixtures/customized-ig-with-local-template/package.json new file mode 100644 index 000000000..34f4cb5b0 --- /dev/null +++ b/test/ig/fixtures/customized-ig-with-local-template/package.json @@ -0,0 +1,20 @@ +{ + "name": "sushi-test", + "version" : "0.1.0", + "canonical" : "http://hl7.org/fhir/sushi-test", + "url" : "http://hl7.org/fhir/sushi-test", + "title" : "FSH Test IG", + "description": "Provides a simple example of how FSH can be used to create an IG", + "dependencies": { + "hl7.fhir.r4.core": "4.0.1" + }, + "language": "en", + "author": "James Tuna", + "maintainers": [ + { + "name": "Bill Cod", + "email": "cod@reef.gov" + } + ], + "license": "CC0-1.0" + } \ No newline at end of file