Skip to content

Commit

Permalink
Add a function to ensure that booleans passed as strings are evaluate…
Browse files Browse the repository at this point in the history
…d as booleans (#229)
  • Loading branch information
CodeVachon committed Apr 4, 2024
1 parent 2b9e418 commit a893e39
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const core = require('@actions/core');
const fs = require('fs');
const { context } = require('@actions/github');
const { isGitHubTag, isBranch, isPullRequest, branchRefToSlug, prRefToSlug, tagRefToSlug } = require('./github');
const { timestamp, cpOptions } = require('./utils');
const { timestamp, cpOptions, asBool } = require('./utils');

const GITHUB_REGISTRY_URLS = ['docker.pkg.github.com', 'ghcr.io'];

Expand Down Expand Up @@ -46,7 +46,7 @@ const createTags = (addLatest, addTimestamp) => {
);
}

if (addLatest) {
if (asBool(addLatest)) {
dockerTags.push('latest');
}

Expand Down
24 changes: 23 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,30 @@ const cpOptions = {
stdio: 'inherit'
};

/**
* Convert a value to a boolean
* @param {*} value the value to convert
* @returns boolean value
*/
const asBool = value => {
switch (
String(value ?? '')
.trim()
.toLowerCase()
) {
case 'true':
case 'y':
case 'yes':
case '1':
return true;
default:
return false;
}
};

module.exports = {
timestamp,
parseArray,
cpOptions
cpOptions,
asBool
};
12 changes: 12 additions & 0 deletions tests/docker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ describe('Create Docker image tags', () => {
expect(tags.length).toEqual(2);
});

test('Create from tag push with addLatest passed as string', () => {
context.ref = 'refs/tags/v1.0';
context.sha = '8d93430eddafb926c668181c71f579556f68668c';
addLatest = 'true';

const tags = docker.createTags(addLatest, addTimestamp);

expect(tags).toContain('v1.0');
expect(tags).toContain('latest');
expect(tags.length).toEqual(2);
});

test('Create from tag push with addTimestamp', () => {
context.ref = 'refs/tags/v1.0';
context.sha = '8d93430eddafb926c668181c71f579556f68668c';
Expand Down
31 changes: 30 additions & 1 deletion tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parseArray } = require('../src/utils');
const { parseArray, asBool } = require('../src/utils');

describe('Parse a comma-delimited strings', () => {
test('Parse string with spaces and return an array', () => {
Expand Down Expand Up @@ -37,3 +37,32 @@ describe('Parse a comma-delimited strings', () => {
expect(value).toEqual(undefined);
});
});

describe('Convert a value to a boolean', () => {
test.each`
value | as | expected
${true} | ${'boolean'} | ${true}
${false} | ${'boolean'} | ${false}
${'true'} | ${'string'} | ${true}
${'false'} | ${'string'} | ${false}
${'1'} | ${'string'} | ${true}
${'0'} | ${'string'} | ${false}
${'-1'} | ${'string'} | ${false}
${'N'} | ${'string'} | ${false}
${'n'} | ${'string'} | ${false}
${'No'} | ${'string'} | ${false}
${'no'} | ${'string'} | ${false}
${'Y'} | ${'string'} | ${true}
${'y'} | ${'string'} | ${true}
${'Yes'} | ${'string'} | ${true}
${'yes'} | ${'string'} | ${true}
${1} | ${'number'} | ${true}
${0} | ${'number'} | ${false}
${-1} | ${'number'} | ${false}
${undefined} | ${'undefined'} | ${false}
`(`returns $expected for '$value' as $as`, ({ value, expected }) => {
const result = asBool(value);
expect(typeof result).toBe('boolean');
expect(result).toEqual(expected);
});
});

0 comments on commit a893e39

Please sign in to comment.