Skip to content

Commit

Permalink
feat(container-metadata): updates for feature parity with metadata-ac…
Browse files Browse the repository at this point in the history
…tion 5.0.0
  • Loading branch information
gperdomor committed Oct 15, 2023
1 parent 5909b45 commit af9c55c
Show file tree
Hide file tree
Showing 9 changed files with 1,330 additions and 936 deletions.
11 changes: 6 additions & 5 deletions packages/container-metadata/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
{
"name": "@nx-tools/container-metadata",
"version": "5.0.3",
"version": "6.0.0-alpha.1",
"type": "commonjs",
"author": "gperdomor <gperdomor@gmail.com>",
"repository": "https://github.com/gperdomor/nx-tools",
"bugs": "https://github.com/gperdomor/nx-tools/issues",
"license": "MIT",
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"dependencies": {
"@nx-tools/core": "5.0.3",
"csv-parse": "5.5.2",
"@nx-tools/ci-context": "5.0.3",
"@nx-tools/ci-context": "6.0.0-alpha.1",
"@nx-tools/core": "6.0.0-alpha.1",
"@renovate/pep440": "1.0.0",
"csv-parse": "5.5.2",
"handlebars": "4.7.8",
"moment-timezone": "0.5.43",
"semver": "7.5.4"
},
"peerDependencies": {
"@nx/devkit": "^16.0.0",
"dotenv": ">=10.0.0",
"tslib": "^2.5.3"
}
}
247 changes: 109 additions & 138 deletions packages/container-metadata/src/lib/context.spec.ts
Original file line number Diff line number Diff line change
@@ -1,159 +1,130 @@
import { ContextProxyFactory, RunnerContext } from '@nx-tools/ci-context';
import { getPosixName } from '@nx-tools/core';
import * as dotenv from 'dotenv';
import mockedEnv, { RestoreFn } from 'mocked-env';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as context from './context';
import { Git } from 'packages/ci-context/src/lib/utils/git';
import { Github } from 'packages/ci-context/src/lib/utils/github';
import { Inputs, getContext, getInputs } from './context';

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

