Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #67 #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).map(x => x.trim());
// Alternative:
// return envArg.split(/,\s*(?=[A-Z]+=)/).map(x => x.trim())
}

module.exports.extractPipelineName = extractPipelineName;
Expand Down
70 changes: 69 additions & 1 deletion test/util.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { extractPipelineName } = require("../src/util");
const {extractPipelineName} = require("../src/util");
const {parseVars} = require("../src/util");

describe("extractPipelineName", () => {
it("general case", () => {
Expand All @@ -22,3 +23,70 @@ 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",
]);
});
it("should trim the spaces", () => {
expect(parseVars("ONE='{}' , TWO=two , THREE=three ")).toMatchObject([
"ONE='{}'",
"TWO=two",
"THREE=three",
]);
});
});