Skip to content

Commit

Permalink
fix: skip empty value for array locations using init (#664)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 authored Nov 29, 2022
1 parent 1de762d commit 1c05f22
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,5 @@ __tests__/e2e/junit.xml

.idea/
seccomp/build

test-projects/
2 changes: 1 addition & 1 deletion __tests__/generator/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
51 changes: 51 additions & 0 deletions __tests__/generator/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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("['']");
});
});
1 change: 1 addition & 0 deletions src/generator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down

0 comments on commit 1c05f22

Please sign in to comment.