Repository: elastic/synthetics
Pull request #664 (2022-11-28 09:41:46)

fix: skip empty value for array locations using init
fixes https://github.com/elastic/synthetics/issues/658

apmmachine: 
## :broken_heart: Build Failed


<!-- BUILD BADGES-->
> _the below badges are clickable and redirect to their specific view in the CI or DOCS_
[![Pipeline View](https://img.shields.io/badge/pipeline-pipeline%20-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-664/detail/PR-664/1//pipeline) [![Test View](https://img.shields.io/badge/test-test-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-664/detail/PR-664/1//tests) [![Changes](https://img.shields.io/badge/changes-changes-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-664/detail/PR-664/1//changes) [![Artifacts](https://img.shields.io/badge/artifacts-artifacts-yellow)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-664/detail/PR-664/1//artifacts) [![preview](https://img.shields.io/badge/docs-preview-yellowgreen)](http://synthetics_664.docs-preview.app.elstc.co/diff)  [![preview](https://img.shields.io/badge/elastic-observability-blue)](https://ci-stats.elastic.co/app/apm/services/apm-ci/transactions/view?rangeFrom=2022-11-28T09:32:02.320Z&rangeTo=2022-11-28T09:52:02.320Z&transactionName=BUILD+apm-agent-rum%2Felastic-synthetics%2FPR-%7Bnumber%7D&transactionType=job&latencyAggregationType=avg&traceId=4f065670e5f700fff8e51ac186083755&transactionId=51a153beae4851f5)

<!-- BUILD SUMMARY-->
<details><summary>Expand to view the summary</summary>
<p>

#### Build stats




* Start Time: 2022-11-28T09:42:02.320+0000


* Duration: 5 min 17 sec





</p>
</details>

<!-- TEST RESULTS IF ANY-->


<!-- STEPS ERRORS IF ANY -->














### Steps errors [![1](https://img.shields.io/badge/1%20-red)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-664/detail/PR-664/1//pipeline)
  <details><summary>Expand to view the steps failures</summary>
  <p>


  
  
  
  
##### `Checks linting`
<ul>
<li>Took 0 min 4 sec . View more details <a href="https://apm-ci.elastic.co//blue/rest/organizations/jenkins/pipelines/apm-agent-rum/pipelines/elastic-synthetics/pipelines/PR-664/runs/1/steps/186/log/?start=0">here</a></li>
<li>Description: <code>npm run-script lint</code></l1>
</ul>
  
  </p>
  </details>



## :robot: GitHub comments

<details><summary>Expand to view the GitHub comments</summary>
<p>

To re-run your PR in the CI, just comment with:



- `/test` : Re-trigger the build.




</p>
</details>

<!--COMMENT_GENERATED_WITH_ID_comment.id-->

Patch:
diff --git a/.gitignore b/.gitignore
index 8399ae0..d83fc53 100644
--- a/.gitignore
+++ b/.gitignore
@@ -119,3 +119,5 @@ __tests__/e2e/junit.xml
 
 .idea/
 seccomp/build
+
+test-projects/
diff --git a/src/generator/utils.ts b/src/generator/utils.ts
index a37bfc0..0231543 100644
--- a/src/generator/utils.ts
+++ b/src/generator/utils.ts
@@ -47,6 +47,7 @@ export function replaceTemplates(input: string, values: Record<string, any>) {
       if (Array.isArray(finalValue)) {
         return String(finalValue)
           .split(',')
+          .filter(Boolean)
           .map(f => `'${f}'`);
       }
       if (typeof finalValue == 'number') {


Test patch:
diff --git a/__tests__/generator/index.test.ts b/__tests__/generator/index.test.ts
index 698f53f..97dbfac 100644
--- a/__tests__/generator/index.test.ts
+++ b/__tests__/generator/index.test.ts
@@ -44,7 +44,7 @@ describe('Generator', () => {
   });
   it('generate synthetics project - NPM', async () => {
     // In a few development environments, this test may take
-    // a few millisseconds more than the default 15000ms timeout
+    // a few milliseconds more than the default 15000ms timeout
     jest.setTimeout(30000);
     const cli = new CLIMock().args(['init', scaffoldDir]).run({
       env: {
diff --git a/__tests__/generator/utils.test.ts b/__tests__/generator/utils.test.ts
new file mode 100644
index 0000000..56e2f52
--- /dev/null
+++ b/__tests__/generator/utils.test.ts
@@ -0,0 +1,51 @@
+/**
+ * MIT License
+ *
+ * Copyright (c) 2020-present, Elastic NV
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+import { readFileSync } from 'fs';
+import { join } from 'path';
+import { replaceTemplates } from '../../src/generator/utils';
+
+describe('Generator utils', () => {
+  it('does not add empty values in array', async () => {
+    const template = readFileSync(
+      join(__dirname, '..', '..', 'templates/synthetics.config.ts'),
+      'utf-8'
+    );
+    const values = {
+      locations: ['us_east'],
+      privateLocations: [''],
+      schedule: 30,
+      id: 'test',
+      space: 'kbn',
+      url: 'foo:bar',
+    };
+    const finalValue = replaceTemplates(template, values);
+
+    expect(finalValue).toContain("locations: ['us_east'],");
+    expect(finalValue).toContain('privateLocations: [],');
+
+    expect(finalValue).not.toContain("['']");
+  });
+});