Skip to content

Commit

Permalink
Merge pull request #11 from MikeGarde/list
Browse files Browse the repository at this point in the history
Safety for lists
  • Loading branch information
MikeGarde authored Jul 6, 2024
2 parents b404ab5 + 658a1e7 commit 2a4ae26
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/escapeAndQuote.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import log from './log.js';

function escapeAndQuote(str: string, quote: boolean): string {
if (str.startsWith('"') && str.endsWith('"')) {
// Remove the quotes
str = str.slice(1, -1);
}
// If string is a list, return as is unless a user has requested quotes
if (!quote && (str.startsWith('[') && str.endsWith(']'))) {
log.debug('List found, returning as is');
return str;
}

const needsQuotes: boolean = quote || /\s|"/.test(str);

Expand Down
2 changes: 1 addition & 1 deletion src/services/handlers/setValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default function setValue(options: Options) {
const key: string = options.targetKeys[0];
const newLines: string = `${key}=${options.setValue}`;

log.debug(`Updating "${key}"`);
log.debug(`Updating or adding "${key}"`);

// Do we want to update or append the .env file?
if (options.envObject[key]) {
Expand Down
15 changes: 15 additions & 0 deletions tests/escape.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,19 @@ describe('Escape and Quote', () => {
const result: string = escapeAndQuote('with\'quote', true);
expect(result).toBe('"with\'quote"');
});

test('list', () => {
const result: string = escapeAndQuote('["one", "two", "three"]', false);
expect(result).toBe('["one", "two", "three"]');
});

test('quoted list', () => {
const result: string = escapeAndQuote('"["one", "two", "three"]"', false);
expect(result).toBe('["one", "two", "three"]');
});

test('escaped list', () => {
const result: string = escapeAndQuote('[\"one\", \"two\", \"three\"]', false);
expect(result).toBe('["one", "two", "three"]');
});
});
19 changes: 19 additions & 0 deletions tests/setAndDelete.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,36 @@ describe('app.ts', () => {
expect(getCommand.toString().trim()).toBe('New stdin value');
});

test('add list', () => {
const setCommand: Buffer = execSync(`node ${appPath} LIST --set '["one", "two", "three"]' --file ${envPath}`);
const getCommand: Buffer = execSync(`node ${appPath} LIST --file ${envPath}`);

expect(setCommand.toString().trim()).toBe('');
expect(getCommand.toString().trim()).toBe('["one", "two", "three"]');
});

test('update list', () => {
const setCommand: Buffer = execSync(`node ${appPath} LIST --set "[\\"four\\", \\"five\\", \\"six\\"]" --file ${envPath}`);
const getCommand: Buffer = execSync(`node ${appPath} LIST --file ${envPath}`);

expect(setCommand.toString().trim()).toBe('');
expect(getCommand.toString().trim()).toBe('["four", "five", "six"]');
});

test('remove all new test keys', () => {
const delOne: Buffer = execSync(`node ${appPath} NEW_ONE --delete --file ${envPath}`);
const delTwo: Buffer = execSync(`node ${appPath} NEW_TWO --delete --file ${envPath}`);
const delList: Buffer = execSync(`node ${appPath} LIST --delete --file ${envPath}`);
const getCommand: Buffer = execSync(`node ${appPath} --file ${envPath}`);
const allJson: any = JSON.parse(getCommand.toString().trim());
const keys: string[] = Object.keys(allJson);

expect(delOne.toString().trim()).toBe('');
expect(delTwo.toString().trim()).toBe('');
expect(delList.toString().trim()).toBe('');
expect(keys).not.toContain('NEW_ONE');
expect(keys).not.toContain('NEW_TWO');
expect(keys).not.toContain('LIST');
});

test('after above .env file is unchanged', () => {
Expand Down

0 comments on commit 2a4ae26

Please sign in to comment.