From 276ebac0a3791929889cda195fb1e40de2e31352 Mon Sep 17 00:00:00 2001 From: Manuel Canepa Date: Tue, 15 Jun 2021 10:37:40 -0300 Subject: [PATCH 1/2] Fix issue #67 --- src/util.js | 8 +++--- test/util.test.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/util.js b/src/util.js index dc52fcf..04b48db 100644 --- a/src/util.js +++ b/src/util.js @@ -17,10 +17,10 @@ function extractPipelineName(pipeline) { } function parseVars(envArg) { - return envArg - .trim() - .split(",") - .map(x => x.trim()); + // @see https://stackoverflow.com/questions/67978415/complex-assignments-with-comma-separator + return envArg.match(/(?=\b[a-z])\w+=(?:(['"])(?:(?!\1).)*\1|[^,]*)/gi); + // Alternative: + // return envArg.split(/,\s*(?=[A-Z]+=)/).map(x => x.trim()) } module.exports.extractPipelineName = extractPipelineName; diff --git a/test/util.test.js b/test/util.test.js index 66315ad..9200d59 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -1,4 +1,5 @@ -const { extractPipelineName } = require("../src/util"); +const {extractPipelineName} = require("../src/util"); +const {parseVars} = require("../src/util"); describe("extractPipelineName", () => { it("general case", () => { @@ -22,3 +23,63 @@ describe("extractPipelineName", () => { }); }); }); +describe("parseVars function", () => { + it("should be one simple variable", () => { + expect(parseVars("ONE=one")).toMatchObject([ + "ONE=one" + ]); + }); + it("should be two simple variable", () => { + expect(parseVars("ONE=one,TWO=two")).toMatchObject([ + "ONE=one", + "TWO=two" + ]); + }); + it("should be two simple variable (Trim space)", () => { + expect(parseVars("ONE=one, TWO=two")).toMatchObject([ + "ONE=one", + "TWO=two" + ]); + }); + it("should be simple json", () => { + expect(parseVars("ONE='{}'")).toMatchObject([ + "ONE='{}'", + ]); + }); + it("should be three simple json", () => { + expect(parseVars("ONE='{}',TWO='{}',THREE='{}'")).toMatchObject([ + "ONE='{}'", + "TWO='{}'", + "THREE='{}'", + ]); + }); + it("should be three simple json (Simple quote)", () => { + expect(parseVars("ONE='{}'")).toMatchObject([ + "ONE='{}'", + ]); + }); + it("should be three simple json with attribute", () => { + expect(parseVars("ONE='{attr: \"value\"}'")).toMatchObject([ + "ONE='{attr: \"value\"}'", + ]); + }); + it("should be complex json with multiple attributes", () => { + expect(parseVars("ONE='{attr1: \"value\", attr2:\"value attr 2\"}'")).toMatchObject([ + "ONE='{attr1: \"value\", attr2:\"value attr 2\"}'", + ]); + }); + + it("should be one json and one simple var", () => { + expect(parseVars("ONE='{}', TWO=two")).toMatchObject([ + "ONE='{}'", + "TWO=two", + ]); + }); + it("should be one json and two simple vars", () => { + expect(parseVars("ONE='{}', TWO=two, THREE=three")).toMatchObject([ + "ONE='{}'", + "TWO=two", + "THREE=three", + ]); + }); +}); From 05949ab8c5736ee954feebe6d40a24f7de997067 Mon Sep 17 00:00:00 2001 From: Manuel Canepa Date: Tue, 15 Jun 2021 10:48:26 -0300 Subject: [PATCH 2/2] I forgot the trim --- src/util.js | 2 +- test/util.test.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/util.js b/src/util.js index 04b48db..8cc4447 100644 --- a/src/util.js +++ b/src/util.js @@ -18,7 +18,7 @@ function extractPipelineName(pipeline) { function parseVars(envArg) { // @see https://stackoverflow.com/questions/67978415/complex-assignments-with-comma-separator - return envArg.match(/(?=\b[a-z])\w+=(?:(['"])(?:(?!\1).)*\1|[^,]*)/gi); + return envArg.match(/(?=\b[a-z])\w+=(?:(['"])(?:(?!\1).)*\1|[^,]*)/gi).map(x => x.trim()); // Alternative: // return envArg.split(/,\s*(?=[A-Z]+=)/).map(x => x.trim()) } diff --git a/test/util.test.js b/test/util.test.js index 9200d59..71b9664 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -82,4 +82,11 @@ describe("parseVars function", () => { "THREE=three", ]); }); + it("should trim the spaces", () => { + expect(parseVars("ONE='{}' , TWO=two , THREE=three ")).toMatchObject([ + "ONE='{}'", + "TWO=two", + "THREE=three", + ]); + }); });