diff --git a/__tests__/docker.test.ts b/__tests__/docker.test.ts index 8823e43..140b399 100644 --- a/__tests__/docker.test.ts +++ b/__tests__/docker.test.ts @@ -50,7 +50,7 @@ describe('configDir', () => { describe('isAvailable', () => { it('cli', async () => { const execSpy = jest.spyOn(exec, 'getExecOutput'); - await Docker.getInstance().isAvailable(); + await Docker.isAvailable(); // eslint-disable-next-line jest/no-standalone-expect expect(execSpy).toHaveBeenCalledWith(`docker`, undefined, { silent: true, @@ -60,43 +60,21 @@ describe('isAvailable', () => { }); describe('printVersion', () => { - it('docker cli', async () => { + it('call docker version', async () => { const execSpy = jest.spyOn(exec, 'exec'); - await Docker.printVersion(false).catch(() => { + await Docker.printVersion().catch(() => { // noop }); - expect(execSpy).toHaveBeenCalledWith(`docker`, ['version'], { - failOnStdErr: false - }); - }); - it('standalone', async () => { - const execSpy = jest.spyOn(exec, 'exec'); - await Docker.printVersion(true).catch(() => { - // noop - }); - expect(execSpy).not.toHaveBeenCalledWith(`docker`, ['version'], { - failOnStdErr: false - }); + expect(execSpy).toHaveBeenCalledWith(`docker`, ['version']); }); }); describe('printInfo', () => { - it('docker cli', () => { + it('call docker info', async () => { const execSpy = jest.spyOn(exec, 'exec'); - Docker.printInfo(false).catch(() => { + await Docker.printInfo().catch(() => { // noop }); - expect(execSpy).toHaveBeenCalledWith(`docker`, ['info'], { - failOnStdErr: false - }); - }); - it('standalone', () => { - const execSpy = jest.spyOn(exec, 'exec'); - Docker.printInfo(true).catch(() => { - // noop - }); - expect(execSpy).not.toHaveBeenCalledWith(`docker`, ['info'], { - failOnStdErr: false - }); + expect(execSpy).toHaveBeenCalledWith(`docker`, ['info']); }); }); diff --git a/src/buildx/buildx.ts b/src/buildx/buildx.ts index 7ca3208..9dc57c7 100644 --- a/src/buildx/buildx.ts +++ b/src/buildx/buildx.ts @@ -55,7 +55,7 @@ export class Buildx { } public async isStandalone(): Promise { - const standalone = this._standalone ?? !(await Docker.getInstance().isAvailable()); + const standalone = this._standalone ?? !(await Docker.isAvailable()); core.debug(`Buildx.isStandalone: ${standalone}`); return standalone; } @@ -70,21 +70,26 @@ export class Buildx { public async isAvailable(): Promise { const cmd = await this.getCommand([]); - return await exec + + const ok: boolean = await exec .getExecOutput(cmd.command, cmd.args, { ignoreReturnCode: true, silent: true }) .then(res => { if (res.stderr.length > 0 && res.exitCode != 0) { + core.debug(`Buildx.isAvailable cmd err: ${res.stderr}`); return false; } return res.exitCode == 0; }) - // eslint-disable-next-line @typescript-eslint/no-unused-vars .catch(error => { + core.debug(`Buildx.isAvailable error: ${error}`); return false; }); + + core.debug(`Buildx.isAvailable: ${ok}`); + return ok; } public async printInspect(name: string): Promise { diff --git a/src/buildx/install.ts b/src/buildx/install.ts index f8f87ee..2e790db 100644 --- a/src/buildx/install.ts +++ b/src/buildx/install.ts @@ -143,7 +143,7 @@ export class Install { } private async isStandalone(): Promise { - const standalone = this._standalone ?? !(await Docker.getInstance().isAvailable()); + const standalone = this._standalone ?? !(await Docker.isAvailable()); core.debug(`Install.isStandalone: ${standalone}`); return standalone; } diff --git a/src/docker.ts b/src/docker.ts index 25e205d..5743fea 100644 --- a/src/docker.ts +++ b/src/docker.ts @@ -20,63 +20,37 @@ import * as core from '@actions/core'; import * as exec from '@actions/exec'; export class Docker { - private static instance?: Docker; - static getInstance = (): Docker => (Docker.instance = Docker.instance ?? new Docker()); - - private _available: boolean | undefined; - - // eslint-disable-next-line @typescript-eslint/no-empty-function - private constructor() {} - static get configDir(): string { return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker'); } - public async isAvailable(): Promise { - if (this._available === undefined) { - await exec - .getExecOutput('docker', undefined, { - ignoreReturnCode: true, - silent: true - }) - .then(res => { - if (res.stderr.length > 0 && res.exitCode != 0) { - core.debug(`Docker.available error: ${res.stderr}`); - this._available = false; - } else { - core.debug(`Docker.available ok`); - this._available = res.exitCode == 0; - } - }) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .catch(error => { - core.debug(`Docker.available failed: ${error}`); - this._available = false; - }); - } - core.debug(`Docker.available: ${this._available}`); - return this._available ?? false; + public static async isAvailable(): Promise { + const ok: boolean = await exec + .getExecOutput('docker', undefined, { + ignoreReturnCode: true, + silent: true + }) + .then(res => { + if (res.stderr.length > 0 && res.exitCode != 0) { + core.debug(`Docker.isAvailable cmd err: ${res.stderr}`); + return false; + } + return res.exitCode == 0; + }) + .catch(error => { + core.debug(`Docker.isAvailable error: ${error}`); + return false; + }); + + core.debug(`Docker.isAvailable: ${ok}`); + return ok; } - public static async printVersion(standalone?: boolean): Promise { - const noDocker = standalone ?? !(await Docker.getInstance().isAvailable()); - if (noDocker) { - core.debug('Docker.printVersion: Docker is not available, skipping.'); - return; - } - await exec.exec('docker', ['version'], { - failOnStdErr: false - }); + public static async printVersion(): Promise { + await exec.exec('docker', ['version']); } - public static async printInfo(standalone?: boolean): Promise { - const noDocker = standalone ?? !(await Docker.getInstance().isAvailable()); - if (noDocker) { - core.debug('Docker.printInfo: Docker is not available, skipping.'); - return; - } - await exec.exec('docker', ['info'], { - failOnStdErr: false - }); + public static async printInfo(): Promise { + await exec.exec('docker', ['info']); } }