Skip to content

Commit

Permalink
preserve quotes for set input fields
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Oct 19, 2022
1 parent 33fe53e commit ae45b6a
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 6 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -127,3 +127,20 @@ jobs:
with:
source: https://github.com/docker/buildx.git#v0.8.2
targets: update-docs

set:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Build
uses: ./
with:
files: |
./test/config.hcl
set: |
*.platform=linux/amd64
*.output=type=image,name="registry.local/foo:v1.0.0,registry.local/foo:latest",push=false
*.tags=
105 changes: 104 additions & 1 deletion __tests__/context.test.ts
@@ -1,6 +1,109 @@
import {describe, expect, it} from '@jest/globals';
import {beforeEach, describe, expect, it, jest, test} from '@jest/globals';
import * as fs from 'fs';
import * as path from 'path';
import * as context from '../src/context';

jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
const tmpDir = path.join('/tmp/.bake-action-jest').split(path.sep).join(path.posix.sep);
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, {recursive: true});
}
return tmpDir;
});

describe('getArgs', () => {
beforeEach(() => {
process.env = Object.keys(process.env).reduce((object, key) => {
if (!key.startsWith('INPUT_')) {
object[key] = process.env[key];
}
return object;
}, {});
});

// prettier-ignore
test.each([
[
0,
new Map<string, string>([
['no-cache', 'false'],
['pull', 'false'],
['load', 'false'],
['push', 'false'],
]),
[
'bake',
'--metadata-file', '/tmp/.bake-action-jest/metadata-file',
]
],
[
1,
new Map<string, string>([
['no-cache', 'true'],
['pull', 'false'],
['load', 'false'],
['push', 'false'],
['files', `docker-bake.hcl\ndocker-bake-prod.hcl`],
['targets', `build,image\nvalidate`],
]),
[
'bake',
'--file', 'docker-bake.hcl',
'--file', 'docker-bake-prod.hcl',
'--metadata-file', '/tmp/.bake-action-jest/metadata-file',
'--no-cache',
'build', 'image', 'validate'
]
],
[
2,
new Map<string, string>([
['no-cache', 'false'],
['pull', 'true'],
['load', 'false'],
['push', 'false'],
['source', 'https://github.com/docker/buildx.git#v0.8.2'],
['targets', 'update-docs'],
]),
[
'bake',
'https://github.com/docker/buildx.git#v0.8.2',
'--metadata-file', '/tmp/.bake-action-jest/metadata-file',
'--pull',
'update-docs'
]
],
[
3,
new Map<string, string>([
['no-cache', 'false'],
['pull', 'false'],
['load', 'false'],
['push', 'false'],
['set', `*.platform=linux/amd64,linux/ppc64le,linux/s390x\n*.output=type=image,name="moby/buildkit:v0.11.0,moby/buildkit:latest",push=true`],
['targets', `"image-all"`],
]),
[
'bake',
'--set', '*.platform=linux/amd64,linux/ppc64le,linux/s390x',
'--set', `*.output=type=image,name="moby/buildkit:v0.11.0,moby/buildkit:latest",push=true`,
'--metadata-file', '/tmp/.bake-action-jest/metadata-file',
'image-all'
]
]
])(
'[%d] given %p as inputs, returns %p',
async (num: number, inputs: Map<string, string>, expected: Array<string>) => {
inputs.forEach((value: string, name: string) => {
setInput(name, value);
});
const inp = await context.getInputs();
const res = await context.getArgs(inp, '0.9.0');
expect(res).toEqual(expected);
}
);
});

describe('getInputList', () => {
it('single line correctly', async () => {
await setInput('foo', 'bar');
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions src/context.ts
Expand Up @@ -42,7 +42,7 @@ export async function getInputs(): Promise<Inputs> {
pull: core.getBooleanInput('pull'),
load: core.getBooleanInput('load'),
push: core.getBooleanInput('push'),
set: getInputList('set', true),
set: getInputList('set', true, true),
source: core.getInput('source')
};
}
Expand Down Expand Up @@ -93,7 +93,7 @@ async function getCommonArgs(inputs: Inputs): Promise<Array<string>> {
return args;
}

export function getInputList(name: string, ignoreComma?: boolean): string[] {
export function getInputList(name: string, ignoreComma?: boolean, preserveQuotes?: boolean): string[] {
const res: Array<string> = [];

const items = core.getInput(name);
Expand All @@ -103,8 +103,11 @@ export function getInputList(name: string, ignoreComma?: boolean): string[] {

const records = parse(items, {
columns: false,
relaxQuotes: true,
comment: '#',
relaxColumnCount: true,
skipEmptyLines: true
skipEmptyLines: true,
quote: preserveQuotes ? false : `"`
});

for (const record of records as Array<string[]>) {
Expand Down

0 comments on commit ae45b6a

Please sign in to comment.