make Context static

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-02-20 07:19:57 +01:00
parent 580aee99c0
commit a0e8f0bf18
14 changed files with 80 additions and 145 deletions

View File

@@ -18,7 +18,6 @@ import {beforeEach, describe, expect, it, jest, test} from '@jest/globals';
import {BuildKit} from '../../src/buildkit/buildkit';
import {Builder} from '../../src/buildx/builder';
import {Context} from '../../src/context';
import {BuilderInfo} from '../../src/types/builder';
@@ -47,13 +46,9 @@ jest.spyOn(Builder.prototype, 'inspect').mockImplementation(async (): Promise<Bu
describe('getVersion', () => {
it('valid', async () => {
const builder = new Builder({
context: new Context()
});
const builder = new Builder();
const builderInfo = await builder.inspect('builder2');
const buildkit = new BuildKit({
context: new Context()
});
const buildkit = new BuildKit();
const version = await buildkit.getVersion(builderInfo.nodes[0]);
expect(version).toBe('v0.11.0');
});
@@ -64,9 +59,7 @@ describe('satisfies', () => {
['builder2', '>=0.10.0', true],
['builder2', '>0.11.0', false]
])('given %p', async (builderName, range, expected) => {
const buildkit = new BuildKit({
context: new Context()
});
const buildkit = new BuildKit();
expect(await buildkit.versionSatisfies(builderName, range)).toBe(expected);
});
});

View File

@@ -27,13 +27,14 @@ const fixturesDir = path.join(__dirname, '..', 'fixtures');
const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildkit-config-jest');
const tmpName = path.join(tmpDir, '.tmpname-jest');
jest.spyOn(Context.prototype, 'tmpDir').mockImplementation((): string => {
jest.spyOn(Context, 'tmpDir').mockImplementation((): string => {
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, {recursive: true});
}
return tmpDir;
});
jest.spyOn(Context.prototype, 'tmpName').mockImplementation((): string => {
jest.spyOn(Context, 'tmpName').mockImplementation((): string => {
return tmpName;
});
@@ -60,9 +61,7 @@ describe('resolve', () => {
]
])('given %p config', async (val, file, exValue, error: Error) => {
try {
const buildkit = new BuildKit({
context: new Context()
});
const buildkit = new BuildKit();
let config: string;
if (file) {
config = buildkit.config.resolveFromFile(val);

View File

@@ -19,7 +19,6 @@ import * as fs from 'fs';
import * as path from 'path';
import {Builder} from '../../src/buildx/builder';
import {Context} from '../../src/context';
import {BuilderInfo} from '../../src/types/builder';
@@ -50,9 +49,7 @@ jest.spyOn(Builder.prototype, 'inspect').mockImplementation(async (): Promise<Bu
describe('inspect', () => {
it('valid', async () => {
const builder = new Builder({
context: new Context()
});
const builder = new Builder();
const builderInfo = await builder.inspect('');
expect(builderInfo).not.toBeUndefined();
expect(builderInfo.name).not.toEqual('');

View File

@@ -30,13 +30,14 @@ import {Cert} from '../../src/types/buildx';
const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-jest');
const tmpName = path.join(tmpDir, '.tmpname-jest');
jest.spyOn(Context.prototype, 'tmpDir').mockImplementation((): string => {
jest.spyOn(Context, 'tmpDir').mockImplementation((): string => {
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, {recursive: true});
}
return tmpDir;
});
jest.spyOn(Context.prototype, 'tmpName').mockImplementation((): string => {
jest.spyOn(Context, 'tmpName').mockImplementation((): string => {
return tmpName;
});
@@ -92,7 +93,6 @@ describe('isAvailable', () => {
it('docker cli', async () => {
const execSpy = jest.spyOn(exec, 'getExecOutput');
const buildx = new Buildx({
context: new Context(),
standalone: false
});
await buildx.isAvailable();
@@ -105,7 +105,6 @@ describe('isAvailable', () => {
it('standalone', async () => {
const execSpy = jest.spyOn(exec, 'getExecOutput');
const buildx = new Buildx({
context: new Context(),
standalone: true
});
await buildx.isAvailable();
@@ -121,7 +120,6 @@ describe('printInspect', () => {
it('prints builder2 instance', async () => {
const execSpy = jest.spyOn(exec, 'exec');
const buildx = new Buildx({
context: new Context(),
standalone: true
});
await buildx.printInspect('builder2').catch(() => {
@@ -137,7 +135,6 @@ describe('printVersion', () => {
it('docker cli', async () => {
const execSpy = jest.spyOn(exec, 'exec');
const buildx = new Buildx({
context: new Context(),
standalone: false
});
await buildx.printVersion();
@@ -148,7 +145,6 @@ describe('printVersion', () => {
it('standalone', async () => {
const execSpy = jest.spyOn(exec, 'exec');
const buildx = new Buildx({
context: new Context(),
standalone: true
});
await buildx.printVersion();
@@ -160,9 +156,7 @@ describe('printVersion', () => {
describe('version', () => {
it('valid', async () => {
const buildx = new Buildx({
context: new Context()
});
const buildx = new Buildx();
expect(semver.valid(await buildx.version)).not.toBeUndefined();
});
});
@@ -184,9 +178,7 @@ describe('versionSatisfies', () => {
['bda4882a65349ca359216b135896bddc1d92461c', '>0.1.0', false],
['f117971', '>0.6.0', true]
])('given %p', async (version, range, expected) => {
const buildx = new Buildx({
context: new Context()
});
const buildx = new Buildx();
expect(await buildx.versionSatisfies(range, version)).toBe(expected);
});
});

View File

@@ -32,13 +32,14 @@ const metadata = `{
"containerimage.digest": "sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c"
}`;
jest.spyOn(Context.prototype, 'tmpDir').mockImplementation((): string => {
jest.spyOn(Context, 'tmpDir').mockImplementation((): string => {
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, {recursive: true});
}
return tmpDir;
});
jest.spyOn(Context.prototype, 'tmpName').mockImplementation((): string => {
jest.spyOn(Context, 'tmpName').mockImplementation((): string => {
return tmpName;
});
@@ -52,9 +53,7 @@ afterEach(() => {
describe('resolveBuildImageID', () => {
it('matches', async () => {
const buildx = new Buildx({
context: new Context()
});
const buildx = new Buildx();
const imageID = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
const imageIDFile = buildx.inputs.getBuildImageIDFilePath();
await fs.writeFileSync(imageIDFile, imageID);
@@ -65,9 +64,7 @@ describe('resolveBuildImageID', () => {
describe('resolveBuildMetadata', () => {
it('matches', async () => {
const buildx = new Buildx({
context: new Context()
});
const buildx = new Buildx();
const metadataFile = buildx.inputs.getBuildMetadataFilePath();
await fs.writeFileSync(metadataFile, metadata);
const expected = buildx.inputs.resolveBuildMetadata();
@@ -77,9 +74,7 @@ describe('resolveBuildMetadata', () => {
describe('resolveDigest', () => {
it('matches', async () => {
const buildx = new Buildx({
context: new Context()
});
const buildx = new Buildx();
const metadataFile = buildx.inputs.getBuildMetadataFilePath();
await fs.writeFileSync(metadataFile, metadata);
const expected = buildx.inputs.resolveDigest();
@@ -129,9 +124,7 @@ describe('getProvenanceInput', () => {
],
])('given input %p', async (input: string, expected: string) => {
await setInput('provenance', input);
const buildx = new Buildx({
context: new Context()
});
const buildx = new Buildx();
expect(buildx.inputs.getProvenanceInput('provenance')).toEqual(expected);
});
});
@@ -160,9 +153,7 @@ describe('resolveProvenanceAttrs', () => {
'builder-id=https://github.com/docker/actions-toolkit/actions/runs/123'
],
])('given %p', async (input: string, expected: string) => {
const buildx = new Buildx({
context: new Context()
});
const buildx = new Buildx();
expect(buildx.inputs.resolveProvenanceAttrs(input)).toEqual(expected);
});
});
@@ -179,9 +170,7 @@ describe('resolveBuildSecret', () => {
[`notfound=secret`, true, '', '', new Error('secret file secret not found')]
])('given %p key and %p secret', async (kvp: string, file: boolean, exKey: string, exValue: string, error: Error) => {
try {
const buildx = new Buildx({
context: new Context()
});
const buildx = new Buildx();
let secret: string;
if (file) {
secret = buildx.inputs.resolveBuildSecretFile(kvp);

View File

@@ -25,13 +25,14 @@ import {Context} from '../src/context';
const tmpDir = path.join(process.env.TEMP || '/tmp', 'context-jest');
const tmpName = path.join(tmpDir, '.tmpname-jest');
jest.spyOn(Context.prototype, 'tmpDir').mockImplementation((): string => {
jest.spyOn(Context, 'tmpDir').mockImplementation((): string => {
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, {recursive: true});
}
return tmpDir;
});
jest.spyOn(Context.prototype, 'tmpName').mockImplementation((): string => {
jest.spyOn(Context, 'tmpName').mockImplementation((): string => {
return tmpName;
});
@@ -43,16 +44,20 @@ afterEach(() => {
rimraf.sync(tmpDir);
});
describe('gitRef', () => {
it('returns refs/heads/master', async () => {
expect(Context.gitRef()).toEqual('refs/heads/master');
});
});
describe('gitContext', () => {
it('returns refs/heads/master', async () => {
const context = new Context();
expect(context.buildGitContext).toEqual('https://github.com/docker/actions-toolkit.git#refs/heads/master');
expect(Context.gitContext()).toEqual('https://github.com/docker/actions-toolkit.git#refs/heads/master');
});
});
describe('provenanceBuilderID', () => {
it('returns 123', async () => {
const context = new Context();
expect(context.provenanceBuilderID).toEqual('https://github.com/docker/actions-toolkit/actions/runs/123');
expect(Context.provenanceBuilderID()).toEqual('https://github.com/docker/actions-toolkit/actions/runs/123');
});
});