From 512b53994f02b6ea872456484eac80fd82901028 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Mon, 10 May 2021 19:52:16 +0100 Subject: [PATCH] Check for numbers when parsing variables (#30) * Ensure numbers are not parsed as JSON * Bump to v0.5.2 --- package-lock.json | 2 +- package.json | 2 +- src/helpers.js | 9 +++++---- test/unit/helpers.test.js | 12 ++++++++++++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2eefa2c..2910b84 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@cda0/terrajs", - "version": "0.5.1", + "version": "0.5.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index d30e356..df71d70 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cda0/terrajs", - "version": "0.5.1", + "version": "0.5.2", "description": "A node interface to Terraform", "main": "src/index.js", "scripts": { diff --git a/src/helpers.js b/src/helpers.js index 21e84a6..9d1b8c0 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -33,12 +33,13 @@ function parseVariable(key, value) { return `'${parsedKey}=${parsedValue.replace(/'/g, '\\\'')}'`; } - if (typeof value === 'boolean') { - return `"${parsedKey}=${value}"`; + if (typeof value === 'string') { + // Double-quotes can prematurely close the string, so they need to be escaped. + return `"${parsedKey}=${value.replace(/"/g, '\\"')}"`; } - // Double-quotes can prematurely close the string, so they need to be escaped. - return `"${parsedKey}=${value.replace(/"/g, '\\"')}"`; + // Booleans and numbers can be returned without parsing. + return `"${parsedKey}=${value}"`; } module.exports = { diff --git a/test/unit/helpers.test.js b/test/unit/helpers.test.js index 082c5d9..c62bbed 100644 --- a/test/unit/helpers.test.js +++ b/test/unit/helpers.test.js @@ -89,6 +89,12 @@ describe('helpers', () => { assert.equal(response, '"some_var=West Europe"'); }); + it('should correctly parse a number variable', () => { + const variable = 42; + const response = parseVariable('someVar', variable); + assert.equal(response, '"some_var=42"'); + }); + it('should correctly parse a boolean variable', () => { const variable = true; const response = parseVariable('someVar', variable); @@ -137,6 +143,12 @@ describe('helpers', () => { assert.equal(response, '"some_var=West Europe"'); }); + it('should correctly parse a number variable', () => { + const variable = 42; + const response = parseVariable('someVar', variable); + assert.equal(response, '"some_var=42"'); + }); + it('should correctly parse a boolean variable', () => { const variable = true; const response = parseVariable('someVar', variable);