describe('getInputList', () => {
it('single line correctly', async () => {
await setInput('foo', 'bar');
const res = context.getInputList('foo');
expect(res).toEqual(['bar']);
describe('getInputs', () => {
beforeEach(() => {
process.env = Object.keys(process.env).reduce((object, key) => {
if (!key.startsWith('INPUT_')) {
object[key] = process.env[key];
}
return object;
}, {} as NodeJS.ProcessEnv);
});

it('multiline correctly', async () => {
setInput('foo', 'bar\nbaz');
const res = context.getInputList('foo');
expect(res).toEqual(['bar', 'baz']);
});

it('empty lines correctly', async () => {
setInput('foo', 'bar\n\nbaz');
const res = context.getInputList('foo');
expect(res).toEqual(['bar', 'baz']);
});

it('comment correctly', async () => {
setInput('foo', 'bar\n#com\n"#taken"\nhello#comment\nbaz');
const res = context.getInputList('foo');
expect(res).toEqual(['bar', '#taken', 'hello', 'baz']);
});

it('comma correctly', async () => {
setInput('foo', 'bar,baz');
const res = context.getInputList('foo');
expect(res).toEqual(['bar', 'baz']);
});

it('empty result correctly', async () => {
setInput('foo', 'bar,baz,');
const res = context.getInputList('foo');
expect(res).toEqual(['bar', 'baz']);
});

it('different new lines correctly', async () => {
setInput('foo', 'bar\r\nbaz');
const res = context.getInputList('foo');
expect(res).toEqual(['bar', 'baz']);
});
// prettier-ignore
test.each([
[
0,
new Map<string, string>([
['images', 'moby/buildkit\nghcr.io/moby/mbuildkit'],
]),
{
"bake-target": 'container-metadata-action',
flavor: [],
"github-token": '',
images: ['moby/buildkit', 'ghcr.io/moby/mbuildkit'],
labels: [],
"sep-labels": '\n',
"sep-tags": '\n',
tags: [],
} as Inputs
],
[
1,
new Map<string, string>([
['bake-target', 'metadata'],
['images', 'moby/buildkit'],
['sep-labels', ','],
['sep-tags', ','],
]),
{
"bake-target": 'metadata',
flavor: [],
"github-token": '',
images: ['moby/buildkit'],
labels: [],
"sep-labels": ',',
"sep-tags": ',',
tags: [],
} as Inputs
],
[
2,
new Map<string, string>([
['images', 'moby/buildkit\n#comment\nghcr.io/moby/mbuildkit'],
]),
{
"bake-target": 'container-metadata-action',
flavor: [],
"github-token": '',
images: ['moby/buildkit', 'ghcr.io/moby/mbuildkit'],
labels: [],
"sep-labels": '\n',
"sep-tags": '\n',
tags: [],
} as Inputs
],
])(
'[%d] given %p as inputs, returns %p',
async (num: number, inputs: Map<string, string>, expected: Inputs) => {
inputs.forEach((value: string, name: string) => {
setInput(name, value);
});
expect(await getInputs({})).toEqual(expected);
}
);
});

it('different new lines and comma correctly', async () => {
setInput('foo', 'bar\r\nbaz,bat');
const res = context.getInputList('foo');
expect(res).toEqual(['bar', 'baz', 'bat']);
describe('getContext', () => {
let restore: RestoreFn;

beforeEach(() => {
jest.resetModules();
restore = mockedEnv(
{
...process.env,
...dotenv.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'tests', 'fixtures/event_create_branch.env'))),
},
{ clear: true }
);
});

it('multiline and ignoring comma correctly', async () => {
setInput('cache-from', 'user/app:cache\ntype=local,src=path/to/dir');
const res = context.getInputList('cache-from', '', undefined, true);
expect(res).toEqual(['user/app:cache', 'type=local,src=path/to/dir']);
afterEach(() => {
restore();
});

it('different new lines and ignoring comma correctly', async () => {
setInput('cache-from', 'user/app:cache\r\ntype=local,src=path/to/dir');
const res = context.getInputList('cache-from', '', undefined, true);
expect(res).toEqual(['user/app:cache', 'type=local,src=path/to/dir']);
});
it('workflow', async () => {
jest.spyOn(ContextProxyFactory, 'create').mockImplementation((): Promise<RunnerContext> => {
return Github.context();
});

it('multiline values', async () => {
setInput(
'secrets',
`GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789
"MYSECRET=aaaaaaaa
bbbbbbb
ccccccccc"
FOO=bar`
);
const res = context.getInputList('secrets', '', undefined, true);
expect(res).toEqual([
'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789',
`MYSECRET=aaaaaaaa
bbbbbbb
ccccccccc`,
'FOO=bar',
]);
const context = await getContext();
expect(context.ref).toEqual('refs/heads/dev');
expect(context.sha).toEqual('5f3331d7f7044c18ca9f12c77d961c4d7cf3276a');
});

it('multiline values with empty lines', async () => {
setInput(
'secrets',
`GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789
"MYSECRET=aaaaaaaa
bbbbbbb
ccccccccc"
FOO=bar
"EMPTYLINE=aaaa
it('git', async () => {
jest.spyOn(Git, 'ref').mockResolvedValue('refs/heads/git-test');
jest.spyOn(Git, 'fullCommit').mockResolvedValue('git-test-sha');

bbbb
ccc"`
);
const res = context.getInputList('secrets', '', undefined, true);
expect(res).toEqual([
'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789',
`MYSECRET=aaaaaaaa
bbbbbbb
ccccccccc`,
'FOO=bar',
`EMPTYLINE=aaaa
jest.spyOn(ContextProxyFactory, 'create').mockImplementation((): Promise<RunnerContext> => {
return Git.context();
});

bbbb
ccc`,
]);
});

it('multiline values without quotes', async () => {
setInput(
'secrets',
`GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789
MYSECRET=aaaaaaaa
bbbbbbb
ccccccccc
FOO=bar`
);
const res = context.getInputList('secrets', '', undefined, true);
expect(res).toEqual([
'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789',
'MYSECRET=aaaaaaaa',
'bbbbbbb',
'ccccccccc',
'FOO=bar',
]);
});

it('multiline values escape quotes', async () => {
setInput(
'secrets',
`GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789
"MYSECRET=aaaaaaaa
bbbb""bbb
ccccccccc"
FOO=bar`
);
const res = context.getInputList('secrets', '', undefined, true);
expect(res).toEqual([
'GIT_AUTH_TOKEN=abcdefgh,ijklmno=0123456789',
`MYSECRET=aaaaaaaa
bbbb"bbb
ccccccccc`,
'FOO=bar',
]);
const context = await getContext();
expect(context.ref).toEqual('refs/heads/git-test');
expect(context.sha).toEqual('git-test-sha');
});
});

Expand Down
Loading

0 comments on commit af9c55c

Please sign in to comment.